This is an automated email from the ASF dual-hosted git repository.

emilles pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git


The following commit(s) were added to refs/heads/master by this push:
     new b3142a3c1a GROOVY-11376: SC: field write for map-based class
b3142a3c1a is described below

commit b3142a3c1a7bda0e0e0a9b0de2813a9a32db8981
Author: Eric Milles <[email protected]>
AuthorDate: Tue May 14 11:04:21 2024 -0500

    GROOVY-11376: SC: field write for map-based class
---
 .../classgen/asm/sc/StaticTypesCallSiteWriter.java |  5 ++-
 src/test/groovy/bugs/Groovy6954.groovy             | 38 ++++++++++++++++++++++
 2 files changed, 42 insertions(+), 1 deletion(-)

diff --git 
a/src/main/java/org/codehaus/groovy/classgen/asm/sc/StaticTypesCallSiteWriter.java
 
b/src/main/java/org/codehaus/groovy/classgen/asm/sc/StaticTypesCallSiteWriter.java
index 6f235ab122..d7c9d55716 100644
--- 
a/src/main/java/org/codehaus/groovy/classgen/asm/sc/StaticTypesCallSiteWriter.java
+++ 
b/src/main/java/org/codehaus/groovy/classgen/asm/sc/StaticTypesCallSiteWriter.java
@@ -784,7 +784,10 @@ public class StaticTypesCallSiteWriter extends 
CallSiteWriter {
                     }
                 }
             }
-            if (isOrImplements(receiverType, MAP_TYPE) && !isClassReceiver[0]) 
{
+            // GROOVY-6954, GROOVY-11376: for map types, replace "map.foo = 
..."
+            // with "map.put('foo', ...)" if no public or protected field 
exists
+            if (!isClassReceiver[0] && isOrImplements(receiverType, MAP_TYPE)
+                    && Optional.ofNullable(getField(receiverType, 
name)).filter(f -> f.isPublic() || f.isProtected()).isEmpty()) {
                 MethodVisitor mv = controller.getMethodVisitor();
 
                 // store value in temporary variable
diff --git a/src/test/groovy/bugs/Groovy6954.groovy 
b/src/test/groovy/bugs/Groovy6954.groovy
index 9e9830f598..1244e0cdb9 100644
--- a/src/test/groovy/bugs/Groovy6954.groovy
+++ b/src/test/groovy/bugs/Groovy6954.groovy
@@ -22,6 +22,44 @@ import 
org.codehaus.groovy.classgen.asm.AbstractBytecodeTestCase
 
 final class Groovy6954 extends AbstractBytecodeTestCase {
 
+    // GROOVY-11376
+    void testSetMapDotField() {
+        assertScript '''import groovy.transform.*
+            @CompileStatic
+            class M extends TreeMap<String,String> {
+                def           a
+                public        b
+                protected     c
+                @PackageScope d
+                private       e
+
+                def testThis() {
+                    this.a = 'a'
+                    this.b = 'b'
+                    this.c = 'c'
+                    this.d = 'd'
+                    this.e = 'e'
+                }
+                def testThat(M that) {
+                    that.a = 'a'
+                    that.b = 'b'
+                    that.c = 'c'
+                    that.d = 'd'
+                    that.e = 'e'
+                }
+            }
+
+            def map = new M()
+            map.testThis()
+            assert map.isEmpty()
+
+            def other = new M()
+            map.testThat(other)
+            assert map.isEmpty()
+            assert other.toString() == '[d:d, e:e]'
+        '''
+    }
+
     void testSetMapDotProperty() {
         extractionOptions.method = 'put'
 

Reply via email to