Repository: groovy Updated Branches: refs/heads/GROOVY_2_4_X 423d47d35 -> f5b022274
GROOVY-8319: Improve smart type on list expressions (closes #602) (cherry picked from commit 70ce561) Project: http://git-wip-us.apache.org/repos/asf/groovy/repo Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/f5b02227 Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/f5b02227 Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/f5b02227 Branch: refs/heads/GROOVY_2_4_X Commit: f5b02227426a4b9c135fd7b1b1d98b1532dc97bf Parents: 423d47d Author: alexey.afanasiev <alexey.afanas...@jetbrains.com> Authored: Fri Sep 15 22:16:11 2017 +0800 Committer: sunlan <sun...@apache.org> Committed: Sat Sep 16 00:31:38 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/f5b02227/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 06ac1eb..86dcec3 100644 --- a/src/main/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java +++ b/src/main/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java @@ -875,6 +875,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); } } @@ -925,7 +927,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/f5b02227/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() + } + ''' + } }