From addea51041d1567a0e804491abe24d9a2f1a20a2 Mon Sep 17 00:00:00 2001 From: Manu Sridharan Date: Sun, 9 Aug 2026 17:52:51 -0700 Subject: [PATCH 1/8] Support nested call inference and prevent reentrant repair --- .../nullaway/generics/GenericsChecks.java | 209 ++++++++++++------ .../jspecify/GenericDiamondTests.java | 105 +++++++++ 2 files changed, 252 insertions(+), 62 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 30e31a0625..512e3035e2 100644 --- a/nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java +++ b/nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java @@ -118,6 +118,13 @@ private InferenceFailure(@Nullable String errorMessage) { /** Maps each {@code var}-declared local to its declaration tree */ private final Map varLocalDeclarations = new LinkedHashMap<>(); + /** + * Tracks generic method invocations currently undergoing nested-nullability repair so re-entrant + * requests for the same invocation can use the already inferred call-site method type rather than + * recursing back through the same repair logic. + */ + private final Set nestedNullabilityRepairInProgress = new LinkedHashSet<>(); + public @Nullable Type getInferredPolyExpressionType(Tree tree) { Preconditions.checkArgument( tree instanceof LambdaExpressionTree || tree instanceof MemberReferenceTree, @@ -843,9 +850,10 @@ private void reportInvalidOverridingMethodParamTypeError( // call. We invoke getEnclosingTypeForCallExpression, which will run // inference if needed, and then recompute the type as a member of the returned // enclosing type + Symbol.MethodSymbol symbol = castToNonNull(ASTHelpers.getSymbol(invocationTree)); Type.MethodType methodType = - getInferenceExecutableType( - invocationTree, state.getPath(), state, calledFromDataflow); + getInvokedMethodTypeAtCall( + symbol, invocationTree, state.getPath(), state, calledFromDataflow); // restore explicit annotations from the return type Type returnType = methodType.getReturnType(); result = @@ -2799,9 +2807,25 @@ private Type substituteTypeArgsInGenericMethodType( Type.MethodType methodTypeAtCallSite = castToNonNull(ASTHelpers.getType(invocationTree.getMethodSelect())).asMethodType(); if (result instanceof InferenceSuccess successResult) { - methodTypeAtCallSite = - restoreNestedNullabilityForTypeVarArguments( - invocationTree, methodType, methodTypeAtCallSite, path, state, calledFromDataflow); + // Repairing dropped nested nullability annotations can itself inspect actual argument + // types. For diamond constructor arguments, that can re-enter method-type computation for + // this same invocation while we are still repairing it. In that case, use the already + // inferred method type and skip the repair on the recursive call. + if (!nestedNullabilityRepairInProgress.contains(invocationTree)) { + nestedNullabilityRepairInProgress.add(invocationTree); + try { + methodTypeAtCallSite = + restoreNestedNullabilityForTypeVarArguments( + invocationTree, + methodType, + methodTypeAtCallSite, + path, + state, + calledFromDataflow); + } finally { + nestedNullabilityRepairInProgress.remove(invocationTree); + } + } return TypeSubstitutionUtils.updateMethodTypeWithInferredNullability( methodTypeAtCallSite, methodType, successResult.typeVarNullability, state, config); } else { @@ -2876,23 +2900,24 @@ private record CallAndContext( */ private CallAndContext getCallAndContextForInference( TreePath path, VisitorState state, boolean calledFromDataflow) { - ExpressionTree call = (ExpressionTree) path.getLeaf(); + CallAndContext directContext = + getDirectCallContextForInference(path, state, calledFromDataflow); TreePath parentPath = path.getParentPath(); Tree parent = parentPath.getLeaf(); while (parent instanceof ParenthesizedTree) { parentPath = parentPath.getParentPath(); parent = parentPath.getLeaf(); } - if (call instanceof MethodInvocationTree - && parent instanceof MethodInvocationTree parentInvocation - && isCallNeedingInference(parentInvocation)) { - // this is the case of a nested generic call, e.g., id(id(x)) where id is generic - // we want to find the outermost call that requires inference, since that is the one whose - // assignment context is relevant - return getCallAndContextForInference( - parentPath, state.withPath(parentPath), calledFromDataflow); + if (parent instanceof ExpressionTree exprParent) { + if ((exprParent instanceof MethodInvocationTree || exprParent instanceof NewClassTree) + && isCallNeedingInference(exprParent)) { + // This is a nested generic call, such as id(id(x)) or id(new Box<>(x)). Find the + // outermost call requiring inference because its assignment context is the relevant one. + return getCallAndContextForInference( + parentPath, state.withPath(parentPath), calledFromDataflow); + } } - return getDirectCallContextForInference(path, state, calledFromDataflow); + return directContext; } /** @@ -2914,8 +2939,9 @@ private CallAndContext getDirectCallContextForInference( call, targetTypeAndAssignmentKind.typeFromAssignmentContext(), targetTypeAndAssignmentKind.assignedToLocal()); - } else if (parent instanceof ReturnTree) { - // find the enclosing method and return its return type + } + if (parent instanceof ReturnTree) { + // Find the enclosing method and return its return type. TreePath enclosingMethodOrLambda = NullabilityUtil.findEnclosingMethodOrLambdaOrInitializer(parentPath); // TODO handle lambdas; https://github.com/uber/NullAway/issues/1288 @@ -2926,62 +2952,120 @@ private CallAndContext getDirectCallContextForInference( return new CallAndContext(call, methodSymbol.getReturnType(), false); } } - } else if (parent instanceof ExpressionTree exprParent) { - // could be a parameter to another method call, or part of a conditional expression, etc. - // in any case, just return the type of the parent expression - if (exprParent instanceof MethodInvocationTree parentInvocation) { - // the call is either a regular parameter to the parent call, or the receiver expression - Type formalParamType = - getFormalParameterTypeForArgument( - parentInvocation, - castToNonNull(ASTHelpers.getType(parentInvocation.getMethodSelect())) - .asMethodType(), - call); - if (formalParamType == null) { - // this can happen if the call is the receiver expression of the parent call, e.g., - // id(x).foo() (note that foo() need not be generic) - ExpressionTree methodSelect = - ASTHelpers.stripParentheses(parentInvocation.getMethodSelect()); - if (methodSelect instanceof MemberSelectTree mst) { - @SuppressWarnings("ReferenceEquality") // deliberate reference equality check - boolean callIsReceiver = ASTHelpers.stripParentheses(mst.getExpression()) == call; - if (callIsReceiver) { - // the call is the receiver expression, so we want the enclosing type of the parent - // invocation - formalParamType = - getEnclosingTypeForCallExpression( - ASTHelpers.getSymbol(parentInvocation), - parentInvocation, - parentPath, - state.withPath(parentPath), - calledFromDataflow); - } else { - throw new RuntimeException( - "did not find invocation " - + state.getSourceForNode(call) - + " as receiver expression of " - + state.getSourceForNode(parentInvocation)); - } + return new CallAndContext(call, null, false); + } + if (parent instanceof MethodInvocationTree parentInvocation) { + Type.MethodType parentMethodType = + getInvokedMethodTypeAtCall( + ASTHelpers.getSymbol(parentInvocation), + parentInvocation, + parentPath, + state.withPath(parentPath), + calledFromDataflow); + Type formalParamType = + getFormalParameterTypeForArgument(parentInvocation, parentMethodType, call); + if (formalParamType == null) { + // This can happen if the call is the receiver expression of the parent call, e.g., + // id(x).foo() (note that foo() need not be generic). + ExpressionTree methodSelect = + ASTHelpers.stripParentheses(parentInvocation.getMethodSelect()); + if (methodSelect instanceof MemberSelectTree mst) { + @SuppressWarnings("ReferenceEquality") // deliberate reference equality check + boolean callIsReceiver = ASTHelpers.stripParentheses(mst.getExpression()) == call; + if (callIsReceiver) { + // The call is the receiver expression, so use the enclosing type of the parent call. + formalParamType = + getEnclosingTypeForCallExpression( + ASTHelpers.getSymbol(parentInvocation), + parentInvocation, + parentPath, + state.withPath(parentPath), + calledFromDataflow); + } else { + throw new RuntimeException( + "did not find invocation " + + state.getSourceForNode(call) + + " as receiver expression of " + + state.getSourceForNode(parentInvocation)); } } - return new CallAndContext(call, formalParamType, false); - } else if (exprParent instanceof ConditionalExpressionTree) { - TreePath conditionalPath = getOutermostConditionalExpressionPath(parentPath); - TargetTypeAndAssignmentKind targetTypeAndAssignmentKind = - getTargetTypeForConditionalExpression( - (ConditionalExpressionTree) conditionalPath.getLeaf(), - state.withPath(conditionalPath), + } + return new CallAndContext(call, formalParamType, false); + } + if (parent instanceof ConditionalExpressionTree) { + TreePath conditionalPath = getOutermostConditionalExpressionPath(parentPath); + TargetTypeAndAssignmentKind targetTypeAndAssignmentKind = + getTargetTypeForConditionalExpression( + (ConditionalExpressionTree) conditionalPath.getLeaf(), + state.withPath(conditionalPath), + calledFromDataflow); + return new CallAndContext( + call, + targetTypeAndAssignmentKind.typeFromAssignmentContext(), + targetTypeAndAssignmentKind.assignedToLocal()); + } + if (parent instanceof NewClassTree parentConstructorCall) { + Type parentClassType; + if (isCallNeedingInference(parentConstructorCall)) { + CallAndContext parentContext = + getCallAndContextForInference(parentPath, state, calledFromDataflow); + parentClassType = + inferCallType( + state, + parentConstructorCall, + parentPath, + parentContext.typeFromAssignmentContext, + parentContext.assignedToLocal, calledFromDataflow); + } else { + parentClassType = getTreeType(parentConstructorCall, state.withPath(parentPath)); + } + if (parentClassType != null) { + Symbol.MethodSymbol parentCtorSymbol = ASTHelpers.getSymbol(parentConstructorCall); + Type parentCtorType = + TypeSubstitutionUtils.memberType( + state.getTypes(), parentClassType, parentCtorSymbol, config); return new CallAndContext( call, - targetTypeAndAssignmentKind.typeFromAssignmentContext(), - targetTypeAndAssignmentKind.assignedToLocal()); + getFormalParameterTypeForArgument( + parentConstructorCall, parentCtorType.asMethodType(), call), + false); } + return new CallAndContext(call, null, false); } // an unhandled case; for now, give up and return no assignment context return new CallAndContext(call, null, false); } + /** + * Returns the invoked method type at a call site after receiver and inferred type-argument + * substitutions and handler-provided models have been applied. + */ + private Type.MethodType getInvokedMethodTypeAtCall( + Symbol.MethodSymbol methodSymbol, + Tree tree, + @Nullable TreePath path, + VisitorState state, + boolean calledFromDataflow) { + Type invokedMethodType = methodSymbol.type; + Type enclosingType = + getEnclosingTypeForCallExpression(methodSymbol, tree, path, state, calledFromDataflow); + if (enclosingType != null) { + invokedMethodType = + TypeSubstitutionUtils.memberType(state.getTypes(), enclosingType, methodSymbol, config); + } + if (tree instanceof MethodInvocationTree + && invokedMethodType instanceof Type.ForAll forAllType) { + invokedMethodType = + substituteTypeArgsInGenericMethodType(tree, forAllType, path, state, calledFromDataflow); + } + return handler.onOverrideMethodType( + methodSymbol, + invokedMethodType.asMethodType(), + state, + tree instanceof MethodInvocationTree invocationTree ? invocationTree : null); + } + /** * Computes the nullness of a formal parameter of a generic method at an invocation, in the * context of the declared type of its receiver argument. If the formal parameter's type is a type @@ -3382,6 +3466,7 @@ public void clearCache() { inferredPolyExpressionTypes.clear(); inferredVarLocalTypes.clear(); varLocalDeclarations.clear(); + nestedNullabilityRepairInProgress.clear(); } public boolean isNullableAnnotated(Type type) { diff --git a/nullaway/src/test/java/com/uber/nullaway/jspecify/GenericDiamondTests.java b/nullaway/src/test/java/com/uber/nullaway/jspecify/GenericDiamondTests.java index b8f185ee52..822678ea12 100644 --- a/nullaway/src/test/java/com/uber/nullaway/jspecify/GenericDiamondTests.java +++ b/nullaway/src/test/java/com/uber/nullaway/jspecify/GenericDiamondTests.java @@ -293,6 +293,111 @@ static void testPositive(Foo f1, Foo<@Nullable String> f2) { .doTest(); } + @Test + public void genericMethodCallWithDiamondConstructorParameter() { + makeHelper() + .addSourceLines( + "Test.java", + """ + import org.jspecify.annotations.*; + @NullMarked + public class Test { + interface Foo {} + static class FooImpl implements Foo { + FooImpl(Foo value) {} + } + static Foo<@Nullable String> makeNullableFoo() { + throw new RuntimeException(); + } + static Foo id(Foo foo) { + throw new RuntimeException(); + } + static void takeFooString(Foo foo) {} + static void takeFooNullableString(Foo<@Nullable String> foo) {} + static void testNegative() { + takeFooNullableString(id(new FooImpl<>(makeNullableFoo()))); + } + static void testPositive() { + // BUG: Diagnostic contains: incompatible types + takeFooString(id(new FooImpl<>(makeNullableFoo()))); + } + } + """) + .doTest(); + } + + @Test + public void diamondConstructorWithGenericMethodCallParameter() { + makeHelper() + .addSourceLines( + "Test.java", + """ + import org.jspecify.annotations.*; + @NullMarked + public class Test { + interface Foo {} + static class Box { + Box(Foo foo) {} + } + static Foo id(Foo foo) { + throw new RuntimeException(); + } + static Foo<@Nullable String> makeNullableFoo() { + throw new RuntimeException(); + } + static Box<@Nullable String> testNegative() { + return new Box<>(id(makeNullableFoo())); + } + static Box testPositive() { + // BUG: Diagnostic contains: incompatible types + return new Box<>(id(makeNullableFoo())); + } + } + """) + .doTest(); + } + + @Test + public void genericMethodTypeVarParamWithDiamondArg() { + makeHelper() + .addSourceLines( + "Test.java", + """ + import org.jspecify.annotations.*; + @NullMarked + public class Test { + static class Box { + Box(T value) {} + } + static @Nullable String nullableString() { + throw new RuntimeException(); + } + static void consume(U u) {} + static void testNoStackOverflow() { + consume(new Box<>(nullableString())); + } + } + """) + .doTest(); + } + + @Test + public void arraysAsList() { + makeHelper() + .addSourceLines( + "Test.java", + """ + import org.jspecify.annotations.*; + import java.util.*; + @NullMarked + public class Test { + @Nullable Object[] makeArr() { return new Object[0]; } + List<@Nullable Object> make() { return new ArrayList<>(Arrays.asList(makeArr())); } + } + """) + .doTest(); + } + private CompilationTestHelper makeHelper() { return makeTestHelperWithArgs( JSpecifyJavacConfig.withJSpecifyModeArgs( From 8413167d83f085edb51be07769eebe1ee7695163 Mon Sep 17 00:00:00 2001 From: Manu Sridharan Date: Fri, 14 Aug 2026 16:46:41 -0700 Subject: [PATCH 2/8] simplify --- .../nullaway/generics/GenericsChecks.java | 173 +++++++++--------- 1 file changed, 85 insertions(+), 88 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 512e3035e2..79bb45a5cf 100644 --- a/nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java +++ b/nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java @@ -2900,24 +2900,21 @@ private record CallAndContext( */ private CallAndContext getCallAndContextForInference( TreePath path, VisitorState state, boolean calledFromDataflow) { - CallAndContext directContext = - getDirectCallContextForInference(path, state, calledFromDataflow); TreePath parentPath = path.getParentPath(); Tree parent = parentPath.getLeaf(); while (parent instanceof ParenthesizedTree) { parentPath = parentPath.getParentPath(); parent = parentPath.getLeaf(); } - if (parent instanceof ExpressionTree exprParent) { - if ((exprParent instanceof MethodInvocationTree || exprParent instanceof NewClassTree) - && isCallNeedingInference(exprParent)) { - // This is a nested generic call, such as id(id(x)) or id(new Box<>(x)). Find the - // outermost call requiring inference because its assignment context is the relevant one. - return getCallAndContextForInference( - parentPath, state.withPath(parentPath), calledFromDataflow); - } + if (parent instanceof ExpressionTree parentCall + && (parentCall instanceof MethodInvocationTree || parentCall instanceof NewClassTree) + && isCallNeedingInference(parentCall)) { + // This is a nested generic call, such as id(id(x)) or id(new Box<>(x)). Find the + // outermost call requiring inference because its assignment context is the relevant one. + return getCallAndContextForInference( + parentPath, state.withPath(parentPath), calledFromDataflow); } - return directContext; + return getDirectCallContextForInference(path, state, calledFromDataflow); } /** @@ -2939,9 +2936,8 @@ private CallAndContext getDirectCallContextForInference( call, targetTypeAndAssignmentKind.typeFromAssignmentContext(), targetTypeAndAssignmentKind.assignedToLocal()); - } - if (parent instanceof ReturnTree) { - // Find the enclosing method and return its return type. + } else if (parent instanceof ReturnTree) { + // find the enclosing method and return its return type TreePath enclosingMethodOrLambda = NullabilityUtil.findEnclosingMethodOrLambdaOrInitializer(parentPath); // TODO handle lambdas; https://github.com/uber/NullAway/issues/1288 @@ -2952,86 +2948,87 @@ private CallAndContext getDirectCallContextForInference( return new CallAndContext(call, methodSymbol.getReturnType(), false); } } - return new CallAndContext(call, null, false); - } - if (parent instanceof MethodInvocationTree parentInvocation) { - Type.MethodType parentMethodType = - getInvokedMethodTypeAtCall( - ASTHelpers.getSymbol(parentInvocation), - parentInvocation, - parentPath, - state.withPath(parentPath), - calledFromDataflow); - Type formalParamType = - getFormalParameterTypeForArgument(parentInvocation, parentMethodType, call); - if (formalParamType == null) { - // This can happen if the call is the receiver expression of the parent call, e.g., - // id(x).foo() (note that foo() need not be generic). - ExpressionTree methodSelect = - ASTHelpers.stripParentheses(parentInvocation.getMethodSelect()); - if (methodSelect instanceof MemberSelectTree mst) { - @SuppressWarnings("ReferenceEquality") // deliberate reference equality check - boolean callIsReceiver = ASTHelpers.stripParentheses(mst.getExpression()) == call; - if (callIsReceiver) { - // The call is the receiver expression, so use the enclosing type of the parent call. - formalParamType = - getEnclosingTypeForCallExpression( - ASTHelpers.getSymbol(parentInvocation), - parentInvocation, - parentPath, - state.withPath(parentPath), - calledFromDataflow); - } else { - throw new RuntimeException( - "did not find invocation " - + state.getSourceForNode(call) - + " as receiver expression of " - + state.getSourceForNode(parentInvocation)); + } else if (parent instanceof ExpressionTree exprParent) { + // could be a parameter to another method call, or part of a conditional expression, etc. + // in any case, just return the type of the parent expression + if (exprParent instanceof MethodInvocationTree parentInvocation) { + // the call is either a regular parameter to the parent call, or the receiver expression + Type.MethodType parentMethodType = + getInvokedMethodTypeAtCall( + ASTHelpers.getSymbol(parentInvocation), + parentInvocation, + parentPath, + state.withPath(parentPath), + calledFromDataflow); + Type formalParamType = + getFormalParameterTypeForArgument(parentInvocation, parentMethodType, call); + if (formalParamType == null) { + // this can happen if the call is the receiver expression of the parent call, e.g., + // id(x).foo() (note that foo() need not be generic) + ExpressionTree methodSelect = + ASTHelpers.stripParentheses(parentInvocation.getMethodSelect()); + if (methodSelect instanceof MemberSelectTree mst) { + @SuppressWarnings("ReferenceEquality") // deliberate reference equality check + boolean callIsReceiver = ASTHelpers.stripParentheses(mst.getExpression()) == call; + if (callIsReceiver) { + // the call is the receiver expression, so we want the enclosing type of the parent + // invocation + formalParamType = + getEnclosingTypeForCallExpression( + ASTHelpers.getSymbol(parentInvocation), + parentInvocation, + parentPath, + state.withPath(parentPath), + calledFromDataflow); + } else { + throw new RuntimeException( + "did not find invocation " + + state.getSourceForNode(call) + + " as receiver expression of " + + state.getSourceForNode(parentInvocation)); + } } } - } - return new CallAndContext(call, formalParamType, false); - } - if (parent instanceof ConditionalExpressionTree) { - TreePath conditionalPath = getOutermostConditionalExpressionPath(parentPath); - TargetTypeAndAssignmentKind targetTypeAndAssignmentKind = - getTargetTypeForConditionalExpression( - (ConditionalExpressionTree) conditionalPath.getLeaf(), - state.withPath(conditionalPath), - calledFromDataflow); - return new CallAndContext( - call, - targetTypeAndAssignmentKind.typeFromAssignmentContext(), - targetTypeAndAssignmentKind.assignedToLocal()); - } - if (parent instanceof NewClassTree parentConstructorCall) { - Type parentClassType; - if (isCallNeedingInference(parentConstructorCall)) { - CallAndContext parentContext = - getCallAndContextForInference(parentPath, state, calledFromDataflow); - parentClassType = - inferCallType( - state, - parentConstructorCall, - parentPath, - parentContext.typeFromAssignmentContext, - parentContext.assignedToLocal, + return new CallAndContext(call, formalParamType, false); + } else if (exprParent instanceof ConditionalExpressionTree) { + TreePath conditionalPath = getOutermostConditionalExpressionPath(parentPath); + TargetTypeAndAssignmentKind targetTypeAndAssignmentKind = + getTargetTypeForConditionalExpression( + (ConditionalExpressionTree) conditionalPath.getLeaf(), + state.withPath(conditionalPath), calledFromDataflow); - } else { - parentClassType = getTreeType(parentConstructorCall, state.withPath(parentPath)); - } - if (parentClassType != null) { - Symbol.MethodSymbol parentCtorSymbol = ASTHelpers.getSymbol(parentConstructorCall); - Type parentCtorType = - TypeSubstitutionUtils.memberType( - state.getTypes(), parentClassType, parentCtorSymbol, config); return new CallAndContext( call, - getFormalParameterTypeForArgument( - parentConstructorCall, parentCtorType.asMethodType(), call), - false); + targetTypeAndAssignmentKind.typeFromAssignmentContext(), + targetTypeAndAssignmentKind.assignedToLocal()); + } else if (exprParent instanceof NewClassTree parentConstructorCall) { + Type parentClassType; + if (isCallNeedingInference(parentConstructorCall)) { + CallAndContext parentContext = + getCallAndContextForInference(parentPath, state, calledFromDataflow); + parentClassType = + inferCallType( + state, + parentConstructorCall, + parentPath, + parentContext.typeFromAssignmentContext, + parentContext.assignedToLocal, + calledFromDataflow); + } else { + parentClassType = getTreeType(parentConstructorCall, state.withPath(parentPath)); + } + if (parentClassType != null) { + Symbol.MethodSymbol parentCtorSymbol = ASTHelpers.getSymbol(parentConstructorCall); + Type parentCtorType = + TypeSubstitutionUtils.memberType( + state.getTypes(), parentClassType, parentCtorSymbol, config); + return new CallAndContext( + call, + getFormalParameterTypeForArgument( + parentConstructorCall, parentCtorType.asMethodType(), call), + false); + } } - return new CallAndContext(call, null, false); } // an unhandled case; for now, give up and return no assignment context return new CallAndContext(call, null, false); From 2e9668f5f953757806de871334961868027a2e8f Mon Sep 17 00:00:00 2001 From: Manu Sridharan Date: Fri, 14 Aug 2026 16:59:26 -0700 Subject: [PATCH 3/8] reuse logic --- .../nullaway/generics/GenericsChecks.java | 25 +------------------ 1 file changed, 1 insertion(+), 24 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 79bb45a5cf..6fd85022c0 100644 --- a/nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java +++ b/nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java @@ -2265,31 +2265,8 @@ public void compareGenericTypeParameterNullabilityForCall( if (!config.isJSpecifyMode()) { return; } - Type invokedMethodType = methodSymbol.type; - Type enclosingType = null; - if (enclosingType == null) { - enclosingType = getEnclosingTypeForCallExpression(methodSymbol, tree, null, state, false); - } - if (enclosingType != null) { - invokedMethodType = - TypeSubstitutionUtils.memberType(state.getTypes(), enclosingType, methodSymbol, config); - } - - // substitute type arguments for generic methods with explicit type arguments - if (tree instanceof MethodInvocationTree && invokedMethodType instanceof Type.ForAll) { - invokedMethodType = - substituteTypeArgsInGenericMethodType( - tree, (Type.ForAll) invokedMethodType, null, state, false); - } - Type.MethodType finalMethodType = - handler.onOverrideMethodType( - methodSymbol, - invokedMethodType.asMethodType(), - state, - tree instanceof MethodInvocationTree methodInvocationTree - ? methodInvocationTree - : null); + getInvokedMethodTypeAtCall(methodSymbol, tree, null, state, false); new InvocationArguments(tree, finalMethodType) .forEach( (currentActualParam, argPos, formalParameter, unused) -> { From cffb866657e8a28693187aab68c04631084a40f0 Mon Sep 17 00:00:00 2001 From: Manu Sridharan Date: Fri, 14 Aug 2026 17:09:34 -0700 Subject: [PATCH 4/8] update javadoc --- .../java/com/uber/nullaway/generics/GenericsChecks.java | 8 ++++++-- 1 file changed, 6 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 6fd85022c0..9e75a26054 100644 --- a/nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java +++ b/nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java @@ -1314,6 +1314,9 @@ private com.sun.tools.javac.util.List getCallTypePara * method signature. For example, for a method returning {@code T} on a receiver {@code * Foo<@Nullable Object>}, the invocation return type is {@code @Nullable Object}, not the * declaration-site type variable {@code T}. + * + *

Unlike {@link #getInvokedMethodTypeAtCall}, this method preserves method type variables so + * inference constraints can be generated for them. */ private Type.MethodType getInferenceExecutableType( ExpressionTree callTree, @@ -3012,8 +3015,9 @@ private CallAndContext getDirectCallContextForInference( } /** - * Returns the invoked method type at a call site after receiver and inferred type-argument - * substitutions and handler-provided models have been applied. + * Returns the resolved method type at a call site after receiver and inferred type-argument + * substitutions and handler-provided models have been applied. Unlike {@link + * #getInferenceExecutableType}, this method substitutes the method type variables. */ private Type.MethodType getInvokedMethodTypeAtCall( Symbol.MethodSymbol methodSymbol, From 6bcd9ef1abfd79bc39debfbe4abf19c711955f24 Mon Sep 17 00:00:00 2001 From: Manu Sridharan Date: Fri, 14 Aug 2026 19:12:31 -0700 Subject: [PATCH 5/8] update comment --- .../uber/nullaway/generics/GenericsChecks.java | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 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 9e75a26054..6a5fbd5532 100644 --- a/nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java +++ b/nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java @@ -842,14 +842,13 @@ private void reportInvalidOverridingMethodParamTypeError( result = ASTHelpers.getType(tree); if (result != null) { if (tree instanceof MethodInvocationTree invocationTree) { - // for a call to an instance method, we may need to run inference on a nested call - // inside the receiver in order to figure out the proper nullability of the receiver's - // type arguments. E.g., for a call `foo(x,y).bar().baz()`, where `foo` is a generic - // method, we need to run inference on the call to `foo` to determine the receiver type - // of the `bar()` call, which could in turn impact the receiver type of the `baz()` - // call. We invoke getEnclosingTypeForCallExpression, which will run - // inference if needed, and then recompute the type as a member of the returned - // enclosing type + // The type javac reports for a method invocation may omit nested nullability + // annotations. Compute the resolved call-site method type, including substitutions + // inferred for the method itself and for generic calls in its receiver. For example, + // in `foo(x, y).bar().baz()`, where `foo` is generic, inference for `foo` determines + // the + // receiver type of `bar()`, which can in turn affect the receiver type of `baz()`. The + // resolved return type is used below to restore annotations on javac's invocation type. Symbol.MethodSymbol symbol = castToNonNull(ASTHelpers.getSymbol(invocationTree)); Type.MethodType methodType = getInvokedMethodTypeAtCall( From f4873422c8f2f7e082a43e934fc96dca51b0d26e Mon Sep 17 00:00:00 2001 From: Manu Sridharan Date: Fri, 14 Aug 2026 19:13:28 -0700 Subject: [PATCH 6/8] formatting --- .../java/com/uber/nullaway/generics/GenericsChecks.java | 6 +++--- 1 file changed, 3 insertions(+), 3 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 6a5fbd5532..4be8b1874b 100644 --- a/nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java +++ b/nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java @@ -846,9 +846,9 @@ private void reportInvalidOverridingMethodParamTypeError( // annotations. Compute the resolved call-site method type, including substitutions // inferred for the method itself and for generic calls in its receiver. For example, // in `foo(x, y).bar().baz()`, where `foo` is generic, inference for `foo` determines - // the - // receiver type of `bar()`, which can in turn affect the receiver type of `baz()`. The - // resolved return type is used below to restore annotations on javac's invocation type. + // the receiver type of `bar()`, which can in turn affect the receiver type of `baz()`. + // The resolved return type is used below to restore annotations on javac's invocation + // type. Symbol.MethodSymbol symbol = castToNonNull(ASTHelpers.getSymbol(invocationTree)); Type.MethodType methodType = getInvokedMethodTypeAtCall( From a19e9818b9d6f9549bb8288f80ec2b6bceeeb7ad Mon Sep 17 00:00:00 2001 From: Manu Sridharan Date: Fri, 14 Aug 2026 19:14:30 -0700 Subject: [PATCH 7/8] tweak --- .../main/java/com/uber/nullaway/generics/GenericsChecks.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 4be8b1874b..509261f941 100644 --- a/nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java +++ b/nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java @@ -121,7 +121,8 @@ private InferenceFailure(@Nullable String errorMessage) { /** * Tracks generic method invocations currently undergoing nested-nullability repair so re-entrant * requests for the same invocation can use the already inferred call-site method type rather than - * recursing back through the same repair logic. + * recursing back through the same repair logic. See {@link + * #substituteTypeArgsInGenericMethodType(Tree, Type.ForAll, TreePath, VisitorState, boolean)} */ private final Set nestedNullabilityRepairInProgress = new LinkedHashSet<>(); From 879135ccb625ddf8d77c3417e485177656d63aba Mon Sep 17 00:00:00 2001 From: Manu Sridharan Date: Sat, 15 Aug 2026 15:55:03 -0700 Subject: [PATCH 8/8] coderabbit --- .../main/java/com/uber/nullaway/generics/GenericsChecks.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 509261f941..fc1c19dd3c 100644 --- a/nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java +++ b/nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java @@ -2995,7 +2995,8 @@ private CallAndContext getDirectCallContextForInference( parentContext.assignedToLocal, calledFromDataflow); } else { - parentClassType = getTreeType(parentConstructorCall, state.withPath(parentPath)); + parentClassType = + getTreeType(parentConstructorCall, state.withPath(parentPath), calledFromDataflow); } if (parentClassType != null) { Symbol.MethodSymbol parentCtorSymbol = ASTHelpers.getSymbol(parentConstructorCall);