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
@@ -0,0 +1,50 @@
package com.uber.nullaway;

import com.sun.tools.javac.code.Symbol;
import org.jspecify.annotations.Nullable;

/**
* Mutable nullness information for a method's formal parameters and varargs array. For a varargs
* method, the nullability of individual arguments is represented in the last parameter position,
* while the nullability of the varargs array itself is represented separately.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we plan to allow using this class in the future to override deep nullability? E.g. allow stuff like overriding argument 1 of foo(List<Bar> bars) to be List<@Nullable Bar> or List<? extends @Nullable Bar> or whatever? Obviously outside of the scope of this PR, but I am curious...

We currently have a separate model from LibraryModels to set these kinds of annotations for third-party libraries / JDK method, right? (using astub(x)?)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right now this is handled by the onOverrideMethodType handler method, see, e.g., here for library models:

https://github.com/uber/NullAway/blob/7b5c9243f89a7a344025006942a0378500d2e954/nullaway/src/main/java/com/uber/nullaway/handlers/LibraryModelsHandler.java#L453

It would be great to unify the two eventually, but it will also be painful given that we still need to handle declaration annotations and other legacy corner cases. Plus, all the original APIs didn't handle nested annotations, and that support has been tacked on for JSpecify mode, in a way that allows for JSpecify mode to be turned off. I think maybe when JSpecify mode is the default, or even better when there is only JSpecify mode for NullAway, we could think about cleaning things up into one handler method.

*/
public final class MethodParameterNullness {

private final @Nullable Nullness[] parameterNullness;
private final boolean hasVarargsArrayNullness;
private @Nullable Nullness varargsArrayNullness;

private MethodParameterNullness(int parameterCount, boolean hasVarargsArrayNullness) {
this.parameterNullness = new Nullness[parameterCount];
this.hasVarargsArrayNullness = hasVarargsArrayNullness;
}

public static MethodParameterNullness create(Symbol.MethodSymbol methodSymbol) {
return new MethodParameterNullness(
methodSymbol.getParameters().size(), methodSymbol.isVarArgs());
}

public @Nullable Nullness getParameterNullness(int index) {
return parameterNullness[index];
}

public void setParameterNullness(int index, @Nullable Nullness nullness) {
parameterNullness[index] = nullness;
}

public @Nullable Nullness getVarargsArrayNullness() {
checkHasVarargsArrayNullness();
return varargsArrayNullness;
}

public void setVarargsArrayNullness(@Nullable Nullness nullness) {
checkHasVarargsArrayNullness();
varargsArrayNullness = nullness;
}

private void checkHasVarargsArrayNullness() {
if (!hasVarargsArrayNullness) {
throw new IllegalStateException("No varargs array nullness slot for non-varargs method");
}
}
}
68 changes: 32 additions & 36 deletions nullaway/src/main/java/com/uber/nullaway/NullAway.java
Original file line number Diff line number Diff line change
Expand Up @@ -816,9 +816,8 @@ private Description checkParamOverriding(
memberReferenceTree, castToNonNull(overridingMethod), state);
}

// Get argument nullability for the overridden method. If overriddenMethodArgNullnessMap[i] is
// null, parameter i is treated as unannotated.
@Nullable Nullness[] overriddenMethodArgNullnessMap = new Nullness[superParamSymbols.size()];
MethodParameterNullness overriddenMethodArgumentNullness =
MethodParameterNullness.create(overriddenMethod);

// Collect @Nullable params of overridden method iff the overridden method is in annotated code
// (otherwise, whether we acknowledge @Nullable in unannotated code or not depends on the
Expand Down Expand Up @@ -861,23 +860,25 @@ private Description checkParamOverriding(
} else {
paramNullness = Nullness.NONNULL;
}
overriddenMethodArgNullnessMap[i] = paramNullness;
overriddenMethodArgumentNullness.setParameterNullness(i, paramNullness);
}
}

// Check handlers for any further/overriding nullness information
overriddenMethodArgNullnessMap =
overriddenMethodArgumentNullness =
handler.onOverrideMethodInvocationParametersNullability(
state.context,
overriddenMethod,
isOverriddenMethodAnnotated,
overriddenMethodArgNullnessMap);
overriddenMethodArgumentNullness);

