You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by su...@apache.org on 2020/06/21 23:38:21 UTC

[groovy] branch GROOVY_3_0_X updated (410e22e -> caeeb06)

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

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


    from 410e22e  Minor tweak: simplify the control flow for better readability
     new 07c71e4  GROOVY-9591: no error for dynamic variable in closure in static context
     new caeeb06  GROOVY-9344, GROOVY-9516: track assign in closure like if/else branch

The 2 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:
 .../codehaus/groovy/control/StaticVerifier.java    |   8 +-
 .../transform/stc/StaticTypeCheckingVisitor.java   |   3 +
 src/test/groovy/bugs/Groovy8327.groovy             | 223 +++++++++++++++++++++
 src/test/groovy/bugs/Groovy8327Bug.groovy          | 121 -----------
 .../groovy/transform/stc/ClosuresSTCTest.groovy    |  18 +-
 .../asm/sc/StaticCompileFlowTypingTest.groovy      |  15 +-
 6 files changed, 255 insertions(+), 133 deletions(-)
 create mode 100644 src/test/groovy/bugs/Groovy8327.groovy
 delete mode 100644 src/test/groovy/bugs/Groovy8327Bug.groovy


[groovy] 01/02: GROOVY-9591: no error for dynamic variable in closure in static context

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

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

commit 07c71e48cd2cbe7c6b2aa69923ed62adac39d0a6
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Sat Jun 13 14:17:41 2020 -0500

    GROOVY-9591: no error for dynamic variable in closure in static context
    
    (cherry picked from commit 836887ceec754027083339e56a8fedd3a12c7380)
---
 .../codehaus/groovy/control/StaticVerifier.java    |   8 +-
 src/test/groovy/bugs/Groovy8327.groovy             | 223 +++++++++++++++++++++
 src/test/groovy/bugs/Groovy8327Bug.groovy          | 121 -----------
 3 files changed, 228 insertions(+), 124 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/control/StaticVerifier.java b/src/main/java/org/codehaus/groovy/control/StaticVerifier.java
