Skip to content

GROOVY-3676: store a map literal's GString key as its String rendering - #3345

Open
daniellansun wants to merge 1 commit into
apache:masterfrom
daniellansun:GROOVY-3676
Open

daniellansun wants to merge 1 commit into
apache:masterfrom
daniellansun:GROOVY-3676

Conversation

@daniellansun

Copy link
Copy Markdown
Contributor

https://issues.apache.org/jira/browse/GROOVY-3676

A map literal such as ["${key}": value] used to store the key as a GString. A subscript renders a GString to a String before Map.get, and String.equals does not treat a GString as equal, so both map["${key}"] and map[key] returned null while iteration still showed the entry.

The literal now stores the String from one toString() call, taken when the literal is evaluated. That is the same rendering a dynamic subscript assignment already stored. A spread still copies the source map's keys unchanged, so a GString passed to Map.put stays distinct from the equivalent String. DefaultGroovyMethods.getAt(Map, Object) looks the GString up as itself first and, only when that key is absent, as the rendered String. Static inference of such a literal key is now String.

This is a Groovy 7 behavior change, documented in COMPATIBILITY.md. Do not sync to GROOVY_6_0_X. Coverage is in Groovy3676 and the re-enabled RegressionTest.testGroovy3676.

A subscript renders a GString to a String before Map.get, so a literal
that kept the GString could not be read back with the same interpolation
or with the equivalent String. The literal now stores that rendering.
A spread still copies the keys the source map stored.
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 90.90909% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 74.9541%. Comparing base (09e84fa) to head (e492c1a).
⚠️ Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
...roovy/transform/stc/StaticTypeCheckingVisitor.java 75.0000% 0 Missing and 1 partial ⚠️
Additional details and impacted files

Impacted file tree graph

@@                Coverage Diff                 @@
##               master      #3345        +/-   ##
==================================================
+ Coverage     74.9518%   74.9541%   +0.0023%     
- Complexity      48908      48941        +33     
==================================================
  Files            1768       1768                
  Lines          161213     161264        +51     
  Branches        31728      31744        +16     
==================================================
+ Hits           120832     120874        +42     
- Misses          28968      28974         +6     
- Partials        11413      11416         +3     
Files with missing lines Coverage Δ
.../codehaus/groovy/runtime/DefaultGroovyMethods.java 76.0173% <100.0000%> (+0.0305%) ⬆️
...ava/org/codehaus/groovy/runtime/InvokerHelper.java 74.8344% <100.0000%> (+0.0836%) ⬆️
...codehaus/groovy/runtime/ScriptBytecodeAdapter.java 70.8716% <ø> (ø)
...roovy/transform/stc/StaticTypeCheckingVisitor.java 87.9422% <75.0000%> (+0.0250%) ⬆️

... and 13 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@testlens-app

testlens-app Bot commented Nov 23, 2026

Copy link
Copy Markdown

✅ All tests passed ✅

🏷️ Commit: e492c1a
▶️ Tests: 144646 executed
⚪️ Checks: 27/27 completed


Learn more about TestLens at testlens.app/docs.

Comment thread COMPATIBILITY.md
subscript assignment `map["${key}"] = value` already stored. A later change
to an interpolated value does not rename the entry. Two literal keys that
render the same text are one entry; the later value wins and the first
position is kept.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This sounds wrong. In dynamic Groovy key=1; map = ["${key}": value]; map["${key}"] is still compiled the same. And actually your changed in DGM depends on that. You should try map."1" in dynamic Groovy for the GString case I mentioned. If I am right this will not find the entry, while m["1"] should. Where you did make a change is for static compilation though.

This branch has not been deployed

No deployments
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.

3 participants