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 2021/05/26 19:19:52 UTC

[groovy] branch GROOVY-10111 created (now 49046dd)

This is an automated email from the ASF dual-hosted git repository.

emilles pushed a change to branch GROOVY-10111
in repository https://gitbox.apache.org/repos/asf/groovy.git.


      at 49046dd  GROOVY-9985, GROOVY-10111: STC: check array sizes and values not as cast

This branch includes the following new commits:

     new 49046dd  GROOVY-9985, GROOVY-10111: STC: check array sizes and values not as cast

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


[groovy] 01/01: GROOVY-9985, GROOVY-10111: STC: check array sizes and values not as cast

Posted by em...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

emilles pushed a commit to branch GROOVY-10111
in repository https://gitbox.apache.org/repos/asf/groovy.git

commit 49046ddcdebbf653f05feef0a553c79f41ab81f6
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Wed May 26 14:03:03 2021 -0500

    GROOVY-9985, GROOVY-10111: STC: check array sizes and values not as cast
---
 .../transform/stc/StaticTypeCheckingVisitor.java   | 22 +++++++++-----
 .../stc/ArraysAndCollectionsSTCTest.groovy         | 35 ++++++++++++++++++++--
 2 files changed, 46 insertions(+), 11 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 ca85db6..5ac3efe 100644
--- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
+++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
@@ -4121,14 +4121,20 @@ 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);
+    public void visitArrayExpression(final ArrayExpression expression) {
+        super.visitArrayExpression(expression);
+        ClassNode elementType;
+        List<Expression> expressions;
+        if (expression.hasInitializer()) {
+            elementType = expression.getElementType();
+            expressions = expression.getExpressions();
+        } else {
+            elementType = int_TYPE;
+            expressions = expression.getSizeExpression();
+        }
+        for (Expression elementExpr : expressions) {
+            if (!checkCompatibleAssignmentTypes(elementType, getType(elementExpr), elementExpr, false)) {
+                addStaticTypeError("Cannot convert from " + prettyPrintType(getType(elementExpr)) + " to " + prettyPrintType(elementType), elementExpr);
             }
         }
     }
diff --git a/src/test/groovy/transform/stc/ArraysAndCollectionsSTCTest.groovy b/src/test/groovy/transform/stc/ArraysAndCollectionsSTCTest.groovy
index 69d7e89..1c8d6da 100644
--- a/src/test/groovy/transform/stc/ArraysAndCollectionsSTCTest.groovy
+++ b/src/test/groovy/transform/stc/ArraysAndCollectionsSTCTest.groovy
@@ -27,26 +27,55 @@ class ArraysAndCollectionsSTCTest extends StaticTypeCheckingTestCase {
         assertScript '''
             String[] strings = ['a','b','c']
             String str = strings[0]
+            assert str == 'a'
         '''
     }
 
-    void testArrayElementReturnType() {
+    void testArrayElementTypeInference() {
         shouldFailWithMessages '''
             String[] strings = ['a','b','c']
             int str = strings[0]
         ''', 'Cannot assign value of type java.lang.String to variable of type int'
     }
 
+    void testRightComponentTypeInArray() {
+        def strings = new String[]{1,(long)2,(short)3}
+        assert strings.every { it.class == String }
+        assert strings.toString() == '[1, 2, 3]'
+    }
+
     void testWrongComponentTypeInArray() {
         shouldFailWithMessages '''
             int[] intArray = ['a']
         ''', 'Cannot assign value of type java.lang.String into array of type int[]'
     }
 
+    // GROOVY-9985
     void testWrongComponentTypeInArrayInitializer() {
         shouldFailWithMessages '''
-            int[] intArray = new int[]{'a'}
-        ''', 'Cannot assign value of type java.lang.String into array of type int[]'
+            new int['a']
+        ''', 'Cannot convert from java.lang.String to int'
+        shouldFailWithMessages '''
+            new int[]{'a'}
+        ''', 'Cannot convert from java.lang.String to int'
+    }
+
+    // GROOVY-10111
+    void testBoundedComponentTypeInArrayInitializer() {
+        assertScript '''
+            class C<X, Y> {
+            }
+            def <X extends C<Number, String>> X[] m() {
+                new X[]{new C<Number, String>()}
+            }
+        '''
+        shouldFailWithMessages '''
+            class C<X, Y> {
+            }
+            def <X extends C<Number, String>> X[] m() {
+                new X[]{new C<Object, String>()}
+            }
+        ''', 'Cannot convert from C<java.lang.Object, java.lang.String> to X'
     }
 
     void testAssignValueInArrayWithCorrectType() {