diff --git a/nullaway/src/main/java/com/uber/nullaway/generics/InferTypeVisitor.java b/nullaway/src/main/java/com/uber/nullaway/generics/InferTypeVisitor.java index 8bbbd04e4b..76fda0aef7 100644 --- a/nullaway/src/main/java/com/uber/nullaway/generics/InferTypeVisitor.java +++ b/nullaway/src/main/java/com/uber/nullaway/generics/InferTypeVisitor.java @@ -19,14 +19,13 @@ public class InferTypeVisitor extends Types.DefaultTypeVisitor<@Nullable Map visitClassType(Type.ClassType rhsType, Type lhsType) { Map genericNullness = new HashMap<>(); - // for each type parameter, call accept with this visitor and add all results to one map com.sun.tools.javac.util.List rhsTypeArguments = rhsType.getTypeArguments(); com.sun.tools.javac.util.List lhsTypeArguments = ((Type.ClassType) lhsType).getTypeArguments(); + // get the inferred type for each type arguments and add them to genericNullness for (int i = 0; i < rhsTypeArguments.size(); i++) { Type rhsTypeArg = rhsTypeArguments.get(i); Type lhsTypeArg = lhsTypeArguments.get(i); - // get the inferred type for each type arguments and add them to genericNullness Map map = rhsTypeArg.accept(this, lhsTypeArg); if (map != null) { genericNullness.putAll(map); @@ -36,7 +35,7 @@ public class InferTypeVisitor extends Types.DefaultTypeVisitor<@Nullable Map visitTypeVar(Type.TypeVar rhsType, Type lhsType) { // type variable itself + public Map visitTypeVar(Type.TypeVar rhsType, Type lhsType) { Map genericNullness = new HashMap<>(); Boolean isLhsNullable = Nullness.hasNullableAnnotation(lhsType.getAnnotationMirrors().stream(), config); @@ -45,7 +44,7 @@ public Map visitTypeVar(Type.TypeVar rhsType, Type lhsType) { // typ Nullness.hasNullableAnnotation(upperBound.getAnnotationMirrors().stream(), config); if (!isLhsNullable) { // lhsType is NonNull, we can just use this genericNullness.put(rhsType, lhsType); - } else if (isRhsNullable) { // lhsType & rhsType is Nullable, can use lhs for inference + } else if (isRhsNullable) { // lhsType & rhsType are Nullable, can use lhs for inference genericNullness.put(rhsType, lhsType); } else { // rhs can't be nullable, use upperbound genericNullness.put(rhsType, upperBound); @@ -55,6 +54,7 @@ public Map visitTypeVar(Type.TypeVar rhsType, Type lhsType) { // typ @Override public @Nullable Map visitArrayType(Type.ArrayType rhsType, Type lhsType) { + // unwrap the type of the array and call accept on it Type rhsComponentType = rhsType.elemtype; Type lhsComponentType = ((Type.ArrayType) lhsType).elemtype; Map genericNullness = rhsComponentType.accept(this, lhsComponentType); diff --git a/nullaway/src/test/java/com/uber/nullaway/jspecify/GenericMethodTests.java b/nullaway/src/test/java/com/uber/nullaway/jspecify/GenericMethodTests.java index b502853fd4..1394687297 100644 --- a/nullaway/src/test/java/com/uber/nullaway/jspecify/GenericMethodTests.java +++ b/nullaway/src/test/java/com/uber/nullaway/jspecify/GenericMethodTests.java @@ -323,15 +323,15 @@ public void genericInferenceOnAssignmentsWithArrays() { " Foo[]> f8 = Foo.test1Nonnull(null);", " Foo[] f9 = Foo.test2Null(new Object());", " Foo<@Nullable Object>[] f10 = Foo.test2Null(new Object());", - " // BUG: Diagnostic contains: passing @Nullable parameter 'null' where @NonNull is required", + " // BUG: Diagnostic contains: passing @Nullable parameter 'null' where @NonNull is required", " Foo[] f11 = Foo.test2Null(null);", " Foo<@Nullable Object>[] f12 = Foo.test2Null(null);", " Foo[] f13 = Foo.test2Nonnull(new Object());", " // BUG: Diagnostic contains: due to mismatched nullability of type parameters", " Foo<@Nullable Object>[] f14 = Foo.test2Nonnull(new Object());", - " // BUG: Diagnostic contains: passing @Nullable parameter 'null' where @NonNull is required", + " // BUG: Diagnostic contains: passing @Nullable parameter 'null' where @NonNull is required", " Foo[] f15 = Foo.test2Nonnull(null);", - " // BUG: Diagnostic contains: passing @Nullable parameter 'null' where @NonNull is required", + " // BUG: Diagnostic contains: passing @Nullable parameter 'null' where @NonNull is required", " Foo<@Nullable Object>[] f16 = Foo.test2Nonnull(null);", " }", " }")