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 Nullability for lambda expression parameters for Generic Types #852

Merged
merged 9 commits into from
Oct 25, 2023

Conversation

akulk022
Copy link
Collaborator

@akulk022 akulk022 commented Oct 20, 2023

We were not getting the desired error for the positive test in the following test case:

class Test {
  interface A<T1 extends @Nullable Object> {
    String function(T1 o);
  }
  static void testPositive() {
        // We were not getting the desired error here and these changes address that
	// BUG: Diagnostic contains: dereferenced expression o is @Nullable
	A<@Nullable Object> p = o -> o.toString();
  }
  static void testNegative() {
    A<Object> p = o -> o.toString();
  }
}

Upon changing the way we compute the Nullability of the functional interface parameters, we are able to get the desired error.

Note: These changes only affect lambda parameters and do not deal with return types. That will be addressed in a future PR.

All the test cases in NullAwayJSpecifyGenericTests.java have passed for these changes.

@codecov
Copy link

codecov bot commented Oct 20, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (bc94dcc) 86.90% compared to head (33b5a48) 86.91%.

Additional details and impacted files
@@             Coverage Diff              @@
##             master     #852      +/-   ##
============================================
+ Coverage     86.90%   86.91%   +0.01%     
- Complexity     1879     1881       +2     
============================================
  Files            74       74              
  Lines          6199     6206       +7     
  Branches       1202     1204       +2     
============================================
+ Hits           5387     5394       +7     
  Misses          405      405              
  Partials        407      407              
Files Coverage Δ
...ullaway/dataflow/CoreNullnessStoreInitializer.java 100.00% <100.00%> (ø)

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

@akulk022 akulk022 changed the title JPsecify: handle assignments for lambda expressions JSpecify: handle assignments for lambda expressions Oct 20, 2023
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 promising! One high-level comment to start

Comment on lines 97 to 100
// This obtains the Types of the functional interface method with preserved annotations in case
// of generic types
List<Type> overridenMethodParamTypeList =
types.memberType(ASTHelpers.getType(code), fiMethodSymbol).getParameterTypes();
Copy link
Collaborator

Choose a reason for hiding this comment

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

I was expecting that somewhere we would have to check the config to see if we were in JSpecify mode. I think that coded this way, this will change NullAway's behavior outside of JSpecify mode, which could turn into a compatibility issue for existing code. Could we change this PR so that the changes only apply in JSpecify mode? Also maybe we should write a test to check that the NullAway behavior when JSpecify mode is off is unchanged.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done! Let me know if this works

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.

The code changes look good but we need more tests here. Can we write a test where the lambda takes two parameters, and only one of them is @Nullable according to the generic type arguments?

Also, this PR handles issues with lambda parameters but not return statements in lambdas. We should make clear in the PR description that handling returns from lambdas will come later.

@akulk022 akulk022 changed the title JSpecify: handle assignments for lambda expressions JSpecify: handle assignments for lambda expression parameters Oct 23, 2023
@akulk022 akulk022 changed the title JSpecify: handle assignments for lambda expression parameters JSpecify: handle Nullability for lambda expression parameters for Generic Types Oct 23, 2023
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.

Thanks!

@msridhar msridhar marked this pull request as ready for review October 25, 2023 10:29
@msridhar msridhar added the jspecify Related to support for jspecify standard (see jspecify.dev) label Oct 25, 2023
@msridhar msridhar merged commit 97e92b9 into uber:master Oct 25, 2023
10 checks passed
msridhar added a commit that referenced this pull request Nov 1, 2023
…or Generic Types. (#854)

```java
class Test {
    interface A<T1 extends @nullable Object> {
        T1 function(Object o);
    }
    static void testPositive() {
        // BUG: Diagnostic contains: returning @nullable expression from method with @nonnull return type
        A<String> p = x -> null;
    }
    static void testNegative() {
        // We were reporting a false positive here since the indirect Nullability through the generic was not handled correctly.
        A<@nullable String> p = x -> null;
    }
}
```
For the negative scenario mentioned above where we do not expect an
exception, we were getting a false positive. After computing the
Nullability correctly when it is indirectly applied through the generic
type we no longer get this false positive.

All the test cases in NullAwayJSpecifyGenericTests.java have passed for
these changes.

Similar changes for handling lambda parameters were made under
#852

---------

Co-authored-by: Manu Sridharan <msridhar@gmail.com>
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.

2 participants