[ https://issues.apache.org/jira/browse/GROOVY-8562?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16487836#comment-16487836 ]
ASF GitHub Bot commented on GROOVY-8562: ---------------------------------------- Github user danielsun1106 commented on a diff in the pull request: https://github.com/apache/groovy/pull/717#discussion_r190359768 --- Diff: src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java --- @@ -596,6 +597,26 @@ public void visitVariableExpression(VariableExpression vexp) { } else if (accessedVariable instanceof FieldNode) { FieldNode fieldNode = (FieldNode) accessedVariable; + TypeCheckingContext.EnclosingClosure enclosingClosure = typeCheckingContext.getEnclosingClosure(); + if (enclosingClosure != null) { + // GROOVY-8562 + // when vexp has the same name as a property of the owner, + // the IMPLICIT_RECEIVER must be set in case it's the delegate + if (tryVariableExpressionAsProperty(vexp, vexp.getName())) { + // IMPLICIT_RECEIVER is handled elsewhere + // however other access needs to be fixed for private access + if (vexp.getNodeMetaData(StaticTypesMarker.IMPLICIT_RECEIVER) == null) { + boolean lhsOfEnclosingAssignment = isLHSOfEnclosingAssignment(vexp); + ClassNode owner = (ClassNode) vexp.getNodeMetaData(StaticCompilationMetadataKeys.PROPERTY_OWNER); + if (owner != null) { + fieldNode = owner.getField(vexp.getName()); + vexp.setAccessedVariable(fieldNode); + checkOrMarkPrivateAccess(vexp, fieldNode, lhsOfEnclosingAssignment); --- End diff -- `lhsOfEnclosingAssignment` is only used here(in the `if` block) > Wrong Closure delegation for property access when using @CompileStatic and > DELEGATE_FIRST / DELEGATE_ONLY > --------------------------------------------------------------------------------------------------------- > > Key: GROOVY-8562 > URL: https://issues.apache.org/jira/browse/GROOVY-8562 > Project: Groovy > Issue Type: Bug > Components: Static compilation > Affects Versions: 2.3.11, 2.6.0-alpha-3, 3.0.0-alpha-2, 2.5.0-rc-1, 2.4.15 > Reporter: Michael Arndt > Priority: Major > > Closures resolve to owner property although `DELEGATE_FIRST` or > `DELEGATE_ONLY` are set as `resolveStrategy`. > This only happens when `@CompileStatic` is used. > This does not apply to method calls. > For Groovy 2.3.11 this only affects read operations. > Only the versions 2.5.0-rc-1, 2.4.15, 2.3.11 have been tested yet. > I will add tests for newer versions, but this requires a different test > framework than Spock. > I have created a demo project at > [https://github.com/MeneDev/groovy-bugreport-closure-strategy] > > {code:java} > class ADelegate { > def x = "delegate" > } > @CompileStatic > class AClass { > public <T> T closureExecuter( > ADelegate d, > @DelegatesTo(value = ADelegate, strategy = DELEGATE_ONLY) > Closure<T> c) { > c.resolveStrategy = DELEGATE_ONLY > c.delegate = d > return c() > } > def x = "owner" > > def test() { > def theDelegate = new ADelegate() > def res = closureExecuter(theDelegate) { > return x > } > > // is "owner" instead of "delegate" > return res > } > } > {code} -- This message was sent by Atlassian JIRA (v7.6.3#76005)