Skip to content

Commit

Permalink
GROOVY-9063: @CompileStatic generates invalid bytecode when accessing…
Browse files Browse the repository at this point in the history
… protected instance member from 2 level of nested closures
  • Loading branch information
paulk-asert committed May 6, 2019
1 parent 1c58239 commit 9456fb2
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ private void checkOrMarkPrivateAccess(Expression source, FieldNode fn, boolean l
*/
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) {
Expand Down Expand Up @@ -688,16 +688,23 @@ public void visitVariableExpression(VariableExpression vexp) {

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;
}
}

Expand Down Expand Up @@ -3155,6 +3162,7 @@ private static void addReceivers(final List<Receiver<String>> receivers,
if (dmd.getParent() == null) {
receivers.addAll(owners);
} else {
//receivers.add(new Receiver<String>(CLOSURE_TYPE, path + "owner"));
addReceivers(receivers, owners, dmd.getParent(), path + "owner.");
}
}
Expand All @@ -3164,6 +3172,7 @@ private static void addReceivers(final List<Receiver<String>> receivers,
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) {
Expand Down Expand Up @@ -3590,6 +3599,15 @@ protected List<Receiver<String>> makeOwnerList(final Expression objectExpression
&& ((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;
}

Expand Down
42 changes: 42 additions & 0 deletions src/test/groovy/bugs/Groovy9063Bug.groovy
Original file line number Diff line number Diff line change
@@ -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
'''
}
}

0 comments on commit 9456fb2

Please sign in to comment.