groovy
  1. groovy
  2. GROOVY-4747

ClosureWriter incorrectly changes the accessedVariable field of a VariableExpression

    Details

    • Type: Bug Bug
    • Status: Closed Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 1.8-rc-2, 1.8-rc-3
    • Fix Version/s: 1.8-rc-4
    • Component/s: class generator
    • Labels:
      None
    • Number of attachments :
      0

      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.

        Activity

        No work has yet been logged on this issue.

          People

          • Assignee:
            blackdrag blackdrag
            Reporter:
            Andrew Eisenberg
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: