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/17 22:34:21 UTC

[groovy] branch master updated: GROOVY-9691: don't make SMCE within closure outside of special ctor call

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

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


The following commit(s) were added to refs/heads/master by this push:
     new fc50dca  GROOVY-9691: don't make SMCE within closure outside of special ctor call
fc50dca is described below

commit fc50dca6a967ef52e0f653c2505bf6f24864c3fd
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Mon Aug 17 17:34:12 2020 -0500

    GROOVY-9691: don't make SMCE within closure outside of special ctor call
    
    closes #1346
---
 .../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]')
     }
 }