Skip to content

Better @MonotonicNonNull support#1149

Merged
msridhar merged 10 commits intomasterfrom
monotonic-nonnull-support
Feb 25, 2025
Merged

Better @MonotonicNonNull support#1149
msridhar merged 10 commits intomasterfrom
monotonic-nonnull-support

Conversation

@msridhar
Copy link
Copy Markdown
Collaborator

@msridhar msridhar commented Feb 24, 2025

Fixes #1148

We add explicit support for any annotation named @MonotonicNonNull and add our own version of the annotation to our annotations package. The main additional support is that we now reason that once assigned a non-null value, @MonotonicNull fields remain non-null when accessed from subsequent lambdas, even if the lambdas are invoked asynchronously.

@codecov
Copy link
Copy Markdown

codecov bot commented Feb 24, 2025

Codecov Report

Attention: Patch coverage is 96.15385% with 1 line in your changes missing coverage. Please review.

Project coverage is 88.15%. Comparing base (15c817a) to head (70f8542).
Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
...va/com/uber/nullaway/ErrorProneCLIFlagsConfig.java 0.00% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master    #1149      +/-   ##
============================================
+ Coverage     88.12%   88.15%   +0.02%     
- Complexity     2268     2281      +13     
============================================
  Files            87       87              
  Lines          7438     7455      +17     
  Branches       1484     1490       +6     
============================================
+ Hits           6555     6572      +17     
  Misses          445      445              
  Partials        438      438              

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

@msridhar msridhar marked this pull request as ready for review February 24, 2025 03:04
|| symbol.isEnum()
|| codeAnnotationInfo.isSymbolUnannotated(symbol, config, null))
&& Nullness.hasNullableAnnotation(symbol, config);
&& Nullness.hasNullableFieldAnnotation(symbol, config);
Copy link
Copy Markdown
Collaborator

@akshayutture akshayutture Feb 24, 2025

Choose a reason for hiding this comment

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

nit: At a first glance, it is not obvious that 'hasNullableFieldAnnotation' includes both @nullable and @MonotonicNonNull. It sounds like it only includes the @nullable annotation.

One possibility is to rename it to something like "hasNullabilityFieldAnnotation", so that one does not mistake it to only include @nullable annotation.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Renamed to hasNullableOrMonotonicNonNullAnnotation in 1aa9747

|| !e.getModifiers().contains(Modifier.FINAL)) {
allAPNonRootElementsAreFinalFields = false;
break;
if (i != elements.size() - 1) { // "inner" elements of the access path, must be fields
Copy link
Copy Markdown
Collaborator

@akshayutture akshayutture Feb 24, 2025

Choose a reason for hiding this comment

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

Why must inner elements of the access path be fields?

We 'need them to be fields', but can't assume that they 'must be fields'. Why has the original "e.getKind().equals(ElementKind.FIELD)" check become unnecessary?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

You're right, this is sketchy. I was trying to simplify logic but may have over-simplified. Will dig in and give a better explanation.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I restored the checks for fields and added tests showing why they are required in bbfd36c

Nullness.isMonotonicNonNullAnnotation(
am.getAnnotationType().toString()))) {
allAPNonRootElementsAreFinalFields = false;
break;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

nit: unnecessary break since this is the last iteration.

Copy link
Copy Markdown
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) February 25, 2025 00:30
@msridhar msridhar merged commit 8e525e6 into master Feb 25, 2025
12 checks passed
@msridhar msridhar deleted the monotonic-nonnull-support branch February 25, 2025 00:41
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.

Feature Request: Use an annotation like @MonotonicNonNull for better reasoning about field nullability inside lambdas

2 participants