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

CompileStatic goes into infinite loop when handling self-referential generics

    XMLWordPrintableJSON

    Details

      Description

      When using @CompileStatic or @TypeChecked the StaticTypeCheckingVisitor recursively calls itself on the following example:

      abstract class Base<A extends Base<A>> {}
      class Done extends Base<Done> { }
      class Next<H, T extends Base<T>> extends Base<Next<H, T>> {
        H head; T tail
        static Next<H, T> next(H h, T t) { new Next<H, T>(head:h, tail:t) }
        String toString() { "Next($head, ${tail.toString()})" }
      }
      import static Next.*
      
      @groovy.transform.CompileStatic
      def foo() {
        Next<Integer, Next<String, Done>> x = next(3, next("foo", new Done()))
      }
      
      println foo() // => Next(3, Next(foo, Done@9b3ec2))
      

      My real example is when using Functional Java's heterogeneous list classes but the above is a cut-down equivalent. The stack trace is:

      java.lang.StackOverflowError
      ...
      org.codehaus.groovy.transform.stc.StaticTypeCheckingVisitor.fullyResolve(StaticTypeCheckingVisitor.java:2836)
      	at org.codehaus.groovy.transform.stc.StaticTypeCheckingVisitor.fullyResolveType(StaticTypeCheckingVisitor.java:2873)
      	at org.codehaus.groovy.transform.stc.StaticTypeCheckingVisitor.fullyResolve(StaticTypeCheckingVisitor.java:2840)
      	at org.codehaus.groovy.transform.stc.StaticTypeCheckingVisitor.fullyResolveType(StaticTypeCheckingVisitor.java:2873)
      	at org.codehaus.groovy.transform.stc.StaticTypeCheckingVisitor.fullyResolve(StaticTypeCheckingVisitor.java:2840)
      	at org.codehaus.groovy.transform.stc.StaticTypeCheckingVisitor.fullyResolveType(StaticTypeCheckingVisitor.java:2873)
      

        Attachments

          Activity

            People

            • Assignee:
              blackdrag Jochen Theodorou
              Reporter:
              paulk Paul King
            • Votes:
              1 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: