diff --git a/nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java b/nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java index c91a84f90f..5a743c5baa 100644 --- a/nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java +++ b/nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java @@ -3,6 +3,7 @@ import static com.google.common.base.Verify.verify; import static com.uber.nullaway.NullabilityUtil.castToNonNull; import static com.uber.nullaway.NullabilityUtil.pathWithLeaf; +import static com.uber.nullaway.generics.TypeMetadataBuilder.TYPE_METADATA_BUILDER; import static java.util.stream.Collectors.joining; import com.google.common.base.Preconditions; @@ -768,7 +769,9 @@ private void reportInvalidOverridingMethodParamTypeError( } if (newClassTree.getIdentifier() instanceof ParameterizedTypeTree paramTypedTree && !paramTypedTree.getTypeArguments().isEmpty()) { - return typeWithPreservedAnnotations(paramTypedTree); + Type typeFromIdentifier = typeWithPreservedAnnotations(paramTypedTree); + return withEnclosingTypeFromQualifier( + typeFromIdentifier, newClassTree, state, calledFromDataflow); } return typeOrNullIfRaw(ASTHelpers.getType(tree)); } else if (tree instanceof NewArrayTree @@ -1940,6 +1943,41 @@ private Type typeWithPreservedAnnotations(Tree tree) { return tree.accept(new PreservedAnnotationTreeVisitor(config), null); } + /** + * For a {@code NewClassTree} with a qualifying enclosing-instance expression, e.g. {@code + * (someExpr).new Inner() { ... }}, replaces {@code type}'s enclosing type (which, coming from + * {@code baseType.getEnclosingType()} in {@link PreservedAnnotationTreeVisitor}, is just {@code + * Inner}'s statically-declared enclosing type, unrelated to the actual enclosing instance the + * qualifier expression evaluates to) with the properly-annotated, substituted type of the + * qualifier expression itself. If there is no qualifying expression, or we cannot determine its + * type, {@code type} is returned unchanged. + * + * @param type the type computed for the {@code NewClassTree}'s identifier, to be given the + * correct enclosing type + * @param newClassTree the {@code NewClassTree}, possibly qualified by an enclosing-instance + * expression + * @param state the visitor state + * @param calledFromDataflow true if the type is being computed as part of dataflow analysis + * @return {@code type} with its enclosing type corrected, or {@code type} unchanged if there is + * no qualifying expression or its type could not be determined + */ + private @Nullable Type withEnclosingTypeFromQualifier( + Type type, NewClassTree newClassTree, VisitorState state, boolean calledFromDataflow) { + ExpressionTree enclosingExpr = newClassTree.getEnclosingExpression(); + if (enclosingExpr == null) { + return type; + } + TreePath pathToEnclosingExpr = new TreePath(state.getPath(), enclosingExpr); + Type enclosingExprType = + getTreeType(enclosingExpr, state.withPath(pathToEnclosingExpr), calledFromDataflow); + if (enclosingExprType == null) { + return type; + } + Type.ClassType classType = (Type.ClassType) type; + return TYPE_METADATA_BUILDER.createClassType( + classType, enclosingExprType, classType.getTypeArguments()); + } + /** * For a conditional expression c, check whether the type parameter nullability for each * sub-expression of c matches the type parameter nullability of c itself. diff --git a/nullaway/src/test/java/com/uber/nullaway/jspecify/GenericsTests.java b/nullaway/src/test/java/com/uber/nullaway/jspecify/GenericsTests.java index ef56515f80..f854ffd7f3 100644 --- a/nullaway/src/test/java/com/uber/nullaway/jspecify/GenericsTests.java +++ b/nullaway/src/test/java/com/uber/nullaway/jspecify/GenericsTests.java @@ -1619,7 +1619,6 @@ static void anonymousClassesFullName() { .doTest(); } - @Ignore("https://github.com/uber/NullAway/issues/836") @Test public void overrideAnonymousNestedClass() { makeHelper() @@ -1639,6 +1638,41 @@ void anonymousNestedClasses() { // BUG: Diagnostic contains: parameter s is @NonNull, but parameter in superclass method public String apply(String s) { return s; } }; + Wrapper.Fn fn2 = (this.new Wrapper()).new Fn() { + public String apply(String s) { return s; } + }; + } + } + """) + .doTest(); + } + + @Test + public void overrideAnonymousDeeplyNestedClass() { + makeHelper() + .addSourceLines( + "Test.java", + """ + package com.uber; + import org.jspecify.annotations.Nullable; + class Test { + class Wrapper

{ + class Middle { + abstract class Fn { + abstract R apply(P p); + } + } + } + void anonymousDeeplyNestedClasses() { + Wrapper<@Nullable String>.Middle.Fn fn1 = + ((this.new Wrapper<@Nullable String>()).new Middle()).new Fn() { + // BUG: Diagnostic contains: parameter p is @NonNull, but parameter in superclass method + public String apply(String p) { return p; } + }; + Wrapper.Middle.Fn fn2 = + ((this.new Wrapper()).new Middle()).new Fn() { + public String apply(String p) { return p; } + }; } } """)