// If we have an unbound method reference, the first parameter of the overridden method must be
// @NonNull, as this parameter will be used as a method receiver inside the generated lambda.
// e.g. String::length is implemented as (@NonNull s -> s.length()) when used as a
// SomeFunc<String> and thus incompatible with, for example, SomeFunc.apply(@Nullable T).
if (unboundMemberRef && Objects.equals(overriddenMethodArgNullnessMap[0], Nullness.NULLABLE)) {
if (unboundMemberRef
&& Objects.equals(
overriddenMethodArgumentNullness.getParameterNullness(0), Nullness.NULLABLE)) {
String message =
"unbound instance method reference cannot be used, as first parameter of "
+ "functional interface method "
Expand All @@ -897,7 +898,9 @@ private Description checkParamOverriding(
int startParam = unboundMemberRef ? 1 : 0;

for (int i = 0; i < superParamSymbols.size(); i++) {
if (!Objects.equals(overriddenMethodArgNullnessMap[i], Nullness.NULLABLE)) {
Nullness overriddenParameterNullness =
overriddenMethodArgumentNullness.getParameterNullness(i);
if (!Objects.equals(overriddenParameterNullness, Nullness.NULLABLE)) {
// No need to check, unless the argument of the overridden method is effectively @Nullable,
// in which case it can't be overridden by a @NonNull arg.
continue;
Expand Down Expand Up @@ -2051,15 +2054,14 @@ private Description handleInvocation(
});
boolean isMethodAnnotated =
!codeAnnotationInfo.isSymbolUnannotated(methodSymbol, config, handler);
// If argumentPositionNullness[i] == null, parameter i is unannotated
@Nullable Nullness[] argumentPositionNullness = new Nullness[formalParams.size()];
MethodParameterNullness argumentNullness = MethodParameterNullness.create(methodSymbol);

if (isMethodAnnotated) {
// compute which arguments are @NonNull
for (int i = 0; i < formalParams.size(); i++) {
VarSymbol param = formalParams.get(i);
if (param.type.isPrimitive()) {
argumentPositionNullness[i] = Nullness.NONNULL;
argumentNullness.setParameterNullness(i, Nullness.NONNULL);
} else if (ASTHelpers.isSameType(
param.type, Suppliers.JAVA_LANG_VOID_TYPE.get(state), state)) {
// Temporarily treat a Void argument type as if it were @Nullable Void. Handling of Void
Expand All @@ -2068,20 +2070,28 @@ private Description handleInvocation(
// JSpecify semantics.
// See the suppression in https://github.com/uber/NullAway/pull/608 for an example of why
// this is needed.
argumentPositionNullness[i] = Nullness.NULLABLE;
argumentNullness.setParameterNullness(i, Nullness.NULLABLE);
} else {
// we need to call paramHasNullableAnnotation here since the invoked method may be defined
// in a class file
argumentPositionNullness[i] =
argumentNullness.setParameterNullness(
i,
Nullness.paramHasNullableAnnotation(methodSymbol, i, config)
? Nullness.NULLABLE
: ((config.isJSpecifyMode()
&& (tree instanceof MethodInvocationTree || tree instanceof NewClassTree))
? genericsChecks.getGenericParameterNullnessAtInvocation(
i, methodSymbol, tree, state)
: Nullness.NONNULL);
: Nullness.NONNULL));
}
}
if (methodSymbol.isVarArgs()) {
VarSymbol varargsFormalParam = formalParams.get(formalParams.size() - 1);
argumentNullness.setVarargsArrayNullness(
Nullness.varargsArrayIsNullable(varargsFormalParam, config)
? Nullness.NULLABLE
: Nullness.NONNULL);
}

// perform generics checks for calls to annotated methods in JSpecify mode
if (config.isJSpecifyMode()) {
Expand All @@ -2092,41 +2102,27 @@ private Description handleInvocation(
}
}

// Allow handlers to override the list of non-null argument positions. For a varargs parameter,
// this array holds the nullness of individual varargs arguments; the case of a varargs array is
// handled separately
@Nullable Nullness[] finalArgumentPositionNullness =
// Allow handlers to override the list of non-null argument positions.
MethodParameterNullness finalArgumentNullness =
handler.onOverrideMethodInvocationParametersNullability(
state.context, methodSymbol, isMethodAnnotated, argumentPositionNullness);
state.context, methodSymbol, isMethodAnnotated, argumentNullness);

// now actually check the arguments
// NOTE: the case of an invocation on a possibly-null reference
// is handled by matchMemberSelect()
invArgs.forEach(
(actual, argPos, formalParamType, varArgsPassedAsArray) -> {
if (argPos >= formalParams.size()) {
// extra varargs argument; nullness info stored in last position
argPos = finalArgumentPositionNullness.length - 1;
// extra varargs argument; nullness info stored in the varargs parameter slot
argPos = formalParams.size() - 1;
}
boolean argIsNonNull =
Objects.equals(Nullness.NONNULL, finalArgumentPositionNullness[argPos]);
Objects.equals(Nullness.NONNULL, finalArgumentNullness.getParameterNullness(argPos));
if (varArgsPassedAsArray) {
// This is the case where an array is explicitly passed in the position of the
// varargs parameter
VarSymbol varargsFormalParam = formalParams.get(formalParams.size() - 1);
// when is the varargs array itself @NonNull?
// 1. For @NullMarked methods, as long as there is no @Nullable annotation
Nullness varargsArrayNullness =
(isMethodAnnotated && !Nullness.varargsArrayIsNullable(varargsFormalParam, config))
? Nullness.NONNULL
: null;
// 2. For @NullUnmarked methods, there should be an explicit @NonNull annotation. This
// is handled by the RestrictiveAnnotationHandler
// 3. Library model indicating it is @NonNull (handled in LibraryModelsHandler)
varargsArrayNullness =
handler.onOverrideMethodInvocationVarargsArrayNullability(
state.context, methodSymbol, isMethodAnnotated, varargsArrayNullness);
argIsNonNull = Objects.equals(varargsArrayNullness, Nullness.NONNULL);
argIsNonNull =
Objects.equals(Nullness.NONNULL, finalArgumentNullness.getVarargsArrayNullness());
}
if (!argIsNonNull) {
// argument can be @Nullable, so nothing to check
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.sun.tools.javac.util.Context;
import com.uber.nullaway.CodeAnnotationInfo;
import com.uber.nullaway.Config;
import com.uber.nullaway.MethodParameterNullness;
import com.uber.nullaway.NullabilityUtil;
import com.uber.nullaway.Nullness;
import com.uber.nullaway.generics.GenericsChecks;
Expand Down Expand Up @@ -132,29 +133,28 @@ private NullnessStore lambdaInitialStore(
List<Type> overridenMethodParamTypeList =
TypeSubstitutionUtils.memberType(types, lambdaType, fiMethodSymbol, config)
.getParameterTypes();
// If fiArgumentPositionNullness[i] == null, parameter position i is unannotated
@Nullable Nullness[] fiArgumentPositionNullness = new Nullness[fiMethodParameters.size()];
MethodParameterNullness fiArgumentNullness = MethodParameterNullness.create(fiMethodSymbol);
boolean isFIAnnotated =
!codeAnnotationInfo.isSymbolUnannotated(fiMethodSymbol, config, handler);
if (isFIAnnotated) {
for (int i = 0; i < fiMethodParameters.size(); i++) {
if (Nullness.hasNullableAnnotation(fiMethodParameters.get(i), config)) {
// Get the Nullness if the Annotation is directly written with the parameter
fiArgumentPositionNullness[i] = NULLABLE;
fiArgumentNullness.setParameterNullness(i, NULLABLE);
} else if (config.isJSpecifyMode()
&& Nullness.hasNullableAnnotation(
overridenMethodParamTypeList.get(i).getAnnotationMirrors().stream(), config)) {
// Get the Nullness if the Annotation is indirectly applied through a generic type if we
// are in JSpecify mode
fiArgumentPositionNullness[i] = NULLABLE;
fiArgumentNullness.setParameterNullness(i, NULLABLE);
} else {
fiArgumentPositionNullness[i] = NONNULL;
fiArgumentNullness.setParameterNullness(i, NONNULL);
}
}
}
fiArgumentPositionNullness =
fiArgumentNullness =
handler.onOverrideMethodInvocationParametersNullability(
context, fiMethodSymbol, isFIAnnotated, fiArgumentPositionNullness);
context, fiMethodSymbol, isFIAnnotated, fiArgumentNullness);

for (int i = 0; i < parameters.size(); i++) {
LocalVariableNode param = parameters.get(i);
Expand All @@ -170,7 +170,8 @@ private NullnessStore lambdaInitialStore(
// treat as non-null
assumed = NONNULL;
} else {
assumed = fiArgumentPositionNullness[i] == null ? NONNULL : fiArgumentPositionNullness[i];
Nullness fiParameterNullness = fiArgumentNullness.getParameterNullness(i);
assumed = fiParameterNullness == null ? NONNULL : fiParameterNullness;
}
result.setInformation(AccessPath.fromLocal(param), assumed);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import com.sun.tools.javac.code.Types;
import com.sun.tools.javac.util.Context;
import com.uber.nullaway.ErrorMessage;
import com.uber.nullaway.MethodParameterNullness;
import com.uber.nullaway.NullAway;
import com.uber.nullaway.Nullness;
import com.uber.nullaway.dataflow.AccessPath;
Expand Down Expand Up @@ -145,31 +146,17 @@ public boolean onOverrideFieldNullability(Symbol field) {
}

@Override
public @Nullable Nullness[] onOverrideMethodInvocationParametersNullability(
public MethodParameterNullness onOverrideMethodInvocationParametersNullability(
Context context,
Symbol.MethodSymbol methodSymbol,
boolean isAnnotated,
@Nullable Nullness[] argumentPositionNullness) {
MethodParameterNullness argumentNullness) {
for (Handler h : handlers) {
argumentPositionNullness =
argumentNullness =
h.onOverrideMethodInvocationParametersNullability(
context, methodSymbol, isAnnotated, argumentPositionNullness);
context, methodSymbol, isAnnotated, argumentNullness);
}
return argumentPositionNullness;
}

@Override
public @Nullable Nullness onOverrideMethodInvocationVarargsArrayNullability(
Context context,
Symbol.MethodSymbol methodSymbol,
boolean isAnnotated,
@Nullable Nullness varargsArrayNullness) {
for (Handler h : handlers) {
varargsArrayNullness =
h.onOverrideMethodInvocationVarargsArrayNullability(
context, methodSymbol, isAnnotated, varargsArrayNullness);
}
return varargsArrayNullness;
return argumentNullness;
}

@Override
Expand Down
43 changes: 9 additions & 34 deletions nullaway/src/main/java/com/uber/nullaway/handlers/Handler.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import com.sun.tools.javac.util.Context;
import com.uber.nullaway.ErrorMessage;
import com.uber.nullaway.LibraryModels;
import com.uber.nullaway.MethodParameterNullness;
import com.uber.nullaway.NullAway;
import com.uber.nullaway.Nullness;
import com.uber.nullaway.dataflow.AccessPath;
Expand Down Expand Up @@ -207,49 +208,23 @@ default boolean onOverrideFieldNullability(Symbol field) {
* @param isAnnotated A boolean flag indicating whether the called method is considered to be
* within isAnnotated or unannotated code, used to avoid querying for this information
* multiple times within the same handler chain.
* @param argumentPositionNullness Nullness info for each argument position as computed by
* upstream handlers and/or the base analysis. Some entries may be {@code null}, indicating
* @param argumentNullness Nullness info for each argument position as computed by upstream
* handlers and/or the base analysis. Some parameter entries may be {@code null}, indicating
* upstream handlers and the base analysis consider the parameter to be nullness-unknown,
* usually since the parameter is from unannotated code. For a varargs parameter, the nullness
* for when individual parameters are passed in the varargs position will be stored in this
* array. See {@link #onOverrideMethodInvocationVarargsArrayNullability} for overriding the
* usually since the parameter is from unannotated code. For a varargs parameter, the formal
* parameter slot stores the nullness for when individual parameters are passed in the varargs
* position, while {@link MethodParameterNullness#getVarargsArrayNullness()} stores the
* nullness of the varargs array itself.
* @return The updated nullness info for each argument position, as computed by the current
* handler.
*/
default @Nullable Nullness[] onOverrideMethodInvocationParametersNullability(
default MethodParameterNullness onOverrideMethodInvocationParametersNullability(
Context context,
Symbol.MethodSymbol methodSymbol,
boolean isAnnotated,
@Nullable Nullness[] argumentPositionNullness) {
MethodParameterNullness argumentNullness) {
// NoOp
return argumentPositionNullness;
}

/**
* Called to potentially override the nullability of the array itself when a varargs method is
* invoked by passing an array in the varargs position.
*
* <p>This hook is intentionally separate from {@link
* #onOverrideMethodInvocationParametersNullability(Context, Symbol.MethodSymbol, boolean,
* Nullness[])}. A single parameter-slot nullness value is not expressive enough for varargs,
* where the array and its elements can have different nullability.
*
* @param context The current context.
* @param methodSymbol The method symbol for the method in question.
* @param isAnnotated A boolean flag indicating whether the called method is annotated
* @param varargsArrayNullness current value for the varargs array nullability, or {@code null} if
* upstream handlers and the base analysis consider the parameter to be nullness-unknown
* @return Updated explicit nullability for the varargs array, or {@code varargsArrayNullness} if
* the current handler does not provide an override.
*/
default @Nullable Nullness onOverrideMethodInvocationVarargsArrayNullability(
Context context,
Symbol.MethodSymbol methodSymbol,
boolean isAnnotated,
@Nullable Nullness varargsArrayNullness) {
// NoOp
return varargsArrayNullness;
return argumentNullness;
}

/**
Expand Down
Loading
Loading