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

Code that causes Groovy Compiler Crash



    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 4.0.15
    • 3.0.20, 5.0.0-alpha-3, 4.0.16
    • Static Type Checker
    • I am running on MacBook Pro, but I think this is a probably regardless of environment. I have been able to repeat this bug for Groovy versions from 3.0.10 to 4.0.15 and all in between.
    • Important


      When attempting to compile the code below, it causes the Groovy compiler to crash with:

      java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1
              at org.codehaus.groovy.ast.tools.GenericsUtils.createGenericsSpec(GenericsUtils.java:494)
              at org.codehaus.groovy.ast.tools.GenericsUtils.createGenericsSpec(GenericsUtils.java:480)
              at org.codehaus.groovy.ast.tools.GenericsUtils.parameterizeType(GenericsUtils.java:293)

      The crash is caused because the GenericType parameter is missing on line 9 in TestCompilerCrashOnTemplateArgCount:

      private Map<Long, String> map = new MapLong<>()   // causes compiler crash


      private Map<Long, String> map2 = new MapLong<String>() // doest not cause compiler crash

      1st source file (TestCompilerCrashOnTemplateArgCount.groovy)

      package com.x.y.z
      import groovy.transform.CompileStatic
      class TestCompilerCrashOnTemplateArgCount
          // The line below crashes Groovy Compiler with ArrayOutOfBoundsException during compilation because no type argument passed to MapLong<>()
          private Map<Long, String> map = new MapLong<>()
          // The line below doesn't crash the compiler:
          private Map<Long, String> map2 = new MapLong<String>()

      2nd source file (MapLong.groovy):

      package com.x.y.z
      import groovy.transform.CompileStatic
      class MapLong<V> implements Map<Long, V>
          int size() { return 0  }
          boolean isEmpty() { return false }
          boolean containsKey(Object key) { return false }
          boolean containsValue(Object value) { return false }
          V get(Object key) { return null }
          V put(Long key, V value) { return null }
          V remove(Object key) { return null }
          void putAll(Map<? extends Long, ? extends V> m) { }
          void clear() { }
          Set<Long> keySet() { return null }
          Collection<V> values() { return null }
          Set<Entry<Long, V>> entrySet() { return null }


      The compiler is expecting one argument, but a no argument array was allocated.  When it attempts to access the array at element [0], the compiler hits ArrayOutOfBoundsException.


      This is super easy to repeat with the two tiny source files attached.


        1. MapLong-3.groovy
          0.6 kB
          John DeRegnaucourt
        2. TestCompilerCrashOnTemplateArgCount.groovy
          0.4 kB
          John DeRegnaucourt

        Issue Links



              emilles Eric Milles
              jdereg John DeRegnaucourt
              0 Vote for this issue
              4 Start watching this issue