This is an automated email from the ASF dual-hosted git repository.
paulk 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 8db3702c0b GROOVY-10953: Extend multi-assignment type checking to
Range expressions
8db3702c0b is described below
commit 8db3702c0bd840ca60a27a913a97eacb6a06e083
Author: Paul King <[email protected]>
AuthorDate: Tue Feb 28 11:29:45 2023 +1000
GROOVY-10953: Extend multi-assignment type checking to Range expressions
---
.../groovy/transform/stc/StaticTypeCheckingVisitor.java | 14 ++++++++++++++
src/test/groovy/transform/stc/STCAssignmentTest.groovy | 15 +++++++++++++++
2 files changed, 29 insertions(+)
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 52c2c133c5..455510e40d 100644
---
a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
+++
b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
@@ -222,6 +222,7 @@ import static
org.codehaus.groovy.ast.tools.GeneralUtils.defaultValueX;
import static org.codehaus.groovy.ast.tools.GeneralUtils.elvisX;
import static org.codehaus.groovy.ast.tools.GeneralUtils.getGetterName;
import static org.codehaus.groovy.ast.tools.GeneralUtils.getSetterName;
+import static org.codehaus.groovy.ast.tools.GeneralUtils.indexX;
import static org.codehaus.groovy.ast.tools.GeneralUtils.isOrImplements;
import static org.codehaus.groovy.ast.tools.GeneralUtils.propX;
import static org.codehaus.groovy.ast.tools.GeneralUtils.thisPropX;
@@ -1210,6 +1211,19 @@ public class StaticTypeCheckingVisitor extends
ClassCodeVisitorSupport {
if (!listExpression.getExpressions().isEmpty()) {
rightExpression = listExpression;
}
+ } else if (rightExpression instanceof RangeExpression) {
+ ListExpression listExpression = new ListExpression();
+ listExpression.setSourcePosition(rightExpression);
+ ClassNode type = getType(((RangeExpression)
rightExpression).getFrom());
+ TupleExpression tuple = (TupleExpression) leftExpression;
+ for (int i = 0; i < tuple.getExpressions().size(); i++) {
+ Expression expression = indexX(rightExpression, constX(i,
true));
+ expression.putNodeMetaData(StaticTypesMarker.INFERRED_TYPE,
type);
+ listExpression.addExpression(expression);
+ }
+ if (!listExpression.getExpressions().isEmpty()) {
+ rightExpression = listExpression;
+ }
}
if (!(rightExpression instanceof ListExpression)) {
diff --git a/src/test/groovy/transform/stc/STCAssignmentTest.groovy
b/src/test/groovy/transform/stc/STCAssignmentTest.groovy
index bdc88ff35d..c4dca150bb 100644
--- a/src/test/groovy/transform/stc/STCAssignmentTest.groovy
+++ b/src/test/groovy/transform/stc/STCAssignmentTest.groovy
@@ -1252,6 +1252,21 @@ class STCAssignmentTest extends
StaticTypeCheckingTestCase {
'''
}
+ // GROOVY-10953
+ void testMultiAssign2() {
+ assertScript '''
+ def m() {
+ def (x, y, z) = 1..4
+ assert "$x $y $z" == '1 2 3'
+ (x, y, z) = -5..-6
+ assert "$x ${y.intValue()} $z" == '-5 -6 null'
+ def (a, _, c) = 'a'..'c'
+ assert "${a.toUpperCase()} ${c.endsWith('c')}" == 'A true'
+ }
+ m()
+ '''
+ }
+
// GROOVY-8220
void testFlowTypingParameterTempTypeAssignmentTracking1() {
assertScript '''