Kotlin Compiler Crashes with data class in function referencing variable


#1

Pretty straightforward. The following crashes the compiler:

class Fail() {
	fun generate() {
		val value = "x"
		data class InsideDataClass(
			val other: String = value
		)
	}
}

with an error like this

➜  /tmp kotlinc test.kt
test.kt:4:33: exception: org.jetbrains.kotlin.codegen.CompilationException: Back-end (JVM) Internal error: wrong code generated
 for default methodorg.jetbrains.kotlin.codegen.CompilationException Back-end (JVM) Internal error: Couldn't transform method node: <init> (Ljava/lang/String;ILkotlin/jvm/internal/DefaultConstructorMarker;)V:
    ILOAD 2
    ICONST_1
    IAND
    IFEQ L0
   L1
    LINENUMBER 5 L1
    ALOAD 0
    GETFIELD Fail$generate$InsideDataClass.$value : Ljava/lang/String;
    ASTORE 1
   L0
    ALOAD 0
    ALOAD 1
    INVOKESPECIAL Fail$generate$InsideDataClass.<init> (Ljava/lang/String;Ljava/lang/String;)V
    RETURN
    MAXSTACK = 2
    MAXLOCALS = 4

Cause: AFTER mandatory stack transformations: incorrect bytecode
Element is unknownThe root cause was thrown at: MethodVerifier.kt:29
bytecode:
    ILOAD 2
    ICONST_1
    IAND
    IFEQ L0
   L1
    LINENUMBER 5 L1
    ALOAD 0
    GETFIELD Fail$generate$InsideDataClass.$value : Ljava/lang/String;
    ASTORE 1
   L0
    ALOAD 0
    ALOAD 1
    INVOKESPECIAL Fail$generate$InsideDataClass.<init> (Ljava/lang/String;Ljava/lang/String;)V
    RETURN
    MAXSTACK = 2
    MAXLOCALS = 4

Cause: Back-end (JVM) Internal error: Couldn't transform method node: <init> (Ljava/lang/String;ILkotlin/jvm/internal/DefaultConstructorMarker;)V:
    ILOAD 2
    ICONST_1
    IAND
    IFEQ L0
   L1
    LINENUMBER 5 L1
    ALOAD 0
    GETFIELD Fail$generate$InsideDataClass.$value : Ljava/lang/String;
    ASTORE 1
   L0
    ALOAD 0
    ALOAD 1
    INVOKESPECIAL Fail$generate$InsideDataClass.<init> (Ljava/lang/String;Ljava/lang/String;)V
    RETURN
    MAXSTACK = 2
    MAXLOCALS = 4

Cause: AFTER mandatory stack transformations: incorrect bytecode
Element is unknownThe root cause was thrown at: MethodVerifier.kt:29
File being compiled and position: (4,33) in /private/tmp/test.kt
PsiElement: (
    			val other: String = value
    		)
