Skip to content

Commit 2b3043b

Browse files
committed
Fix CFG problem for 'when' in Konan
If type of 'when' expression is 'Nothing', it should be kept that way even if the expression itself is implicitly coerced to Unit.
1 parent 1f34dfa commit 2b3043b

File tree

3 files changed

+14
-8
lines changed

3 files changed

+14
-8
lines changed

compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/generators/BranchingExpressionGenerator.kt

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.jetbrains.kotlin.psi2ir.generators
1818

19+
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
1920
import org.jetbrains.kotlin.ir.UNDEFINED_OFFSET
2021
import org.jetbrains.kotlin.ir.builders.whenComma
2122
import org.jetbrains.kotlin.ir.declarations.IrVariable
@@ -85,12 +86,17 @@ class BranchingExpressionGenerator(statementGenerator: StatementGenerator) : Sta
8586
scope.createTemporaryVariable(statementGenerator.generateExpression(it), "subject")
8687
}
8788

89+
90+
val inferredType = getInferredTypeWithImplicitCastsOrFail(expression)
91+
8892
// TODO relies on ControlFlowInformationProvider, get rid of it
89-
val resultType =
90-
if (context.bindingContext[BindingContext.USED_AS_EXPRESSION, expression] ?: false)
91-
getInferredTypeWithImplicitCastsOrFail(expression)
92-
else
93-
context.builtIns.unitType
93+
val isUsedAsExpression = context.bindingContext[BindingContext.USED_AS_EXPRESSION, expression] ?: false
94+
95+
val resultType = when {
96+
isUsedAsExpression -> inferredType
97+
KotlinBuiltIns.isNothing(inferredType) -> inferredType
98+
else -> context.builtIns.unitType
99+
}
94100

95101
val irWhen = IrWhenImpl(expression.startOffset, expression.endOffset, resultType, IrStatementOrigin.WHEN)
96102

compiler/testData/ir/irCfg/when/whenReturn.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ CONTENT
55
1 FUN public fun toString(grade: kotlin.String): kotlin.String
66
2 GET_VAR 'value-parameter grade: String' type=kotlin.String origin=null
77
3 VAR IR_TEMPORARY_VARIABLE val tmp0_subject: kotlin.String
8-
4 WHEN type=kotlin.Unit origin=WHEN
8+
4 WHEN type=kotlin.Nothing origin=WHEN
99
5 GET_VAR 'tmp0_subject: String' type=kotlin.String origin=null
1010
6 CONST String type=kotlin.String value='A'
1111
OUTGOING -> BB 1, 5

compiler/testData/ir/irText/expressions/whenReturn.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ FILE /whenReturn.kt
22
FUN public fun toString(grade: kotlin.String): kotlin.String
33
VALUE_PARAMETER value-parameter grade: kotlin.String
44
BLOCK_BODY
5-
BLOCK type=kotlin.Unit origin=WHEN
5+
BLOCK type=kotlin.Nothing origin=WHEN
66
VAR IR_TEMPORARY_VARIABLE val tmp0_subject: kotlin.String
77
GET_VAR 'value-parameter grade: String' type=kotlin.String origin=null
8-
WHEN type=kotlin.Unit origin=WHEN
8+
WHEN type=kotlin.Nothing origin=WHEN
99
BRANCH
1010
if: CALL 'EQEQ(Any?, Any?): Boolean' type=kotlin.Boolean origin=EQEQ
1111
arg0: GET_VAR 'tmp0_subject: String' type=kotlin.String origin=null

0 commit comments

Comments
 (0)