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/06 23:15:12 UTC
[groovy] branch GROOVY_2_5_X updated: GROOVY-9063: @CompileStatic
generates invalid bytecode when accessing protected instance member from 2
level of nested closures
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 9456fb2 GROOVY-9063: @CompileStatic generates invalid bytecode when accessing protected instance member from 2 level of nested closures
9456fb2 is described below
commit 9456fb2bf5f0718475743043afaf9d71dc150356
Author: Paul King <pa...@asert.com.au>
AuthorDate: Tue May 7 08:43:10 2019 +1000
GROOVY-9063: @CompileStatic generates invalid bytecode when accessing protected instance member from 2 level of nested closures
---
.../transform/stc/StaticTypeCheckingVisitor.java | 40 +++++++++++++++------
src/test/groovy/bugs/Groovy9063Bug.groovy | 42 ++++++++++++++++++++++
2 files changed, 71 insertions(+), 11 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 d132808..edb8e8d 100644
--- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
+++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
@@ -520,7 +520,7 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
*/
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.getEnclosingClosure() != null) {
+ if (source instanceof PropertyExpression && typeCheckingContext.getEnclosingClosureStack().size() == 1) {
PropertyExpression pe = (PropertyExpression) source;
boolean ownerProperty = !("this".equals(pe.getPropertyAsString()));
if (ownerProperty && pe.getObjectExpression() instanceof VariableExpression) {
@@ -688,16 +688,23 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
TypeCheckingContext.EnclosingClosure enclosingClosure = typeCheckingContext.getEnclosingClosure();
if (enclosingClosure != null) {
- String name = vexp.getName();
- if (name.equals("owner") || name.equals("thisObject")) {
- storeType(vexp, typeCheckingContext.getEnclosingClassNode());
- return;
- } else if ("delegate".equals(name)) {
- DelegationMetadata md = getDelegationMetadata(enclosingClosure.getClosureExpression());
- ClassNode type = typeCheckingContext.getEnclosingClassNode();
- if (md != null) type = md.getType();
- storeType(vexp, type);
- return;
+ switch (vexp.getName()) {
+ case "delegate":
+ DelegationMetadata md = getDelegationMetadata(enclosingClosure.getClosureExpression());
+ if (md != null) {
+ storeType(vexp, md.getType());
+ return;
+ }
+ // falls through
+ case "owner":
+ if (typeCheckingContext.getEnclosingClosureStack().size() > 1) {
+ storeType(vexp, CLOSURE_TYPE);
+ return;
+ }
+ // falls through
+ case "thisObject":
+ storeType(vexp, typeCheckingContext.getEnclosingClassNode());
+ return;
}
}
@@ -3155,6 +3162,7 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
if (dmd.getParent() == null) {
receivers.addAll(owners);
} else {
+ //receivers.add(new Receiver<String>(CLOSURE_TYPE, path + "owner"));
addReceivers(receivers, owners, dmd.getParent(), path + "owner.");
}
}
@@ -3164,6 +3172,7 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
if (dmd.getParent() == null) {
receivers.addAll(owners);
} else {
+ //receivers.add(new Receiver<String>(CLOSURE_TYPE, path + "owner"));
addReceivers(receivers, owners, dmd.getParent(), path + "owner.");
}
if (strategy == Closure.OWNER_FIRST) {
@@ -3590,6 +3599,15 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
&& ((VariableExpression) objectExpression).getName().equals("it")) {
owners.add(Receiver.<String>make(typeCheckingContext.lastImplicitItType));
}
+ if (typeCheckingContext.delegationMetadata != null
+ && objectExpression instanceof VariableExpression
+ && ((VariableExpression) objectExpression).getName().equals("owner")
+ && /*isNested:*/typeCheckingContext.delegationMetadata.getParent() != null) {
+ owners.clear();
+ List<Receiver<String>> enclosingClass = Collections.singletonList(
+ Receiver.<String>make(typeCheckingContext.getEnclosingClassNode()));
+ addReceivers(owners, enclosingClass, typeCheckingContext.delegationMetadata.getParent(), "owner.");
+ }
return owners;
}
diff --git a/src/test/groovy/bugs/Groovy9063Bug.groovy b/src/test/groovy/bugs/Groovy9063Bug.groovy
new file mode 100644
index 0000000..9374e82
--- /dev/null
+++ b/src/test/groovy/bugs/Groovy9063Bug.groovy
@@ -0,0 +1,42 @@
+/*
+ * 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
+
+class Groovy9063Bug extends GroovyTestCase {
+ void testProtectedFieldInClosureInEnum() {
+ assertScript '''
+ import groovy.transform.CompileStatic
+
+ @CompileStatic
+ class Groovy9063 {
+ protected String message = "hello"
+
+ int nestedClosures() {
+ { ->
+ { ->
+ message.length()
+ }.call()
+ }.call()
+ }
+ }
+
+ assert new Groovy9063().nestedClosures() == 5
+ '''
+ }
+}