-
Notifications
You must be signed in to change notification settings - Fork 299
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
JSpecify: handle incorrect method parameter nullability for method reference #845
JSpecify: handle incorrect method parameter nullability for method reference #845
Conversation
Codecov ReportAll modified lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #845 +/- ##
============================================
+ Coverage 86.84% 86.85% +0.01%
- Complexity 1878 1879 +1
============================================
Files 74 74
Lines 6189 6194 +5
Branches 1201 1201
============================================
+ Hits 5375 5380 +5
Misses 406 406
Partials 408 408
☔ View full report in Codecov by Sentry. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very excited that it looks like this works! In the spirit of small PRs, let's get this cleaned up and land it. We can do a separate PR for handling return types of method references.
@@ -876,9 +877,13 @@ public static Nullness getGenericMethodParameterNullness( | |||
int parameterIndex, | |||
Symbol.MethodSymbol method, | |||
Symbol enclosingSymbol, | |||
@Nullable MemberReferenceTree memberReferenceTree, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool that this works! But it's a bit unclean to pass in a MemberReferenceTree
directly. Instead, how about we have a parameter @Nullable Type enclosingType
, and at the caller you pass in the type of the MemberReferenceTree
if the tree is non-null. Then in this method, if enclosingType == null
, we set it to be getTypeForSymbol(enclosingSymbol, state)
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We will also need to update the Javadoc for this method to document the new parameter.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, I have a better idea. Just below this method (line 909), there is another overload of getGenericMethodParameterNullness()
that takes a Type
rather than a Symbol
. How about we not modify this method, and instead at the call site just invoke the other method if we have a MemberReferenceTree
? Right now we'll have to do a null check on the Type
at the caller, but I am also open to making the parameter of the other method @Nullable
and doing the check there.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if we have a MemberReferenceTree!=null, it's type will also not be null right? if I understand correctly is this what you're suggesting?, the place where we call the method from NullAway.java (line 734) to be something like:
if (isOverriddenMethodAnnotated) {
for (int i = 0; i < superParamSymbols.size(); i++) {
overriddenMethodArgNullnessMap[i] =
Nullness.paramHasNullableAnnotation(overriddenMethod, i, config)
? Nullness.NULLABLE
: (config.isJSpecifyMode()
? (memberReferenceTree != null)
? GenericsChecks.getGenericMethodParameterNullness(
i,
overriddenMethod,
ASTHelpers.getType(memberReferenceTree),
state,
config)
: GenericsChecks.getGenericMethodParameterNullness(
i,
overriddenMethod,
overridingParamSymbols.get(i).owner.owner,
state,
config)
: Nullness.NONNULL);
}
}
This does make that line very complicated but then we don't have to change any method signatures. Maybe I misunderstood and what you're suggesting is even simpler
@@ -429,8 +429,7 @@ public void testForMethodReferenceInAnAssignment() { | |||
" return o.toString();", | |||
" }", | |||
" static void testPositive() {", | |||
" // TODO: we should report an error here, since Test::foo cannot take", | |||
" // a @Nullable parameter. we don't catch this yet", | |||
" // BUG: Diagnostic contains: referenced method is @NonNull", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right now, the full error message we emit here is:
warning: [NullAway] parameter o of referenced method is @NonNull, but parameter in functional interface method com.uber.Test.A.function(T1) is @Nullable
It'd be nice if we could find a way to print the functional interface method type so it refers to the annotated type arguments rather than the T1
type variable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree, I'll try and find a way to do that
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After further investigation getting better error messages needs more thought. I'll file a follow-up issue but let's not hold up this PR on this.
…ment to modify the caller instead of the method
I have done some further cleanup here and I think this is ready to land. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
We now report an error for the following test case: