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

Add Guava 31+ support by treating @ParametricNullness as @Nullable #629

Merged
merged 5 commits into from
Aug 2, 2022

Conversation

lazaroclapp
Copy link
Collaborator

Guava versions from 31.0 onwards do away with annotating methods as
returning or taking @Nullable if nullability depends on the type
parameter. Instead adopting JSpecify semantics for precise handling
of type parameters.

For compatibility (with Kotlin, among other tools), Guava still marks
such returns and arguments specially as @ParametricNullness.

While imprecise, we can handle Guava as annotated code to at least the
same level as we did previously, by treating @ParametricNullness as
an alias for @Nullable, until we have full type parameter / generics
support within NullAway.

To test this change without bumping our own Guava dependency, we
introduce a new test target: :guava-recent-unit-tests.

Additionally, since there are actually multiple instances of
@ParametricNullness in Guava (one per subpackage), we hard-code
a check for com.google.common.*.ParametricNullness in our core
isNullableAnnotation(...) check.

An alternative would be to extend -XepOpt:NullAway:CustomNullableAnnotations
to allow simple names or regular expressions, but that would make
the mechanism more costly in common case.

See also: #628, google/guava#6126

Guava versions from 31.0 onwards do away with annotating methods as
returning or taking `@Nullable` if nullability depends on a type
parameter. Instead adopting JSpecify semantics.

For compatibility (with Kotlin, among other tools), Guava still marks
such returns and arguments specially as `@ParametricNullness`.

While imprecise, we can handle Guava as annotated code to at least the
same level as we did previously, by treating `@ParametricNullness` as
an alias for `@Nullable`, until we have full type parameter / generics
support within NullAway.

To test this change without bumping our own Guava dependency, we
intoduce a new test target: `guava-recent-unit-tests`.

Additionally, since there are actually multiple instances of
`@ParametricNullness` in Guava (one per subpackage), we hard-code
a check for `com.google.common.*.ParametricNullness` in our core
`isNullableAnnotation(...)` check.

An alternative would be to extend `-XepOpt:NullAway:CustomNullableAnnotations`
to allow simple names or regular expressions, but that would make
the mechanism more costly in common case.

See:
@lazaroclapp lazaroclapp requested a review from msridhar August 2, 2022 20:29
@coveralls
Copy link

coveralls commented Aug 2, 2022

Pull Request Test Coverage Report for Build #896

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.02%) to 92.488%

Totals Coverage Status
Change from base Build #893: 0.02%
Covered Lines: 4802
Relevant Lines: 5192

💛 - Coveralls

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.

LGTM! Approved assuming comment is addressed before landing

*/
plugins {
id 'java-library'
id 'nullaway.jacoco-conventions'
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 want coverage from tests in this module to be included as part of the overall code coverage report (sent to Coveralls), we should add an implementation dependence on this module here:

dependencies {
implementation project(':nullaway')
implementation project(':jar-infer:jar-infer-lib')
implementation project(':jar-infer:nullaway-integration-test')
implementation project(':jdk17-unit-tests')
}

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. Also making sure this builds on all JDKs we support.

@lazaroclapp lazaroclapp merged commit 6ab3dcd into master Aug 2, 2022
@lazaroclapp lazaroclapp deleted the lazaro_guava_parametric_nullness branch August 2, 2022 20:53
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.

3 participants