[FIR2IR] Don't emit empty body of while/do_while loop
Merge-request: KT-MR-12283 Merged-by: Vladimir Sukharev <Vladimir.Sukharev@jetbrains.com>
This commit is contained in:
committed by
Space Team
parent
5b4409e34c
commit
e9d4de658d
@@ -1303,7 +1303,9 @@ class Fir2IrVisitor(
|
||||
).apply {
|
||||
loopMap[doWhileLoop] = this
|
||||
label = doWhileLoop.label?.name
|
||||
body = doWhileLoop.block.convertToIrExpressionOrBlock(origin)
|
||||
body = runUnless(doWhileLoop.block is FirEmptyExpressionBlock) {
|
||||
doWhileLoop.block.convertToIrExpressionOrBlock(origin)
|
||||
}
|
||||
condition = convertToIrExpression(doWhileLoop.condition)
|
||||
loopMap.remove(doWhileLoop)
|
||||
}
|
||||
@@ -1324,49 +1326,51 @@ class Fir2IrVisitor(
|
||||
loopMap[whileLoop] = this
|
||||
label = whileLoop.label?.name
|
||||
condition = convertToIrExpression(whileLoop.condition)
|
||||
body = if (isForLoop) {
|
||||
/*
|
||||
* for loops in IR must have their body in the exact following form
|
||||
* because some of the lowerings (e.g. `ForLoopLowering`) expect it:
|
||||
*
|
||||
* for (x in list) { ...body...}
|
||||
*
|
||||
* IR (loop body):
|
||||
* IrBlock:
|
||||
* x = <iterator>.next()
|
||||
* ... possible destructured loop variables, in case iterator is a tuple: `for ((a,b,c) in list) { ...body...}` ...
|
||||
* IrBlock:
|
||||
* ...body...
|
||||
*/
|
||||
firLoopBody.convertWithOffsets { innerStartOffset, innerEndOffset ->
|
||||
val loopBodyStatements = firLoopBody.statements
|
||||
val firLoopVarStmt = loopBodyStatements.firstOrNull()
|
||||
?: error("Unexpected shape of for loop body: missing body statements")
|
||||
body = runUnless(firLoopBody is FirEmptyExpressionBlock) {
|
||||
if (isForLoop) {
|
||||
/*
|
||||
* for loops in IR must have their body in the exact following form
|
||||
* because some of the lowerings (e.g. `ForLoopLowering`) expect it:
|
||||
*
|
||||
* for (x in list) { ...body...}
|
||||
*
|
||||
* IR (loop body):
|
||||
* IrBlock:
|
||||
* x = <iterator>.next()
|
||||
* ... possible destructured loop variables, in case iterator is a tuple: `for ((a,b,c) in list) { ...body...}` ...
|
||||
* IrBlock:
|
||||
* ...body...
|
||||
*/
|
||||
firLoopBody.convertWithOffsets { innerStartOffset, innerEndOffset ->
|
||||
val loopBodyStatements = firLoopBody.statements
|
||||
val firLoopVarStmt = loopBodyStatements.firstOrNull()
|
||||
?: error("Unexpected shape of for loop body: missing body statements")
|
||||
|
||||
val (destructuredLoopVariables, realStatements) = loopBodyStatements.drop(1).partition {
|
||||
it is FirProperty && it.initializer is FirComponentCall
|
||||
}
|
||||
val firExpression = realStatements.singleOrNull() as? FirExpression
|
||||
?: error("Unexpected shape of for loop body: must be single real loop statement, but got ${realStatements.size}")
|
||||
|
||||
val irStatements = buildList {
|
||||
addIfNotNull(firLoopVarStmt.toIrStatement())
|
||||
destructuredLoopVariables.forEach { addIfNotNull(it.toIrStatement()) }
|
||||
if (firExpression !is FirEmptyExpressionBlock) {
|
||||
add(convertToIrExpression(firExpression))
|
||||
val (destructuredLoopVariables, realStatements) = loopBodyStatements.drop(1).partition {
|
||||
it is FirProperty && it.initializer is FirComponentCall
|
||||
}
|
||||
}
|
||||
val firExpression = realStatements.singleOrNull() as? FirExpression
|
||||
?: error("Unexpected shape of for loop body: must be single real loop statement, but got ${realStatements.size}")
|
||||
|
||||
IrBlockImpl(
|
||||
innerStartOffset,
|
||||
innerEndOffset,
|
||||
irBuiltIns.unitType,
|
||||
origin,
|
||||
irStatements,
|
||||
)
|
||||
val irStatements = buildList {
|
||||
addIfNotNull(firLoopVarStmt.toIrStatement())
|
||||
destructuredLoopVariables.forEach { addIfNotNull(it.toIrStatement()) }
|
||||
if (firExpression !is FirEmptyExpressionBlock) {
|
||||
add(convertToIrExpression(firExpression))
|
||||
}
|
||||
}
|
||||
|
||||
IrBlockImpl(
|
||||
innerStartOffset,
|
||||
innerEndOffset,
|
||||
irBuiltIns.unitType,
|
||||
origin,
|
||||
irStatements,
|
||||
)
|
||||
}
|
||||
} else {
|
||||
firLoopBody.convertToIrExpressionOrBlock(origin)
|
||||
}
|
||||
} else {
|
||||
firLoopBody.convertToIrExpressionOrBlock(origin)
|
||||
}
|
||||
loopMap.remove(whileLoop)
|
||||
}
|
||||
|
||||
@@ -18,7 +18,6 @@ FILE fqName:<root> fileName:/breakContinueInLoopHeader.kt
|
||||
BRANCH
|
||||
if: CONST Boolean type=kotlin.Boolean value=true
|
||||
then: GET_VAR 'val tmp_0: kotlin.Boolean? declared in <root>.test1' type=kotlin.Boolean? origin=null
|
||||
body: BLOCK type=kotlin.Unit origin=null
|
||||
FUN name:test2 visibility:public modality:FINAL <> (c:kotlin.Boolean?) returnType:kotlin.Unit
|
||||
VALUE_PARAMETER name:c index:0 type:kotlin.Boolean?
|
||||
BLOCK_BODY
|
||||
@@ -38,7 +37,6 @@ FILE fqName:<root> fileName:/breakContinueInLoopHeader.kt
|
||||
BRANCH
|
||||
if: CONST Boolean type=kotlin.Boolean value=true
|
||||
then: GET_VAR 'val tmp_1: kotlin.Boolean? declared in <root>.test2' type=kotlin.Boolean? origin=null
|
||||
body: BLOCK type=kotlin.Unit origin=null
|
||||
FUN name:test3 visibility:public modality:FINAL <> (ss:kotlin.collections.List<kotlin.String>?) returnType:kotlin.Unit
|
||||
VALUE_PARAMETER name:ss index:0 type:kotlin.collections.List<kotlin.String>?
|
||||
BLOCK_BODY
|
||||
|
||||
+2
-4
@@ -6,8 +6,7 @@ fun test1(c: Boolean?) {
|
||||
EQEQ(arg0 = <elvis>, arg1 = null) -> break@L
|
||||
else -> <elvis>
|
||||
}
|
||||
}) { // BLOCK
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,8 +18,7 @@ fun test2(c: Boolean?) {
|
||||
EQEQ(arg0 = <elvis>, arg1 = null) -> continue@L
|
||||
else -> <elvis>
|
||||
}
|
||||
}) { // BLOCK
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -7,7 +7,6 @@ FILE fqName:<root> fileName:/whileDoWhile.kt
|
||||
condition: CALL 'public final fun less (arg0: kotlin.Int, arg1: kotlin.Int): kotlin.Boolean declared in kotlin.internal.ir' type=kotlin.Boolean origin=LT
|
||||
arg0: GET_VAR 'var x: kotlin.Int declared in <root>.test' type=kotlin.Int origin=null
|
||||
arg1: CONST Int type=kotlin.Int value=0
|
||||
body: BLOCK type=kotlin.Unit origin=null
|
||||
WHILE label=null origin=WHILE_LOOP
|
||||
condition: CALL 'public final fun less (arg0: kotlin.Int, arg1: kotlin.Int): kotlin.Boolean declared in kotlin.internal.ir' type=kotlin.Boolean origin=LT
|
||||
arg0: GET_VAR 'var x: kotlin.Int declared in <root>.test' type=kotlin.Int origin=null
|
||||
@@ -33,10 +32,15 @@ FILE fqName:<root> fileName:/whileDoWhile.kt
|
||||
GET_VAR 'val tmp_1: kotlin.Int declared in <root>.test' type=kotlin.Int origin=null
|
||||
BLOCK type=kotlin.Unit origin=null
|
||||
DO_WHILE label=null origin=DO_WHILE_LOOP
|
||||
body: COMPOSITE type=kotlin.Unit origin=null
|
||||
condition: CALL 'public final fun less (arg0: kotlin.Int, arg1: kotlin.Int): kotlin.Boolean declared in kotlin.internal.ir' type=kotlin.Boolean origin=LT
|
||||
arg0: GET_VAR 'var x: kotlin.Int declared in <root>.test' type=kotlin.Int origin=null
|
||||
arg1: CONST Int type=kotlin.Int value=0
|
||||
BLOCK type=kotlin.Unit origin=null
|
||||
DO_WHILE label=null origin=DO_WHILE_LOOP
|
||||
body: COMPOSITE type=kotlin.Unit origin=null
|
||||
condition: CALL 'public final fun less (arg0: kotlin.Int, arg1: kotlin.Int): kotlin.Boolean declared in kotlin.internal.ir' type=kotlin.Boolean origin=LT
|
||||
arg0: GET_VAR 'var x: kotlin.Int declared in <root>.test' type=kotlin.Int origin=null
|
||||
arg1: CONST Int type=kotlin.Int value=7
|
||||
BLOCK type=kotlin.Unit origin=null
|
||||
DO_WHILE label=null origin=DO_WHILE_LOOP
|
||||
body: BLOCK type=kotlin.Int origin=POSTFIX_INCR
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
fun test() {
|
||||
var x: Int = 0
|
||||
while (less(arg0 = x, arg1 = 0)) { // BLOCK
|
||||
}
|
||||
while (less(arg0 = x, arg1 = 0))
|
||||
while (less(arg0 = x, arg1 = 5)) { // BLOCK
|
||||
val <unary>: Int = x
|
||||
x = <unary>.inc()
|
||||
@@ -14,9 +13,12 @@ fun test() {
|
||||
<unary>
|
||||
}
|
||||
}
|
||||
{ // BLOCK
|
||||
do while (less(arg0 = x, arg1 = 0))
|
||||
}
|
||||
{ // BLOCK
|
||||
do// COMPOSITE {
|
||||
// } while (less(arg0 = x, arg1 = 0))
|
||||
// } while (less(arg0 = x, arg1 = 7))
|
||||
}
|
||||
{ // BLOCK
|
||||
do{ // BLOCK
|
||||
|
||||
@@ -37,6 +37,12 @@ FILE fqName:<root> fileName:/whileDoWhile.kt
|
||||
condition: CALL 'public final fun less (arg0: kotlin.Int, arg1: kotlin.Int): kotlin.Boolean declared in kotlin.internal.ir' type=kotlin.Boolean origin=LT
|
||||
arg0: GET_VAR 'var x: kotlin.Int declared in <root>.test' type=kotlin.Int origin=null
|
||||
arg1: CONST Int type=kotlin.Int value=0
|
||||
BLOCK type=kotlin.Unit origin=null
|
||||
DO_WHILE label=null origin=DO_WHILE_LOOP
|
||||
body: COMPOSITE type=kotlin.Unit origin=null
|
||||
condition: CALL 'public final fun less (arg0: kotlin.Int, arg1: kotlin.Int): kotlin.Boolean declared in kotlin.internal.ir' type=kotlin.Boolean origin=LT
|
||||
arg0: GET_VAR 'var x: kotlin.Int declared in <root>.test' type=kotlin.Int origin=null
|
||||
arg1: CONST Int type=kotlin.Int value=7
|
||||
BLOCK type=kotlin.Unit origin=null
|
||||
DO_WHILE label=null origin=DO_WHILE_LOOP
|
||||
body: TYPE_OP type=kotlin.Unit origin=IMPLICIT_COERCION_TO_UNIT typeOperand=kotlin.Unit
|
||||
|
||||
@@ -4,6 +4,7 @@ fun test() {
|
||||
while (x < 5) x++
|
||||
while (x < 10) { x++ }
|
||||
do while (x < 0)
|
||||
do {} while (x < 7)
|
||||
do x++ while (x < 15)
|
||||
do { x ++ } while (x < 20)
|
||||
}
|
||||
|
||||
@@ -16,6 +16,10 @@ fun test() {
|
||||
{ // BLOCK
|
||||
do while (less(arg0 = x, arg1 = 0))
|
||||
}
|
||||
{ // BLOCK
|
||||
do// COMPOSITE {
|
||||
// } while (less(arg0 = x, arg1 = 7))
|
||||
}
|
||||
{ // BLOCK
|
||||
do{ // BLOCK
|
||||
val tmp2: Int = x
|
||||
|
||||
Reference in New Issue
Block a user