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/08/17 17:36:40 UTC

[groovy] branch GROOVY_3_0_X updated (23094d0 -> ba00810)

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

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


    from 23094d0  GROOVY-9064: add test case
     new 6e5b36f  GROOVY-10179: STC: for-in collection type: consider instanceof flow-type
     new de2687e  GROOVY-10164: make RootLoader(ClassLoader) public
     new b1dd5b3  GROOVY-10079: STC: Character can be implicitly unboxed to char at return
     new ba00810  GROOVY-10087: STC: char can be implicitly boxed to Character at return

The 4 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:
 .../java/org/codehaus/groovy/tools/RootLoader.java |  4 +-
 .../transform/stc/StaticTypeCheckingSupport.java   | 18 ++----
 .../transform/stc/StaticTypeCheckingVisitor.java   |  7 ++-
 src/test/groovy/transform/stc/LoopsSTCTest.groovy  | 66 ++++++++++++++++++++-
 .../groovy/transform/stc/ReturnsSTCTest.groovy     | 51 ++++++++++++++++
 .../classgen/asm/sc/bugs/Groovy6240Bug.groovy      | 69 ----------------------
 6 files changed, 127 insertions(+), 88 deletions(-)
 delete mode 100644 src/test/org/codehaus/groovy/classgen/asm/sc/bugs/Groovy6240Bug.groovy

[groovy] 02/04: GROOVY-10164: make RootLoader(ClassLoader) public

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

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

commit de2687e52621b431b29ae0d3719dbaecba456f75
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Fri Jul 16 11:48:04 2021 -0500

    GROOVY-10164: make RootLoader(ClassLoader) public
---
 src/main/java/org/codehaus/groovy/tools/RootLoader.java | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/tools/RootLoader.java b/src/main/java/org/codehaus/groovy/tools/RootLoader.java
