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

@CompileStatic: Binary operation with property throws ClassCastException

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 3.0.7
    • 3.0.8, 4.0.0-alpha-3
    • Compiler
    • None

    Description

      Problem
      Build fails when applying a binary operation to a property inside a method, depending on whether a parameter is involved:

      ClassCastException: class org.codehaus.groovy.ast.Parameter cannot be cast to class org.codehaus.groovy.ast.expr.VariableExpression
      

      or not:

      BUG! exception in phase 'class generation' in source unit
      

      Problem does not appear under @TypeChecked, nor when the class member is a field or the field is not accessed throug a property.

      Expected
      Code should compile.

      Sample Code

      @Test @Ignore
      void 'Groovy 3-0-7 Arithmetic operation with property throws ClassCastException'() {
      	final goo = new Goo()
      
      	//
      	// ClassCastException: class org.codehaus.groovy.ast.Parameter cannot be cast to class org.codehaus.groovy.ast.expr.VariableExpression
      	//
      	final result = goo.calc(123)
      
      	//
      	// BUG! exception in phase 'class generation' in source unit ... On receiver: 3 with message: minus and arguments: nrNewEntries
      	//
       	final result = goo.calc()
      	assert result == 333
      }
      
      @CompileStatic
      //@TypeChecked // works
      class Goo {
      	// int nrNewEntries // field without property wrapper works
      	// int nrNewEntries() { nrNewEntriesField } // non-property method works
      	int nrNewEntriesField
      	int getNrNewEntries() { nrNewEntriesField }
      
      	/*
      			Groovyc: While compiling [tests of GroovySimple]: java.lang.ClassCastException: class org.codehaus.groovy.ast.Parameter cannot be cast to class org.codehaus.groovy.ast.expr.VariableExpression (org.codehaus.groovy.ast.Parameter and org.codehaus.groovy.ast.expr.VariableExpression are in unnamed module of loader org.jetbrains.jps.incremental.groovy.JointCompilationClassLoader @12011fbf)
      			at org.codehaus.groovy.classgen.asm.sc.StaticTypesCallSiteWriter.makeSingleArgumentCall(StaticTypesCallSiteWriter.java:598)
      			at org.codehaus.groovy.classgen.asm.InvocationWriter.makeSingleArgumentCall(InvocationWriter.java:616)
      			at org.codehaus.groovy.classgen.asm.BinaryExpressionHelper.evaluateBinaryExpression(BinaryExpressionHelper.java:590)
      			at org.codehaus.groovy.classgen.asm.BinaryExpressionMultiTypeDispatcher.evaluateBinaryExpression(BinaryExpressionMultiTypeDispatcher.java:242)
      			at org.codehaus.groovy.classgen.asm.BinaryExpressionHelper.eval(BinaryExpressionHelper.java:222)
      			at org.codehaus.groovy.classgen.AsmClassGenerator.visitBinaryExpression(AsmClassGenerator.java:707)
      			at org.codehaus.groovy.ast.expr.BinaryExpression.visit(BinaryExpression.java:58)
      			at org.codehaus.groovy.classgen.asm.StatementWriter.writeReturn(StatementWriter.java:613)
      			at org.codehaus.groovy.classgen.AsmClassGenerator.visitReturnStatement(AsmClassGenerator.java:681)
      			at org.codehaus.groovy.ast.stmt.ReturnStatement.visit(ReturnStatement.java:73)
      			at org.codehaus.groovy.classgen.asm.StatementWriter.writeBlockStatement(StatementWriter.java:94)
      			at org.codehaus.groovy.classgen.asm.sc.StaticTypesStatementWriter.writeBlockStatement(StaticTypesStatementWriter.java:78)
      			at org.codehaus.groovy.classgen.AsmClassGenerator.visitBlockStatement(AsmClassGenerator.java:617)
      			at org.codehaus.groovy.ast.stmt.BlockStatement.visit(BlockStatement.java:69)
      			at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitClassCodeContainer(ClassCodeVisitorSupport.java:138)
      			at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitConstructorOrMethod(ClassCodeVisitorSupport.java:111)
      			at org.codehaus.groovy.classgen.AsmClassGenerator.visitStdMethod(AsmClassGenerator.java:461)
      			at org.codehaus.groovy.classgen.AsmClassGenerator.visitConstructorOrMethod(AsmClassGenerator.java:408)
      			at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitMethod(ClassCodeVisitorSupport.java:106)
      			at org.codehaus.groovy.classgen.AsmClassGenerator.visitMethod(AsmClassGenerator.java:556)
      			at org.codehaus.groovy.ast.ClassNode.visitMethods(ClassNode.java:1099)
      			at org.codehaus.groovy.ast.ClassNode.visitContents(ClassNode.java:1092)
      			at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitClass(ClassCodeVisitorSupport.java:52)
      			at org.codehaus.groovy.classgen.AsmClassGenerator.visitClass(AsmClassGenerator.java:271)
      			at org.codehaus.groovy.control.CompilationUnit$3.call(CompilationUnit.java:797)
      			at org.codehaus.groovy.control.CompilationUnit$IPrimaryClassNodeOperation.doPhaseOperation(CompilationUnit.java:942)
      			at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:671)
      			at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:635)
      	*/
      Integer calc(int i) { i - nrNewEntries }
      
      /*
      		Groovyc: While compiling [tests of GroovySimple]: BUG! exception in phase 'class generation' in source unit 'C:\source\GroovySimple\test\simple\groovy\bugs\groovy3\gb_2021_03_08\property_arithmetic_op_throws_class_cast\Goo.groovy' At line 49 column 19
      		On receiver: 3 with message: minus and arguments: nrNewEntries
      		This method should not have been called. Please try to create a simple example reproducing
      		this error and file a bug report at https://issues.apache.org/jira/browse/GROOVY
      			at org.codehaus.groovy.classgen.asm.sc.StaticTypesCallSiteWriter.makeSingleArgumentCall(StaticTypesCallSiteWriter.java:607)
      			at org.codehaus.groovy.classgen.asm.InvocationWriter.makeSingleArgumentCall(InvocationWriter.java:616)
      			at org.codehaus.groovy.classgen.asm.BinaryExpressionHelper.evaluateBinaryExpression(BinaryExpressionHelper.java:590)
      			at org.codehaus.groovy.classgen.asm.BinaryExpressionMultiTypeDispatcher.evaluateBinaryExpression(BinaryExpressionMultiTypeDispatcher.java:242)
      			at org.codehaus.groovy.classgen.asm.BinaryExpressionHelper.eval(BinaryExpressionHelper.java:222)
      			at org.codehaus.groovy.classgen.AsmClassGenerator.visitBinaryExpression(AsmClassGenerator.java:707)
      			at org.codehaus.groovy.ast.expr.BinaryExpression.visit(BinaryExpression.java:58)
      			at org.codehaus.groovy.classgen.asm.StatementWriter.writeReturn(StatementWriter.java:613)
      			at org.codehaus.groovy.classgen.AsmClassGenerator.visitReturnStatement(AsmClassGenerator.java:681)
      			at org.codehaus.groovy.ast.stmt.ReturnStatement.visit(ReturnStatement.java:73)
      			at org.codehaus.groovy.classgen.asm.StatementWriter.writeBlockStatement(StatementWriter.java:94)
      			at org.codehaus.groovy.classgen.asm.sc.StaticTypesStatementWriter.writeBlockStatement(StaticTypesStatementWriter.java:78)
      			at org.codehaus.groovy.classgen.AsmClassGenerator.visitBlockStatement(AsmClassGenerator.java:617)
      			at org.codehaus.groovy.ast.stmt.BlockStatement.visit(BlockStatement.java:69)
      			at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitClassCodeContainer(ClassCodeVisitorSupport.java:138)
      			at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitConstructorOrMethod(ClassCodeVisitorSupport.java:111)
      			at org.codehaus.groovy.classgen.AsmClassGenerator.visitStdMethod(AsmClassGenerator.java:461)
      			at org.codehaus.groovy.classgen.AsmClassGenerator.visitConstructorOrMethod(AsmClassGenerator.java:408)
      			at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitMethod(ClassCodeVisitorSupport.java:106)
      			at org.codehaus.groovy.classgen.AsmClassGenerator.visitMethod(AsmClassGenerator.java:556)
      			at org.codehaus.groovy.ast.ClassNode.visitMethods(ClassNode.java:1099)
      			at org.codehaus.groovy.ast.ClassNode.visitContents(ClassNode.java:1092)
      			at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitClass(ClassCodeVisitorSupport.java:52)
      			at org.codehaus.groovy.classgen.AsmClassGenerator.visitClass(AsmClassGenerator.java:271)
      			at org.codehaus.groovy.control.CompilationUnit$3.call(CompilationUnit.java:797)
      			at org.codehaus.groovy.control.CompilationUnit$IPrimaryClassNodeOperation.doPhaseOperation(CompilationUnit.java:942)
      			at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:671)
      			at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:635)
      			at org.jetbrains.groovy.compiler.rt.GroovyCompilerWrapper.compile(GroovyCompilerWrapper.java:48)
      			at org.jetbrains.groovy.compiler.rt.DependentGroovycRunner.runGroovyc(DependentGroovycRunner.java:118)
      			at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      			at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      			at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      			at java.base/java.lang.reflect.Method.invoke(Method.java:566)
      			at org.jetbrains.groovy.compiler.rt.GroovycRunner.intMain2(GroovycRunner.java:81)
      			at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      			at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      			at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      			at java.base/java.lang.reflect.Method.invoke(Method.java:566)
      			at org.jetbrains.jps.incremental.groovy.InProcessGroovyc.runGroovycInThisProcess(InProcessGroovyc.java:167)
      			at org.jetbrains.jps.incremental.groovy.InProcessGroovyc.lambda$runGroovyc$0(InProcessGroovyc.java:77)
      			at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
      			at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
      			at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
      			at java.base/java.lang.Thread.run(Thread.java:834)
      */
      Integer calc() { 333 - nrNewEntries }
      }
      

      Attachments

        Activity

          People

            emilles Eric Milles
            emge mgroovy
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: