Repository: groovy Updated Branches: refs/heads/master 10cba2aff -> 70ce561c1
GROOVY-8319: Improve smart type on list expressions (closes #602) Project: http://git-wip-us.apache.org/repos/asf/groovy/repo Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/70ce561c Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/70ce561c Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/70ce561c Branch: refs/heads/master Commit: 70ce561c1d797bd19f0bcbef79dd2f45867b2441 Parents: 10cba2a Author: alexey.afanasiev <alexey.afanas...@jetbrains.com> Authored: Fri Sep 15 17:16:11 2017 +0300 Committer: sunlan <sun...@apache.org> Committed: Sat Sep 16 00:25:00 2017 +0800 ---------------------------------------------------------------------- .../stc/StaticTypeCheckingVisitor.java | 4 ++- .../transform/stc/STCAssignmentTest.groovy | 28 ++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/groovy/blob/70ce561c/src/main/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java ---------------------------------------------------------------------- diff --git a/src/main/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java b/src/main/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java index a257aa0..28c6368 100644 --- a/src/main/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java +++ b/src/main/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java @@ -873,6 +873,8 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport { if (!isAssignableTo(elemType, tupleType)) { addStaticTypeError("Cannot assign value of type " + elemType.toString(false) + " to variable of type " + tupleType.toString(false), rightExpression); return false; // avoids too many errors + } else { + storeType(tupleExpression, elemType); } } @@ -923,7 +925,7 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport { } } else if (rightExpression instanceof ListExpression) { for (Expression element : ((ListExpression) rightExpression).getExpressions()) { - ClassNode rightComponentType = element.getType().redirect(); + ClassNode rightComponentType = this.getType(element); if (!checkCompatibleAssignmentTypes(leftComponentType, rightComponentType) && !(isNullConstant(element) && !isPrimitiveType(leftComponentType))) { addStaticTypeError("Cannot assign value of type " + rightComponentType.toString(false) + " into array of type " + lhsType.toString(false), rightExpression); http://git-wip-us.apache.org/repos/asf/groovy/blob/70ce561c/src/test/groovy/transform/stc/STCAssignmentTest.groovy ---------------------------------------------------------------------- diff --git a/src/test/groovy/transform/stc/STCAssignmentTest.groovy b/src/test/groovy/transform/stc/STCAssignmentTest.groovy index 7b40304..3c00185 100644 --- a/src/test/groovy/transform/stc/STCAssignmentTest.groovy +++ b/src/test/groovy/transform/stc/STCAssignmentTest.groovy @@ -844,5 +844,33 @@ class STCAssignmentTest extends StaticTypeCheckingTestCase { assert fooParameterAssignment(null) == 42 ''' } + + void testIntegerArraySmartType() { + assertScript ''' + def m() { + def a = 1 + Integer[] b = [a] + } + ''' + } + + void testIntegerSecondDimArraySmartType() { + assertScript ''' + def m() { + def a = new int[5] + int[][] b = [a] + } + ''' + } + + void testMultiAssign() { + assertScript ''' + def m() { + def row = ["", "", ""] + def (left, right) = [row[0], row[1]] + left.toUpperCase() + } + ''' + } }