Maintain bounds evaluation order in intrinsics for 'reversed'
Makes sense for 'rangeTo', 'downTo', and 'until' with non-const bounds.
This commit is contained in:
+4
-1
@@ -43,7 +43,10 @@ class DownToProgressionRangeValue(rangeCall: ResolvedCall<out CallableDescriptor
|
||||
|
||||
override fun createForInReversedLoopGenerator(codegen: ExpressionCodegen, forExpression: KtForExpression) =
|
||||
createConstBoundedForInReversedDownToGenerator(codegen, forExpression) ?:
|
||||
ForInSimpleProgressionLoopGenerator.fromBoundedValueWithStep1(codegen, forExpression, getBoundedValue(codegen))
|
||||
ForInSimpleProgressionLoopGenerator.fromBoundedValueWithStep1(
|
||||
codegen, forExpression, getBoundedValue(codegen),
|
||||
inverseBoundsEvaluationOrder = true
|
||||
)
|
||||
|
||||
private fun createConstBoundedForInDownToGenerator(
|
||||
codegen: ExpressionCodegen,
|
||||
|
||||
+4
-1
@@ -41,7 +41,10 @@ class PrimitiveNumberRangeLiteralRangeValue(
|
||||
|
||||
override fun createForInReversedLoopGenerator(codegen: ExpressionCodegen, forExpression: KtForExpression): ForLoopGenerator =
|
||||
createConstBoundedRangeForInReversedRangeLiteralGenerator(codegen, forExpression) ?:
|
||||
ForInSimpleProgressionLoopGenerator.fromBoundedValueWithStepMinus1(codegen, forExpression, getBoundedValue(codegen))
|
||||
ForInSimpleProgressionLoopGenerator.fromBoundedValueWithStepMinus1(
|
||||
codegen, forExpression, getBoundedValue(codegen),
|
||||
inverseBoundsEvaluationOrder = true
|
||||
)
|
||||
|
||||
private fun createConstBoundedForInRangeLiteralGenerator(
|
||||
codegen: ExpressionCodegen,
|
||||
|
||||
+4
-1
@@ -36,7 +36,10 @@ class PrimitiveNumberUntilRangeValue(rangeCall: ResolvedCall<out CallableDescrip
|
||||
|
||||
override fun createForInReversedLoopGenerator(codegen: ExpressionCodegen, forExpression: KtForExpression) =
|
||||
createConstBoundedForInReversedUntilGenerator(codegen, forExpression) ?:
|
||||
ForInSimpleProgressionLoopGenerator.fromBoundedValueWithStepMinus1(codegen, forExpression, getBoundedValue(codegen))
|
||||
ForInSimpleProgressionLoopGenerator.fromBoundedValueWithStepMinus1(
|
||||
codegen, forExpression, getBoundedValue(codegen),
|
||||
inverseBoundsEvaluationOrder = true
|
||||
)
|
||||
|
||||
private fun createConstBoundedForInReversedUntilGenerator(
|
||||
codegen: ExpressionCodegen,
|
||||
|
||||
+26
-7
@@ -30,6 +30,7 @@ class ForInSimpleProgressionLoopGenerator(
|
||||
private val isStartInclusive: Boolean,
|
||||
private val endValue: StackValue,
|
||||
private val isEndInclusive: Boolean,
|
||||
private val inverseBoundsEvaluationOrder: Boolean,
|
||||
step: Int
|
||||
) : AbstractForInRangeLoopGenerator(codegen, forExpression, step) {
|
||||
|
||||
@@ -37,6 +38,7 @@ class ForInSimpleProgressionLoopGenerator(
|
||||
codegen: ExpressionCodegen,
|
||||
forExpression: KtForExpression,
|
||||
boundedValue: SimpleBoundedValue,
|
||||
inverseBoundsEvaluationOrder: Boolean,
|
||||
step: Int
|
||||
) : this(
|
||||
codegen, forExpression,
|
||||
@@ -44,23 +46,40 @@ class ForInSimpleProgressionLoopGenerator(
|
||||
isStartInclusive = if (step == 1) boundedValue.isLowInclusive else boundedValue.isHighInclusive,
|
||||
endValue = if (step == 1) boundedValue.highBound else boundedValue.lowBound,
|
||||
isEndInclusive = if (step == 1) boundedValue.isHighInclusive else boundedValue.isLowInclusive,
|
||||
inverseBoundsEvaluationOrder = inverseBoundsEvaluationOrder,
|
||||
step = step
|
||||
)
|
||||
|
||||
companion object {
|
||||
fun fromBoundedValueWithStep1(codegen: ExpressionCodegen, forExpression: KtForExpression, boundedValue: SimpleBoundedValue) =
|
||||
ForInSimpleProgressionLoopGenerator(codegen, forExpression, boundedValue, 1)
|
||||
fun fromBoundedValueWithStep1(
|
||||
codegen: ExpressionCodegen,
|
||||
forExpression: KtForExpression,
|
||||
boundedValue: SimpleBoundedValue,
|
||||
inverseBoundsEvaluationOrder: Boolean = false
|
||||
) =
|
||||
ForInSimpleProgressionLoopGenerator(codegen, forExpression, boundedValue, inverseBoundsEvaluationOrder, 1)
|
||||
|
||||
fun fromBoundedValueWithStepMinus1(codegen: ExpressionCodegen, forExpression: KtForExpression, boundedValue: SimpleBoundedValue) =
|
||||
ForInSimpleProgressionLoopGenerator(codegen, forExpression, boundedValue, -1)
|
||||
fun fromBoundedValueWithStepMinus1(
|
||||
codegen: ExpressionCodegen,
|
||||
forExpression: KtForExpression,
|
||||
boundedValue: SimpleBoundedValue,
|
||||
inverseBoundsEvaluationOrder: Boolean = false
|
||||
) =
|
||||
ForInSimpleProgressionLoopGenerator(codegen, forExpression, boundedValue, inverseBoundsEvaluationOrder, -1)
|
||||
}
|
||||
|
||||
override fun storeRangeStartAndEnd() {
|
||||
loopParameter().store(startValue, v)
|
||||
if (inverseBoundsEvaluationOrder) {
|
||||
StackValue.local(endVar, asmElementType).store(endValue, v)
|
||||
loopParameter().store(startValue, v)
|
||||
}
|
||||
else {
|
||||
loopParameter().store(startValue, v)
|
||||
StackValue.local(endVar, asmElementType).store(endValue, v)
|
||||
}
|
||||
|
||||
// Skip 1st element if start is not inclusive.
|
||||
if (!isStartInclusive) incrementLoopVariable()
|
||||
|
||||
StackValue.local(endVar, asmElementType).store(endValue, v)
|
||||
}
|
||||
|
||||
override fun checkEmptyLoop(loopExit: Label) {
|
||||
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
// WITH_RUNTIME
|
||||
import kotlin.test.*
|
||||
|
||||
val log = StringBuilder()
|
||||
|
||||
fun logged(message: String, value: Int) =
|
||||
value.also { log.append(message) }
|
||||
|
||||
fun box(): String {
|
||||
var s = 0
|
||||
for (i in (logged("start;", 2) downTo logged("end;", 1)).reversed()) {
|
||||
s += i
|
||||
}
|
||||
|
||||
assertEquals(3, s)
|
||||
|
||||
assertEquals("start;end;", log.toString())
|
||||
|
||||
return "OK"
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
// WITH_RUNTIME
|
||||
import kotlin.test.*
|
||||
|
||||
val log = StringBuilder()
|
||||
|
||||
fun logged(message: String, value: Int) =
|
||||
value.also { log.append(message) }
|
||||
|
||||
fun box(): String {
|
||||
var s = 0
|
||||
for (i in (logged("start;", 1) .. logged("end;", 2)).reversed()) {
|
||||
s += i
|
||||
}
|
||||
|
||||
assertEquals(3, s)
|
||||
|
||||
assertEquals("start;end;", log.toString())
|
||||
|
||||
return "OK"
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
// WITH_RUNTIME
|
||||
import kotlin.test.*
|
||||
|
||||
val log = StringBuilder()
|
||||
|
||||
fun logged(message: String, value: Int) =
|
||||
value.also { log.append(message) }
|
||||
|
||||
fun box(): String {
|
||||
var s = 0
|
||||
for (i in (logged("start;", 1) until logged("end;", 3)).reversed()) {
|
||||
s += i
|
||||
}
|
||||
|
||||
assertEquals(3, s)
|
||||
|
||||
assertEquals("start;end;", log.toString())
|
||||
|
||||
return "OK"
|
||||
}
|
||||
+27
@@ -15324,6 +15324,33 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/ranges/forInReversed/forInReversedUntilWithNonConstBounds.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("compiler/testData/codegen/box/ranges/forInReversed/evaluationOrder")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
public static class EvaluationOrder extends AbstractIrBlackBoxCodegenTest {
|
||||
public void testAllFilesPresentInEvaluationOrder() throws Exception {
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/ranges/forInReversed/evaluationOrder"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JVM, true);
|
||||
}
|
||||
|
||||
@TestMetadata("forInReversedDownToEvaluationOrder.kt")
|
||||
public void testForInReversedDownToEvaluationOrder() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/ranges/forInReversed/evaluationOrder/forInReversedDownToEvaluationOrder.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("forInReversedRangeLiteralEvaluationOrder.kt")
|
||||
public void testForInReversedRangeLiteralEvaluationOrder() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/ranges/forInReversed/evaluationOrder/forInReversedRangeLiteralEvaluationOrder.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("forInReversedUntilEvaluationOrder.kt")
|
||||
public void testForInReversedUntilEvaluationOrder() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/ranges/forInReversed/evaluationOrder/forInReversedUntilEvaluationOrder.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("compiler/testData/codegen/box/ranges/forInUntil")
|
||||
|
||||
@@ -15324,6 +15324,33 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/ranges/forInReversed/forInReversedUntilWithNonConstBounds.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("compiler/testData/codegen/box/ranges/forInReversed/evaluationOrder")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
public static class EvaluationOrder extends AbstractBlackBoxCodegenTest {
|
||||
public void testAllFilesPresentInEvaluationOrder() throws Exception {
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/ranges/forInReversed/evaluationOrder"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JVM, true);
|
||||
}
|
||||
|
||||
@TestMetadata("forInReversedDownToEvaluationOrder.kt")
|
||||
public void testForInReversedDownToEvaluationOrder() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/ranges/forInReversed/evaluationOrder/forInReversedDownToEvaluationOrder.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("forInReversedRangeLiteralEvaluationOrder.kt")
|
||||
public void testForInReversedRangeLiteralEvaluationOrder() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/ranges/forInReversed/evaluationOrder/forInReversedRangeLiteralEvaluationOrder.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("forInReversedUntilEvaluationOrder.kt")
|
||||
public void testForInReversedUntilEvaluationOrder() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/ranges/forInReversed/evaluationOrder/forInReversedUntilEvaluationOrder.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("compiler/testData/codegen/box/ranges/forInUntil")
|
||||
|
||||
@@ -15324,6 +15324,33 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/ranges/forInReversed/forInReversedUntilWithNonConstBounds.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("compiler/testData/codegen/box/ranges/forInReversed/evaluationOrder")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
public static class EvaluationOrder extends AbstractLightAnalysisModeTest {
|
||||
public void testAllFilesPresentInEvaluationOrder() throws Exception {
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/ranges/forInReversed/evaluationOrder"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JVM, true);
|
||||
}
|
||||
|
||||
@TestMetadata("forInReversedDownToEvaluationOrder.kt")
|
||||
public void testForInReversedDownToEvaluationOrder() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/ranges/forInReversed/evaluationOrder/forInReversedDownToEvaluationOrder.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("forInReversedRangeLiteralEvaluationOrder.kt")
|
||||
public void testForInReversedRangeLiteralEvaluationOrder() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/ranges/forInReversed/evaluationOrder/forInReversedRangeLiteralEvaluationOrder.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("forInReversedUntilEvaluationOrder.kt")
|
||||
public void testForInReversedUntilEvaluationOrder() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/ranges/forInReversed/evaluationOrder/forInReversedUntilEvaluationOrder.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("compiler/testData/codegen/box/ranges/forInUntil")
|
||||
|
||||
+27
@@ -16740,6 +16740,33 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/ranges/forInReversed/forInReversedUntilWithNonConstBounds.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("compiler/testData/codegen/box/ranges/forInReversed/evaluationOrder")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
public static class EvaluationOrder extends AbstractJsCodegenBoxTest {
|
||||
public void testAllFilesPresentInEvaluationOrder() throws Exception {
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/ranges/forInReversed/evaluationOrder"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JS, true);
|
||||
}
|
||||
|
||||
@TestMetadata("forInReversedDownToEvaluationOrder.kt")
|
||||
public void testForInReversedDownToEvaluationOrder() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/ranges/forInReversed/evaluationOrder/forInReversedDownToEvaluationOrder.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("forInReversedRangeLiteralEvaluationOrder.kt")
|
||||
public void testForInReversedRangeLiteralEvaluationOrder() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/ranges/forInReversed/evaluationOrder/forInReversedRangeLiteralEvaluationOrder.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("forInReversedUntilEvaluationOrder.kt")
|
||||
public void testForInReversedUntilEvaluationOrder() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/ranges/forInReversed/evaluationOrder/forInReversedUntilEvaluationOrder.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("compiler/testData/codegen/box/ranges/forInUntil")
|
||||
|
||||
Reference in New Issue
Block a user