From 4878c7967a962cff1febc1112ab65423c6b47997 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Steven=20Sch=C3=A4fer?= Date: Wed, 16 Oct 2019 19:23:49 +0200 Subject: [PATCH] JVM: Handle inline class equality in when statement with declaration #KT-34268 Fixed --- .../kotlin/codegen/ExpressionCodegen.java | 16 +++++++++------- .../codegen/box/inlineClasses/kt34268.kt | 9 +++++++++ .../codegen/BlackBoxCodegenTestGenerated.java | 5 +++++ .../codegen/LightAnalysisModeTestGenerated.java | 5 +++++ .../ir/IrBlackBoxCodegenTestGenerated.java | 5 +++++ .../semantics/IrJsCodegenBoxTestGenerated.java | 5 +++++ .../semantics/JsCodegenBoxTestGenerated.java | 5 +++++ 7 files changed, 43 insertions(+), 7 deletions(-) create mode 100644 compiler/testData/codegen/box/inlineClasses/kt34268.kt diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java index d5ecf0a5959..6b007454e2d 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java @@ -3297,7 +3297,7 @@ public class ExpressionCodegen extends KtVisitor impleme else if (opToken == KtTokens.EQEQ || opToken == KtTokens.EXCLEQ || opToken == KtTokens.EQEQEQ || opToken == KtTokens.EXCLEQEQEQ) { return generateEquals( - expression.getLeft(), expression.getRight(), opToken, null, + expression.getLeft(), expression.getRight(), opToken, null, null, bindingContext.get(BindingContext.PRIMITIVE_NUMERIC_COMPARISON_INFO, expression) ); } @@ -3372,6 +3372,7 @@ public class ExpressionCodegen extends KtVisitor impleme @Nullable KtExpression right, @NotNull IElementType opToken, @Nullable StackValue pregeneratedSubject, + @Nullable KotlinType subjectKotlinType, @Nullable PrimitiveNumericComparisonInfo primitiveNumericComparisonInfo ) { if (left == null || right == null) { @@ -3382,7 +3383,7 @@ public class ExpressionCodegen extends KtVisitor impleme }); } - KotlinType leftKotlinType = kotlinType(left); + KotlinType leftKotlinType = (subjectKotlinType != null) ? subjectKotlinType : kotlinType(left); KotlinType rightKotlinType = kotlinType(right); boolean leftIsInlineClass = leftKotlinType != null && InlineClassesUtilsKt.isInlineClassType(leftKotlinType); boolean rightIsInlineClass = rightKotlinType != null && InlineClassesUtilsKt.isInlineClassType(rightKotlinType); @@ -4892,10 +4893,10 @@ The "returned" value of try expression with no finally is either the last expres return generateIsCheck(match, expression.getTypeReference(), expression.isNegated()); } - private StackValue generateExpressionMatch(StackValue expressionToMatch, KtExpression subjectExpression, KtExpression patternExpression) { + private StackValue generateExpressionMatch(StackValue expressionToMatch, KtExpression subjectExpression, KotlinType subjectKotlinType, KtExpression patternExpression) { if (expressionToMatch != null) { return generateEquals( - subjectExpression, patternExpression, KtTokens.EQEQ, expressionToMatch, + subjectExpression, patternExpression, KtTokens.EQEQ, expressionToMatch, subjectKotlinType, bindingContext.get(BindingContext.PRIMITIVE_NUMERIC_COMPARISON_INFO, patternExpression) ); } @@ -4996,6 +4997,7 @@ The "returned" value of try expression with no finally is either the last expres else { subjectLocal = -1; subjectType = Type.VOID_TYPE; + subjectKotlinType = null; } Label begin = new Label(); @@ -5015,7 +5017,7 @@ The "returned" value of try expression with no finally is either the last expres if (!whenEntry.isElse()) { KtWhenCondition[] conditions = whenEntry.getConditions(); for (int i = 0; i < conditions.length; i++) { - StackValue conditionValue = generateWhenCondition(subjectExpression, subjectType, subjectLocal, conditions[i]); + StackValue conditionValue = generateWhenCondition(subjectExpression, subjectType, subjectKotlinType, subjectLocal, conditions[i]); BranchedValue.Companion.condJump(conditionValue, nextCondition, true, v); if (i < conditions.length - 1) { v.goTo(thisEntry); @@ -5069,7 +5071,7 @@ The "returned" value of try expression with no finally is either the last expres } } - private StackValue generateWhenCondition(KtExpression subjectExpression, Type subjectType, int subjectLocal, KtWhenCondition condition) { + private StackValue generateWhenCondition(KtExpression subjectExpression, Type subjectType, KotlinType subjectKotlinType, int subjectLocal, KtWhenCondition condition) { if (condition instanceof KtWhenConditionInRange) { KtWhenConditionInRange conditionInRange = (KtWhenConditionInRange) condition; return generateIn(StackValue.local(subjectLocal, subjectType), @@ -5083,7 +5085,7 @@ The "returned" value of try expression with no finally is either the last expres } else if (condition instanceof KtWhenConditionWithExpression) { KtExpression patternExpression = ((KtWhenConditionWithExpression) condition).getExpression(); - return generateExpressionMatch(match, subjectExpression, patternExpression); + return generateExpressionMatch(match, subjectExpression, subjectKotlinType, patternExpression); } else { throw new UnsupportedOperationException("unsupported kind of when condition"); diff --git a/compiler/testData/codegen/box/inlineClasses/kt34268.kt b/compiler/testData/codegen/box/inlineClasses/kt34268.kt new file mode 100644 index 00000000000..4270f85d841 --- /dev/null +++ b/compiler/testData/codegen/box/inlineClasses/kt34268.kt @@ -0,0 +1,9 @@ +// WITH_RUNTIME +// KJS_WITH_FULL_RUNTIME + +fun box(): String { + return when(val foo = 42UL) { + 42UL -> "OK" + else -> "Fail" + } +} diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java index 2b715d3f5d8..b73db0c1a9c 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java @@ -12818,6 +12818,11 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { runTest("compiler/testData/codegen/box/inlineClasses/kt28920_javaPrimitiveType.kt"); } + @TestMetadata("kt34268.kt") + public void testKt34268() throws Exception { + runTest("compiler/testData/codegen/box/inlineClasses/kt34268.kt"); + } + @TestMetadata("mangledDefaultParameterFunction.kt") public void testMangledDefaultParameterFunction() throws Exception { runTest("compiler/testData/codegen/box/inlineClasses/mangledDefaultParameterFunction.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java index 7d16c654fdf..e10f87b16ba 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -12823,6 +12823,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes runTest("compiler/testData/codegen/box/inlineClasses/kt28920_javaPrimitiveType.kt"); } + @TestMetadata("kt34268.kt") + public void testKt34268() throws Exception { + runTest("compiler/testData/codegen/box/inlineClasses/kt34268.kt"); + } + @TestMetadata("mangledDefaultParameterFunction.kt") public void testMangledDefaultParameterFunction() throws Exception { runTest("compiler/testData/codegen/box/inlineClasses/mangledDefaultParameterFunction.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java index 12d34b755aa..fc25daf5764 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java @@ -11703,6 +11703,11 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes runTest("compiler/testData/codegen/box/inlineClasses/kt28920_javaPrimitiveType.kt"); } + @TestMetadata("kt34268.kt") + public void testKt34268() throws Exception { + runTest("compiler/testData/codegen/box/inlineClasses/kt34268.kt"); + } + @TestMetadata("mangledDefaultParameterFunction.kt") public void testMangledDefaultParameterFunction() throws Exception { runTest("compiler/testData/codegen/box/inlineClasses/mangledDefaultParameterFunction.kt"); diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java b/js/js.tests/test/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java index aaa4b627def..abddeb42d19 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java @@ -10098,6 +10098,11 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest { runTest("compiler/testData/codegen/box/inlineClasses/kt28585.kt"); } + @TestMetadata("kt34268.kt") + public void testKt34268() throws Exception { + runTest("compiler/testData/codegen/box/inlineClasses/kt34268.kt"); + } + @TestMetadata("mangledDefaultParameterFunction.kt") public void testMangledDefaultParameterFunction() throws Exception { runTest("compiler/testData/codegen/box/inlineClasses/mangledDefaultParameterFunction.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 506f6ca0362..2c1ae4b712e 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 @@ -11253,6 +11253,11 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest { runTest("compiler/testData/codegen/box/inlineClasses/kt28585.kt"); } + @TestMetadata("kt34268.kt") + public void testKt34268() throws Exception { + runTest("compiler/testData/codegen/box/inlineClasses/kt34268.kt"); + } + @TestMetadata("mangledDefaultParameterFunction.kt") public void testMangledDefaultParameterFunction() throws Exception { runTest("compiler/testData/codegen/box/inlineClasses/mangledDefaultParameterFunction.kt");