-
Notifications
You must be signed in to change notification settings - Fork 299
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: preserve explicit nullability annotations on type variables when performing substitutions #1143
base: master
Are you sure you want to change the base?
Conversation
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #1143 +/- ##
============================================
- Coverage 88.25% 88.12% -0.13%
- Complexity 2264 2265 +1
============================================
Files 85 86 +1
Lines 7320 7427 +107
Branches 1463 1483 +20
============================================
+ Hits 6460 6545 +85
- Misses 432 445 +13
- Partials 428 437 +9 ☔ View full report in Codecov by Sentry. |
@@ -1,9 +1,18 @@ | |||
package com.uber.nullaway.generics; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What kind of substitution are we referring to here exactly (is it for generic types?)? And why are nullability annotations from type variables lost after performing a substitution - Could you clarify more in the diff description?
@@ -634,13 +634,14 @@ public static void compareGenericTypeParameterNullabilityForCall( | |||
} | |||
if (enclosingType != null) { | |||
invokedMethodType = | |||
TypeSubstitutionUtils.memberType(state.getTypes(), enclosingType, methodSymbol); | |||
TypeSubstitutionUtils.memberType(state.getTypes(), enclosingType, methodSymbol, config); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the passing around of the config variable everywhere required for this bug fix? I don't see it being used anywhere.
Or is it an unrelated change which has been combined with this PR? If so, can we clarify this in the PR description?
Fixes #1091
Consider the following code:
The call to
foo
should not type check. Since the type of its parameterf
isFunction<@Nullable V, @Nullable V>
, with explicit@Nullable
annotations on the type variables, anyFunction
passed tofoo
must have@Nullable
type arguments. In typechecking this code, NullAway previously substituted the type arguments for the type variables infoo
just using built-injavac
routines. But, this would yield a formal parameter typeFunction<String, String>
, as thejavac
routine would not retain the explicit type arguments in the right places. So we would miss reporting an error. This PR fixes the substitutions and re-introduces the annotations on type variables, so we get the typeFunction<@Nullable String, @Nullable String>
for the formal parameter at the call, and report an error correctly.The main logic changes are in
TypeSubstitutionUtils
. We add a newRestoreNullnessAnnotationsVisitor
and use it to restore nullability annotations from type variables after performing a substitution.We also extract the
TypeMetadataBuilder
logic to a top-level source file, and add new methods as needed for this PR. Some of this could have been split into a separate PR but it's a bit of a pain to extract it now.