From 8d24ca65a35a24b0865f479c334e0137aa3d762e Mon Sep 17 00:00:00 2001 From: Mikhail Zarechenskiy Date: Tue, 10 Jul 2018 15:44:43 +0300 Subject: [PATCH] Propagate `KotlinType` into `when` expression codegen This commit removes unneeded boxing when result expression of `when` is value of inline class type --- .../kotlin/codegen/ExpressionCodegen.java | 5 +++-- .../inlineClasses/castInsideWhenExpression.kt | 18 ++++++++++++++++++ .../ir/IrBlackBoxCodegenTestGenerated.java | 5 +++++ .../codegen/BlackBoxCodegenTestGenerated.java | 5 +++++ .../LightAnalysisModeTestGenerated.java | 5 +++++ .../semantics/IrJsCodegenBoxTestGenerated.java | 5 +++++ .../semantics/JsCodegenBoxTestGenerated.java | 5 +++++ 7 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 compiler/testData/codegen/box/inlineClasses/castInsideWhenExpression.kt diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java index 18d8c25dba0..017f4ac7eb4 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java @@ -4573,8 +4573,9 @@ The "returned" value of try expression with no finally is either the last expres public StackValue generateWhenExpression(KtWhenExpression expression, boolean isStatement) { Type resultType = isStatement ? Type.VOID_TYPE : expressionType(expression); + KotlinType resultKotlinType = isStatement ? null : kotlinType(expression); - return StackValue.operation(resultType, v -> { + return StackValue.operation(resultType, resultKotlinType, v -> { KtProperty subjectVariable = expression.getSubjectVariable(); KtExpression subjectExpression = expression.getSubjectExpression(); @@ -4637,7 +4638,7 @@ The "returned" value of try expression with no finally is either the last expres } v.visitLabel(thisEntry); - gen(whenEntry.getExpression(), resultType); + gen(whenEntry.getExpression(), resultType, resultKotlinType); mark.dropTo(); if (!whenEntry.isElse()) { v.goTo(end); diff --git a/compiler/testData/codegen/box/inlineClasses/castInsideWhenExpression.kt b/compiler/testData/codegen/box/inlineClasses/castInsideWhenExpression.kt new file mode 100644 index 00000000000..95700b20a13 --- /dev/null +++ b/compiler/testData/codegen/box/inlineClasses/castInsideWhenExpression.kt @@ -0,0 +1,18 @@ +// !LANGUAGE: +InlineClasses + +inline class Foo(val x: Any) { + fun bar() {} +} + +fun transform(f: Foo): Foo { + return when { + true -> f as Foo + else -> TODO() + } +} + +fun box(): String { + val f = Foo(42) + val t = transform(f) + return if (t.x !is Number) "Fail" else "OK" +} diff --git a/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java b/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java index 5056ffd3562..72f1032e887 100644 --- a/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java +++ b/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java @@ -11021,6 +11021,11 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes runTest("compiler/testData/codegen/box/inlineClasses/callableReferencesWithInlineClasses.kt"); } + @TestMetadata("castInsideWhenExpression.kt") + public void testCastInsideWhenExpression() throws Exception { + runTest("compiler/testData/codegen/box/inlineClasses/castInsideWhenExpression.kt"); + } + @TestMetadata("checkBoxUnboxOfArgumentsOnInlinedFunctions.kt") public void testCheckBoxUnboxOfArgumentsOnInlinedFunctions() throws Exception { runTest("compiler/testData/codegen/box/inlineClasses/checkBoxUnboxOfArgumentsOnInlinedFunctions.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java index 948c59f4610..90765ded41e 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java @@ -11021,6 +11021,11 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { runTest("compiler/testData/codegen/box/inlineClasses/callableReferencesWithInlineClasses.kt"); } + @TestMetadata("castInsideWhenExpression.kt") + public void testCastInsideWhenExpression() throws Exception { + runTest("compiler/testData/codegen/box/inlineClasses/castInsideWhenExpression.kt"); + } + @TestMetadata("checkBoxUnboxOfArgumentsOnInlinedFunctions.kt") public void testCheckBoxUnboxOfArgumentsOnInlinedFunctions() throws Exception { runTest("compiler/testData/codegen/box/inlineClasses/checkBoxUnboxOfArgumentsOnInlinedFunctions.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java index 68ed208c787..ac287dfaa44 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -11021,6 +11021,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes runTest("compiler/testData/codegen/box/inlineClasses/callableReferencesWithInlineClasses.kt"); } + @TestMetadata("castInsideWhenExpression.kt") + public void testCastInsideWhenExpression() throws Exception { + runTest("compiler/testData/codegen/box/inlineClasses/castInsideWhenExpression.kt"); + } + @TestMetadata("checkBoxUnboxOfArgumentsOnInlinedFunctions.kt") public void testCheckBoxUnboxOfArgumentsOnInlinedFunctions() throws Exception { runTest("compiler/testData/codegen/box/inlineClasses/checkBoxUnboxOfArgumentsOnInlinedFunctions.kt"); diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/IrJsCodegenBoxTestGenerated.java b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/IrJsCodegenBoxTestGenerated.java index 8407768fde5..ce5c9613fa9 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/IrJsCodegenBoxTestGenerated.java +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/IrJsCodegenBoxTestGenerated.java @@ -9671,6 +9671,11 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest { runTest("compiler/testData/codegen/box/inlineClasses/callableReferencesWithInlineClasses.kt"); } + @TestMetadata("castInsideWhenExpression.kt") + public void testCastInsideWhenExpression() throws Exception { + runTest("compiler/testData/codegen/box/inlineClasses/castInsideWhenExpression.kt"); + } + @TestMetadata("checkBoxUnboxOfArgumentsOnInlinedFunctions.kt") public void testCheckBoxUnboxOfArgumentsOnInlinedFunctions() throws Exception { runTest("compiler/testData/codegen/box/inlineClasses/checkBoxUnboxOfArgumentsOnInlinedFunctions.kt"); diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java index ba89c86c66d..f03b98f3cc9 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java @@ -9671,6 +9671,11 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest { runTest("compiler/testData/codegen/box/inlineClasses/callableReferencesWithInlineClasses.kt"); } + @TestMetadata("castInsideWhenExpression.kt") + public void testCastInsideWhenExpression() throws Exception { + runTest("compiler/testData/codegen/box/inlineClasses/castInsideWhenExpression.kt"); + } + @TestMetadata("checkBoxUnboxOfArgumentsOnInlinedFunctions.kt") public void testCheckBoxUnboxOfArgumentsOnInlinedFunctions() throws Exception { runTest("compiler/testData/codegen/box/inlineClasses/checkBoxUnboxOfArgumentsOnInlinedFunctions.kt");