Fix const range bounds generation

This commit is contained in:
Dmitry Petrov
2017-09-25 11:51:47 +03:00
parent e82544ffb0
commit 8f9ea3e08b
7 changed files with 77 additions and 3 deletions
@@ -179,6 +179,19 @@ public abstract class StackValue {
return type == Type.VOID_TYPE ? none() : new OnStack(type);
}
@NotNull
public static StackValue integerConstant(int value, @NotNull Type type) {
if (type == Type.LONG_TYPE) {
return constant(Long.valueOf(value), type);
}
else if (type == Type.BYTE_TYPE || type == Type.SHORT_TYPE || type == Type.INT_TYPE) {
return constant(Integer.valueOf(value), type);
}
else {
throw new AssertionError("Unexpected integer type: " + type);
}
}
@NotNull
public static StackValue constant(@Nullable Object value, @NotNull Type type) {
if (type == Type.BOOLEAN_TYPE) {
@@ -18,10 +18,8 @@ package org.jetbrains.kotlin.codegen.range.forLoop
import org.jetbrains.kotlin.codegen.ExpressionCodegen
import org.jetbrains.kotlin.codegen.StackValue
import org.jetbrains.kotlin.psi.KtExpression
import org.jetbrains.kotlin.psi.KtForExpression
import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall
import org.jetbrains.kotlin.resolve.constants.*
import org.jetbrains.kotlin.resolve.scopes.receivers.ReceiverValue
class ForInUntilConstantRangeLoopGenerator(
@@ -36,5 +34,5 @@ class ForInUntilConstantRangeLoopGenerator(
codegen.generateReceiverValue(from, false)
override fun generateTo(): StackValue =
StackValue.constant(untilValue, asmElementType)
StackValue.integerConstant(untilValue, asmElementType)
}
@@ -0,0 +1,39 @@
fun test1(): Long {
var s = 0L
for (i in 1L..4) {
s = s * 10 + i
}
return s
}
fun test2(): Long {
var s = 0L
for (i in 1L..4.toShort()) {
s = s * 10 + i
}
return s
}
fun testLI(a: Long, b: Int): Long {
var s = 0L
for (i in a..b) {
s = s * 10 + i
}
return s
}
fun testLS(a: Long, b: Short): Long {
var s = 0L
for (i in a..b) {
s = s * 10 + i
}
return s
}
fun box(): String {
if (test1() != 1234L) return "Fail 1"
if (test2() != 1234L) return "Fail 1"
if (testLI(1L, 4) != 1234L) return "Fail 2"
if (testLS(1L, 4.toShort()) != 1234L) return "Fail 2"
return "OK"
}
@@ -13739,6 +13739,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
doTest(fileName);
}
@TestMetadata("forInRangeLiteralWithMixedTypeBounds.kt")
public void testForInRangeLiteralWithMixedTypeBounds() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/ranges/forInRangeLiteralWithMixedTypeBounds.kt");
doTest(fileName);
}
@TestMetadata("forInRangeToConstWithOverflow.kt")
public void testForInRangeToConstWithOverflow() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/ranges/forInRangeToConstWithOverflow.kt");
@@ -13739,6 +13739,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
doTest(fileName);
}
@TestMetadata("forInRangeLiteralWithMixedTypeBounds.kt")
public void testForInRangeLiteralWithMixedTypeBounds() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/ranges/forInRangeLiteralWithMixedTypeBounds.kt");
doTest(fileName);
}
@TestMetadata("forInRangeToConstWithOverflow.kt")
public void testForInRangeToConstWithOverflow() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/ranges/forInRangeToConstWithOverflow.kt");
@@ -13739,6 +13739,12 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
doTest(fileName);
}
@TestMetadata("forInRangeLiteralWithMixedTypeBounds.kt")
public void testForInRangeLiteralWithMixedTypeBounds() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/ranges/forInRangeLiteralWithMixedTypeBounds.kt");
doTest(fileName);
}
@TestMetadata("forInRangeToConstWithOverflow.kt")
public void testForInRangeToConstWithOverflow() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/ranges/forInRangeToConstWithOverflow.kt");
@@ -15203,6 +15203,12 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest {
doTest(fileName);
}
@TestMetadata("forInRangeLiteralWithMixedTypeBounds.kt")
public void testForInRangeLiteralWithMixedTypeBounds() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/ranges/forInRangeLiteralWithMixedTypeBounds.kt");
doTest(fileName);
}
@TestMetadata("forInRangeToConstWithOverflow.kt")
public void testForInRangeToConstWithOverflow() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/ranges/forInRangeToConstWithOverflow.kt");