Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-4721

variable declared in try block is in scope in finally block

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Open
    • Major
    • Resolution: Unresolved
    • 2.4.0-rc-1
    • None
    • Compiler

    Description

      This code should fail because 'x' is out of scope in the finally block.

      class MyClass {
          def myMethod() {
              try {
                  def x = 'foo'
                  println x
              }
              finally {
                  println x
              }
          }
      }
      new MyClass().myMethod()
      

      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)

      Attachments

        Activity

          People

            Unassigned Unassigned
            hamletdrc Hamlet D'Arcy
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated: