Skip to content

Improve annotations on KeySetView and related APIs. - #183

Merged
cpovirk merged 1 commit into
mainfrom
cve
Nov 18, 2026
Merged

cpovirk merged 1 commit into
mainfrom
cve

Conversation

@cpovirk

@cpovirk cpovirk commented Nov 10, 2026

Copy link
Copy Markdown
Collaborator

(noticed during #179)

Interesting cases:

  • I notice that removeAll can handle null elements if it falls into the c instanceof Set<?> && c.size() > t.length case or the map.table null case. I'm not sure whether to consider that enough for the parameter's type argument to be nullable or not. Is it fundamentally different from noticing how the method can handle an argument with a nullable element type if that argument happens to contain no elements? (By that logic, even ImmutableList.copyOf should use a nullable type argument for its parameter!) I ended up moving to including a non-null bound on the wildcard.
  • contains and remove are defined in the superclass CollectionView, and each can handle null in at least one package-private subclasses, despite the blanket @throws clause in the documentation. But they do throw for KeySetView, which is the only user-visible type. So I went with a non-null type there, as well.
  • Instead of declaring the type parameter of KeySetView as V extends @Nullable Object, we could leave it as V but declare the obscure getMappedValue() as returning @Nullable V. (I see no calls to that method in our depot, and I don't think I knew it existed before today.) I decided to go with the principled V extends @Nullable Object approach, which might require a handful of updates in Google's Kotlin code (to declare ConcurrentHashMap.KeySetView<K, V?> instead of ConcurrentHashMap.KeySetView<K, V>, but they should probably use Set<K> instead, anyway).

(noticed during #179)

Interesting cases:

- I notice that `removeAll` can handle null elements if it falls into the `c instanceof Set<?> && c.size() > t.length` case or the `map.table` null case. I'm not sure whether to consider that enough for the parameter's type argument to be nullable or not. Is it fundamentally different from noticing how the method can handle an argument with a nullable element type _if that argument happens to contain no elements_? (By _that_ logic, even `ImmutableList.copyOf` should use a nullable type argument for its parameter!) I ended up moving to including a non-null bound on the wildcard.
- `contains` and `remove` are defined in the superclass `CollectionView`, and each can handle `null` in at least one package-private subclasses, despite the blanket `@throws` clause in the documentation. But they do throw for `KeySetView`, which is the only user-visible type. So I went with a non-null type there, as well.
- Instead of declaring the type parameter of `KeySetView` as `V extends @nullable Object`, we could leave it as `V` but declare the obscure `getMappedValue()` as returning `@Nullable V`. (I see no calls to that method in our depot, and I don't think I knew it existed before today.) I decided to go with the principled `V extends @nullable Object` approach, which might require a handful of updates in Google's Kotlin code (to declare `ConcurrentHashMap.KeySetView<K, V?>` instead of `ConcurrentHashMap.KeySetView<K, V>`, but they should probably use `Set<K>` instead, anyway).
@cpovirk

cpovirk commented Nov 10, 2026

Copy link
Copy Markdown
Collaborator Author

(I have another PR in the works, but that's more of an "in a few weeks" timeline. This one should be all for today, at the very least :))

@cpovirk

cpovirk commented Nov 11, 2026 •

Copy link
Copy Markdown
Collaborator Author

Instead of declaring the type parameter of KeySetView as V extends @Nullable Object, we could leave it as V but declare the obscure getMappedValue() as returning @Nullable V. (I see no calls to that method in our depot, and I don't think I knew it existed before today.) I decided to go with the principled V extends @Nullable Object approach, which might require a handful of updates in Google's Kotlin code (to declare ConcurrentHashMap.KeySetView<K, V?> instead of ConcurrentHashMap.KeySetView<K, V>, but they should probably use Set<K> instead, anyway).

My overnight testing ran into a problem [edit: unrelated to my actual changes], so I don't have full results yet. But based on a quick manual search, I'm not sure we'll even need to update any users of KeySetView: The relatively small number of users who write the type KeySetView<...> appear to all be using the static newKeySet() method, which continues to return a KetSetView<K, Boolean>. The only way for callers to be affected is for them to call the instance keySet() method and store the result as a KeySetView<K, Something> (in which case they'd need to switch to KeySetView<K, Something?>).

So I continue to like the proposed version in this CL. (Plus, I'm nowadays always excited when I find a good use case for @NonNull :))

@msridhar

Copy link
Copy Markdown
Collaborator

I'm a bit slammed for the next couple of days. @wmdietl do you have time to look at this one?

@cpovirk

cpovirk commented Nov 11, 2026

Copy link
Copy Markdown
Collaborator Author

(Certainly do feel welcome to review, but zero rush!)

@msridhar msridhar left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@cpovirk
cpovirk merged commit 7409f31 into main Nov 18, 2026
8 checks passed
@cpovirk
cpovirk deleted the cve branch November 18, 2026 23:52
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.

2 participants