The root cause was thrown at: TransformationMethodVisitor.java:88
	at org.jetbrains.kotlin.codegen.FunctionCodegen.endVisit(FunctionCodegen.java:832)
	at org.jetbrains.kotlin.codegen.FunctionCodegen.generateDefaultIfNeeded(FunctionCodegen.java:1021)
	at org.jetbrains.kotlin.codegen.ImplementationBodyCodegen.generatePrimaryConstructor(ImplementationBodyCodegen.java:934)
	at org.jetbrains.kotlin.codegen.ImplementationBodyCodegen.generateConstructors(ImplementationBodyCodegen.java:392)
	at org.jetbrains.kotlin.codegen.ClassBodyCodegen.generateBody(ClassBodyCodegen.java:92)
	at org.jetbrains.kotlin.codegen.MemberCodegen.generate(MemberCodegen.java:145)
	at org.jetbrains.kotlin.codegen.ExpressionCodegen.visitClassOrObject(ExpressionCodegen.java:390)
	at org.jetbrains.kotlin.codegen.ExpressionCodegen.visitClass(ExpressionCodegen.java:374)
	at org.jetbrains.kotlin.codegen.ExpressionCodegen.visitClass(ExpressionCodegen.java:122)
	at org.jetbrains.kotlin.psi.KtClass.accept(KtClass.kt:34)
	at org.jetbrains.kotlin.codegen.CodegenStatementVisitor.visitKtElement(CodegenStatementVisitor.java:31)
	at org.jetbrains.kotlin.codegen.CodegenStatementVisitor.visitKtElement(CodegenStatementVisitor.java:22)
	at org.jetbrains.kotlin.psi.KtVisitor.visitExpression(KtVisitor.java:182)
	at org.jetbrains.kotlin.psi.KtVisitor.visitDeclaration(KtVisitor.java:29)
	at org.jetbrains.kotlin.psi.KtVisitor.visitNamedDeclaration(KtVisitor.java:398)
	at org.jetbrains.kotlin.psi.KtVisitor.visitClassOrObject(KtVisitor.java:41)
	at org.jetbrains.kotlin.psi.KtVisitor.visitClass(KtVisitor.java:33)
	at org.jetbrains.kotlin.psi.KtClass.accept(KtClass.kt:34)
	at org.jetbrains.kotlin.codegen.ExpressionCodegen.genQualified(ExpressionCodegen.java:309)
	at org.jetbrains.kotlin.codegen.ExpressionCodegen.genStatement(ExpressionCodegen.java:369)
	at org.jetbrains.kotlin.codegen.ExpressionCodegen.generateBlock(ExpressionCodegen.java:1230)
	at org.jetbrains.kotlin.codegen.ExpressionCodegen.generateBlock(ExpressionCodegen.java:1180)
	at org.jetbrains.kotlin.codegen.CodegenStatementVisitor.visitBlockExpression(CodegenStatementVisitor.java:56)
	at org.jetbrains.kotlin.codegen.CodegenStatementVisitor.visitBlockExpression(CodegenStatementVisitor.java:22)
	at org.jetbrains.kotlin.psi.KtBlockExpression.accept(KtBlockExpression.java:44)
	at org.jetbrains.kotlin.codegen.ExpressionCodegen.genQualified(ExpressionCodegen.java:309)
	at org.jetbrains.kotlin.codegen.ExpressionCodegen.genStatement(ExpressionCodegen.java:369)
	at org.jetbrains.kotlin.codegen.ExpressionCodegen.gen(ExpressionCodegen.java:335)
	at org.jetbrains.kotlin.codegen.ExpressionCodegen.returnExpression(ExpressionCodegen.java:1627)
	at org.jetbrains.kotlin.codegen.FunctionGenerationStrategy$FunctionDefault.doGenerateBody(FunctionGenerationStrategy.java:52)
	at org.jetbrains.kotlin.codegen.FunctionGenerationStrategy$CodegenBased.generateBody(FunctionGenerationStrategy.java:72)
	at org.jetbrains.kotlin.codegen.FunctionCodegen.generateMethodBody(FunctionCodegen.java:569)
	at org.jetbrains.kotlin.codegen.FunctionCodegen.generateMethodBody(FunctionCodegen.java:332)
	at org.jetbrains.kotlin.codegen.FunctionCodegen.generateMethod(FunctionCodegen.java:298)
	at org.jetbrains.kotlin.codegen.FunctionCodegen.generateMethod(FunctionCodegen.java:176)
	at org.jetbrains.kotlin.codegen.FunctionCodegen.gen(FunctionCodegen.java:147)
	at org.jetbrains.kotlin.codegen.MemberCodegen.genSimpleMember(MemberCodegen.java:213)
	at org.jetbrains.kotlin.codegen.ClassBodyCodegen.generateDeclaration(ClassBodyCodegen.java:168)
	at org.jetbrains.kotlin.codegen.ClassBodyCodegen.generateBody(ClassBodyCodegen.java:84)
	at org.jetbrains.kotlin.codegen.MemberCodegen.generate(MemberCodegen.java:145)
	at org.jetbrains.kotlin.codegen.MemberCodegen.genClassOrObject(MemberCodegen.java:319)
	at org.jetbrains.kotlin.codegen.MemberCodegen.genClassOrObject(MemberCodegen.java:303)
	at org.jetbrains.kotlin.codegen.PackageCodegenImpl.generateClassOrObject(PackageCodegenImpl.java:162)
	at org.jetbrains.kotlin.codegen.PackageCodegenImpl.generateClassesAndObjectsInFile(PackageCodegenImpl.java:86)
	at org.jetbrains.kotlin.codegen.PackageCodegenImpl.generateFile(PackageCodegenImpl.java:124)
	at org.jetbrains.kotlin.codegen.PackageCodegenImpl.generate(PackageCodegenImpl.java:66)
	at org.jetbrains.kotlin.codegen.KotlinCodegenFacade.generatePackage(KotlinCodegenFacade.java:99)
	at org.jetbrains.kotlin.codegen.KotlinCodegenFacade.doGenerateFiles(KotlinCodegenFacade.java:77)
	at org.jetbrains.kotlin.codegen.KotlinCodegenFacade.compileCorrectFiles(KotlinCodegenFacade.java:44)
	at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.generate(KotlinToJVMBytecodeCompiler.kt:442)
	at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.analyzeAndGenerate(KotlinToJVMBytecodeCompiler.kt:359)
	at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.compileBunchOfSources(KotlinToJVMBytecodeCompiler.kt:248)
	at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:203)
	at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:58)
	at org.jetbrains.kotlin.cli.common.CLICompiler.execImpl(CLICompiler.java:103)
	at org.jetbrains.kotlin.cli.common.CLICompiler.execImpl(CLICompiler.java:51)
	at org.jetbrains.kotlin.cli.common.CLITool.exec(CLITool.kt:92)
	at org.jetbrains.kotlin.cli.common.CLITool.exec(CLITool.kt:70)
	at org.jetbrains.kotlin.cli.common.CLITool.exec(CLITool.kt:36)
	at org.jetbrains.kotlin.cli.common.CLITool$Companion.doMainNoExit(CLITool.kt:157)
	at org.jetbrains.kotlin.cli.common.CLITool$Companion.doMain(CLITool.kt:148)
	at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler$Companion.main(K2JVMCompiler.kt:316)
	at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.main(K2JVMCompiler.kt)
	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:497)
	at org.jetbrains.kotlin.preloading.Preloader.run(Preloader.java:81)
	at org.jetbrains.kotlin.preloading.Preloader.main(Preloader.java:43)
