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/05/25 06:35:12 UTC

[groovy] branch GROOVY_2_5_X updated: GROOVY-9136: check for non-closure parameter references

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 3a8d932  GROOVY-9136: check for non-closure parameter references
3a8d932 is described below

commit 3a8d932962d0c1e3396bf99cbb8d87072e0b8cd4
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Fri May 24 20:04:15 2019 -0500

    GROOVY-9136: check for non-closure parameter references
---
 .../transform/stc/StaticTypeCheckingVisitor.java   | 15 +++----
 src/test/groovy/bugs/Groovy9136.groovy             | 46 ++++++++++++++++++++++
 2 files changed, 52 insertions(+), 9 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 f855239..c23824a 100644
--- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
+++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
@@ -102,7 +102,6 @@ import org.codehaus.groovy.runtime.MetaClassHelper;
 import org.codehaus.groovy.syntax.SyntaxException;
 import org.codehaus.groovy.syntax.Token;
 import org.codehaus.groovy.syntax.TokenUtil;
-import org.codehaus.groovy.syntax.Types;
 import org.codehaus.groovy.transform.StaticTypesTransformation;
 import org.codehaus.groovy.transform.sc.StaticCompilationMetadataKeys;
 import org.codehaus.groovy.transform.trait.Traits;
@@ -199,7 +198,6 @@ import static org.codehaus.groovy.syntax.Types.ASSIGNMENT_OPERATOR;
 import static org.codehaus.groovy.syntax.Types.COMPARE_EQUAL;
 import static org.codehaus.groovy.syntax.Types.COMPARE_IDENTICAL;
 import static org.codehaus.groovy.syntax.Types.COMPARE_NOT_EQUAL;
-import static org.codehaus.groovy.syntax.Types.COMPARE_NOT_IDENTICAL;
 import static org.codehaus.groovy.syntax.Types.COMPARE_TO;
 import static org.codehaus.groovy.syntax.Types.DIVIDE;
 import static org.codehaus.groovy.syntax.Types.DIVIDE_EQUAL;
@@ -520,14 +518,13 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
      * Checks valid cases for accessing a field from an inner class.
      */
     private String checkOrMarkInnerPropertyOwnerAccess(Expression source, FieldNode fn, boolean lhsOfAssignment, String delegationData) {
-        if (fn == null || fn.isStatic() || fn.isPrivate() || "delegate".equals(delegationData)) return delegationData;
-        if (source instanceof PropertyExpression && typeCheckingContext.getEnclosingClosureStack().size() == 1) {
+        if (fn != null && !fn.isStatic() && !fn.isPrivate() && !"delegate".equals(delegationData) &&
+                source instanceof PropertyExpression && typeCheckingContext.getEnclosingClosureStack().size() == 1) {
             PropertyExpression pe = (PropertyExpression) source;
             boolean ownerProperty = !("this".equals(pe.getPropertyAsString()));
+            // check for reference to method, closure, for loop or catch block parameter
             if (ownerProperty && pe.getObjectExpression() instanceof VariableExpression) {
-                Variable accessedVariable = ((VariableExpression) pe.getObjectExpression()).getAccessedVariable();
-                Variable declaredVariable = typeCheckingContext.getEnclosingClosure().getClosureExpression().getVariableScope().getDeclaredVariable(pe.getObjectExpression().getText());
-                if (accessedVariable != null && accessedVariable == declaredVariable) ownerProperty = false;
+                ownerProperty = !(((VariableExpression) pe.getObjectExpression()).getAccessedVariable() instanceof Parameter);
             }
             if (ownerProperty) {
                 delegationData = "owner";
@@ -811,7 +808,7 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
     @Override
     public void visitBinaryExpression(BinaryExpression expression) {
         int op = expression.getOperation().getType();
-        if (op == COMPARE_IDENTICAL || op == COMPARE_NOT_IDENTICAL) {
+        if (op == COMPARE_IDENTICAL) {
             return; // we'll report those as errors later
         }
         BinaryExpression enclosingBinaryExpression = typeCheckingContext.getEnclosingBinaryExpression();
@@ -3779,7 +3776,7 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
         }
         BinaryExpression instanceOfExpression = (BinaryExpression) expression;
         int op = instanceOfExpression.getOperation().getType();
-        if (op != Types.KEYWORD_INSTANCEOF) {
+        if (op != KEYWORD_INSTANCEOF) {
             return null;
         }
         Statement block = ifElse.getIfBlock();
diff --git a/src/test/groovy/bugs/Groovy9136.groovy b/src/test/groovy/bugs/Groovy9136.groovy
new file mode 100644
index 0000000..f85aff2
--- /dev/null
+++ b/src/test/groovy/bugs/Groovy9136.groovy
@@ -0,0 +1,46 @@
+/*
+ *  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
+
+@CompileStatic
+final class Groovy9136 extends GroovyTestCase {
+
+    void testMethodParameterAccessFromClosure() {
+        assertScript '''
+            @groovy.transform.CompileStatic
+            class Foo {
+                public String field = 'foo'
+            }
+            @groovy.transform.CompileStatic
+            class Bar {
+                def doIt(Foo foo) {
+                    'baz'.with {
+                        foo.field // GROOVY-9136: Access to Foo#foo is forbidden at line: -1, column: -1
+                    }
+                }
+            }
+
+            def bar = new Bar()
+            def out = bar.doIt(new Foo())
+            assert out == 'foo'
+        '''
+    }
+}