From b7d7d1eb01ebc590c859548a0260e627daee36f6 Mon Sep 17 00:00:00 2001 From: Mikhail Zarechenskiy Date: Thu, 11 Oct 2018 19:14:06 +0300 Subject: [PATCH] Fix inline class type coercion in `==` with generic call #KT-27393 Fixed --- .../kotlin/codegen/ExpressionCodegen.java | 15 ++++++++-- .../equalsOperatorWithGenericCall.kt | 30 +++++++++++++++++++ .../codegen/BlackBoxCodegenTestGenerated.java | 5 ++++ .../LightAnalysisModeTestGenerated.java | 5 ++++ .../ir/IrBlackBoxCodegenTestGenerated.java | 5 ++++ .../IrJsCodegenBoxTestGenerated.java | 5 ++++ .../semantics/JsCodegenBoxTestGenerated.java | 5 ++++ 7 files changed, 67 insertions(+), 3 deletions(-) create mode 100644 compiler/testData/codegen/box/inlineClasses/equalsOperatorWithGenericCall.kt diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java index f4c37e5a911..88d9eea5f34 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java @@ -3254,7 +3254,16 @@ public class ExpressionCodegen extends KtVisitor impleme } private StackValue genLazyUnlessProvided(@Nullable StackValue pregenerated, @NotNull KtExpression expr, @NotNull Type type) { - return pregenerated != null ? StackValue.coercion(pregenerated, type, null) : genLazy(expr, type); + return genLazyUnlessProvided(pregenerated, expr, type, null); + } + + private StackValue genLazyUnlessProvided( + @Nullable StackValue pregenerated, + @NotNull KtExpression expr, + @NotNull Type type, + @Nullable KotlinType kotlinType + ) { + return pregenerated != null ? StackValue.coercion(pregenerated, type, kotlinType) : genLazy(expr, type, kotlinType); } private StackValue genUnlessProvided(@Nullable StackValue pregenerated, @NotNull KtExpression expr, @NotNull Type type) { @@ -3399,8 +3408,8 @@ public class ExpressionCodegen extends KtVisitor impleme KotlinType leftKotlinType = kotlinType(left); KotlinType rightKotlinType = kotlinType(right); - StackValue leftValue = genLazyUnlessProvided(pregeneratedSubject, left, leftType); - StackValue rightValue = genLazy(right, rightType); + StackValue leftValue = genLazyUnlessProvided(pregeneratedSubject, left, leftType, leftKotlinType); + StackValue rightValue = genLazy(right, rightType, rightKotlinType); return StackValue.operation(Type.BOOLEAN_TYPE, v -> { KotlinType nullableAnyType = state.getModule().getBuiltIns().getNullableAnyType(); diff --git a/compiler/testData/codegen/box/inlineClasses/equalsOperatorWithGenericCall.kt b/compiler/testData/codegen/box/inlineClasses/equalsOperatorWithGenericCall.kt new file mode 100644 index 00000000000..b096edde21a --- /dev/null +++ b/compiler/testData/codegen/box/inlineClasses/equalsOperatorWithGenericCall.kt @@ -0,0 +1,30 @@ +inline class IcAny(val x: Any?) +inline class IcInt(val x: Int) +inline class IcLong(val x: Long) + +fun id(x: T) = x + +fun box(): String { + if (IcInt(42) == id(IcInt(24))) return "Error 1" + if (IcInt(42) != id(IcInt(42))) return "Error 2" + if (id(IcInt(42)) == IcInt(24)) return "Error 3" + if (id(IcInt(42)) != IcInt(42)) return "Error 4" + if (id(IcInt(42)) == id(IcInt(24))) return "Error 5" + if (id(IcInt(42)) != id(IcInt(42))) return "Error 6" + + if (IcLong(42) == id(IcLong(24))) return "Error 2, 1" + if (IcLong(42) != id(IcLong(42))) return "Error 2, 2" + if (id(IcLong(42)) == IcLong(24)) return "Error 2, 3" + if (id(IcLong(42)) != IcLong(42)) return "Error 2, 4" + if (id(IcLong(42)) == id(IcLong(24))) return "Error 2, 5" + if (id(IcLong(42)) != id(IcLong(42))) return "Error 2, 6" + + if (IcAny(42) == id(IcAny(24))) return "Error 3, 1" + if (IcAny(42) != id(IcAny(42))) return "Error 3, 2" + if (id(IcAny(42)) == IcAny(24)) return "Error 3, 3" + if (id(IcAny(42)) != IcAny(42)) return "Error 3, 4" + if (id(IcAny(42)) == id(IcAny(24))) return "Error 3, 5" + if (id(IcAny(42)) != id(IcAny(42))) return "Error 3, 6" + + return "OK" +} diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java index 01c1ad7c251..0fa268b12d1 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java @@ -11674,6 +11674,11 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { runTest("compiler/testData/codegen/box/inlineClasses/equalityForBoxesOfNullableValuesOfInlineClass.kt"); } + @TestMetadata("equalsOperatorWithGenericCall.kt") + public void testEqualsOperatorWithGenericCall() throws Exception { + runTest("compiler/testData/codegen/box/inlineClasses/equalsOperatorWithGenericCall.kt"); + } + @TestMetadata("extLambdaInInlineClassFun.kt") public void testExtLambdaInInlineClassFun() throws Exception { runTest("compiler/testData/codegen/box/inlineClasses/extLambdaInInlineClassFun.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java index 5503bb6f09b..476f0cf4ae7 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -11674,6 +11674,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes runTest("compiler/testData/codegen/box/inlineClasses/equalityForBoxesOfNullableValuesOfInlineClass.kt"); } + @TestMetadata("equalsOperatorWithGenericCall.kt") + public void testEqualsOperatorWithGenericCall() throws Exception { + runTest("compiler/testData/codegen/box/inlineClasses/equalsOperatorWithGenericCall.kt"); + } + @TestMetadata("extLambdaInInlineClassFun.kt") public void testExtLambdaInInlineClassFun() throws Exception { runTest("compiler/testData/codegen/box/inlineClasses/extLambdaInInlineClassFun.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java index 16b0a071f8c..c7c11a1a652 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java @@ -11679,6 +11679,11 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes runTest("compiler/testData/codegen/box/inlineClasses/equalityForBoxesOfNullableValuesOfInlineClass.kt"); } + @TestMetadata("equalsOperatorWithGenericCall.kt") + public void testEqualsOperatorWithGenericCall() throws Exception { + runTest("compiler/testData/codegen/box/inlineClasses/equalsOperatorWithGenericCall.kt"); + } + @TestMetadata("extLambdaInInlineClassFun.kt") public void testExtLambdaInInlineClassFun() throws Exception { runTest("compiler/testData/codegen/box/inlineClasses/extLambdaInInlineClassFun.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 9fc4d7bb380..5ba68a17468 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 @@ -10214,6 +10214,11 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest { runTest("compiler/testData/codegen/box/inlineClasses/equalityForBoxesOfNullableValuesOfInlineClass.kt"); } + @TestMetadata("equalsOperatorWithGenericCall.kt") + public void testEqualsOperatorWithGenericCall() throws Exception { + runTest("compiler/testData/codegen/box/inlineClasses/equalsOperatorWithGenericCall.kt"); + } + @TestMetadata("extLambdaInInlineClassFun.kt") public void testExtLambdaInInlineClassFun() throws Exception { runTest("compiler/testData/codegen/box/inlineClasses/extLambdaInInlineClassFun.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 62f31402bce..c00a25dea7b 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 @@ -11259,6 +11259,11 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest { runTest("compiler/testData/codegen/box/inlineClasses/equalityForBoxesOfNullableValuesOfInlineClass.kt"); } + @TestMetadata("equalsOperatorWithGenericCall.kt") + public void testEqualsOperatorWithGenericCall() throws Exception { + runTest("compiler/testData/codegen/box/inlineClasses/equalsOperatorWithGenericCall.kt"); + } + @TestMetadata("extLambdaInInlineClassFun.kt") public void testExtLambdaInInlineClassFun() throws Exception { runTest("compiler/testData/codegen/box/inlineClasses/extLambdaInInlineClassFun.kt");