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

ClosureWriter incorrectly changes the accessedVariable field of a VariableExpression

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 1.8-rc-2, 1.8-rc-3
    • 1.8-rc-4
    • class generator
    • None

    Description

      Since 1.8, there is a ClosureWriter class, which (among other things) corrects the accessed variable field of VariableExpressions inside of closures. The method correctAccessedVariable looks at each VariableExpression and tries to map the variable name to a field name inside of a synthetic Closure class generated by the ClosureWriter. If no such variable name is found, then the accessed variable field is set to null.

      This last part is incorrect. The accessed variable should not be set to null. The accessed variable may point to a field in the class declaring the closure, or it may point to a closure parameter, or a local variable, etc.

      I am not sure if this has any runtime effect on Groovy code, but it does have an effect on Groovy-Eclipse. Because of this problem. Field references inside of closures cannot be resolved. This affects content assist, navigation, hovers, search, refactoring (etc).

      A simple null check fixes the problem in Groovy-Eclipse. Here is the change (from 1.8-rc2):

      On line 288, change:

      expression.setAccessedVariable(fn);
      

      to:

      if (fn != null) // only overwrite if we find something more specific
          expression.setAccessedVariable(fn);
      

      I'll think about some good test cases for this.

      Attachments

        Activity

          People

            blackdrag Jochen Theodorou
            werdna Andrew Eisenberg
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: