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

Problem with empty case in switches when not in last position

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Minor
    • Resolution: Duplicate
    • 2.5.10
    • None
    • syntax
    • None
    • openjdk version "1.8.0_242" (zulu)

    Description

      There seems to be a problem with empty cases for switch with groovy 2.5.10 except when they are in the last position.

      String s = ''
      
      switch (s) {
      
        case 'a':
        	println s
      	break
      
        // will generate a compilation error with groovy 2.5.10
        case 'empty':
              break
      
        case 'b':
              println s
      	break
      }
      

      The exception is :

      org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:General error during class generation: org.codehaus.groovy.ast.stmt.ReturnStatement cannot be cast to org.codehaus.groovy.ast.stmt.BlockStatement
      java.lang.ClassCastException: org.codehaus.groovy.ast.stmt.ReturnStatement cannot be cast to org.codehaus.groovy.ast.stmt.BlockStatement at org.codehaus.groovy.classgen.FinalVariableAnalyzer.fallsThrough(FinalVariableAnalyzer.java:480) at org.codehaus.groovy.classgen.FinalVariableAnalyzer.visitSwitch(FinalVariableAnalyzer.java:344) at org.codehaus.groovy.ast.stmt.SwitchStatement.visit(SwitchStatement.java:53) at org.codehaus.groovy.ast.CodeVisitorSupport.visitBlockStatement(CodeVisitorSupport.java:86) at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitBlockStatement(ClassCodeVisitorSupport.java:106) at org.codehaus.groovy.classgen.FinalVariableAnalyzer.visitBlockStatement(FinalVariableAnalyzer.java:147) at org.codehaus.groovy.ast.stmt.BlockStatement.visit(BlockStatement.java:69) at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitClassCodeContainer(ClassCodeVisitorSupport.java:110) at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitConstructorOrMethod(ClassCodeVisitorSupport.java:121) at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitMethod(ClassCodeVisitorSupport.java:132) at org.codehaus.groovy.ast.ClassNode.visitContents(ClassNode.java:1103) at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitClass(ClassCodeVisitorSupport.java:54) at org.codehaus.groovy.classgen.Verifier.checkFinalVariables(Verifier.java:263) at org.codehaus.groovy.classgen.Verifier.visitClass(Verifier.java:258) at org.codehaus.groovy.control.CompilationUnit$18.call(CompilationUnit.java:811) at org.codehaus.groovy.control.CompilationUnit.applyToPrimaryClassNodes(CompilationUnit.java:1084) at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:640) at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:618) at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:595) at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:401) at groovy.lang.GroovyClassLoader.access$300(GroovyClassLoader.java:89) at groovy.lang.GroovyClassLoader$5.provide(GroovyClassLoader.java:341) at groovy.lang.GroovyClassLoader$5.provide(GroovyClassLoader.java:338) at org.codehaus.groovy.runtime.memoize.ConcurrentCommonCache.getAndPut(ConcurrentCommonCache.java:147) at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:336) at groovy.lang.GroovyShell.parseClass(GroovyShell.java:546) at groovy.lang.GroovyShell.run(GroovyShell.java:375) at groovy.lang.GroovyShell.run(GroovyShell.java:365) at groovy.ui.GroovyMain.processOnce(GroovyMain.java:592) at groovy.ui.GroovyMain.run(GroovyMain.java:336) at groovy.ui.GroovyMain.access$1400(GroovyMain.java:69) at groovy.ui.GroovyMain$GroovyCommand.process(GroovyMain.java:295) at groovy.ui.GroovyMain.processArgs(GroovyMain.java:134) at groovy.ui.GroovyMain.main(GroovyMain.java:116) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.codehaus.groovy.tools.GroovyStarter.rootLoader(GroovyStarter.java:110) at org.codehaus.groovy.tools.GroovyStarter.main(GroovyStarter.java:128)
      1 error 

      It works with groovy 2.5.9 and 3.0.2.

      In attachment, you'll find a small test case to reproduce the problem.

      Attachments

        1. testcase.tgz
          0.5 kB
          Sylvain Dusart

        Issue Links

          Activity

            People

              Unassigned Unassigned
              sdusart Sylvain Dusart
              Votes:
              1 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: