Skip to content
Draft
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
14 changes: 10 additions & 4 deletions nullaway/src/main/java/com/uber/nullaway/NullAway.java
Original file line number Diff line number Diff line change
Expand Up @@ -966,7 +966,7 @@ public Description matchAnnotatedType(AnnotatedTypeTree tree, VisitorState state
* @param resolvedReference the shared method-reference resolution, or {@code null} for an
* ordinary override, lambda, or unsupported reference
* @param modeledOverriddenMethodType overridden method type after substitution and application of
* library models, for a regular override in JSpecify mode; otherwise {@code null}
* library models in JSpecify mode; otherwise {@code null}
* @param state visitor state
* @param overridingMethod if available, the symbol for the overriding method
* @return discovered error, or {@link Description#NO_MATCH} if no error
Expand Down Expand Up @@ -1512,10 +1512,13 @@ public Description matchLambdaExpression(LambdaExpressionTree tree, VisitorState
// we update the environment mapping before running any handlers, as some handlers
// (like Rx nullability) run dataflow analysis
updateEnvironmentMapping(state.getPath(), state);
Type.MethodType modeledFunctionalInterfaceMethodType = null;
if (config.isJSpecifyMode()) {
genericsChecks.maybeStoreLibraryModeledPolyExpressionType(tree, state);
modeledFunctionalInterfaceMethodType =
genericsChecks.getModeledFunctionalInterfaceMethodType(tree, state);
genericsChecks.checkTypeParameterNullnessForExplicitLambdaParameters(
tree, funcInterfaceMethod, state);
tree, funcInterfaceMethod, modeledFunctionalInterfaceMethodType, state);
}
handler.onMatchLambdaExpression(
tree, new MethodAnalysisContext(this, state, funcInterfaceMethod));
Expand All @@ -1529,7 +1532,7 @@ public Description matchLambdaExpression(LambdaExpressionTree tree, VisitorState
tree,
/* memberReferenceTree= */ null,
/* resolvedReference= */ null,
/* modeledOverriddenMethodType= */ null,
modeledFunctionalInterfaceMethodType,
state,
/* overridingMethod= */ null);
if (!description.equals(Description.NO_MATCH)) {
Expand Down Expand Up @@ -1581,7 +1584,7 @@ public Description matchMemberReference(MemberReferenceTree tree, VisitorState s
* @param overriddenMethod method being overridden
* @param overridingMethod overriding method
* @param modeledOverriddenMethodType overridden method type after substitution and application of
* library models, for a regular override in JSpecify mode; otherwise {@code null}
* library models in JSpecify mode; otherwise {@code null}
* @param memberReferenceTree if override is via a method reference, the relevant {@link
* MemberReferenceTree}; otherwise {@code null}
* @param state visitor state.
Expand All @@ -1600,6 +1603,9 @@ private Description checkOverriding(
resolvedReference =
genericsChecks.resolveMemberReference(
memberReferenceTree, overridingMethod, functionalInterfaceType, state);
if (config.isJSpecifyMode() && resolvedReference != null) {
modeledOverriddenMethodType = resolvedReference.functionalInterfaceMethodType();
}
}
}
if (config.isJSpecifyMode()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
* references, outside JSpecify mode, or when unavailable
* @param functionalInterfaceMethod the symbol of the target functional interface's abstract method
* @param functionalInterfaceMethodType the functional interface method's type as a member of the
* ground target type in JSpecify mode, or its declared type otherwise
* ground target type with library models applied in JSpecify mode, or its declared type
* otherwise
* @param isUnbound whether the first functional interface parameter supplies the instance receiver,
* as in {@code String::length}
* @param returnNullnessHint the return-nullness refinement from handlers or the core {@code
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
import com.sun.tools.javac.code.Symbol;
import com.sun.tools.javac.code.Symtab;
import com.sun.tools.javac.code.Type;
import com.sun.tools.javac.code.Types;
import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.tree.TreeInfo;
import com.sun.tools.javac.util.Name;
Expand Down Expand Up @@ -200,7 +199,7 @@ private InferenceFailure(@Nullable String errorMessage) {
}

/**
* Returns the substituted functional-interface method type of a lambda or method reference.
* Returns the substituted, modeled functional-interface method type for nested return checking.
*
* @param polyExpressionTree lambda or method-reference tree
* @param state visitor state
Expand All @@ -210,7 +209,6 @@ private InferenceFailure(@Nullable String errorMessage) {
@SuppressWarnings("ReferenceEquality") // locate the existing path to the expression
public Type.@Nullable MethodType getFunctionalInterfaceMethodType(
ExpressionTree polyExpressionTree, VisitorState state) {
Type functionalInterfaceType = getPolyExpressionType(polyExpressionTree);
TreePath expressionPath = state.getPath();
while (expressionPath != null && expressionPath.getLeaf() != polyExpressionTree) {
expressionPath = expressionPath.getParentPath();
Expand All @@ -227,10 +225,23 @@ && isCallNeedingInference(parentExpression)) {
// inference solution, but only the outer call's direct arguments have cached poly types.
return null;
}
return getModeledFunctionalInterfaceMethodType(polyExpressionTree, state);
}

/**
* Returns the functional-interface method type after substitution and library modeling.
*
* @param polyExpressionTree lambda or method reference
* @param state visitor state
* @return the modeled method type, or {@code null} if the target is unavailable or raw
*/
public Type.@Nullable MethodType getModeledFunctionalInterfaceMethodType(
ExpressionTree polyExpressionTree, VisitorState state) {
Type functionalInterfaceType = getPolyExpressionType(polyExpressionTree);
if (functionalInterfaceType == null || functionalInterfaceType.isRaw()) {
return null;
}
return getFunctionalInterfaceMethodType(
return getModeledFunctionalInterfaceMethodType(
NullabilityUtil.getFunctionalInterfaceMethod(polyExpressionTree, state.getTypes()),
functionalInterfaceType,
state);
Expand Down Expand Up @@ -1135,15 +1146,11 @@ private static Type getConstructedTypeAtCallSite(NewClassTree newClassTree) {
VariableTree param = params.get(i);
Symbol paramSymbol = ASTHelpers.getSymbol(param);
if (paramSymbol != null && paramSymbol.equals(symbol)) {
// get the type of the functional interface method as a member of the inferred type
// of the lambda
Types types = state.getTypes();
var fiMethodType =
TypeSubstitutionUtils.memberType(
types,
Type.MethodType fiMethodType =
getModeledFunctionalInterfaceMethodType(
NullabilityUtil.getFunctionalInterfaceMethod(lambdaTree, state.getTypes()),
inferredLambdaType,
NullabilityUtil.getFunctionalInterfaceMethod(lambdaTree, types),
config);
state);
return fiMethodType.getParameterTypes().get(i);
}
}
Expand Down Expand Up @@ -2133,7 +2140,7 @@ private void handleMethodRefInGenericMethodInference(
NullabilityUtil.getFunctionalInterfaceMethod(memberReferenceTree, state.getTypes());
Type.MethodType functionalInterfaceMethodType =
config.isJSpecifyMode()
? getFunctionalInterfaceMethodType(
? getModeledFunctionalInterfaceMethodType(
functionalInterfaceMethod,
GenericsUtils.groundTargetType(targetType, state, config, handler),
state)
Expand Down Expand Up @@ -2232,20 +2239,23 @@ private NullnessHint getMapGetOrDefaultReturnNullnessHint(

/**
* Gets a functional interface method's type as a member of the ground target type, preserving
* nullness when substituting type arguments. Only suitable for use in JSpecify mode.
* nullness when substituting type arguments and applying library models. Only suitable for use in
* JSpecify mode.
*
* @param functionalInterfaceMethod the functional interface method
* @param targetType the method reference's ground target type from javac attribution, with any
* cached NullAway nullness refinements
* @param state the current visitor state
* @return the functional interface method type as a member of the ground target type
*/
private Type.MethodType getFunctionalInterfaceMethodType(
private Type.MethodType getModeledFunctionalInterfaceMethodType(
Symbol.MethodSymbol functionalInterfaceMethod, Type targetType, VisitorState state) {
verify(config.isJSpecifyMode(), "Method only suitable for use in JSpecify mode");
return TypeSubstitutionUtils.memberType(
state.getTypes(), targetType, functionalInterfaceMethod, config)
.asMethodType();
Type.MethodType methodType =
TypeSubstitutionUtils.memberType(
state.getTypes(), targetType, functionalInterfaceMethod, config)
.asMethodType();
return handler.onOverrideMethodType(functionalInterfaceMethod, methodType, state, null);
}

/**
Expand Down Expand Up @@ -3188,26 +3198,22 @@ public void checkTypeParameterNullnessForMethodOverriding(
*
* @param tree the lambda
* @param fiMethod the functional interface method the lambda implements
* @param fiMethodType its substituted, modeled type, or {@code null} for a raw target
* @param state the visitor state
*/
public void checkTypeParameterNullnessForExplicitLambdaParameters(
LambdaExpressionTree tree, Symbol.MethodSymbol fiMethod, VisitorState state) {
LambdaExpressionTree tree,
Symbol.MethodSymbol fiMethod,
Type.@Nullable MethodType fiMethodType,
VisitorState state) {
if (((JCTree.JCLambda) tree).paramKind != JCTree.JCLambda.ParameterKind.EXPLICIT
|| CodeAnnotationInfo.instance(state.context)
.isSymbolUnannotated(fiMethod, config, handler)) {
return;
}
Type targetType = typeOrNullIfRawNonArray(getPolyExpressionType(tree));
if (targetType == null) {
if (fiMethodType == null) {
return;
}
Type.MethodType fiMethodType =
handler.onOverrideMethodType(
fiMethod,
TypeSubstitutionUtils.memberType(state.getTypes(), targetType, fiMethod, config)
.asMethodType(),
state,
null);
List<? extends VariableTree> parameters = tree.getParameters();
List<Type> fiParameterTypes = fiMethodType.getParameterTypes();
for (int i = 0; i < parameters.size(); i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,72 @@ static class NonNullElementsList extends AbstractList<String> {
.doTest();
}

@Test
public void invocationHandlerLambdaUsesJSpecifyModel() {
makeHelper()
.addSourceLines(
"Test.java",
"""
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;
@NullMarked
class Test {
void test() {
InvocationHandler topLevelMismatch =
// BUG: Diagnostic contains: parameter args is @NonNull
(Object proxy, Method method, Object[] args) -> null;
InvocationHandler handler =
// BUG: Diagnostic contains: mismatched type parameter nullability
(Object proxy, Method method, Object @Nullable [] args) -> null;
InvocationHandler safe =
(Object proxy, Method method, @Nullable Object @Nullable [] args) -> null;
}
}
""")
.doTest();
}

@Test
public void invocationHandlerMethodReferenceUsesJSpecifyModel() {
makeHelper()
.addSourceLines(
"Test.java",
"""
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;
@NullMarked
class Test {
void test() {
// BUG: Diagnostic contains: parameter args of referenced method is @NonNull
InvocationHandler topLevelMismatch = Test::handleTopLevelMismatch;
// BUG: Diagnostic contains: mismatched type parameter nullability
InvocationHandler handler = Test::handle;
InvocationHandler safe = Test::handleSafe;
}

static @Nullable Object handleTopLevelMismatch(
Object proxy, Method method, Object[] args) {
return null;
}

static @Nullable Object handle(
Object proxy, Method method, Object @Nullable [] args) {
return null;
}

static @Nullable Object handleSafe(
Object proxy, Method method, @Nullable Object @Nullable [] args) {
return null;
}
}
""")
.doTest();
}

@Test
public void modeledMethodTypeVariableBoundUsedForOverride() {
makeHelper()
Expand Down
Loading