From 486a447b93e0b90064aa0a3c539d81729fc8a4a8 Mon Sep 17 00:00:00 2001 From: Daniel Widdis Date: Tue, 11 Aug 2026 15:55:49 -0700 Subject: [PATCH 1/2] JSpecify: support annotated type arguments from an enclosing class (#836) The previously-@Ignore'd test overrideAnonymousNestedClass exercises an anonymous class instantiated via a qualified new expression with two levels of generics, e.g. (this.new Wrapper<@Nullable String>()).new Fn() {...}, where the abstract method being overridden, Fn.apply(P p), references the OUTER class Wrapper's type variable P. Running the test (rather than assuming from the issue text) showed this was a hard, uncaught VerifyException crash, not a silently-missed diagnostic: GenericsChecks.getTreeType's NewClassTree handling only ever consulted newClassTree.getIdentifier() (Fn) via PreservedAnnotationTreeVisitor, and PreservedAnnotationTreeVisitor's visitParameterizedType built the enclosing type from baseType.getEnclosingType() -- Fn's statically-declared enclosing type Wrapper

, with the raw type variable P, completely unconnected to the qualifier expression's actual instantiation. getTypeForSymbol's sanity-check verify() correctly caught the resulting type mismatch and threw. Adds GenericsChecks.withEnclosingTypeFromQualifier, which recovers the qualifier expression's own correctly-substituted type (recursing via the existing getTreeType, so arbitrary nesting depth is handled for free) and splices it in as the enclosing type, replacing baseType.getEnclosingType()'s incorrect result. This is separate from, and does not touch, the diamond-operator anonymous-class gap tracked in #1475. --- .../nullaway/generics/GenericsChecks.java | 46 ++++++++++++++++++- .../uber/nullaway/jspecify/GenericsTests.java | 36 ++++++++++++++- 2 files changed, 80 insertions(+), 2 deletions(-) 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..2dba782763 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,47 @@ 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( + @Nullable Type type, + NewClassTree newClassTree, + VisitorState state, + boolean calledFromDataflow) { + if (type == null) { + return null; + } + 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; } + }; } } """) From 952f84a38e74177df5f67935d4f9278495b735f9 Mon Sep 17 00:00:00 2001 From: Daniel Widdis Date: Wed, 12 Aug 2026 18:34:43 -0700 Subject: [PATCH 2/2] Drop dead null check on type param in withEnclosingTypeFromQualifier type is always the result of typeWithPreservedAnnotations(paramTypedTree), which is never null when called with a ParameterizedTypeTree, so the check and @Nullable annotation were unreachable. --- .../java/com/uber/nullaway/generics/GenericsChecks.java | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) 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 2dba782763..5a743c5baa 100644 --- a/nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java +++ b/nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java @@ -1962,13 +1962,7 @@ private Type typeWithPreservedAnnotations(Tree tree) { * no qualifying expression or its type could not be determined */ private @Nullable Type withEnclosingTypeFromQualifier( - @Nullable Type type, - NewClassTree newClassTree, - VisitorState state, - boolean calledFromDataflow) { - if (type == null) { - return null; - } + Type type, NewClassTree newClassTree, VisitorState state, boolean calledFromDataflow) { ExpressionTree enclosingExpr = newClassTree.getEnclosingExpression(); if (enclosingExpr == null) { return type;