groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pa...@apache.org
Subject incubator-groovy git commit: GROOVY-7485: BUG! exception in phase 'semantic analysis'... when trying to call instance method as default for a constructor parameter
Date Sat, 04 Jul 2015 01:20:25 GMT
Repository: incubator-groovy
Updated Branches:
  refs/heads/master c03761f9c -> 63afb6d7e


GROOVY-7485: BUG! exception in phase 'semantic analysis'... when trying to call instance method
as default for a constructor parameter


Project: http://git-wip-us.apache.org/repos/asf/incubator-groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-groovy/commit/63afb6d7
Tree: http://git-wip-us.apache.org/repos/asf/incubator-groovy/tree/63afb6d7
Diff: http://git-wip-us.apache.org/repos/asf/incubator-groovy/diff/63afb6d7

Branch: refs/heads/master
Commit: 63afb6d7ea76688cd311c99e802f341a755de572
Parents: c03761f
Author: paulk <paulk@asert.com.au>
Authored: Thu Jul 2 20:35:16 2015 +1000
Committer: paulk <paulk@asert.com.au>
Committed: Sat Jul 4 11:19:45 2015 +1000

----------------------------------------------------------------------
 .../codehaus/groovy/control/StaticVerifier.java | 16 +++++++-
 .../groovy/bugs/ConstructorParameterBug.groovy  | 39 +++++++++++++++-----
 2 files changed, 44 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-groovy/blob/63afb6d7/src/main/org/codehaus/groovy/control/StaticVerifier.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/control/StaticVerifier.java b/src/main/org/codehaus/groovy/control/StaticVerifier.java
index 89bddad..731fe54 100644
--- a/src/main/org/codehaus/groovy/control/StaticVerifier.java
+++ b/src/main/org/codehaus/groovy/control/StaticVerifier.java
@@ -76,7 +76,7 @@ public class StaticVerifier extends ClassCodeVisitorSupport {
         super.visitConstructorOrMethod(node, isConstructor);
         if (isConstructor) {
             final HashSet<String> exceptions = new HashSet<String>();
-            for (Parameter param : node.getParameters()) {
+            for (final Parameter param : node.getParameters()) {
                 exceptions.add(param.getName());
                 if (param.hasInitialExpression()) {
                     param.getInitialExpression().visit(new CodeVisitorSupport() {
@@ -88,6 +88,20 @@ public class StaticVerifier extends ClassCodeVisitorSupport {
                                 addVariableError(ve);
                             }
                         }
+
+                        @Override
+                        public void visitMethodCallExpression(MethodCallExpression call)
{
+                            Expression objectExpression = call.getObjectExpression();
+                            if (objectExpression instanceof VariableExpression) {
+                                VariableExpression ve = (VariableExpression) objectExpression;
+                                if (ve.isThisExpression()) {
+                                    addError("Can't access instance method '" + call.getMethodAsString()
+ "' for a constructor parameter default value", param);
+                                    return;
+                                }
+                            }
+                            super.visitMethodCallExpression(call);
+                        }
+
                         @Override
                         public void visitClosureExpression(ClosureExpression expression)
{
                             //skip contents, because of dynamic scope

http://git-wip-us.apache.org/repos/asf/incubator-groovy/blob/63afb6d7/src/test/groovy/bugs/ConstructorParameterBug.groovy
----------------------------------------------------------------------
diff --git a/src/test/groovy/bugs/ConstructorParameterBug.groovy b/src/test/groovy/bugs/ConstructorParameterBug.groovy
index 8e094d0..c881b96 100644
--- a/src/test/groovy/bugs/ConstructorParameterBug.groovy
+++ b/src/test/groovy/bugs/ConstructorParameterBug.groovy
@@ -18,18 +18,37 @@
  */
 package groovy.bugs
 
-class ConstructorParameterBug extends GroovyTestCase {
+import gls.CompilableTestSupport
 
-    void testMethodWithNativeArray() {
-        int[] value = [2*2]
-        println "${value} of type ${value.class}"
-        /** @todo fixme!
-        blah2(value)
-        */
-    }
+class ConstructorParameterBug extends CompilableTestSupport {
 
-    def blah2(int[] wobble) {
-       println(wobble)
+    void testParamWithDefaultCallingStaticMethod() {
+        assertScript '''
+        class StaticDefault {
+            def name
+            StaticDefault(name = baz()) {
+                this.name = name
+            }
+            private static baz() {
+                'baz'
+            }
+        }
+        assert 'baz' == new StaticDefault().name
+        '''
     }
 
+    void testParamWithDefaultCallingInstanceMethod() {
+        def msg = shouldFail '''
+        class InstanceDefault {
+            def name
+            InstanceDefault(name = baz()) {
+                this.name = name
+            }
+            private baz() {
+                'baz'
+            }
+        }
+        '''
+        assert msg.contains("Can't access instance method 'baz' for a constructor parameter
default value")
+    }
 }


Mime
View raw message