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 2020/06/13 19:17:59 UTC

[groovy] branch GROOVY-9591 created (now 2f1200a)

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

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


      at 2f1200a  GROOVY-9591: no error for dynamic variable in closure in static context

This branch includes the following new commits:

     new 2f1200a  GROOVY-9591: no error for dynamic variable in closure in static context

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-9591: no error for dynamic variable in closure in static context

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

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

commit 2f1200ae0226b84d71e187887816a5a2214c4c83
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
---
 .../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()
-        '''
-    }
-}