From 9d72036ba5397bad8f2cc4e9b6e38a338d4a7120 Mon Sep 17 00:00:00 2001 From: Michael Bogdanov Date: Sat, 11 Oct 2014 14:51:20 +0400 Subject: [PATCH] Fix for KT-5995: Labeled val causes an exception from the back-end #KT-5995 Fixed --- .../jetbrains/jet/codegen/ExpressionCodegen.java | 9 +++++++-- .../codegen/box/labels/labeledDeclarations.kt | 16 ++++++++++++++++ .../codegen/box/labels/labeledDeclarations2.kt | 16 ++++++++++++++++ .../generated/BlackBoxCodegenTestGenerated.java | 12 ++++++++++++ 4 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 compiler/testData/codegen/box/labels/labeledDeclarations.kt create mode 100644 compiler/testData/codegen/box/labels/labeledDeclarations2.kt diff --git a/compiler/backend/src/org/jetbrains/jet/codegen/ExpressionCodegen.java b/compiler/backend/src/org/jetbrains/jet/codegen/ExpressionCodegen.java index f36743d4cc4..847ec6302ff 100644 --- a/compiler/backend/src/org/jetbrains/jet/codegen/ExpressionCodegen.java +++ b/compiler/backend/src/org/jetbrains/jet/codegen/ExpressionCodegen.java @@ -1430,7 +1430,12 @@ public class ExpressionCodegen extends JetVisitor implem StackValue answer = StackValue.none(); for (Iterator iterator = statements.iterator(); iterator.hasNext(); ) { - JetElement statement = iterator.next(); + JetElement possiblyLabeledStatement = iterator.next(); + + JetElement statement = possiblyLabeledStatement instanceof JetExpression + ? JetPsiUtil.safeDeparenthesize((JetExpression) possiblyLabeledStatement, true) + : possiblyLabeledStatement; + if (statement instanceof JetNamedDeclaration) { JetNamedDeclaration declaration = (JetNamedDeclaration) statement; @@ -1459,7 +1464,7 @@ public class ExpressionCodegen extends JetVisitor implem v.mark(labelBeforeLastExpression); } - StackValue result = isExpression ? gen(statement) : genStatement(statement); + StackValue result = isExpression ? gen(possiblyLabeledStatement) : genStatement(possiblyLabeledStatement); if (!iterator.hasNext()) { answer = result; diff --git a/compiler/testData/codegen/box/labels/labeledDeclarations.kt b/compiler/testData/codegen/box/labels/labeledDeclarations.kt new file mode 100644 index 00000000000..ca0098fa8e6 --- /dev/null +++ b/compiler/testData/codegen/box/labels/labeledDeclarations.kt @@ -0,0 +1,16 @@ +data class A(val a: Int, val b: Int) + +fun box() : String +{ + @a val x = 1 + @b fun a() = 2 + @c val (z, z2) = A(1, 2) + + if (x != 1) return "fail 1" + + if (a() != 2) return "fail 2" + + if (z != 1 || z2 != 2) return "fail 3" + + return "OK" +} \ No newline at end of file diff --git a/compiler/testData/codegen/box/labels/labeledDeclarations2.kt b/compiler/testData/codegen/box/labels/labeledDeclarations2.kt new file mode 100644 index 00000000000..914c9db430d --- /dev/null +++ b/compiler/testData/codegen/box/labels/labeledDeclarations2.kt @@ -0,0 +1,16 @@ +data class A(val a: Int, val b: Int) + +fun box() : String +{ + (@a val x = 1) + (@b fun a() = 2) + (@c val (z, z2) = A(1, 2)) + + if (x != 1) return "fail 1" + + if (a() != 2) return "fail 2" + + if (z != 1 || z2 != 2) return "fail 3" + + return "OK" +} \ No newline at end of file diff --git a/compiler/tests/org/jetbrains/jet/codegen/generated/BlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/jet/codegen/generated/BlackBoxCodegenTestGenerated.java index 52ed9904903..8c35d924863 100644 --- a/compiler/tests/org/jetbrains/jet/codegen/generated/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/jet/codegen/generated/BlackBoxCodegenTestGenerated.java @@ -4075,6 +4075,18 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/labels"), Pattern.compile("^(.+)\\.kt$"), true); } + @TestMetadata("labeledDeclarations.kt") + public void testLabeledDeclarations() throws Exception { + String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/box/labels/labeledDeclarations.kt"); + doTest(fileName); + } + + @TestMetadata("labeledDeclarations2.kt") + public void testLabeledDeclarations2() throws Exception { + String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/box/labels/labeledDeclarations2.kt"); + doTest(fileName); + } + @TestMetadata("propertyAccessor.kt") public void testPropertyAccessor() throws Exception { String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/box/labels/propertyAccessor.kt");