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 21:03:38 UTC
[groovy] branch GROOVY_3_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_3_0_X
in repository https://gitbox.apache.org/repos/asf/groovy.git
The following commit(s) were added to refs/heads/GROOVY_3_0_X by this push:
new 7bcf3cdca0 GROOVY-10628: STC: fix inference for elvis assignment with setter target
7bcf3cdca0 is described below
commit 7bcf3cdca031b7e9bc42f3e2d51d641c2a2ef69c
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
3_0_X backport
---
.../transform/stc/StaticTypeCheckingVisitor.java | 13 +++++++----
.../groovy/transform/stc/STCAssignmentTest.groovy | 25 ++++++++++++++++------
2 files changed, 28 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 76f4c35b17..ba83a7144e 100644
--- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
+++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
@@ -986,9 +986,14 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
valueExpression = binX(leftExpression, op, rightExpression);
}
}
- MethodCallExpression call = callX(ve, setterInfo.name, valueExpression);
- call.setImplicitThis(false);
- visitMethodCallExpression(call);
+ MethodCallExpression call = new MethodCallExpression(ve, setterInfo.name, valueExpression);
+ typeCheckingContext.pushEnclosingBinaryExpression(null); // GROOVY-10628: LHS re-purposed
+ try {
+ call.setImplicitThis(false);
+ visitMethodCallExpression(call);
+ } finally {
+ typeCheckingContext.popEnclosingBinaryExpression();
+ }
MethodNode directSetterCandidate = call.getNodeMetaData(DIRECT_METHOD_CALL_TARGET);
if (directSetterCandidate == null) {
// this may happen if there's a setter of type boolean/String/Class, and that we are using the property
@@ -996,7 +1001,7 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
for (MethodNode setter : setterInfo.setters) {
ClassNode type = getWrapper(setter.getParameters()[0].getOriginType());
if (Boolean_TYPE.equals(type) || STRING_TYPE.equals(type) || CLASS_Type.equals(type)) {
- call = callX(ve, setterInfo.name, castX(type, valueExpression));
+ call = new MethodCallExpression(ve, setterInfo.name, castX(type, valueExpression));
call.setImplicitThis(false);
visitMethodCallExpression(call);
directSetterCandidate = call.getNodeMetaData(DIRECT_METHOD_CALL_TARGET);
diff --git a/src/test/groovy/transform/stc/STCAssignmentTest.groovy b/src/test/groovy/transform/stc/STCAssignmentTest.groovy
index 13fb4347af..b852eff171 100644
--- a/src/test/groovy/transform/stc/STCAssignmentTest.groovy
+++ b/src/test/groovy/transform/stc/STCAssignmentTest.groovy
@@ -1114,7 +1114,7 @@ class STCAssignmentTest extends StaticTypeCheckingTestCase {
}
// GROOVY-10419
- void testElvisAssignmentAndSetter() {
+ void testElvisAssignmentAndSetter1() {
assertScript '''
class C {
def p
@@ -1122,13 +1122,26 @@ 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'
+ '''
+ }
}