groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sun...@apache.org
Subject [groovy] branch master updated: GROOVY-9265: do not set "owner" receiver for parameter or local variable
Date Mon, 07 Oct 2019 03:53:27 GMT
This is an automated email from the ASF dual-hosted git repository.

sunlan 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 9b89995  GROOVY-9265: do not set "owner" receiver for parameter or local variable
9b89995 is described below

commit 9b8999558d6aee7b6c66e591112031be97ca42c3
Author: Eric Milles <eric.milles@thomsonreuters.com>
AuthorDate: Wed Oct 2 15:52:26 2019 -0500

    GROOVY-9265: do not set "owner" receiver for parameter or local variable
---
 .../transform/stc/StaticTypeCheckingVisitor.java   | 18 +++++---
 src/test/groovy/bugs/Groovy9265.groovy             | 49 ++++++++++++++++++++++
 2 files changed, 61 insertions(+), 6 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
index 878cbd0..0e54e69 100644
--- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
+++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
@@ -532,16 +532,22 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport
{
      */
     private String checkOrMarkInnerPropertyOwnerAccess(PropertyExpression source, boolean
lhsOfAssignment, String delegationData) {
         // check for reference to method, closure, for loop, try with, or catch block parameter
from a non-nested closure
-        if (typeCheckingContext.getEnclosingClosureStack().size() == 1 && !"this".equals(source.getPropertyAsString()))
{
-            if (!(source.getObjectExpression() instanceof VariableExpression &&
-                    ((VariableExpression) source.getObjectExpression()).getAccessedVariable()
instanceof Parameter)) {
-                delegationData = "owner";
-                source.getObjectExpression().putNodeMetaData(StaticTypesMarker.IMPLICIT_RECEIVER,
delegationData);
-            }
+        if (typeCheckingContext.getEnclosingClosureStack().size() == 1 && !"this".equals(source.getPropertyAsString())
&&
+                !isParameterOrVariableReference(source.getObjectExpression())) {
+            delegationData = "owner";
+            source.getObjectExpression().putNodeMetaData(StaticTypesMarker.IMPLICIT_RECEIVER,
delegationData);
         }
         return delegationData;
     }
 
+    private static boolean isParameterOrVariableReference(Expression objectExpr) {
+        if (objectExpr instanceof VariableExpression) {
+            Variable accessedVar = ((VariableExpression) objectExpr).getAccessedVariable();
+            return (accessedVar instanceof Parameter || accessedVar instanceof VariableExpression);
+        }
+        return false;
+    }
+
     private MethodNode findValidGetter(ClassNode classNode, String name) {
         MethodNode getterMethod = classNode.getGetterMethod(name);
         if (getterMethod != null && (getterMethod.isPublic() || getterMethod.isProtected()))
{
diff --git a/src/test/groovy/bugs/Groovy9265.groovy b/src/test/groovy/bugs/Groovy9265.groovy
new file mode 100644
index 0000000..ba9b36d
--- /dev/null
+++ b/src/test/groovy/bugs/Groovy9265.groovy
@@ -0,0 +1,49 @@
+/*
+ *  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 Groovy9265 {
+
+    @Test
+    void testAccessInnerClassFieldFromClosure() {
+        assertScript '''
+            class Outer {
+                static class Inner {
+                    public String field = 'works'
+                }
+            }
+
+            @groovy.transform.CompileStatic
+            def test() {
+                return { ->
+                    def var = new Outer.Inner()
+                    var.field
+                }()
+            }
+
+            assert test() == 'works'
+        '''
+    }
+}


Mime
View raw message