Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -772,17 +772,9 @@
// 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 =
Expand Down Expand Up @@ -1119,7 +1111,6 @@
boolean calledFromDataflow) {
Verify.verify(isGenericCallNeedingInference(invocationTree));
Symbol.MethodSymbol methodSymbol = ASTHelpers.getSymbol(invocationTree);
Type type = methodSymbol.type;
Map<Element, ConstraintSolver.InferredNullability> typeVarNullability = null;
MethodInferenceResult result = inferredTypeVarNullabilityForGenericCalls.get(invocationTree);
if (result == null) { // have not yet attempted inference for this call
Expand All @@ -1135,7 +1126,9 @@
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);
Expand Down Expand Up @@ -1199,7 +1192,10 @@
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
Expand Down Expand Up @@ -1250,6 +1246,33 @@
e.getTypeVariable());
}

/**
* Gets the type of a method at an invocation, substituting type arguments from the receiver and
* applying any handler-provided models.
*
* <p>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.
*
Expand Down Expand Up @@ -1281,8 +1304,8 @@
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(
Expand Down Expand Up @@ -2332,10 +2355,10 @@
* <p>Consider the following example:
*
* <pre>
* interface Fn<P extends @Nullable Object, R extends @Nullable Object> {

Check warning on line 2358 in nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java

View workflow job for this annotation

GitHub Actions / Build spring-framework with snapshot

invalid input: '<'

Check warning on line 2358 in nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java

View workflow job for this annotation

GitHub Actions / Build junit-framework with snapshot

invalid input: '<'

Check warning on line 2358 in nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java

View workflow job for this annotation

GitHub Actions / Build spring-boot with snapshot

invalid input: '<'

Check warning on line 2358 in nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java

View workflow job for this annotation

GitHub Actions / Build caffeine with snapshot

invalid input: '<'

Check warning on line 2358 in nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java

View workflow job for this annotation

GitHub Actions / Build and test on macos-latest

invalid input: '<'

Check warning on line 2358 in nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java

View workflow job for this annotation

GitHub Actions / Build and test on ubuntu-latest

invalid input: '<'
* R apply(P p);
* }
* class C implements Fn<String, @Nullable String> {

Check warning on line 2361 in nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java

View workflow job for this annotation

GitHub Actions / Build spring-framework with snapshot

invalid input: '<'

Check warning on line 2361 in nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java

View workflow job for this annotation

GitHub Actions / Build junit-framework with snapshot

invalid input: '<'

Check warning on line 2361 in nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java

View workflow job for this annotation

GitHub Actions / Build spring-boot with snapshot

invalid input: '<'

Check warning on line 2361 in nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java

View workflow job for this annotation

GitHub Actions / Build caffeine with snapshot

invalid input: '<'

Check warning on line 2361 in nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java

View workflow job for this annotation

GitHub Actions / Build and test on macos-latest

invalid input: '<'

Check warning on line 2361 in nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java

View workflow job for this annotation

GitHub Actions / Build and test on ubuntu-latest

invalid input: '<'
* public @Nullable String apply(String p) {
* return null;
* }
Expand Down Expand Up @@ -2426,16 +2449,16 @@
* <p>Consider the following example:
*
* <pre>
* interface Fn<P extends @Nullable Object, R extends @Nullable Object> {

Check warning on line 2452 in nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java

View workflow job for this annotation

GitHub Actions / Build spring-framework with snapshot

invalid input: '<'

Check warning on line 2452 in nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java

View workflow job for this annotation

GitHub Actions / Build junit-framework with snapshot

invalid input: '<'

Check warning on line 2452 in nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java

View workflow job for this annotation

GitHub Actions / Build spring-boot with snapshot

invalid input: '<'

Check warning on line 2452 in nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java

View workflow job for this annotation

GitHub Actions / Build caffeine with snapshot

invalid input: '<'

Check warning on line 2452 in nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java

View workflow job for this annotation

GitHub Actions / Build and test on macos-latest

invalid input: '<'

Check warning on line 2452 in nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java

View workflow job for this annotation

GitHub Actions / Build and test on ubuntu-latest

invalid input: '<'
* R apply(P p);
* }
* class C implements Fn<String, @Nullable String> {

Check warning on line 2455 in nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java

View workflow job for this annotation

GitHub Actions / Build spring-framework with snapshot

invalid input: '<'

Check warning on line 2455 in nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java

View workflow job for this annotation

GitHub Actions / Build junit-framework with snapshot

invalid input: '<'

Check warning on line 2455 in nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java

View workflow job for this annotation

GitHub Actions / Build spring-boot with snapshot

invalid input: '<'

Check warning on line 2455 in nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java

View workflow job for this annotation

GitHub Actions / Build caffeine with snapshot

invalid input: '<'

Check warning on line 2455 in nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java

View workflow job for this annotation

GitHub Actions / Build and test on macos-latest

invalid input: '<'

Check warning on line 2455 in nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java

View workflow job for this annotation

GitHub Actions / Build and test on ubuntu-latest

invalid input: '<'
* public @Nullable String apply(String p) {
* return null;
* }
* }
* static void m() {
* Fn<String, @Nullable String> f = new C();

Check warning on line 2461 in nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java

View workflow job for this annotation

GitHub Actions / Build spring-framework with snapshot

invalid input: '<'

Check warning on line 2461 in nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java

View workflow job for this annotation

GitHub Actions / Build junit-framework with snapshot

invalid input: '<'

Check warning on line 2461 in nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java

View workflow job for this annotation

GitHub Actions / Build spring-boot with snapshot

invalid input: '<'

Check warning on line 2461 in nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java

View workflow job for this annotation

GitHub Actions / Build caffeine with snapshot

invalid input: '<'

Check warning on line 2461 in nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java

View workflow job for this annotation

GitHub Actions / Build and test on macos-latest

invalid input: '<'

Check warning on line 2461 in nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java

View workflow job for this annotation

GitHub Actions / Build and test on ubuntu-latest

invalid input: '<'
* f.apply("hello").hashCode(); // NPE
* }
* </pre>
Expand Down Expand Up @@ -2720,16 +2743,16 @@
* <p>Consider the following example:
*
* <pre>
* interface Fn<P extends @Nullable Object, R extends @Nullable Object> {

Check warning on line 2746 in nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java

View workflow job for this annotation

GitHub Actions / Build spring-framework with snapshot

invalid input: '<'

Check warning on line 2746 in nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java

View workflow job for this annotation

GitHub Actions / Build junit-framework with snapshot

invalid input: '<'

Check warning on line 2746 in nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java

View workflow job for this annotation

GitHub Actions / Build spring-boot with snapshot

invalid input: '<'

Check warning on line 2746 in nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java

View workflow job for this annotation

GitHub Actions / Build caffeine with snapshot

invalid input: '<'

Check warning on line 2746 in nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java

View workflow job for this annotation

GitHub Actions / Build and test on macos-latest

invalid input: '<'

Check warning on line 2746 in nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java

View workflow job for this annotation

GitHub Actions / Build and test on ubuntu-latest

invalid input: '<'
* R apply(P p);
* }
* class C implements Fn<@Nullable String, String> {

Check warning on line 2749 in nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java

View workflow job for this annotation

GitHub Actions / Build spring-framework with snapshot

invalid input: '<'

Check warning on line 2749 in nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java

View workflow job for this annotation

GitHub Actions / Build junit-framework with snapshot

invalid input: '<'

Check warning on line 2749 in nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java

View workflow job for this annotation

GitHub Actions / Build spring-boot with snapshot

invalid input: '<'

Check warning on line 2749 in nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java

View workflow job for this annotation

GitHub Actions / Build caffeine with snapshot

invalid input: '<'

Check warning on line 2749 in nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java

View workflow job for this annotation

GitHub Actions / Build and test on macos-latest

invalid input: '<'

Check warning on line 2749 in nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java

View workflow job for this annotation

GitHub Actions / Build and test on ubuntu-latest

invalid input: '<'
* public String apply(@Nullable String p) {
* return "";
* }
* }
* static void m() {
* Fn<@Nullable String, String> f = new C();

Check warning on line 2755 in nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java

View workflow job for this annotation

GitHub Actions / Build spring-framework with snapshot

invalid input: '<'

Check warning on line 2755 in nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java

View workflow job for this annotation

GitHub Actions / Build junit-framework with snapshot

invalid input: '<'

Check warning on line 2755 in nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java

View workflow job for this annotation

GitHub Actions / Build spring-boot with snapshot

invalid input: '<'

Check warning on line 2755 in nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java

View workflow job for this annotation

GitHub Actions / Build caffeine with snapshot

invalid input: '<'

Check warning on line 2755 in nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java

View workflow job for this annotation

GitHub Actions / Build and test on macos-latest

invalid input: '<'

Check warning on line 2755 in nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java

View workflow job for this annotation

GitHub Actions / Build and test on ubuntu-latest

invalid input: '<'
* f.apply(null);
* }
* </pre>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 extends @Nullable Object>(T value) {}
record Box<V extends @Nullable Object>(V value) {
<E extends Exception> Holder<V> getOrThrow(
Function<? super Exception, E> exceptionTransformer) throws E {
return new Holder<>(value);
}
}
static <T extends @Nullable Object> Box<T> 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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> 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 extends @Nullable Object>(V value) {
<E extends Exception> V getOrThrow(Function<? super Exception, E> exceptionTransformer) throws E {
return value;
}
}
}
""")
.doTest();
}

private CompilationTestHelper makeHelper() {
return makeTestHelperWithArgs(
JSpecifyJavacConfig.withJSpecifyModeArgs(
Expand Down
Loading