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

"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
copybara-service bot pushed a commit to google/error-prone that referenced this pull request Oct 17, 2024
I discovered these issues while using this code in NullAway; see uber/NullAway#1055.  (We support older Error Prone versions so can't directly call this API and had to adapt the code instead.)  The previous code didn't handle explicit annotations on lambda parameters (e.g., `(@nullable Object x) -> { ... }`) and annotations on enum constants.  Not sure the best way to add tests for this but happy to add them if given a suggestion.  Actually the enum constant case was weird; I was unable to repro with bytecodes output by `javac` and I only observed the case with an `ijar` from Bazel.

FYI @cpovirk @cushon

Fixes #4620

FUTURE_COPYBARA_INTEGRATE_REVIEW=#4620 from msridhar:more-annotations-tweaks fb3690b
PiperOrigin-RevId: 686729409
copybara-service bot pushed a commit to google/error-prone that referenced this pull request Oct 17, 2024
I discovered these issues while using this code in NullAway; see uber/NullAway#1055.  (We support older Error Prone versions so can't directly call this API and had to adapt the code instead.)  The previous code didn't handle explicit annotations on lambda parameters (e.g., `(@nullable Object x) -> { ... }`) and annotations on enum constants.  Not sure the best way to add tests for this but happy to add them if given a suggestion.  Actually the enum constant case was weird; I was unable to repro with bytecodes output by `javac` and I only observed the case with an `ijar` from Bazel.

FYI @cpovirk @cushon

Fixes #4620

COPYBARA_INTEGRATE_REVIEW=#4620 from msridhar:more-annotations-tweaks fb3690b
PiperOrigin-RevId: 686916301
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