diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/range/comparison/ComparisonGenerator.kt b/compiler/backend/src/org/jetbrains/kotlin/codegen/range/comparison/ComparisonGenerator.kt index a9c8c1bb045..4e6c05422da 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/range/comparison/ComparisonGenerator.kt +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/range/comparison/ComparisonGenerator.kt @@ -35,7 +35,8 @@ interface ComparisonGenerator { fun getComparisonGeneratorForPrimitiveType(type: Type): ComparisonGenerator = when { - type.isRepresentedAsPrimitiveInt() -> IntComparisonGenerator + type == Type.CHAR_TYPE -> CharComparisonGenerator + type.isPrimitiveIntOrCoercible() -> IntComparisonGenerator type == Type.LONG_TYPE -> LongComparisonGenerator type == Type.FLOAT_TYPE -> FloatComparisonGenerator type == Type.DOUBLE_TYPE -> DoubleComparisonGenerator @@ -61,11 +62,11 @@ fun getComparisonGeneratorForRangeContainsCall( asmElementType == asmValueParameterType -> getComparisonGeneratorForPrimitiveType(asmElementType) - asmElementType.isRepresentedAsPrimitiveInt() && asmValueParameterType.isRepresentedAsPrimitiveInt() -> + asmElementType.isPrimitiveIntOrCoercible() && asmValueParameterType.isPrimitiveIntOrCoercible() -> IntComparisonGenerator - asmElementType.isRepresentedAsPrimitiveInt() && asmValueParameterType == Type.LONG_TYPE || - asmValueParameterType.isRepresentedAsPrimitiveInt() && asmElementType == Type.LONG_TYPE -> + asmElementType.isPrimitiveIntOrCoercible() && asmValueParameterType == Type.LONG_TYPE || + asmValueParameterType.isPrimitiveIntOrCoercible() && asmElementType == Type.LONG_TYPE -> LongComparisonGenerator asmElementType == Type.FLOAT_TYPE && asmValueParameterType == Type.DOUBLE_TYPE || @@ -76,5 +77,5 @@ fun getComparisonGeneratorForRangeContainsCall( } } -private fun Type.isRepresentedAsPrimitiveInt() = - this == Type.INT_TYPE || this == Type.SHORT_TYPE || this == Type.BYTE_TYPE || this == Type.CHAR_TYPE \ No newline at end of file +private fun Type.isPrimitiveIntOrCoercible() = + this == Type.INT_TYPE || this == Type.SHORT_TYPE || this == Type.BYTE_TYPE \ No newline at end of file diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/range/comparison/IntComparisonGenerator.kt b/compiler/backend/src/org/jetbrains/kotlin/codegen/range/comparison/IntComparisonGenerator.kt index 6c86a98829c..6926efff022 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/range/comparison/IntComparisonGenerator.kt +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/range/comparison/IntComparisonGenerator.kt @@ -20,9 +20,7 @@ import org.jetbrains.org.objectweb.asm.Label import org.jetbrains.org.objectweb.asm.Type import org.jetbrains.org.objectweb.asm.commons.InstructionAdapter -object IntComparisonGenerator : ComparisonGenerator { - override val comparedType: Type = Type.INT_TYPE - +abstract class IntegerComparisonGenerator(override val comparedType: Type) : ComparisonGenerator { override fun jumpIfGreaterOrEqual(v: InstructionAdapter, label: Label) { v.ificmpge(label) } @@ -38,4 +36,8 @@ object IntComparisonGenerator : ComparisonGenerator { override fun jumpIfLess(v: InstructionAdapter, label: Label) { v.ificmplt(label) } -} \ No newline at end of file +} + +object IntComparisonGenerator : IntegerComparisonGenerator(Type.INT_TYPE) + +object CharComparisonGenerator : IntegerComparisonGenerator(Type.CHAR_TYPE) \ No newline at end of file diff --git a/compiler/testData/codegen/box/ranges/contains/genericCharInRangeLiteral.kt b/compiler/testData/codegen/box/ranges/contains/genericCharInRangeLiteral.kt new file mode 100644 index 00000000000..810ed639224 --- /dev/null +++ b/compiler/testData/codegen/box/ranges/contains/genericCharInRangeLiteral.kt @@ -0,0 +1,7 @@ +class Cell(val value: T) + +fun box(): String = + if (Cell('a').value in 'a'..'z') + "OK" + else + "fail" \ No newline at end of file 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 a0e2c8ab364..1437322c6e3 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 @@ -15398,6 +15398,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes doTest(fileName); } + @TestMetadata("genericCharInRangeLiteral.kt") + public void testGenericCharInRangeLiteral() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/ranges/contains/genericCharInRangeLiteral.kt"); + doTest(fileName); + } + @TestMetadata("inArray.kt") public void testInArray() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/ranges/contains/inArray.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java index a88743ee5e3..5c1a211c605 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java @@ -15398,6 +15398,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { doTest(fileName); } + @TestMetadata("genericCharInRangeLiteral.kt") + public void testGenericCharInRangeLiteral() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/ranges/contains/genericCharInRangeLiteral.kt"); + doTest(fileName); + } + @TestMetadata("inArray.kt") public void testInArray() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/ranges/contains/inArray.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java index 1a9e093951f..430d74ef195 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -15398,6 +15398,12 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes doTest(fileName); } + @TestMetadata("genericCharInRangeLiteral.kt") + public void testGenericCharInRangeLiteral() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/ranges/contains/genericCharInRangeLiteral.kt"); + doTest(fileName); + } + @TestMetadata("inArray.kt") public void testInArray() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/ranges/contains/inArray.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 a45e8b78df2..60e24e8ba64 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 @@ -16658,6 +16658,12 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest { doTest(fileName); } + @TestMetadata("genericCharInRangeLiteral.kt") + public void testGenericCharInRangeLiteral() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/ranges/contains/genericCharInRangeLiteral.kt"); + doTest(fileName); + } + @TestMetadata("inArray.kt") public void testInArray() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/ranges/contains/inArray.kt");