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

@Canonical creates invalid hashCode

    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Critical
    • Resolution: Fixed
    • Affects Version/s: 2.5.2
    • Fix Version/s: 3.0.0-alpha-4, 2.5.3
    • Component/s: Compiler
    • Labels:
      None

      Description

      Applying @Canonical to a class will generate an invalid hashCode method that throws a runtime cast exception. This does not happen if you apply the individual annotations by themselves.

      These tests can be injected into a Groovy test class to see the problem: https://gist.github.com/kenzierocks/fb0932756c3955a7ddbc0b710791af11

      I suspect the underlying cause is that @EqualsAndHashCode does not set accessedVariable on its _result variable, which causes the verifier to overwrite it with toString's _result variable (see FinalVariableAnalyzer#fixVar). This results in an invalid cast. I observed this behavior while debugging the compiler. I'm not sure why this doesn't also happen if the two annotations are applied by themselves, rather than via @Canonical.

       

      I was able to fix this problem by setting _result to reference itself in the AST, but perhaps that's not the right fix here.

        Attachments

          Activity

            People

            • Assignee:
              paulk Paul King
              Reporter:
              kenzierocks Kenzie Togami
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: