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

Compiler crash when trying to cast a boolean to float variable

    XMLWordPrintableJSON

Details

    Description

      I have the following Groovy program.

      @groovy.transform.CompileStatic
      class Main {
      
        void foo() {
          float x = true
        }
      }
      
      

      Actual Behavior

      The compiler crashes, with the following stacktrace.

      org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
      General error during canonicalization: class java.lang.Boolean cannot be cast to class java.lang.Number (java.lang.Boolean and java.lang.Number are in module java.base of loader 'bootstrap')
      
      java.lang.ClassCastException: class java.lang.Boolean cannot be cast to class java.lang.Number (java.lang.Boolean and java.lang.Number are in module java.base of loader 'bootstrap')
              at org.codehaus.groovy.transform.sc.transformers.BinaryExpressionTransformer.optimizeConstantInitialization(BinaryExpressionTransformer.java:368)
              at org.codehaus.groovy.transform.sc.transformers.BinaryExpressionTransformer.transformBinaryExpression(BinaryExpressionTransformer.java:107)
              at org.codehaus.groovy.transform.sc.transformers.StaticCompilationTransformer.transform(StaticCompilationTransformer.java:113)
              at org.codehaus.groovy.ast.ClassCodeExpressionTransformer.visitExpressionStatement(ClassCodeExpressionTransformer.java:108)
              at org.codehaus.groovy.ast.stmt.ExpressionStatement.visit(ExpressionStatement.java:41)
              at org.codehaus.groovy.ast.CodeVisitorSupport.visitBlockStatement(CodeVisitorSupport.java:86)
              at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitBlockStatement(ClassCodeVisitorSupport.java:164)
              at org.codehaus.groovy.ast.stmt.BlockStatement.visit(BlockStatement.java:70)
              at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitClassCodeContainer(ClassCodeVisitorSupport.java:138)
              at org.codehaus.groovy.transform.sc.transformers.StaticCompilationTransformer.visitClassCodeContainer(StaticCompilationTransformer.java:104)
              at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitConstructorOrMethod(ClassCodeVisitorSupport.java:111)
              at org.codehaus.groovy.ast.ClassCodeExpressionTransformer.visitConstructorOrMethod(ClassCodeExpressionTransformer.java:66)
              at org.codehaus.groovy.transform.sc.transformers.StaticCompilationTransformer.visitConstructorOrMethod(StaticCompilationTransformer.java:166)
              at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitMethod(ClassCodeVisitorSupport.java:106)
              at org.codehaus.groovy.ast.ClassNode.visitMethods(ClassNode.java:1110)
              at org.codehaus.groovy.ast.ClassNode.visitContents(ClassNode.java:1103)
              at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitClass(ClassCodeVisitorSupport.java:52)
              at org.codehaus.groovy.transform.sc.transformers.StaticCompilationTransformer.visitClass(StaticCompilationTransformer.java:153)
              at org.codehaus.groovy.transform.sc.StaticCompileTransformation.visit(StaticCompileTransformation.java:90)
              at org.codehaus.groovy.transform.ASTTransformationVisitor.visitClass(ASTTransformationVisitor.java:146)
              at org.codehaus.groovy.transform.ASTTransformationVisitor.lambda$addPhaseOperations$2(ASTTransformationVisitor.java:229)
              at org.codehaus.groovy.control.CompilationUnit$IPrimaryClassNodeOperation.doPhaseOperation(CompilationUnit.java:928)
              at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:657)
              at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:634)
              at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:609)
              at org.codehaus.groovy.tools.FileSystemCompiler.compile(FileSystemCompiler.java:311)
              at org.codehaus.groovy.tools.FileSystemCompiler.doCompilation(FileSystemCompiler.java:240)
              at org.codehaus.groovy.tools.FileSystemCompiler.commandLineCompile(FileSystemCompiler.java:165)
              at org.codehaus.groovy.tools.FileSystemCompiler.commandLineCompileWithErrorHandling(FileSystemCompiler.java:205)
              at org.codehaus.groovy.tools.FileSystemCompiler.main(FileSystemCompiler.java:189)
      
      1 error
      
      
      

      Expected Behavior

      Compile-time error.

      Comment

      Note that if we use Typecheck instead of CompileStatic the program compiles without any error.

      Affected Version

      This programs fails when compiled with the compiler from the master (commit: e33e2849e247608c5e1826b76098d3a68d16bebf), 4.0.0-alpha-2, and 3.0.8.

      Attachments

        Activity

          People

            emilles Eric Milles
            schaliasos Stefanos Chaliasos
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: