Skip UseMapOf when the anonymous HashMap is assigned to a concrete HashMap variable#1149
Merged
Merged
Conversation
b80a19b to
cb3243d
Compare
HashMap declared type when UseMapOf rewrites it to Map.of(..)UseMapOf rewrite when the assignment target is a concrete HashMap
cb3243d to
1251368
Compare
UseMapOf rewrite when the assignment target is a concrete HashMapUseMapOf when the anonymous HashMap is assigned to a concrete HashMap variable
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.
Fixes #1148
Problem
For the anonymous-class form,
UseMapOfreplacednew HashMap<>() {{ put(..); }}with an immutableMap.of(..)but left the declared type untouched. When the variable was declared with the concrete typeHashMap(rather than theMapinterface), the result no longer compiled, sinceMap.of(..)returns an immutablejava.util.Mapthat is not assignable to aHashMapvariable:Fix
Skip the anonymous-class rewrite when its result is assigned to a variable whose declared type is the concrete
HashMaprather than theMapinterface (option 2 in the issue). Beyond the assignability problem, widening the declared type toMapwould also risk breaking later calls toHashMap-specific methods, so leaving such declarations untouched is the safer behaviour.The guard checks the enclosing variable's declared type straight off the
NamedVariable:The common cases — assigning to a
Map-typed variable, returning, or using the value inline — are unaffected and still rewrite toMap.of(..). The mutable prose form (new HashMap<>(); x.put(..)→new HashMap<>(Map.of(..))) already stays aHashMapand so was never affected.Tests
Added field and local-variable cases asserting that a concrete
HashMapdeclaration is left unchanged (the field case mirrors the issue's reproducer withjavaVersion(25)). Existing tests — including theMap-interface,LinkedHashMap/TreeMap, null-bail, and prose-form cases — continue to pass.