Refine the nullability logic for type variables.#2048
Merged
copybara-service[bot] merged 1 commit intomainfrom May 1, 2026
Merged
Refine the nullability logic for type variables.#2048copybara-service[bot] merged 1 commit intomainfrom
copybara-service[bot] merged 1 commit intomainfrom
Conversation
We allow an AutoValue or AutoBuilder property of type `T` to be null (meaning we omit null checks) if `T` has a nullable bound, for example `<T extends @nullable Object>`. But previously we implemented this in a confusing way, essentially pretending that `T` had its own `@Nullable` annotation. The problem is that sometimes that would lead us not to add `@Nullable` to a declaration of type `T` because we thought there already was one. For example, non-primitive builder fields start out null, so they should be declared `@Nullable` even if the final built property is not `@Nullable`. That didn't work for `<T extends @nullable Object>`: because we were pretending that `T` already had a `@Nullable` annotation, we ended up declaring the builder field as `private T bar;` instead of `private @nullable T bar;`. To fix this problem, we stop pretending that `<T extends @nullable Object>` has a `@Nullable` annotation, and instead introduce a separate method that specifically tests whether `T` is a type variable with a `@Nullable` bound. RELNOTES=A problem has been fixed where builder field declarations for properties whose type was a type variable with a `@Nullable` bound would not have their own `@Nullable` marker. PiperOrigin-RevId: 908747236
0e451a5 to
ae8b1e3
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Refine the nullability logic for type variables.
We allow an AutoValue or AutoBuilder property of type
Tto be null (meaning we omit null checks) ifThas a nullable bound, for example<T extends @Nullable Object>. But previously we implemented this in a confusing way, essentially pretending thatThad its own@Nullableannotation. The problem is that sometimes that would lead us not to add@Nullableto a declaration of typeTbecause we thought there already was one. For example, non-primitive builder fields start out null, so they should be declared@Nullableeven if the final built property is not@Nullable. That didn't work for<T extends @Nullable Object>: because we were pretending thatTalready had a@Nullableannotation, we ended up declaring the builder field asprivate T bar;instead ofprivate @Nullable T bar;.To fix this problem, we stop pretending that
<T extends @Nullable Object>has a@Nullableannotation, and instead introduce a separate method that specifically tests whetherTis a type variable with a@Nullablebound.RELNOTES=A problem has been fixed where builder field declarations for properties whose type was a type variable with a
@Nullablebound would not have their own@Nullablemarker.