Skip to content

Check IdentityHashMap method arguments for value classes - #6034

Open
jihxonx wants to merge 1 commit into
google:masterfrom
jihxonx:feat/value-class-identity-map-arguments
Open

Check IdentityHashMap method arguments for value classes#6034
jihxonx wants to merge 1 commit into
google:masterfrom
jihxonx:feat/value-class-identity-map-arguments

Conversation

@jihxonx

@jihxonx jihxonx commented Aug 14, 2026

Copy link
Copy Markdown
Contributor

Summary

ValueClassIdentity checks the type arguments at the construction site, so a map
declared with Object type arguments hides the problem:

IdentityHashMap<Object, Object> map = new IdentityHashMap<>();
map.put(LocalDate.of(2026, 8, 14), "x");
map.put(LocalDate.of(2026, 8, 14), "y");
System.out.println(map.size());

On Temurin 28-ea+11 with --enable-preview this prints 1 instead of 2, and
javac -Xlint:all reports nothing. The same applies where the map is a parameter
or field, since the construction site may not be in the same compilation unit.

  • Report value-based expressions passed directly to identity-sensitive IdentityHashMap methods.
  • Handle containsKey, containsValue, get, put, putIfAbsent, and remove.
  • Match IdentityHashMap subclasses while avoiding unrelated overloads.
  • Keep the analysis local to the invocation and avoid dataflow or interprocedural tracking.
  • Check both key and value positions that use reference identity, including the value argument of remove(Object, Object).

Test

  • mvn -pl core test "-Dtest=ValueClassIdentityTest"

Follow-up to #6029

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