From e788ef408fe602cbc4e671280e57adfabc6d8e8c Mon Sep 17 00:00:00 2001 From: Vsevolod Date: Tue, 11 Oct 2016 21:14:57 +0300 Subject: [PATCH] KT-5044 code cleanup, test added --- .../kotlin/codegen/ExpressionCodegen.java | 6 ++--- .../kotlin/codegen/RangeCodegenUtil.java | 18 ++++++------- .../ranges/contains/inRangeWithSmartCast.kt | 26 +++++++++++++++++++ .../codegen/BlackBoxCodegenTestGenerated.java | 6 +++++ 4 files changed, 44 insertions(+), 12 deletions(-) create mode 100644 compiler/testData/codegen/box/ranges/contains/inRangeWithSmartCast.kt diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java index 43a727d0705..51ed12b8c70 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java @@ -3470,13 +3470,13 @@ public class ExpressionCodegen extends KtVisitor impleme * Translates x in a..b to a <= x && x <= b * and x !in a..b to a > x || x > b for any primitive type */ - private void generateInPrimitiveRange(StackValue leftValue, KtBinaryExpression rangeExpression, boolean isInverted) { - Type rangeType = leftValue.type; + private void generateInPrimitiveRange(StackValue argument, KtBinaryExpression rangeExpression, boolean isInverted) { + Type rangeType = argument.type; int localVarIndex = myFrameMap.enterTemp(rangeType); // Load left bound gen(rangeExpression.getLeft(), rangeType); // Load x into local variable to avoid StackValue#put side-effects - leftValue.put(rangeType, v); + argument.put(rangeType, v); v.store(localVarIndex, rangeType); v.load(localVarIndex, rangeType); diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/RangeCodegenUtil.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/RangeCodegenUtil.java index ca6052e29dd..7842f7ec4f5 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/RangeCodegenUtil.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/RangeCodegenUtil.java @@ -217,15 +217,6 @@ public class RangeCodegenUtil { return false; } - private static boolean isBuiltInRangeTo(@NotNull CallableDescriptor descriptor) { - if (!isTopLevelInPackage(descriptor, "rangeTo", "kotlin.ranges")) { - return false; - } - - ReceiverParameterDescriptor extensionReceiver = descriptor.getExtensionReceiverParameter(); - return extensionReceiver != null; - } - /* * Checks whether for expression 'x in a..b' a..b is primitive integral range * with same type as x. @@ -256,6 +247,15 @@ public class RangeCodegenUtil { return false; } + private static boolean isBuiltInRangeTo(@NotNull CallableDescriptor descriptor) { + if (!isTopLevelInPackage(descriptor, "rangeTo", "kotlin.ranges")) { + return false; + } + + ReceiverParameterDescriptor extensionReceiver = descriptor.getExtensionReceiverParameter(); + return extensionReceiver != null; + } + private static boolean isTopLevelInPackage(@NotNull CallableDescriptor descriptor, @NotNull String name, @NotNull String packageName) { if (!name.equals(descriptor.getName().asString())) return false; diff --git a/compiler/testData/codegen/box/ranges/contains/inRangeWithSmartCast.kt b/compiler/testData/codegen/box/ranges/contains/inRangeWithSmartCast.kt new file mode 100644 index 00000000000..f5dabff8542 --- /dev/null +++ b/compiler/testData/codegen/box/ranges/contains/inRangeWithSmartCast.kt @@ -0,0 +1,26 @@ +// WITH_RUNTIME + +fun check(x: Any?): Boolean { + if (x is Int) { + return x in 239..240 + } + + throw java.lang.AssertionError() +} + +fun check(x: Any?, l: Any?, r: Any?): Boolean { + if (x is Int && l is Int && r is Int) { + return x in l..r + } + + throw java.lang.AssertionError() +} + + +fun box(): String { + assert(check(239)) + assert(check(239, 239, 240)) + assert(!check(238)) + assert(!check(238, 239, 240)) + return "OK" +} diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java index f06eabea490..80418fd8c3f 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java @@ -11271,6 +11271,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { doTest(fileName); } + @TestMetadata("inRangeWithSmartCast.kt") + public void testInRangeWithSmartCast() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/ranges/contains/inRangeWithSmartCast.kt"); + doTest(fileName); + } + @TestMetadata("rangeContainsString.kt") public void testRangeContainsString() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/ranges/contains/rangeContainsString.kt");