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

@Canonical creates invalid hashCode

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Critical
    • Resolution: Fixed
    • 2.5.2
    • 3.0.0-alpha-4, 2.5.3
    • Compiler
    • 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

            paulk Paul King
            kenzierocks Octavia Togami
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: