This is an automated email from the ASF dual-hosted git repository. sunlan pushed a commit to branch GROOVY_3_0_X in repository https://gitbox.apache.org/repos/asf/groovy.git
commit 980ea54db460a76106cc3569381a5c5525f7f6f7 Author: Eric Milles <[email protected]> AuthorDate: Tue Oct 15 13:32:11 2019 -0500 GROOVY-6996: adjust 6834 workaround to apply to synthetic variables only (cherry picked from commit 4e0ed234e9530f5bbaea89111b26ab0f83eb4d5f) --- .../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 12ae1ed..e6ffc6f 100644 --- a/src/main/java/org/codehaus/groovy/classgen/VariableScopeVisitor.java +++ b/src/main/java/org/codehaus/groovy/classgen/VariableScopeVisitor.java @@ -558,22 +558,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 + } + } + } + ''' + } +}
