psi2ir: keep the type of when when possible
Ideally, the type of `IrWhen` should be provided by type inference for a consistent behavior. `USED_AS_EXPRESSION` from CFG isn't always consistent with type inference, unfortunately. The behavior is now aligned with `if`. The type of `when` is kept when it *can* be an expression, instead of whether it is used or not.
This commit is contained in:
committed by
max-kammerer
parent
cd651be461
commit
1b4d26e490
+2
-4
@@ -104,11 +104,9 @@ class BranchingExpressionGenerator(statementGenerator: StatementGenerator) : Sta
|
||||
|
||||
val inferredType = getInferredTypeWithImplicitCastsOrFail(expression)
|
||||
|
||||
// TODO relies on ControlFlowInformationProvider, get rid of it
|
||||
val isUsedAsExpression = get(BindingContext.USED_AS_EXPRESSION, expression) ?: false
|
||||
|
||||
val resultType = when {
|
||||
isUsedAsExpression -> inferredType.toIrType()
|
||||
// Non-exhaustive when can only be used as statement.
|
||||
expression.isExhaustiveWhen() -> inferredType.toIrType()
|
||||
KotlinBuiltIns.isNothing(inferredType) -> inferredType.toIrType()
|
||||
else -> context.irBuiltIns.unitType
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user