index d8854ce..83c462e 100644
--- a/src/main/java/org/codehaus/groovy/tools/RootLoader.java
+++ b/src/main/java/org/codehaus/groovy/tools/RootLoader.java
@@ -74,7 +74,7 @@ import java.util.Map;
  */
 public class RootLoader extends URLClassLoader {
     private static final URL[] EMPTY_URL_ARRAY = new URL[0];
-    private final Map<String, Class> customClasses = new HashMap<String, Class>();
+    private final Map<String, Class> customClasses = new HashMap<>();
     private static final String ORG_W3C_DOM_NODE = "org.w3c.dom.Node";
 
     /**
@@ -82,7 +82,7 @@ public class RootLoader extends URLClassLoader {
      *
      * @param parent the parent Loader
      */
-    private RootLoader(ClassLoader parent) {
+    public RootLoader(ClassLoader parent) {
         this(EMPTY_URL_ARRAY, parent);
     }
 

[groovy] 01/04: GROOVY-10179: STC: for-in collection type: consider instanceof flow-type

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

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

commit 6e5b36fa2d7e1962b8c71a6b912c27bccb3e2ae8
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Fri Jul 23 14:24:04 2021 -0500

    GROOVY-10179: STC: for-in collection type: consider instanceof flow-type
---
 .../transform/stc/StaticTypeCheckingVisitor.java   |  7 ++-
 src/test/groovy/transform/stc/LoopsSTCTest.groovy  | 66 ++++++++++++++++++++-
 .../classgen/asm/sc/bugs/Groovy6240Bug.groovy      | 69 ----------------------
 3 files changed, 69 insertions(+), 73 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 795b0ba..897e5ba 100644
--- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
+++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
@@ -1872,7 +1872,12 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
             super.visitForLoop(forLoop);
         } else {
             collectionExpression.visit(this);
-            ClassNode collectionType = getType(collectionExpression);
+            ClassNode collectionType;
+            if (collectionExpression instanceof VariableExpression && hasInferredReturnType(collectionExpression)) {
+                collectionType = getInferredReturnType(collectionExpression);
+            } else {
+                collectionType = getType(collectionExpression);
+            }
             ClassNode forLoopVariableType = forLoop.getVariableType();
             ClassNode componentType;
             if (Character_TYPE.equals(getWrapper(forLoopVariableType)) && STRING_TYPE.equals(collectionType)) {
diff --git a/src/test/groovy/transform/stc/LoopsSTCTest.groovy b/src/test/groovy/transform/stc/LoopsSTCTest.groovy
index 6a9bff6..2798cfb 100644
--- a/src/test/groovy/transform/stc/LoopsSTCTest.groovy
+++ b/src/test/groovy/transform/stc/LoopsSTCTest.groovy
@@ -128,9 +128,9 @@ class LoopsSTCTest extends StaticTypeCheckingTestCase {
     }
 
     // GROOVY-5587
-    void testMapEntryInForInLoop() {
+    void testForInLoopOnMap1() {
         assertScript '''
-            @ASTTest(phase=INSTRUCTION_SELECTION, value= {
+            @ASTTest(phase=INSTRUCTION_SELECTION, value={
                 lookup('forLoop').each {
                     assert it instanceof org.codehaus.groovy.ast.stmt.ForStatement
                     def collection = it.collectionExpression // MethodCallExpression
@@ -157,6 +157,67 @@ class LoopsSTCTest extends StaticTypeCheckingTestCase {
         '''
     }
 
+    // GROOVY-6240
+    void testForInLoopOnMap2() {
+        assertScript '''
+            Map<String, Integer> map = [foo: 123, bar: 456]
+            for (entry in map) {
+                assert entry.key.reverse() in ['oof','rab']
+                assert entry.value * 2 in [246, 912]
+            }
+        '''
+    }
+
+    void testForInLoopOnMap3() {
+        assertScript '''
+            class MyMap extends LinkedHashMap<String,Integer> {
+            }
+            def map = new MyMap([foo: 123, bar: 456])
+            for (entry in map) {
+                assert entry.key.reverse() in ['oof','rab']
+                assert entry.value * 2 in [246, 912]
+            }
+        '''
+    }
+
+    // GROOVY-10179
+    void testForInLoopOnMap4() {
+        assertScript '''
+            void test(args) {
+                if (args instanceof Map) {
+                    for (e in args) {
+                        print "$e.key $e.value"
+                    }
+                }
+            }
+            test(a:1,b:2,c:3.14)
+        '''
+    }
+
+    // GROOVY-6123
+    void testForInLoopOnEnumeration() {
+        assertScript '''
+            Vector<String> v = new Vector<>()
+            v.add('ooo')
+            def en = v.elements()
+            for (e in en) {
+                assert e.toUpperCase() == 'OOO'
+            }
+            v.add('groovy')
+            en = v.elements()
+            for (e in en) {
+                assert e.toUpperCase() == 'OOO'
+                break
+            }
+
+            en = v.elements()
+            for (e in en) {
+                assert e.toUpperCase() in ['OOO','GROOVY']
+                if (e=='ooo') continue
+            }
+        '''
+    }
+
     void testShouldNotInferSoftReferenceAsComponentType() {
         assertScript '''import java.lang.reflect.Field
             import org.codehaus.groovy.ast.stmt.ForStatement
@@ -233,4 +294,3 @@ class LoopsSTCTest extends StaticTypeCheckingTestCase {
         '''
     }
 }
-
diff --git a/src/test/org/codehaus/groovy/classgen/asm/sc/bugs/Groovy6240Bug.groovy b/src/test/org/codehaus/groovy/classgen/asm/sc/bugs/Groovy6240Bug.groovy
deleted file mode 100644
index f263a44..0000000
--- a/src/test/org/codehaus/groovy/classgen/asm/sc/bugs/Groovy6240Bug.groovy
+++ /dev/null
@@ -1,69 +0,0 @@
-/*
- *  Licensed to the Apache Software Foundation (ASF) under one
- *  or more contributor license agreements.  See the NOTICE file
- *  distributed with this work for additional information
- *  regarding copyright ownership.  The ASF licenses this file
- *  to you under the Apache License, Version 2.0 (the
- *  "License"); you may not use this file except in compliance
- *  with the License.  You may obtain a copy of the License at
- *
- *    http://www.apache.org/licenses/LICENSE-2.0
- *
- *  Unless required by applicable law or agreed to in writing,
- *  software distributed under the License is distributed on an
- *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- *  KIND, either express or implied.  See the License for the
- *  specific language governing permissions and limitations
- *  under the License.
- */
-package org.codehaus.groovy.classgen.asm.sc.bugs
-
-import groovy.transform.stc.StaticTypeCheckingTestCase
-import org.codehaus.groovy.classgen.asm.sc.StaticCompilationTestSupport
-
-class Groovy6240Bug extends StaticTypeCheckingTestCase implements StaticCompilationTestSupport {
-    void testGroovyAllowsIteratingOnMapDirectlyWithForEachLoop() {
-        assertScript '''
-            Map<String, Integer> map = [foo: 123, bar: 456]
-            for (entry in map) {
-                assert entry.key.reverse() in ['oof','rab']
-                assert entry.value * 2 in [246, 912]
-            }
-        '''
-    }
-
-    void testGroovyAllowsIteratingOnMapDirectlyWithForEachLoopCustomMapType() {
-        assertScript '''
-            class MyMap extends LinkedHashMap<String,Integer>{}
-            def map = new MyMap([foo: 123, bar: 456])
-            for (entry in map) {
-                assert entry.key.reverse() in ['oof','rab']
-                assert entry.value * 2 in [246, 912]
-            }
-        '''
-    }
-
-    // GROOVY-6123
-    void testGroovyAllowsIteratingOnEnumerationDirectlyWithForEachLoop() {
-        assertScript '''
-            Vector<String> v = new Vector<>()
-            v.add('ooo')
-            def en = v.elements()
-            for (e in en) {
-                assert e.toUpperCase() == 'OOO'
-            }
-            v.add('groovy')
-            en = v.elements()
-            for (e in en) {
-                assert e.toUpperCase() == 'OOO'
-                break
-            }
-
-            en = v.elements()
-            for (e in en) {
-                assert e.toUpperCase() in ['OOO','GROOVY']
-                if (e=='ooo') continue
-            }
-        '''
-    }
-}

[groovy] 03/04: GROOVY-10079: STC: Character can be implicitly unboxed to char at return

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

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

commit b1dd5b39e26620d8894c7ba786f5699c0ec30d34
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Tue May 11 18:16:04 2021 -0500

    GROOVY-10079: STC: Character can be implicitly unboxed to char at return
---
 .../transform/stc/StaticTypeCheckingSupport.java   |  5 +++--
 .../groovy/transform/stc/ReturnsSTCTest.groovy     | 24 ++++++++++++++++++++++
 2 files changed, 27 insertions(+), 2 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java
index ba2de6b..a934833 100644
--- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java
+++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java
@@ -678,8 +678,9 @@ public abstract class StaticTypeCheckingSupport {
         if (isWildcardLeftHandSide(leftRedirect) && !(boolean_TYPE.equals(left) && rightExpressionIsNull)) return true;
 
         // char as left expression
-        if (leftRedirect == char_TYPE && rightRedirect == STRING_TYPE) {
-            if (rightExpression instanceof ConstantExpression) {
+        if (leftRedirect == char_TYPE) {
+            if (rightRedirect == Character_TYPE) return true;
+            if (rightRedirect == STRING_TYPE && rightExpression instanceof ConstantExpression) {
                 String value = rightExpression.getText();
                 return value.length() == 1;
             }
diff --git a/src/test/groovy/transform/stc/ReturnsSTCTest.groovy b/src/test/groovy/transform/stc/ReturnsSTCTest.groovy
index 5e2a870..f4cd334 100644
--- a/src/test/groovy/transform/stc/ReturnsSTCTest.groovy
+++ b/src/test/groovy/transform/stc/ReturnsSTCTest.groovy
@@ -199,6 +199,30 @@ class ReturnsSTCTest extends StaticTypeCheckingTestCase {
         ''', 'No such property: years for class: java.lang.String'
     }
 
+    // GROOVY-10079
+    void testImplicitReturnToPrimitive() {
+        assertScript '''
+            int foo() {
+                Integer.valueOf(42)
+            }
+            assert foo() == 42
+        '''
+
+        assertScript '''
+            long foo() {
+                Long.valueOf(1234L)
+            }
+            assert foo() == 1234L
+        '''
+
+        assertScript '''
+            char foo() {
+                Character.valueOf((char)'x')
+            }
+            assert foo() == 'x'
+        '''
+    }
+
     // GROOVY-5835
     void testReturnInClosureShouldNotBeConsideredAsReturnOfEnclosingMethod() {
         assertScript '''

[groovy] 04/04: GROOVY-10087: STC: char can be implicitly boxed to Character at return

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

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

commit ba00810ed70cef52448bf742e62bcc32447ef6b1
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Sat May 15 18:55:11 2021 -0500

    GROOVY-10087: STC: char can be implicitly boxed to Character at return
    
    Conflicts:
    	src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java
---
 .../transform/stc/StaticTypeCheckingSupport.java   | 19 ++++-----------
 .../groovy/transform/stc/ReturnsSTCTest.groovy     | 27 ++++++++++++++++++++++
 2 files changed, 32 insertions(+), 14 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java
index a934833..2692f16 100644
--- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java
+++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java
@@ -666,27 +666,18 @@ public abstract class StaticTypeCheckingSupport {
             }
         }
 
-        // if rightExpression is null and leftExpression is not a primitive type, it's ok
         boolean rightExpressionIsNull = isNullConstant(rightExpression);
         if (rightExpressionIsNull && !isPrimitiveType(left)) {
             return true;
         }
 
-        // on an assignment everything that can be done by a GroovyCast is allowed
-
-        // anything can be assigned to an Object, String, Boolean or Class typed variable
+        // anything can be assigned to an Object, String, [Bb]oolean or Class receiver; except null to boolean
         if (isWildcardLeftHandSide(leftRedirect) && !(boolean_TYPE.equals(left) && rightExpressionIsNull)) return true;
 
-        // char as left expression
-        if (leftRedirect == char_TYPE) {
-            if (rightRedirect == Character_TYPE) return true;
-            if (rightRedirect == STRING_TYPE && rightExpression instanceof ConstantExpression) {
-                String value = rightExpression.getText();
-                return value.length() == 1;
-            }
-        }
-        if (leftRedirect == Character_TYPE && (rightRedirect == STRING_TYPE || rightExpressionIsNull)) {
-            return rightExpressionIsNull || (rightExpression instanceof ConstantExpression && rightExpression.getText().length() == 1);
+        if (leftRedirect == char_TYPE && rightRedirect == Character_TYPE) return true;
+        if (leftRedirect == Character_TYPE && rightRedirect == char_TYPE) return true;
+        if ((leftRedirect == char_TYPE || leftRedirect == Character_TYPE) && rightRedirect == STRING_TYPE) {
+            return rightExpression instanceof ConstantExpression && rightExpression.getText().length() == 1;
         }
 
         // if left is Enum and right is String or GString we do valueOf
diff --git a/src/test/groovy/transform/stc/ReturnsSTCTest.groovy b/src/test/groovy/transform/stc/ReturnsSTCTest.groovy
index f4cd334..e5e3a24 100644
--- a/src/test/groovy/transform/stc/ReturnsSTCTest.groovy
+++ b/src/test/groovy/transform/stc/ReturnsSTCTest.groovy
@@ -223,6 +223,33 @@ class ReturnsSTCTest extends StaticTypeCheckingTestCase {
         '''
     }
 
+    // GROOVY-10087
+    void testImplicitReturnToWrapper() {
+        assertScript '''
+            Integer foo() {
+                int x = 42
+                return x
+            }
+            assert foo().intValue() == 42
+        '''
+
+        assertScript '''
+            Long foo() {
+                long x = 42L
+                return x
+            }
+            assert foo().longValue() == 42L
+        '''
+
+        assertScript '''
+            Character foo() {
+                char x = 'x'
+                return x
+            }
+            assert foo().charValue() == 'x'
+        '''
+    }
+
     // GROOVY-5835
     void testReturnInClosureShouldNotBeConsideredAsReturnOfEnclosingMethod() {
         assertScript '''