You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by pa...@apache.org on 2019/11/12 00:45:03 UTC

[groovy] branch GROOVY_2_5_X updated: GROOVY-6996: adjust 6834 workaround to apply to synthetic variables only

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

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


The following commit(s) were added to refs/heads/GROOVY_2_5_X by this push:
     new 6d10b79  GROOVY-6996: adjust 6834 workaround to apply to synthetic variables only
6d10b79 is described below

commit 6d10b7955df3a39b78b8cc94cd00f7346a601169
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Tue Oct 15 13:32:11 2019 -0500

    GROOVY-6996: adjust 6834 workaround to apply to synthetic variables only
---
 .../groovy/classgen/VariableScopeVisitor.java      | 17 +++----
 src/test/groovy/bugs/Groovy6996.groovy             | 59 ++++++++++++++++++++++
 2 files changed, 67 insertions(+), 9 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/classgen/VariableScopeVisitor.java b/src/main/java/org/codehaus/groovy/classgen/VariableScopeVisitor.java
index 66f314d..1795c2b 100644
--- a/src/main/java/org/codehaus/groovy/classgen/VariableScopeVisitor.java
+++ b/src/main/java/org/codehaus/groovy/classgen/VariableScopeVisitor.java
@@ -564,22 +564,21 @@ public class VariableScopeVisitor extends ClassCodeVisitorSupport {
         currentScope.setInStaticContext(false);
         for (MethodNode method : innerClass.getMethods()) {
             Parameter[] parameters = method.getParameters();
-            if (parameters.length == 0) parameters = null; // null means no implicit "it"
+            if (parameters.length == 0)
+                parameters = null; // null means no implicit "it"
             ClosureExpression cl = new ClosureExpression(parameters, method.getCode());
             visitClosureExpression(cl);
         }
 
         for (FieldNode field : innerClass.getFields()) {
-            final Expression expression = field.getInitialExpression();
+            Expression expression = field.getInitialExpression();
             pushState(field.isStatic());
             if (expression != null) {
-                if (expression instanceof VariableExpression) {
-                    VariableExpression vexp = (VariableExpression) expression;
-                    if (vexp.getAccessedVariable() instanceof Parameter) {
-                        // workaround for GROOVY-6834: accessing a parameter which is not yet seen in scope
-                        popState();
-                        continue;
-                    }
+                if (expression.isSynthetic() && expression instanceof VariableExpression &&
+                        ((VariableExpression) expression).getAccessedVariable() instanceof Parameter) {
+                    // GROOVY-6834: accessing a parameter which is not yet seen in scope
+                    popState();
+                    continue;
                 }
                 expression.visit(this);
             }
diff --git a/src/test/groovy/bugs/Groovy6996.groovy b/src/test/groovy/bugs/Groovy6996.groovy
new file mode 100644
index 0000000..ae0f209
--- /dev/null
+++ b/src/test/groovy/bugs/Groovy6996.groovy
@@ -0,0 +1,59 @@
+/*
+ *  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.transform.CompileStatic
+import org.junit.Test
+
+import static groovy.test.GroovyAssert.assertScript
+
+@CompileStatic
+final class Groovy6996 {
+
+    @Test
+    void testAnonInnerClassLocalVariableAccess() {
+        assertScript '''
+            class C {
+                interface I {
+                }
+                static main(args) {
+                    def var = args
+                    new I() {
+                        def prop = var
+                    }
+                }
+            }
+        '''
+    }
+
+    @Test
+    void testAnonInnerClassParameterAccess() {
+        assertScript '''
+            class C {
+                interface I {
+                }
+                static main(args) {
+                    new I() {
+                        def prop = args // MissingPropertyException: No such property: args for class: C
+                    }
+                }
+            }
+        '''
+    }
+}