You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by pa...@apache.org on 2021/03/18 04:16:51 UTC
[groovy] branch GROOVY_3_0_X updated: GROOVY-9985: STC does not
report type mismatches in array initializers
This is an automated email from the ASF dual-hosted git repository.
paulk 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 d539039 GROOVY-9985: STC does not report type mismatches in array initializers
d539039 is described below
commit d539039b96a8e6d1d1bd873df9fdbbe20b8cfb23
Author: Paul King <pa...@asert.com.au>
AuthorDate: Tue Mar 16 21:08:23 2021 +1000
GROOVY-9985: STC does not report type mismatches in array initializers
---
.../groovy/transform/stc/StaticTypeCheckingVisitor.java | 14 ++++++++++++++
.../transform/stc/ArraysAndCollectionsSTCTest.groovy | 6 ++++++
2 files changed, 20 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 37a224d..7b79d14 100644
--- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
+++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
@@ -49,6 +49,7 @@ import org.codehaus.groovy.ast.PropertyNode;
import org.codehaus.groovy.ast.Variable;
import org.codehaus.groovy.ast.expr.AnnotationConstantExpression;
import org.codehaus.groovy.ast.expr.ArgumentListExpression;
+import org.codehaus.groovy.ast.expr.ArrayExpression;
import org.codehaus.groovy.ast.expr.AttributeExpression;
import org.codehaus.groovy.ast.expr.BinaryExpression;
import org.codehaus.groovy.ast.expr.BitwiseNegationExpression;
@@ -4059,6 +4060,19 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
}
@Override
+ public void visitArrayExpression(ArrayExpression source) {
+ super.visitArrayExpression(source);
+ ClassNode elementType = source.getElementType();
+ for (Expression expression : source.getExpressions()) {
+ if (!checkCast(elementType, expression)) {
+ addStaticTypeError("Cannot assign value of type " +
+ prettyPrintType(getType(expression)) + " into array of type " +
+ prettyPrintType(source.getType()), expression);
+ }
+ }
+ }
+
+ @Override
public void visitCastExpression(final CastExpression expression) {
super.visitCastExpression(expression);
if (!expression.isCoerce()) {
diff --git a/src/test/groovy/transform/stc/ArraysAndCollectionsSTCTest.groovy b/src/test/groovy/transform/stc/ArraysAndCollectionsSTCTest.groovy
index d841ed5..fb0d2ab 100644
--- a/src/test/groovy/transform/stc/ArraysAndCollectionsSTCTest.groovy
+++ b/src/test/groovy/transform/stc/ArraysAndCollectionsSTCTest.groovy
@@ -44,6 +44,12 @@ class ArraysAndCollectionsSTCTest extends StaticTypeCheckingTestCase {
''', 'Cannot assign value of type java.lang.String into array of type int[]'
}
+ void testWrongComponentTypeInArrayInitializer() {
+ shouldFailWithMessages '''
+ int[] intArray = new int[]{'a'}
+ ''', 'Cannot assign value of type java.lang.String into array of type int[]'
+ }
+
void testAssignValueInArrayWithCorrectType() {
assertScript '''
int[] arr2 = [1, 2, 3]