KT-5044 code cleanup, test added

This commit is contained in:
Vsevolod
2016-10-11 21:14:57 +03:00
committed by Dmitry Petrov
parent bd6f83b9b7
commit e788ef408f
4 changed files with 44 additions and 12 deletions
@@ -3470,13 +3470,13 @@ public class ExpressionCodegen extends KtVisitor<StackValue, StackValue> 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);
@@ -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;
@@ -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"
}
@@ -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");