Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -729,7 +729,14 @@ private MethodInferenceResult runInferenceForCall(
methodSymbol,
invocationTree,
allInvocations);
typeVarNullability = solver.solve();
typeVarNullability = new HashMap<>(solver.solve());
// The solver only computes a solution for variables that appear in constraints. For
// unconstrained variables, treat them as NONNULL, consistent with solver behavior for
// unconstrained variables that do appear in the constraint graph.
for (int i = 0; i < methodSymbol.getTypeParameters().size(); i++) {
Symbol.TypeVariableSymbol typeVar = methodSymbol.getTypeParameters().get(i);
typeVarNullability.putIfAbsent(typeVar, ConstraintSolver.InferredNullability.NONNULL);
}

// Store inferred types for lambda arguments
new InvocationArguments(invocationTree, methodSymbol.type.asMethodType())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1528,6 +1528,46 @@ public static <T> T notNull(@Nullable T object, String message) {
.doTest();
}

@Test
public void issue1453() {
makeHelper()
.addSourceLines(
"NullUtil.java",
"""
package com.uber;
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;
import org.jetbrains.annotations.Contract;
@NullMarked
public class NullUtil {
@SuppressWarnings("NullAway")
public static <T> T assumeNonNull(@Nullable T object) {
return object;
}
}
""")
.addSourceLines(
"Test.java",
"""
package com.uber;
import java.util.List;
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;
import static com.uber.NullUtil.assumeNonNull;
import java.util.concurrent.atomic.AtomicReference;
@NullMarked
class Test {
private final AtomicReference<@Nullable List<String>> ref = new AtomicReference<>();
void test() {
for (String s: assumeNonNull(ref.get())) {
System.out.println(s);
}
}
}
""")
.doTest();
}

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