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

Fix bugs in reading varargs annotations from bytecodes #1055

Merged
merged 22 commits into from
Oct 14, 2024

Conversation

msridhar
Copy link
Collaborator

@msridhar msridhar commented Oct 9, 2024

For the case of declaration annotations, we were using Flags.VARARGS to check if a Symbol represented a varargs parameter, but this only works if the method is defined in source code. We add new methods for varargs parameters that may be defined in bytecodes, and always check for declaration annotations in such cases.

For type use annotations on parameters, for a method from bytecodes the annotation info is stored on the method's symbol, not the parameter's. We borrow some Error Prone code to handle this case.

Copy link

codecov bot commented Oct 9, 2024

Codecov Report

Attention: Patch coverage is 75.67568% with 9 lines in your changes missing coverage. Please review.

Project coverage is 87.60%. Comparing base (2a9188b) to head (f30299a).
Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
...c/main/java/com/uber/nullaway/NullabilityUtil.java 72.72% 5 Missing and 4 partials ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master    #1055      +/-   ##
============================================
- Coverage     87.61%   87.60%   -0.02%     
- Complexity     2183     2203      +20     
============================================
  Files            85       85              
  Lines          7161     7187      +26     
  Branches       1404     1415      +11     
============================================
+ Hits           6274     6296      +22     
- Misses          453      456       +3     
- Partials        434      435       +1     

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

@msridhar msridhar marked this pull request as draft October 10, 2024 00:20
@msridhar
Copy link
Collaborator Author

Found another bug, will try to fix it as part of this PR

@msridhar msridhar changed the title Fix bug in reading varargs declaration annotations from bytecodes Fix bugs in reading varargs annotations from bytecodes Oct 10, 2024
@msridhar msridhar marked this pull request as ready for review October 10, 2024 03:28
@msridhar msridhar marked this pull request as draft October 10, 2024 15:52
@msridhar msridhar marked this pull request as ready for review October 11, 2024 19:44
@msridhar
Copy link
Collaborator Author

Now ready for review again 🙂

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.

Some nits, but overall LGTM

@@ -47,4 +47,24 @@ public void methodReferenceOnNullableVariable() {
"}")
.doTest();
}

@Test
public void testNullableLambdaParamTypeUse() {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do we have the true positive case test somewhere? E.g. something showing that:

NonNullParamFunctionTypeUse n3 = (@Nullable Object x) -> (x == null) ? \"null\" : x.toString();",

Fails if NonNullParamFunctionTypeUse is equivalent to NonNullParamFunctionTypeUse but without @Nullable on takeVal.

Also, how about assigning a lambda that doesn't check for null?

Feel free to ignore if these tests already exist somewhere (they well might), just checking since this test below implies those cases too.

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 think those tests exist here:

https://github.com/uber/NullAway/blob/master/nullaway/src/test/resources/com/uber/nullaway/testdata/NullAwayJava8NegativeCases.java
https://github.com/uber/NullAway/blob/master/nullaway/src/test/resources/com/uber/nullaway/testdata/NullAwayJava8PositiveCases.java

The key difference in the new unit test is that @Nullable is a type-use annotation rather than a declaration annotation, and with our new logic we initially weren't reading it out correctly. I added a comment in a59a7a8

"public class Test {",
" public void testDeclaration() {",
" String x = null;",
" Varargs s = new Varargs(x);",
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we add the case passing new Varargs(x, y) where x is non-null but y is @Nullable?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

" String[] y = null;",
" // BUG: Diagnostic contains: passing @Nullable parameter 'x'",
" RestrictivelyAnnotatedVarargs.test(x);",
" RestrictivelyAnnotatedVarargs.test(y);",
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is there a way to annotate varargs as non-null array of non-null elements? Do we want a test case for that? (separate from RestrictivelyAnnotatedVarargs.test)

I'd even argue that it is exceedingly rare for the intention of a varargs argument to be "a potentially nullable array of non-null elements", but I guess we should follow JSpecify here either way.

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 added this test in f30299a and it exposed a bug which I fixed! We still don't report an error when passing a @Nullable array for this case; that requires a full and proper fix for #1027

Comment on lines 347 to 348
switch (position.type) {
case METHOD_FORMAL_PARAMETER:
Copy link
Collaborator

Choose a reason for hiding this comment

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

If we care only about one case, why is this a switch and not an if?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@msridhar msridhar enabled auto-merge (squash) October 14, 2024 23:37
@msridhar msridhar merged commit 91cf25d into uber:master Oct 14, 2024
9 of 11 checks passed
@msridhar msridhar deleted the varargs-bytecode-bug branch October 14, 2024 23:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants