[
https://issues.apache.org/jira/browse/GROOVY-4721?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15886030#comment-15886030
]
Daniel Sun commented on GROOVY-4721:
------------------------------------
Here is my test code, which I expect to fail:
*groovy source code:*
{code}
String m() {
try {
String str = "hello";
return str;
} finally {
System.out.println(str);
}
}
m()
{code}
*bytecode:*
{code}
// access flags 0x1
public m()Ljava/lang/String;
TRYCATCHBLOCK L0 L1 L2 null
TRYCATCHBLOCK L3 L4 L2 null
L5
INVOKESTATIC script1488211656663.$getCallSiteArray
()[Lorg/codehaus/groovy/runtime/callsite/CallSite;
ASTORE 1
L0
LINENUMBER 3 L0
LDC "hello"
ASTORE 2
L6
ALOAD 2
POP
L7
LINENUMBER 4 L7
ALOAD 2
ASTORE 3
NOP
L1
LINENUMBER 6 L1
ALOAD 1
LDC 2
AALOAD
ALOAD 1
LDC 3
AALOAD
LDC Ljava/lang/System;.class
INVOKEINTERFACE
org/codehaus/groovy/runtime/callsite/CallSite.callGetProperty
(Ljava/lang/Object;)Ljava/lang/Object;
ALOAD 2
INVOKEINTERFACE org/codehaus/groovy/runtime/callsite/CallSite.call
(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
POP
NOP
L3
ALOAD 3
ARETURN
L8
GOTO L4
L4
ALOAD 1
LDC 4
AALOAD
ALOAD 1
LDC 5
AALOAD
LDC Ljava/lang/System;.class
INVOKEINTERFACE
org/codehaus/groovy/runtime/callsite/CallSite.callGetProperty
(Ljava/lang/Object;)Ljava/lang/Object;
ALOAD 1
LDC 6
AALOAD
ALOAD 0
INVOKEINTERFACE
org/codehaus/groovy/runtime/callsite/CallSite.callGroovyObjectGetProperty
(Ljava/lang/Object;)Ljava/lang/Object;
INVOKEINTERFACE org/codehaus/groovy/runtime/callsite/CallSite.call
(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
POP
NOP
GOTO L9
L2
ASTORE 4
ALOAD 1
LDC 7
AALOAD
ALOAD 1
LDC 8
AALOAD
LDC Ljava/lang/System;.class
INVOKEINTERFACE
org/codehaus/groovy/runtime/callsite/CallSite.callGetProperty
(Ljava/lang/Object;)Ljava/lang/Object;
ALOAD 1
LDC 9
AALOAD
ALOAD 0
INVOKEINTERFACE
org/codehaus/groovy/runtime/callsite/CallSite.callGroovyObjectGetProperty
(Ljava/lang/Object;)Ljava/lang/Object;
INVOKEINTERFACE org/codehaus/groovy/runtime/callsite/CallSite.call
(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
POP
ALOAD 4
ATHROW
L9
ACONST_NULL
ARETURN
LOCALVARIABLE this Lscript1488211656663; L5 L9 0
LOCALVARIABLE str Ljava/lang/String; L6 L8 2
MAXSTACK = 4
MAXLOCALS = 5
{code}
> 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
>
> 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
(v6.3.15#6346)