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

Closure default params can cause BUG! exception in phase 'class generation'

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Minor
    • Resolution: Fixed
    • 2.0.1
    • 2.1.0-rc-1, 2.0.7
    • Compiler
    • None
    • Groovy Version: 2.0.1 JVM: 1.6.0_33 Vendor: Apple Inc. OS: Mac OS X

    Description

      Default parameters for methods can be used in further default parameters, ie:

      def f( int x, fn={ -> x } ) {
        fn()
      }
      
      f( 10 )
      

      (outputs 10)

      However, if I give x a default value:

      def f( int x=3, fn={ -> x } ) {
        fn()
      }
      
      f( 10 )
      

      We get:

      BUG! exception in phase 'class generation' in source unit 'ConsoleScript96' tried to get a variable with the name x as stack variable, but a variable with this name was not created
      	at org.codehaus.groovy.classgen.asm.CompileStack.getVariable(CompileStack.java:280)
      	at org.codehaus.groovy.classgen.asm.ClosureWriter.loadReference(ClosureWriter.java:131)
      	at org.codehaus.groovy.classgen.asm.ClosureWriter.writeClosure(ClosureWriter.java:102)
      	at org.codehaus.groovy.classgen.AsmClassGenerator.visitClosureExpression(AsmClassGenerator.java:546)
      	at org.codehaus.groovy.ast.expr.ClosureExpression.visit(ClosureExpression.java:43)
      	at org.codehaus.groovy.classgen.AsmClassGenerator.visitCastExpression(AsmClassGenerator.java:616)
      	at org.codehaus.groovy.ast.expr.CastExpression.visit(CastExpression.java:66)
      	at org.codehaus.groovy.classgen.asm.InvocationWriter.loadArguments(InvocationWriter.java:183)
      	at org.codehaus.groovy.classgen.asm.InvocationWriter.writeDirectMethodCall(InvocationWriter.java:129)
      	at org.codehaus.groovy.classgen.asm.InvocationWriter.makeCall(InvocationWriter.java:221)
      	at org.codehaus.groovy.classgen.asm.InvocationWriter.makeCall(InvocationWriter.java:76)
      	at org.codehaus.groovy.classgen.asm.InvocationWriter.makeInvokeMethodCall(InvocationWriter.java:60)
      	at org.codehaus.groovy.classgen.asm.InvocationWriter.writeInvokeMethod(InvocationWriter.java:334)
      	at org.codehaus.groovy.classgen.AsmClassGenerator.visitMethodCallExpression(AsmClassGenerator.java:648)
      	at org.codehaus.groovy.ast.expr.MethodCallExpression.visit(MethodCallExpression.java:67)
      	at org.codehaus.groovy.classgen.asm.StatementWriter.writeReturn(StatementWriter.java:582)
      	at org.codehaus.groovy.classgen.asm.OptimizingStatementWriter.writeReturn(OptimizingStatementWriter.java:316)
      	at org.codehaus.groovy.classgen.AsmClassGenerator.visitReturnStatement(AsmClassGenerator.java:505)
      	at org.codehaus.groovy.ast.stmt.ReturnStatement.visit(ReturnStatement.java:47)
      	at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitClassCodeContainer(ClassCodeVisitorSupport.java:101)
      	at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitConstructorOrMethod(ClassCodeVisitorSupport.java:112)
      	at org.codehaus.groovy.classgen.AsmClassGenerator.visitStdMethod(AsmClassGenerator.java:319)
      	at org.codehaus.groovy.classgen.AsmClassGenerator.visitConstructorOrMethod(AsmClassGenerator.java:276)
      	at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitMethod(ClassCodeVisitorSupport.java:123)
      	at org.codehaus.groovy.classgen.AsmClassGenerator.visitMethod(AsmClassGenerator.java:396)
      	at org.codehaus.groovy.ast.ClassNode.visitContents(ClassNode.java:1056)
      	at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitClass(ClassCodeVisitorSupport.java:50)
      	at org.codehaus.groovy.classgen.AsmClassGenerator.visitClass(AsmClassGenerator.java:180)
      	at org.codehaus.groovy.control.CompilationUnit$14.call(CompilationUnit.java:783)
      	at org.codehaus.groovy.control.CompilationUnit.applyToPrimaryClassNodes(CompilationUnit.java:1024)
      	at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:562)
      	at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:540)
      	at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:517)
      	at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:283)
      	at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:264)
      	at groovy.lang.GroovyShell.parseClass(GroovyShell.java:613)
      	at groovy.lang.GroovyShell.run(GroovyShell.java:480)
      	at groovy.lang.GroovyShell.run(GroovyShell.java:163)
      	at groovy.lang.GroovyShell$run.call(Unknown Source)
      	at groovy.ui.Console$_runScriptImpl_closure17.doCall(Console.groovy:951)
      	at groovy.ui.Console$_runScriptImpl_closure17.doCall(Console.groovy)
      	at sun.reflect.GeneratedMethodAccessor218.invoke(Unknown Source)
      	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      	at java.lang.reflect.Method.invoke(Method.java:597)
      	at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:90)
      	at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)
      	at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:272)
      	at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:904)
      	at groovy.lang.Closure.call(Closure.java:410)
      	at groovy.lang.Closure.call(Closure.java:404)
      	at groovy.lang.Closure.run(Closure.java:488)
      	at java.lang.Thread.run(Thread.java:680)
      

      Attachments

        Activity

          People

            blackdrag Jochen Theodorou
            tim_yates Tim Yates
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: