You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Daniel Sun (JIRA)" <ji...@apache.org> on 2017/02/27 08:08:46 UTC

[jira] [Commented] (GROOVY-4721) variable declared in try block is in scope in finally block

    [ https://issues.apache.org/jira/browse/GROOVY-4721?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15885321#comment-15885321 ] 

Daniel Sun commented on GROOVY-4721:
------------------------------------

We can investigate the range(start, end) in the the following code and try to fix it:
https://github.com/apache/groovy/blob/master/src/main/org/codehaus/groovy/classgen/asm/CompileStack.java#L370
{code}
            for (Iterator iterator = usedVariables.iterator(); iterator.hasNext();) {
                BytecodeVariable v = (BytecodeVariable) iterator.next();
                ClassNode t = v.getType();
                if (v.isHolder()) t = ClassHelper.REFERENCE_TYPE;
                String type = BytecodeHelper.getTypeDescription(t);
                Label start = v.getStartLabel();
                Label end = v.getEndLabel();
                mv.visitLocalVariable(v.getName(), type, null, start, end, v.getIndex());
            }
{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)