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/08/15 17:06:44 UTC

[groovy] branch GROOVY-9691 created (now 1550502)

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

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


      at 1550502  GROOVY-9691: don't make SMCE within closure outside of special ctor call

This branch includes the following new commits:

     new 1550502  GROOVY-9691: don't make SMCE within closure outside of special ctor call

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-9691: don't make SMCE within closure outside of special ctor call

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

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

commit 15505025b17cfb022d9db024ba92f72cd03cad2c
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Sat Aug 15 12:06:28 2020 -0500

    GROOVY-9691: don't make SMCE within closure outside of special ctor call
---
 .../groovy/control/StaticImportVisitor.java        | 28 +++++++++-------
 .../{Groovy8816Bug.groovy => Groovy8816.groovy}    | 25 +++++++-------
 .../{Groovy8816Bug.groovy => Groovy9691.groovy}    | 39 +++++++++++++++-------
 3 files changed, 56 insertions(+), 36 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/control/StaticImportVisitor.java b/src/main/java/org/codehaus/groovy/control/StaticImportVisitor.java
index f83a84e..de4e174 100644
--- a/src/main/java/org/codehaus/groovy/control/StaticImportVisitor.java
+++ b/src/main/java/org/codehaus/groovy/control/StaticImportVisitor.java
@@ -281,22 +281,26 @@ public class StaticImportVisitor extends ClassCodeExpressionTransformer {
                 return false;
             };
 
-            if (isInnerClass(currentClass)) {
-                if (mce.isImplicitThis() && !inClosure && inSpecialConstructorCall && !foundInstanceMethod) {
-                    if (currentClass.getOuterClass().hasPossibleMethod(methodName, args)) {
-                        object = new PropertyExpression(new ClassExpression(currentClass.getOuterClass()), new ConstantExpression("this"));
-                    } else if (hasPossibleStaticMember.test(currentClass.getOuterClass())) {
-                        Expression result = new StaticMethodCallExpression(currentClass.getOuterClass(), methodName, args);
+            if (mce.isImplicitThis()) {
+                if (isInnerClass(currentClass)) {
+                    if (inSpecialConstructorCall && !foundInstanceMethod) {
+                        // check for reference to outer class method in this(...) or super(...)
+                        if (currentClass.getOuterClass().hasPossibleMethod(methodName, args)) {
+                            object = new PropertyExpression(new ClassExpression(currentClass.getOuterClass()), new ConstantExpression("this"));
+                        } else if (hasPossibleStaticMember.test(currentClass.getOuterClass())) {
+                            Expression result = new StaticMethodCallExpression(currentClass.getOuterClass(), methodName, args);
+                            result.setSourcePosition(mce);
+                            return result;
+                        }
+                    }
+                } else if (inSpecialConstructorCall || (!inClosure && !foundInstanceMethod && !methodName.equals("call"))) {
+                    // check for reference to static method in this(...) or super(...) or when call not resolved
+                    if (hasPossibleStaticMember.test(currentClass)) {
+                        Expression result = new StaticMethodCallExpression(currentClass, methodName, args);
                         result.setSourcePosition(mce);
                         return result;
                     }
                 }
-            } else if (inSpecialConstructorCall || (!foundInstanceMethod && !methodName.equals("call"))) {
-                if (hasPossibleStaticMember.test(currentClass)) {
-                    Expression result = new StaticMethodCallExpression(currentClass, methodName, args);
-                    result.setSourcePosition(mce);
-                    return result;
-                }
             }
         }
 
diff --git a/src/test/groovy/bugs/Groovy8816Bug.groovy b/src/test/groovy/bugs/Groovy8816.groovy
similarity index 67%
copy from src/test/groovy/bugs/Groovy8816Bug.groovy
copy to src/test/groovy/bugs/Groovy8816.groovy
index 2d6854d..0bd56e4 100644
--- a/src/test/groovy/bugs/Groovy8816Bug.groovy
+++ b/src/test/groovy/bugs/Groovy8816.groovy
@@ -18,21 +18,22 @@
  */
 package groovy.bugs
 
-import groovy.test.GroovyTestCase
+import org.junit.Test
 
-class Groovy8816Bug extends GroovyTestCase {
-    void testCallNoArgClosureWithArg() {
-        def msg = shouldFail MissingMethodException, '''
-        import groovy.transform.CompileStatic
+import static groovy.test.GroovyAssert.shouldFail
 
-        @CompileStatic
-        void main() {
-            [0].each { -> }
-        }
+final class Groovy8816 {
 
-        main()
+    @Test
+    void testCallNoArgClosureWithArg() {
+        def err = shouldFail MissingMethodException, '''
+            @groovy.transform.CompileStatic
+            void test() {
+                [0].each { -> }
+            }
+            test()
         '''
-        assert msg.contains('No signature of method:')
-        assert msg.contains('doCall() is applicable for argument types: (Integer) values: [0]')
+
+        assert err =~ /No signature of method: .*\.doCall\(\) is applicable for argument types: \(Integer\) values: \[0\]/
     }
 }
diff --git a/src/test/groovy/bugs/Groovy8816Bug.groovy b/src/test/groovy/bugs/Groovy9691.groovy
similarity index 50%
rename from src/test/groovy/bugs/Groovy8816Bug.groovy
rename to src/test/groovy/bugs/Groovy9691.groovy
index 2d6854d..6e179f2 100644
--- a/src/test/groovy/bugs/Groovy8816Bug.groovy
+++ b/src/test/groovy/bugs/Groovy9691.groovy
@@ -18,21 +18,36 @@
  */
 package groovy.bugs
 
-import groovy.test.GroovyTestCase
+import org.junit.Test
 
-class Groovy8816Bug extends GroovyTestCase {
-    void testCallNoArgClosureWithArg() {
-        def msg = shouldFail MissingMethodException, '''
-        import groovy.transform.CompileStatic
+import static groovy.test.GroovyAssert.assertScript
 
-        @CompileStatic
-        void main() {
-            [0].each { -> }
-        }
+final class Groovy9691 {
 
-        main()
+    @Test
+    void testMainCallInClosure() {
+        assertScript '''
+            import org.codehaus.groovy.ast.expr.*
+
+            void sourceSets(Closure block) {
+                // no-op
+            }
+
+            sourceSets {
+                @groovy.transform.ASTTest({
+                    def call = node.rightExpression
+                    assert call instanceof MethodCall
+                    assert !(call instanceof StaticMethodCallExpression)
+                })
+                x = main {
+                    java { srcDirs = [] }
+                    groovy { srcDirs = ['src/main'] }
+                }
+                test {
+                    java { srcDirs = [] }
+                    groovy { srcDirs = ['src/test'] }
+                }
+            }
         '''
-        assert msg.contains('No signature of method:')
-        assert msg.contains('doCall() is applicable for argument types: (Integer) values: [0]')
     }
 }