-
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
Proper support for type use annotation locations + develop a deprecation path for incorrect locations #708
Comments
This resolves #705 by broadening our slightly overzealous filtering from #702. We still want to filter type use annotations out when they happen on wildcards or type parameters. However, we can handle annotations on inner types (i.e. `Foo.@nullable Bar`). More importantly, these inner types appear implicitly in natural code involving inner classes, such as: ``` class Bar { @nullable Foo foo; // <- this is, implicitly Bar.@nullable Foo !! class Foo { } } ``` Not handling this case leads to confusing an unintuitive errors where a `@Nullable` annotation with a type use location will seemingly stop working for certain fields at random, if those fields are of an inner type. Additionally, as part of this fix, we restore the handling of type use annotations at the start of an array type as meaning the array object itself is `@Nullable` (i.e. `@Nullable Foo[] arr`, in addition to the correct type use form `Foo @nullable[] arr`). This is technically incorrect, but prohibiting it would break backwards compatibility with older versions of NullAway and surprise users, specially those switching from declaration annotations to type use annotations. See #708 for a full discussion of the tradeoffs and the path forward towards the proper semantics for type use annotations. Co-authored-by: Manu Sridharan <[email protected]>
Added unit tests for annotations on array in JSpecify mode and modified behavior. Currently, NullAway does not support `@Nullable` annotations on array elements (#708). Both `@Nullable String[]` and `String @nullable []` are currently treated identically, and NullAway considers both annotations as the array itself might be null. This is the first step to bring the annotation syntax in line with JSpecify norms, which treats `@Nullable String[]` as the array elements could be null while `String @nullable []` implies the array itself could be null. After this change, NullAway completely ignores type-use annotations on array element types, such as `@Nullable String[]`, and only considers type-use annotations in the correct position for the top-level type, i.e., `String @nullable []`. This is an intermediary change, and support for type annotations will be extended eventually. Additionally, this **only** applies to **JSpecify mode** Unit tests currently conform to the current behavior of NullAway after the changes and some tests have TODOs until we support annotations on type and are able to detect the nullability of array elements. --------- Co-authored-by: Manu Sridharan <[email protected]> Co-authored-by: Lázaro Clapp <[email protected]>
(When you do revisit this, you might find cushon's recent google/error-prone@5123cd5 to be helpful in handling annotations on inner types.) |
My rough plan here is as follows. In an upcoming NullAway release, if you are using type-use annotations like JSpecify's, by default NullAway will only recognize them in the correct locations, even outside JSpecify mode. This is because with the release of JSpecify 1.0 we want to encourage correct placement of these annotations. There will be a flag For declaration annotations outside of JSpecify mode, we will try our best to preserve NullAway's extant behavior. Inside JSpecify mode we can do the same, but maybe also warn about their use? Not sure. If a single annotation is both declaration and type-use, we'll treat it as declaration. JSpecify and Checker Framework I'm not yet sure about multiple annotations on a single element, possibly mixing declaration and type use, that possibly contradict each other. I think mixing contradictory JSpecify annotations is against the JSpecify spec and we can warn on that in JSpecify mode. Seems like maybe we should try to detect and warn on all these cases and see what happens, since it's almost surely some kind of mistake. But, maybe not the highest priority. |
For context, see #706 and particularly this summary by @msridhar (slightly edited from here):
The stop gap fix for the issues above implemented in #706 does the following:
@Nullable Foo [][]
,Foo @Nullable [][]
, andFoo [] @Nullable []
) as marking the array itself as being nullable.A.B.C [] foo
the only two valid places for the annotation are@Nullable A.B.C [] foo
andA.B.C @Nullable [] foo
)Still, this means we fail to correctly follow standard semantics for type use annotations, which will only get more confusing once we want to introduce JSpecify-like support for annotating the nullability of array elements.
We should, relatively soon, introduce a breaking change intentionally that prevents adding
@Nullable
in the wrong position for arrays and inner types. To do this, we probably need to refactor some of our location parsing logic inNullabilityUtil
to provide more information about what is being annotated/queried. We also want a reasonable UX for developers upgrading past this breaking change and/or switching between declaration and type use annotations at any point after the change (i.e. If possible, we should avoid a situation where changing the imports for@Nullable
to a different annotation silently causes nullability to switch from applying to the array reference itself to applying to the elements! Though I am not whether we can do something here...)At any rate, the version introducing this breaking change should bump the "minor" release number, not the "patch" number (not that NullAway follows semver, per se)
The text was updated successfully, but these errors were encountered: