Skip to content

Refine the nullability logic for type variables.#2048

Merged
copybara-service[bot] merged 1 commit intomainfrom
test_907230601
May 1, 2026
Merged

Refine the nullability logic for type variables.#2048
copybara-service[bot] merged 1 commit intomainfrom
test_907230601

Conversation

@copybara-service
Copy link
Copy Markdown
Contributor

Refine the nullability logic for type variables.

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.

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
@copybara-service copybara-service Bot merged commit ae8b1e3 into main May 1, 2026
@copybara-service copybara-service Bot deleted the test_907230601 branch May 1, 2026 15:32
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.

1 participant