index 2fdee68..59bd2f9 100644
--- a/src/main/java/org/codehaus/groovy/control/StaticVerifier.java
+++ b/src/main/java/org/codehaus/groovy/control/StaticVerifier.java
@@ -87,11 +87,13 @@ public class StaticVerifier extends ClassCodeVisitorSupport {
 
     @Override
     public void visitVariableExpression(VariableExpression ve) {
-        if (ve.getAccessedVariable() instanceof DynamicVariable
-                && (inSpecialConstructorCall || (!inClosure && ve.isInStaticContext()))) {
+        if (ve.getAccessedVariable() instanceof DynamicVariable && (ve.isInStaticContext() || inSpecialConstructorCall) && !inClosure) {
+            // GROOVY-5687: interface constants not visible to implementing sub-class in static context
             if (methodNode != null && methodNode.isStatic()) {
                 FieldNode fieldNode = getDeclaredOrInheritedField(methodNode.getDeclaringClass(), ve.getName());
-                if (fieldNode != null && fieldNode.isStatic()) return;
+                if (fieldNode != null && fieldNode.isStatic()) {
+                    return;
+                }
             }
             addError("Apparent variable '" + ve.getName() + "' was found in a static scope but doesn't refer to a local variable, static field or class. Possible causes:\n" +
                     "You attempted to reference a variable in the binding or an instance variable from a static context.\n" +
diff --git a/src/test/groovy/bugs/Groovy8327.groovy b/src/test/groovy/bugs/Groovy8327.groovy
new file mode 100644
index 0000000..0647db6
--- /dev/null
+++ b/src/test/groovy/bugs/Groovy8327.groovy
@@ -0,0 +1,223 @@
+/*
+ *  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 groovy.bugs
+
+import groovy.test.NotYetImplemented
+import groovy.transform.CompileStatic
+import org.junit.Test
+
+import static groovy.test.GroovyAssert.assertScript
+
+@CompileStatic
+final class Groovy8327 {
+
+    @Test
+    void testCallStaticMethodInThisConstructor() {
+        assertScript '''
+            class A {
+                static String f = '123'
+                static String g() { 'abc' }
+                A() {
+                    this(g() + getF())
+                }
+                A(a) { assert a == 'abc123' }
+            }
+
+            new A()
+        '''
+    }
+
+    @Test
+    void testCallStaticMethodInSuperConstructor() {
+        assertScript '''
+            class A {
+                A(a) { assert a == 'abc123' }
+            }
+
+            class B extends A {
+                static String f = '123'
+                static String g() { 'abc' }
+                B() {
+                    super(g() + getF())
+                }
+            }
+
+            new B()
+        '''
+    }
+
+    @Test
+    void testCallSuperStaticMethodInSuperConstructor() {
+        assertScript '''
+            class A {
+                static String f = '123'
+                static String g() { 'abc' }
+                A(a) { assert a == 'abc123' }
+            }
+
+            class B extends A {
+                B() {
+                    super(g() + getF())
+                }
+            }
+
+            new B()
+        '''
+    }
+
+    // GROOVY-8327:
+
+    @Test
+    void testCallStaticMethodInClosureParamOfThisConstructor() {
+        assertScript '''
+            class A {
+                static String f = '123'
+                static String g() { 'abc' }
+                A() {
+                    this({ -> g() + getF()})
+                }
+                A(a) { assert a() == 'abc123' }
+            }
+
+            new A()
+        '''
+    }
+
+    @Test
+    void testCallStaticMethodInClosureParamOfSuperConstructor() {
+        assertScript '''
+            class A {
+                A(a) { assert a() == 'abc123' }
+            }
+
+            class B extends A {
+                static String f = '123'
+                static String g() { 'abc' }
+                B() {
+                    super({ -> g() + getF() })
+                }
+            }
+
+            new B()
+        '''
+    }
+
+    @Test
+    void testCallSuperStaticMethodInClosureParamOfSuperConstructor() {
+        assertScript '''
+            class A {
+                static String f = '123'
+                static String g() { 'abc' }
+                A(a) { assert a() == 'abc123' }
+            }
+
+            class B extends A {
+                B() {
+                    super({ -> g() + getF() })
+                }
+            }
+
+            new B()
+        '''
+    }
+
+    // GROOVY-9591:
+
+    @Test
+    void testTapExpressionAsSpecialConstructorArgument1() {
+        assertScript '''
+            @groovy.transform.ToString
+            class A {
+                A(x) {}
+                def b
+            }
+            class C {
+                C() {
+                    this(new A(null).tap { b = 42 })
+                }
+                C(x) {
+                    assert x.toString() == 'A(42)'
+                }
+            }
+            new C()
+        '''
+    }
+
+    @NotYetImplemented @Test
+    void testTapExpressionAsSpecialConstructorArgument2() {
+        assertScript '''
+            @groovy.transform.ToString
+            class A {
+                A(x) {}
+                def b
+                void init() { b = 42 }
+            }
+            class C {
+                C() {
+                    this(new A(null).tap { init() })
+                }
+                C(x) {
+                    assert x.toString() == 'A(42)'
+                }
+            }
+            new C()
+        '''
+    }
+
+    @Test
+    void testWithExpressionAsSpecialConstructorArgument1() {
+        assertScript '''
+            @groovy.transform.ToString
+            class A {
+                A(x) {}
+                def b
+            }
+            class C {
+                C() {
+                    this(new A(null).with { b = 42; return it })
+                }
+                C(x) {
+                    assert x.toString() == 'A(42)'
+                }
+            }
+            new C()
+        '''
+    }
+
+    @NotYetImplemented @Test
+    void testWithExpressionAsSpecialConstructorArgument2() {
+        assertScript '''
+            @groovy.transform.ToString
+            class A {
+                A(x) {}
+                def b
+                void init() { b = 42 }
+            }
+            class C {
+                C() {
+                    this(new A(null).with { init(); return it })
+                }
+                C(x) {
+                    assert x.toString() == 'A(42)'
+                }
+            }
+            new C()
+        '''
+    }
+}
diff --git a/src/test/groovy/bugs/Groovy8327Bug.groovy b/src/test/groovy/bugs/Groovy8327Bug.groovy
deleted file mode 100644
index 8a5ad26..0000000
--- a/src/test/groovy/bugs/Groovy8327Bug.groovy
+++ /dev/null
@@ -1,121 +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 groovy.bugs
-
-import groovy.test.GroovyTestCase
-
-class Groovy8327Bug extends GroovyTestCase {
-    void testCallStaticMethodInClosureParamOfThisConstructor() {
-        assertScript '''
-            class A {
-                static String x = '123'
-                static String g() { 'abc' }
-                A() {
-                    this({g() + getX()})
-                }
-                A(a) { assert 'abc123' == a() }
-            }
-            
-            assert new A()
-        '''
-    }
-
-    void testCallStaticMethodInThisConstructor() {
-        assertScript '''
-            class A {
-                static String x = '123'
-                static String g() { 'abc' }
-                A() {
-                    this(g() + getX())
-                }
-                A(a) { assert 'abc123' == a }
-            }
-            
-            assert new A()
-        '''
-    }
-
-    void testCallStaticMethodInClosureParamOfSuperConstructor() {
-        assertScript '''
-            class B {
-                B(b) { assert 'abc123' == b() }
-            }
-            class A extends B {
-                static String x = '123'
-                static String g() { 'abc' }
-                A() {
-                    super({g() + getX()})
-                }
-            }
-            
-            assert new A()
-        '''
-    }
-
-    void testCallStaticMethodInSuperConstructor() {
-        assertScript '''
-            class B {
-                B(b) { assert 'abc123' == b }
-            }
-            class A extends B {
-                static String x = '123'
-                static String g() { 'abc' }
-                A() {
-                    super(g() + getX())
-                }
-            }
-            
-            assert new A()
-        '''
-    }
-
-    void testCallSuperStaticMethodInClosureParamOfSuperConstructor() {
-        assertScript '''
-            class B {
-                B(b) { assert 'abc123' == b() }
-                static String x = '123'
-                static String g() { 'abc' }
-            }
-            class A extends B {
-                A() {
-                    super({g() + getX()})
-                }
-            }
-            
-            assert new A()
-        '''
-    }
-
-    void testCallSuperStaticMethodInSuperConstructor() {
-        assertScript '''
-            class B {
-                B(b) { assert 'abc123' == b }
-                static String x = '123'
-                static String g() { 'abc' }
-            }
-            class A extends B {
-                A() {
-                    super(g() + getX())
-                }
-            }
-            
-            assert new A()
-        '''
-    }
-}


[groovy] 02/02: GROOVY-9344, GROOVY-9516: track assign in closure like if/else branch

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

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

commit caeeb062673be07c8e6dae12489625150eb4d396
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Sun Jun 14 07:51:15 2020 -0500

    GROOVY-9344, GROOVY-9516: track assign in closure like if/else branch
    
    (cherry picked from commit bf614817fdd5d5136c79f89eca3fa8b4fae3fbf8)
---
 .../transform/stc/StaticTypeCheckingVisitor.java       |  3 +++
 src/test/groovy/transform/stc/ClosuresSTCTest.groovy   | 18 +++++++++++++++++-
 .../classgen/asm/sc/StaticCompileFlowTypingTest.groovy | 15 +++++++--------
 3 files changed, 27 insertions(+), 9 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 f5485fa..723965c 100644
--- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
+++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
@@ -2392,6 +2392,9 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
         if (typesBeforeVisit != null) {
             for (Map.Entry<VariableExpression, Map<StaticTypesMarker, Object>> entry : typesBeforeVisit.entrySet()) {
                 for (StaticTypesMarker marker : StaticTypesMarker.values()) {
+                    // GROOVY-9344, GROOVY-9516
+                    if (marker == INFERRED_TYPE) continue;
+
                     Object value = entry.getValue().get(marker);
                     if (value == null) {
                         entry.getKey().removeNodeMetaData(marker);
diff --git a/src/test/groovy/transform/stc/ClosuresSTCTest.groovy b/src/test/groovy/transform/stc/ClosuresSTCTest.groovy
index 11ce461..3eb1502 100644
--- a/src/test/groovy/transform/stc/ClosuresSTCTest.groovy
+++ b/src/test/groovy/transform/stc/ClosuresSTCTest.groovy
@@ -168,7 +168,23 @@ class ClosuresSTCTest extends StaticTypeCheckingTestCase {
             def x = '123';
             { -> x = 123 }
             x.charAt(0) // available in String but not available in Integer
-        ''', 'A closure shared variable [x] has been assigned with various types and the method [charAt(int)] does not exist in the lowest upper bound'
+        ''', 'Cannot find matching method java.io.Serializable or java.lang.Comparable'
+    }
+
+    // GROOVY-9516
+    void testClosureSharedVariable3() {
+        shouldFailWithMessages '''
+            class A {}
+            class B extends A { def m() {} }
+            class C extends A {}
+
+            void test() {
+              def x = new B();
+              { -> x = new C() }();
+              def c = x
+              c.m()
+            }
+        ''', 'Cannot find matching method A#m()'
     }
 
     void testClosureCallAsAMethod() {
diff --git a/src/test/org/codehaus/groovy/classgen/asm/sc/StaticCompileFlowTypingTest.groovy b/src/test/org/codehaus/groovy/classgen/asm/sc/StaticCompileFlowTypingTest.groovy
index 86632f8..4b48fe4 100644
--- a/src/test/org/codehaus/groovy/classgen/asm/sc/StaticCompileFlowTypingTest.groovy
+++ b/src/test/org/codehaus/groovy/classgen/asm/sc/StaticCompileFlowTypingTest.groovy
@@ -18,7 +18,6 @@
  */
 package org.codehaus.groovy.classgen.asm.sc
 
-import groovy.test.NotYetImplemented
 import groovy.transform.CompileStatic
 import org.junit.Test
 
@@ -51,10 +50,10 @@ final class StaticCompileFlowTypingTest {
 
             @groovy.transform.CompileStatic
             String m() {
-                def var = new A()
+                def x = new A()
                 def c = { ->
-                    var = new B()
-                    var.class.simpleName
+                    x = new B()
+                    x.class.simpleName
                 }
                 c()
             }
@@ -62,7 +61,7 @@ final class StaticCompileFlowTypingTest {
         '''
     }
 
-    @NotYetImplemented @Test // GROOVY-9344
+    @Test // GROOVY-9344
     void testFlowTyping3() {
         assertScript '''
             class A {}
@@ -70,12 +69,12 @@ final class StaticCompileFlowTypingTest {
 
             @groovy.transform.CompileStatic
             String m() {
-                def var = new A()
+                def x = new A()
                 def c = { ->
-                    var = new B()
+                    x = new B()
                 }
                 c()
-                var.class.simpleName
+                x.class.simpleName
             }
             assert m() == 'B'
         '''