Skip to content

Commit

Permalink
Fix lower bound of type variables and unspecified subtypes. (#175)
Browse files Browse the repository at this point in the history
  • Loading branch information
wmdietl authored Apr 22, 2024
1 parent 68e9072 commit 2d53314
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

package com.google.jspecify.nullness;

import static com.google.jspecify.nullness.NullSpecAnnotatedTypeFactory.IsDeclaredOrArray.IS_DECLARED_OR_ARRAY;
import static com.google.jspecify.nullness.NullSpecAnnotatedTypeFactory.IsDeclaredOrArrayOrNull.IS_DECLARED_OR_ARRAY_OR_NULL;
import static com.google.jspecify.nullness.Util.nameMatches;
import static com.sun.source.tree.Tree.Kind.IDENTIFIER;
import static com.sun.source.tree.Tree.Kind.MEMBER_SELECT;
Expand All @@ -29,6 +29,7 @@
import static javax.lang.model.element.ElementKind.ENUM_CONSTANT;
import static javax.lang.model.type.TypeKind.ARRAY;
import static javax.lang.model.type.TypeKind.DECLARED;
import static javax.lang.model.type.TypeKind.NULL;
import static javax.lang.model.type.TypeKind.TYPEVAR;
import static javax.lang.model.type.TypeKind.WILDCARD;
import static org.checkerframework.framework.util.AnnotatedTypes.asSuper;
Expand Down Expand Up @@ -139,7 +140,6 @@ final class NullSpecAnnotatedTypeFactory
new TypeUseLocation[] {
TypeUseLocation.CONSTRUCTOR_RESULT,
TypeUseLocation.EXCEPTION_PARAMETER,
TypeUseLocation.IMPLICIT_LOWER_BOUND,
TypeUseLocation.RECEIVER,
};

Expand All @@ -152,6 +152,10 @@ final class NullSpecAnnotatedTypeFactory

private static final TypeUseLocation[] defaultLocationsUnspecified =
new TypeUseLocation[] {
// Lower bounds could be MinusNull, but all uses in unmarked code would become unspecified
// anyways.
// Revisit once https://github.com/eisop/checker-framework/issues/741 is fixed.
TypeUseLocation.IMPLICIT_LOWER_BOUND,
TypeUseLocation.IMPLICIT_WILDCARD_UPPER_BOUND_NO_SUPER,
TypeUseLocation.TYPE_VARIABLE_USE,
TypeUseLocation.OTHERWISE
Expand Down Expand Up @@ -699,7 +703,7 @@ && isNullInclusiveUnderEveryParameterization(
}

boolean isNullExclusiveUnderEveryParameterization(AnnotatedTypeMirror type) {
return nullnessEstablishingPathExists(type, IS_DECLARED_OR_ARRAY);
return nullnessEstablishingPathExists(type, IS_DECLARED_OR_ARRAY_OR_NULL);
}

private boolean nullnessEstablishingPathExists(
Expand Down Expand Up @@ -1852,12 +1856,12 @@ private AnnotatedDeclaredType createType(TypeElement element) {

// Avoid lambdas so that our Predicates can have a useful toString() for logging purposes.

enum IsDeclaredOrArray implements Predicate<TypeMirror> {
IS_DECLARED_OR_ARRAY;
enum IsDeclaredOrArrayOrNull implements Predicate<TypeMirror> {
IS_DECLARED_OR_ARRAY_OR_NULL;

@Override
public boolean test(TypeMirror t) {
return t.getKind() == DECLARED || t.getKind() == ARRAY;
return t.getKind() == DECLARED || t.getKind() == ARRAY || t.getKind() == NULL;
}
}

Expand Down
33 changes: 33 additions & 0 deletions tests/regression/Issue172.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2024 The JSpecify Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Test case for Issue 172:
// https://github.com/jspecify/jspecify-reference-checker/issues/172

import org.jspecify.annotations.NullMarked;

class Issue172<E> {
E e() {
return null;
}
}

class Issue172UnmarkedUse {
void foo(Issue172<Object> p) {}
}

@NullMarked
class Issue172MarkedUse {
void foo(Issue172<Object> p) {}
}

0 comments on commit 2d53314

Please sign in to comment.