[ https://issues.apache.org/jira/browse/GROOVY-4721?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17941558#comment-17941558 ]
ASF GitHub Bot commented on GROOVY-4721: ---------------------------------------- daniellansun commented on code in PR #2172: URL: https://github.com/apache/groovy/pull/2172#discussion_r2031095137 ########## subprojects/groovy-groovysh/src/test/groovy/org/apache/groovy/groovysh/GroovyshTest.groovy: ########## @@ -392,17 +394,34 @@ class GroovyshInterpreterModeTest extends GroovyshTest { } } + // groovy:000> int x = 3 + // ===> 3 + // groovy:000> x + // Unknown property: x + // groovy:000> x = 3 + // ===> 3 + // groovy:000> x + // ===> 3 + // groovy:000> void testBoundVar() { Groovysh groovysh = createGroovysh() - groovysh.execute('int x = 3') + groovysh.execute('x = 3') assert mockOut.toString().length() > 0 assert ' 3\n' == mockOut.toString().normalize()[-3..-1] groovysh.execute('x') assert mockOut.toString().length() > 0 assert ' 3\n' == mockOut.toString().normalize()[-3..-1] } + // collecting variables relies on GROOVY-4721, e.g. accessing variable declared in try block is in scope in finally block + // but after GROOVY-4721 is fixed, this test will fail, but it is aligned with the behavior of the groovySh: + // groovy:000> int x, y, z + // ===> 0 + // groovy:000> y + // Unknown property: y + // groovy:000> + @NotYetImplemented Review Comment: if the code of `current` throws an exception, the variables will still fail to collect. > variable declared in try block is in scope in finally block > ----------------------------------------------------------- > > Key: GROOVY-4721 > URL: https://issues.apache.org/jira/browse/GROOVY-4721 > Project: Groovy > Issue Type: Bug > Components: Compiler > Affects Versions: 2.4.0-rc-1 > Reporter: Hamlet D'Arcy > Assignee: Daniel Sun > Priority: Major > > This code should fail because 'x' is out of scope in the finally block. > {code} > class MyClass { > def myMethod() { > try { > def x = 'foo' > println x > } > finally { > println x > } > } > } > new MyClass().myMethod() > {code} > Instead it prints 'foo' twice. > if myMethod is static then it shows the behavior I expect. > Could be something wrong with VariableScope somewhere? > This example is from the Groovy codebase. We have code that relies on this > behavior (although it is easy to find and fix, just run the UnusedVariable > codenarc inspection, which is how I found it) -- This message was sent by Atlassian Jira (v8.20.10#820010)