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 b46b8115bb..0d120176ae 100644 --- a/nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java +++ b/nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java @@ -772,17 +772,9 @@ private void reportInvalidOverridingMethodParamTypeError( // inference if needed, and then recompute the type as a member of the returned // enclosing type Symbol.MethodSymbol symbol = castToNonNull(ASTHelpers.getSymbol(invocationTree)); - Type invokedMethodType = symbol.type; - Type enclosingType = - getEnclosingTypeForCallExpression( - symbol, invocationTree, state.getPath(), state, calledFromDataflow); - if (enclosingType != null) { - invokedMethodType = - TypeSubstitutionUtils.memberType(state.getTypes(), enclosingType, symbol, config); - } Type.MethodType methodType = - handler.onOverrideMethodType( - symbol, invokedMethodType.asMethodType(), state, invocationTree); + getMethodTypeForInvocation( + symbol, invocationTree, state.getPath(), state, calledFromDataflow); // restore explicit annotations from the return type Type returnType = methodType.getReturnType(); result = @@ -1119,7 +1111,6 @@ private Type inferGenericMethodCallType( boolean calledFromDataflow) { Verify.verify(isGenericCallNeedingInference(invocationTree)); Symbol.MethodSymbol methodSymbol = ASTHelpers.getSymbol(invocationTree); - Type type = methodSymbol.type; Map typeVarNullability = null; MethodInferenceResult result = inferredTypeVarNullabilityForGenericCalls.get(invocationTree); if (result == null) { // have not yet attempted inference for this call @@ -1135,7 +1126,9 @@ private Type inferGenericMethodCallType( if (result instanceof InferenceSuccess) { typeVarNullability = ((InferenceSuccess) result).typeVarNullability; } - Type methodReturnType = ((Type.ForAll) type).qtype.getReturnType(); + Type methodReturnType = + getMethodTypeForInvocation(methodSymbol, invocationTree, path, state, calledFromDataflow) + .getReturnType(); Type returnTypeAtCallSite = castToNonNull(ASTHelpers.getType(invocationTree)); return TypeSubstitutionUtils.updateTypeWithInferredNullability( returnTypeAtCallSite, methodReturnType, typeVarNullability, state, config); @@ -1199,7 +1192,10 @@ private MethodInferenceResult runInferenceForCall( inferredTypeVarNullabilityForGenericCalls.put(invTree, successResult); } // Store inferred types for lambda or method reference arguments - new InvocationArguments(invocationTree, methodSymbol.type.asMethodType()) + Type.MethodType methodType = + getMethodTypeForInvocation( + methodSymbol, invocationTree, path, state, calledFromDataflow); + new InvocationArguments(invocationTree, methodType) .forEach( (argument, argPos, formalParamType, unused) -> { if (argument instanceof LambdaExpressionTree @@ -1250,6 +1246,33 @@ private String inferenceFailureMessage(UnsatisfiableConstraintsException e) { e.getTypeVariable()); } + /** + * Gets the type of a method at an invocation, substituting type arguments from the receiver and + * applying any handler-provided models. + * + *

Receiver substitution is necessary when an enclosing class type variable appears in the + * 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}. + */ + private Type.MethodType getMethodTypeForInvocation( + Symbol.MethodSymbol methodSymbol, + MethodInvocationTree methodInvocationTree, + @Nullable TreePath path, + VisitorState state, + boolean calledFromDataflow) { + Type invokedMethodType = methodSymbol.type; + Type enclosingType = + getEnclosingTypeForCallExpression( + methodSymbol, methodInvocationTree, path, state, calledFromDataflow); + if (enclosingType != null) { + invokedMethodType = + TypeSubstitutionUtils.memberType(state.getTypes(), enclosingType, methodSymbol, config); + } + return handler.onOverrideMethodType( + methodSymbol, invokedMethodType.asMethodType(), state, methodInvocationTree); + } + /** * Generates inference constraints for a generic method call, including nested calls. * @@ -1281,8 +1304,8 @@ private void generateConstraintsForCall( boolean calledFromDataflow) throws UnsatisfiableConstraintsException { Type.MethodType methodType = - handler.onOverrideMethodType( - methodSymbol, methodSymbol.type.asMethodType(), state, methodInvocationTree); + getMethodTypeForInvocation( + methodSymbol, methodInvocationTree, path, state, calledFromDataflow); // first, handle the return type flow if (typeFromAssignmentContext != null) { solver.addSubtypeConstraint( 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 8c209fcb55..d8e838d705 100644 --- a/nullaway/src/test/java/com/uber/nullaway/jspecify/GenericMethodTests.java +++ b/nullaway/src/test/java/com/uber/nullaway/jspecify/GenericMethodTests.java @@ -113,6 +113,36 @@ public void run(Foo<@Nullable String, Character> f) { .doTest(); } + @Test + public void inferredGenericInstanceMethodReturnTypeUsesReceiverType() { + makeHelper() + .addSourceLines( + "Test.java", + """ + import java.util.function.Function; + import org.jspecify.annotations.*; + @NullMarked + class Test { + record Holder(T value) {} + record Box(V value) { + Holder getOrThrow( + Function exceptionTransformer) throws E { + return new Holder<>(value); + } + } + static Box box(T value) { + return new Box<>(value); + } + static void test() { + var holder = box(null).getOrThrow(RuntimeException::new); + // BUG: Diagnostic contains: dereferenced expression 'holder.value()' is @Nullable + holder.value().hashCode(); + } + } + """) + .doTest(); + } + @Test public void genericMethodAndVoidType() { makeHelper() diff --git a/nullaway/src/test/java/com/uber/nullaway/jspecify/WildcardTests.java b/nullaway/src/test/java/com/uber/nullaway/jspecify/WildcardTests.java index 6040c9a14e..4b85365fe4 100644 --- a/nullaway/src/test/java/com/uber/nullaway/jspecify/WildcardTests.java +++ b/nullaway/src/test/java/com/uber/nullaway/jspecify/WildcardTests.java @@ -983,6 +983,37 @@ static Flux[] convert(Object source) { .doTest(); } + @Test + public void lambdaInferenceUsesGenericInstanceMethodReceiverType() { + makeHelper() + .addSourceLines( + "Repro.java", + """ + import java.util.List; + import java.util.function.Function; + import org.jspecify.annotations.*; + @NullMarked + final class Repro { + static List readValues(List inputs) { + return inputs.stream() + // no error here: the lambda returns @Nullable Object, so we + // should infer type variable R of Stream.map to be @Nullable Object + .map(input -> nullableBox().getOrThrow(RuntimeException::new)) + .toList(); + } + private static Box<@Nullable Object> nullableBox() { + return new Box<>(null); + } + private record Box(V value) { + V getOrThrow(Function exceptionTransformer) throws E { + return value; + } + } + } + """) + .doTest(); + } + private CompilationTestHelper makeHelper() { return makeTestHelperWithArgs( JSpecifyJavacConfig.withJSpecifyModeArgs(