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

Adding CompileStatic via compiler config script to class with eachWithIndex results in exception in instruction selection phase

    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Minor
    • Resolution: Fixed
    • Affects Version/s: 2.4.7
    • Fix Version/s: 3.0.0-beta-1, 2.5.7
    • Component/s: Compiler
    • Labels:
      None
    • Environment:
      Groovy Version: 2.4.7 JVM: 1.8.0_92 Vendor: Oracle Corporation OS: Linux

      Description

      I have run into an issue when adding the CompileStatic AST transformation to a class via the Groovy compiler configscript argument.

      Demo.groovy
      class Demo {
          void test() {
              ['a', 'b'].eachWithIndex {String s, int i -> println "$i: $s"}
          }
      }
      
      config.groovy
      import groovy.transform.CompileStatic
      import org.codehaus.groovy.ast.ClassNode
      
      withConfig(configuration) {
          source(classValidator: {ClassNode classNode -> classNode.nameWithoutPackage == 'Demo'}) {
              ast(CompileStatic)
          }
      }
      
      Compile using --configscript
      $ groovyc --configscript config.groovy Demo.groovy
      
      Output (abbreviated)
      >>> a serious error occurred: BUG! exception in phase 'instruction selection' in source unit 'Demo.groovy' unexpected NullpointerException
      >>> stacktrace:
      BUG! exception in phase 'instruction selection' in source unit 'Demo.groovy' unexpected NullpointerException
          at org.codehaus.groovy.control.CompilationUnit.applyToPrimaryClassNodes(CompilationUnit.java:1058)
          at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:591)
          at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:569)
          ...
      Caused by: java.lang.NullPointerException
          at org.codehaus.groovy.control.ClassNodeResolver.tryAsLoaderClassOrScript(ClassNodeResolver.java:180)
          at org.codehaus.groovy.control.ClassNodeResolver.findClassNode(ClassNodeResolver.java:170)
          at org.codehaus.groovy.control.ClassNodeResolver.resolveName(ClassNodeResolver.java:126)
        ...
      

      Notes

      • Groovy & JVM versions: Groovy Version: 2.4.7 JVM: 1.8.0_92 Vendor: Oracle Corporation OS: Linux
        • Have tried Groovy versions going back to 2.4.3 and Oracle JVM 1.7.0_79
      • Changing any of the following will result in no compilation error:
        • Remove the source(classValidator: ...) code around ast, leaving ast(CompileStatic), in the compiler config script
        • Replace the eachWithIndex call in Demo with each
        • Annotate Demo with @CompileStatic and compile without --configscript

      I have detailed and demoed the bug here with some code you can clone and run.

        Attachments

          Activity

            People

            • Assignee:
              paulk Paul King
              Reporter:
              rvenutolo Rick Venutolo
            • Votes:
              1 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: