Skip to content
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

Merged

Conversation

akulk022
Copy link
Collaborator

@akulk022 akulk022 commented Oct 12, 2023

We now report an error for the following test case:

class Test {
    interface A<T1 extends @Nullable Object> {
        String function(T1 o);
    }
    static String foo(Object o) {
        return o.toString();
    }
    static void testPositive() {
        // we now report an error here, as foo's parameter needs to be @Nullable
        A<@Nullable Object> p = Test::foo;
    }
    static void testNegative() {
        A<Object> p = Test::foo;
    }
}

@codecov
Copy link

codecov bot commented Oct 12, 2023

Codecov Report

All modified lines are covered by tests ✅

Comparison is base (2d2b829) 86.84% compared to head (6092a56) 86.85%.

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              
Files Coverage Δ
...rc/main/java/com/uber/nullaway/GenericsChecks.java 90.22% <100.00%> (ø)
...away/src/main/java/com/uber/nullaway/NullAway.java 89.73% <100.00%> (+0.04%) ⬆️

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Collaborator

@msridhar msridhar left a 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,
Copy link
Collaborator

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).

Copy link
Collaborator

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.

Copy link
Collaborator

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.

Copy link
Collaborator Author

@akulk022 akulk022 Oct 12, 2023

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",
Copy link
Collaborator

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.

Copy link
Collaborator Author

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

Copy link
Collaborator

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.

@msridhar msridhar marked this pull request as ready for review October 13, 2023 21:51
@msridhar msridhar changed the title changes for the test case: testForMethodReferenceInAnAssignment JSpecify: handle incorrect method parameter nullability for method reference Oct 13, 2023
@msridhar msridhar added the jspecify Related to support for jspecify standard (see jspecify.dev) label Oct 13, 2023
@msridhar
Copy link
Collaborator

I have done some further cleanup here and I think this is ready to land.

Copy link
Collaborator

@lazaroclapp lazaroclapp left a comment

Choose a reason for hiding this comment

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

LGTM!

@msridhar msridhar merged commit e7623f7 into uber:master Oct 15, 2023
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
jspecify Related to support for jspecify standard (see jspecify.dev)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants