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 22:21:42 UTC

[groovy] branch GROOVY-10111 updated (da9d992 -> b924629)

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.


 discard da9d992  GROOVY-9985, GROOVY-10111: STC: check array sizes and values not as cast
     new b924629  GROOVY-9985, GROOVY-9994, GROOVY-10111: STC check array sizes and values

This update added new revisions after undoing existing revisions.
That is to say, some revisions that were in the old version of the
branch are not in the new version.  This situation occurs
when a user --force pushes a change and generates a repository
containing something like this:

 * -- * -- B -- O -- O -- O   (da9d992)
            \
             N -- N -- N   refs/heads/GROOVY-10111 (b924629)

You should already have received notification emails for all of the O
revisions, and so the following emails describe only the N revisions
from the common base, B.

Any revisions marked "omit" are not gone; other references still
refer to them.  Any revisions marked "discard" are gone forever.

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.


Summary of changes:
 src/test/groovy/transform/stc/ArraysAndCollectionsSTCTest.groovy | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

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

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 b9246296ddba80dab3f9d90a75874b808fde138a
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Wed May 26 14:03:03 2021 -0500

    GROOVY-9985, GROOVY-9994, GROOVY-10111: STC check array sizes and values
---
 .../transform/stc/StaticTypeCheckingVisitor.java   | 22 +++++++-----
 .../stc/ArraysAndCollectionsSTCTest.groovy         | 40 ++++++++++++++++++++--
 2 files changed, 51 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..86e5fd2 100644
--- a/src/test/groovy/transform/stc/ArraysAndCollectionsSTCTest.groovy
+++ b/src/test/groovy/transform/stc/ArraysAndCollectionsSTCTest.groovy
@@ -27,10 +27,11 @@ 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]
@@ -43,10 +44,43 @@ class ArraysAndCollectionsSTCTest extends StaticTypeCheckingTestCase {
         ''', 'Cannot assign value of type java.lang.String into array of type int[]'
     }
 
+    // GROOVY-9985, GROOVY-9994
     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'
+        shouldFailWithMessages '''
+            new Integer[]{new Object(),1}
+        ''', 'Cannot convert from java.lang.Object to java.lang.Integer'
+    }
+
+    // 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 testConvertibleTypesInArrayInitializer() {
+        assertScript '''
+            def strings = new String[]{1,(long)2,(short)3}
+            assert strings.every { it.class == String }
+            assert strings.toString() == '[1, 2, 3]'
+        '''
     }
 
     void testAssignValueInArrayWithCorrectType() {