Caused by: org.jetbrains.kotlin.codegen.CompilationException: Back-end (JVM) Internal error: Couldn't transform method node: <init> (Ljava/lang/String;ILkotlin/jvm/internal/DefaultConstructorMarker;)V:
    ILOAD 2
    ICONST_1
    IAND
    IFEQ L0
   L1
    LINENUMBER 5 L1
    ALOAD 0
    GETFIELD Fail$generate$InsideDataClass.$value : Ljava/lang/String;
    ASTORE 1
   L0
    ALOAD 0
    ALOAD 1
    INVOKESPECIAL Fail$generate$InsideDataClass.<init> (Ljava/lang/String;Ljava/lang/String;)V
    RETURN
    MAXSTACK = 2
    MAXLOCALS = 4

Cause: AFTER mandatory stack transformations: incorrect bytecode
Element is unknownThe root cause was thrown at: MethodVerifier.kt:29
	at org.jetbrains.kotlin.codegen.TransformationMethodVisitor.visitEnd(TransformationMethodVisitor.java:88)
	at org.jetbrains.kotlin.codegen.FunctionCodegen.endVisit(FunctionCodegen.java:820)
	... 68 more
Caused by: java.lang.AssertionError: AFTER mandatory stack transformations: incorrect bytecode
	at org.jetbrains.kotlin.codegen.optimization.MethodVerifier.transform(MethodVerifier.kt:29)
	at org.jetbrains.kotlin.codegen.optimization.transformer.CompositeMethodTransformer.transform(CompositeMethodTransformer.kt:23)
	at org.jetbrains.kotlin.codegen.optimization.OptimizationMethodVisitor.performTransformations(OptimizationMethodVisitor.kt:40)
	at org.jetbrains.kotlin.codegen.TransformationMethodVisitor.visitEnd(TransformationMethodVisitor.java:65)
	... 69 more
Caused by: java.lang.RuntimeException: org.jetbrains.org.objectweb.asm.tree.analysis.AnalyzerException: Error at instruction 12: Cannot pop operand off an empty stack.
	at org.jetbrains.kotlin.codegen.optimization.transformer.MethodTransformer.runAnalyzer(MethodTransformer.java:34)
	at org.jetbrains.kotlin.codegen.optimization.transformer.MethodTransformer.analyze(MethodTransformer.java:44)
	at org.jetbrains.kotlin.codegen.optimization.MethodVerifier.transform(MethodVerifier.kt:26)
	... 72 more
Caused by: org.jetbrains.org.objectweb.asm.tree.analysis.AnalyzerException: Error at instruction 12: Cannot pop operand off an empty stack.
	at org.jetbrains.org.objectweb.asm.tree.analysis.Analyzer.analyze(Analyzer.java:298)
	at org.jetbrains.kotlin.codegen.optimization.transformer.MethodTransformer.runAnalyzer(MethodTransformer.java:31)
	... 74 more
Caused by: java.lang.IndexOutOfBoundsException: Cannot pop operand off an empty stack.
	at org.jetbrains.org.objectweb.asm.tree.analysis.Frame.pop(Frame.java:221)
	at org.jetbrains.org.objectweb.asm.tree.analysis.Frame.execute(Frame.java:615)
	at org.jetbrains.org.objectweb.asm.tree.analysis.Analyzer.analyze(Analyzer.java:200)
	... 75 more

    		data class InsideDataClass(
                                ^

#2

You should report it on http://kotl.in/issue