Skip to content
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

Make some type parameters inside ConcurrentHashMap non-null. #70

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

cpovirk
Copy link
Collaborator

@cpovirk cpovirk commented Oct 3, 2024

We already had the parameters on the class itself correct. However, we
had declarations inside incorrect. This PR fixes most but not all.

For the most part, this PR doesn't matter: Most of these APIs are not
user-visible. We had them annotated in the first place only because our
CF-stub-conversion tool changes <T> to <T extends @Nullable Object>
everywhere inside @NullMarked code, without regard to whether the API
is publicly visible. In fairness, this makes some sense because a
non-publicly visible type can be extended by a publicly visible type,
which can render the non-publicly visible type's APIs suddenly publicly
visible. In fact, that happens in this very file: The package-private
CollectionView is extended by the public KeySetView. Still, for
most of the non-visible APIs, this PR has no actual effect: It's just
a simplification (by removing information about non-visible APIs, which
we don't normally annotate) that happens to also make those APIs'
annotations more correct.

For public APIs like KeySetView itself, it would have made sense for
the CF stubs to have <T extends Object>, which would have translated
into plain <T> in our stubs. The PR's change to <T> could affect
existing callers.

I will be back with another PR that handles some other type parameters
as part of actually annotating some other unannotated APIs in the class.

We already had the parameters on the class itself correct. However, we
had declarations _inside_ incorrect. This PR fixes most but not all.

For the most part, this PR doesn't matter: Most of these APIs are not
user-visible. We had them annotated in the first place only because our
CF-stub-conversion tool changes `<T>` to `<T extends @nullable Object>`
everywhere inside `@NullMarked` code, without regard to whether the API
is publicly visible. In fairness, this makes some sense because a
non-publicly visible type can be extended by a publicly visible type,
which can render the non-publicly visible type's APIs suddenly publicly
visible. In fact, that happens in this very file: The package-private
`CollectionView` is extended by the public `KeySetView`. Still, for
_most_ of the non-visible APIs, this PR has no actual effect: It's just
a simplification (by removing information about non-visible APIs, which
we don't normally annotate) that happens to also make those APIs'
annotations more correct.

For public APIs like `KeySetView` itself, it would have made sense for
the CF stubs to have `<T extends Object>`, which would have translated
into plain `<T>` in our stubs. The PR's change to `<T>` could affect
existing callers.

I will be back with another PR that handles some other type parameters
as part of actually annotating some other unannotated APIs in the class.
Copy link
Collaborator

@wmdietl wmdietl left a comment

Choose a reason for hiding this comment

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

Null-hostility looks good, just one place would benefit from some comment.

@@ -627,7 +627,7 @@ public class ConcurrentHashMap<K,V> extends AbstractMap<K,V>
* are special, and contain null keys and values (but are never
* exported). Otherwise, keys and vals are never null.
*/
static class Node<K extends @Nullable Object,V extends @Nullable Object> implements Map.Entry<K,V> {
static class Node<K,V> implements Map.Entry<K,V> {
Copy link
Collaborator

Choose a reason for hiding this comment

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

The comment above says Subclasses of Node with a negative hash field are special, and contain null keys and values.
To account for these subclasses, the type parameters here should allow null.
If you did consider this, could you add a CFComment why this is better?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Thanks, I hadn't. This change was just part of my mission to remove annotations from APIs that aren't visible to users. I left some new notes about that in #101. Let me know whether you have thoughts on that approach, whether this type is exposed through a subtype you missed, or if there's anything else I might be overlooking here.

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