You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by em...@apache.org on 2022/05/19 20:42:04 UTC

[groovy] branch GROOVY_4_0_X updated: GROOVY-10628: STC: fix inference for elvis assignment with setter target

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

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


The following commit(s) were added to refs/heads/GROOVY_4_0_X by this push:
     new 2311cefe9b GROOVY-10628: STC: fix inference for elvis assignment with setter target
2311cefe9b is described below

commit 2311cefe9bcbbabbc7ad839be856feefe1a2de69
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Thu May 19 14:57:46 2022 -0500

    GROOVY-10628: STC: fix inference for elvis assignment with setter target
---
 .../transform/stc/StaticTypeCheckingVisitor.java   | 13 +++++---
 .../groovy/transform/stc/STCAssignmentTest.groovy  | 35 ++++++++++++++++++----
 2 files changed, 38 insertions(+), 10 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
index e06abf7932..6a4894afe6 100644
--- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
+++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
@@ -1020,10 +1020,15 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
         }
 
         Function<Expression, MethodNode> setterCall = right -> {
-            MethodCallExpression call = callX(receiver, setterInfo.name, right);
-            call.setImplicitThis(false);
-            visitMethodCallExpression(call);
-            return call.getNodeMetaData(DIRECT_METHOD_CALL_TARGET);
+            typeCheckingContext.pushEnclosingBinaryExpression(null); // GROOVY-10628: LHS re-purposed
+            try {
+                MethodCallExpression call = new MethodCallExpression(receiver, setterInfo.name, right);
+                call.setImplicitThis(false);
+                visitMethodCallExpression(call);
+                return call.getNodeMetaData(DIRECT_METHOD_CALL_TARGET);
+            } finally {
+                typeCheckingContext.popEnclosingBinaryExpression();
+            }
         };
 
         Function<MethodNode, ClassNode> setterType = setter -> {
diff --git a/src/test/groovy/transform/stc/STCAssignmentTest.groovy b/src/test/groovy/transform/stc/STCAssignmentTest.groovy
index 2af80307e8..519743417a 100644
--- a/src/test/groovy/transform/stc/STCAssignmentTest.groovy
+++ b/src/test/groovy/transform/stc/STCAssignmentTest.groovy
@@ -1277,7 +1277,7 @@ class STCAssignmentTest extends StaticTypeCheckingTestCase {
     }
 
     // GROOVY-10419
-    void testElvisAssignmentAndSetter() {
+    void testElvisAssignmentAndSetter1() {
         assertScript '''
             class C {
                 def p
@@ -1285,13 +1285,36 @@ class STCAssignmentTest extends StaticTypeCheckingTestCase {
                     this.p = p
                 }
             }
-            @groovy.transform.TypeChecked
-            void test(C c) {
-                c.p ?= 'x'
-            }
             def c = new C()
-            test(c)
+            c.p ?= 'x'
+            assert c.p == 'x'
+            c.with {
+                p ?= 'y'
+            }
             assert c.p == 'x'
         '''
     }
+
+    // GROOVY-10628
+    void testElvisAssignmentAndSetter2() {
+        assertScript '''
+            class C {
+                String getFoo() {
+                }
+                void setFoo(String foo) {
+                }
+            }
+            new C().foo ?= 'bar'
+        '''
+    }
+
+    void testElvisAssignmentMismatched() {
+        shouldFailWithMessages '''
+            class C {
+                Number foo
+            }
+            new C().foo ?= 'bar'
+        ''',
+        'Cannot assign value of type java.io.Serializable to variable of type java.lang.Number'
+    }
 }