KT-16437 Incorrect type inference for some when coerced to Unit

If the result of 'when' is not used in an expression,
this 'when' expression has type 'Unit' despite of whatever FE has inferred.
This commit is contained in:
Dmitry Petrov
2017-02-27 16:21:16 +03:00
parent e2e57e5b6d
commit d0134f2c64
6 changed files with 35 additions and 4 deletions
@@ -85,7 +85,12 @@ class BranchingExpressionGenerator(statementGenerator: StatementGenerator) : Sta
scope.createTemporaryVariable(statementGenerator.generateExpression(it), "subject")
}
val resultType = getInferredTypeWithImplicitCastsOrFail(expression)
// TODO relies on ControlFlowInformationProvider, get rid of it
val resultType =
if (context.bindingContext[BindingContext.USED_AS_EXPRESSION, expression] ?: false)
getInferredTypeWithImplicitCastsOrFail(expression)
else
context.builtIns.unitType
val irWhen = IrWhenImpl(expression.startOffset, expression.endOffset, resultType, IrStatementOrigin.WHEN)
+1 -1
View File
@@ -5,7 +5,7 @@ CONTENT
1 FUN public fun toString(grade: kotlin.String): kotlin.String
2 GET_VAR 'value-parameter grade: String' type=kotlin.String origin=null
3 VAR IR_TEMPORARY_VARIABLE val tmp0_subject: kotlin.String
4 WHEN type=kotlin.Nothing origin=WHEN
4 WHEN type=kotlin.Unit origin=WHEN
5 GET_VAR 'tmp0_subject: String' type=kotlin.String origin=null
6 CONST String type=kotlin.String value='A'
OUTGOING -> BB 1, 5
@@ -0,0 +1,7 @@
// WITH_RUNTIME
fun foo(x: Int) {
when (x) {
0 -> 0
}
}
@@ -0,0 +1,13 @@
FILE /whenCoercedToUnit.kt
FUN public fun foo(x: kotlin.Int): kotlin.Unit
BLOCK_BODY
BLOCK type=kotlin.Unit origin=WHEN
VAR IR_TEMPORARY_VARIABLE val tmp0_subject: kotlin.Int
GET_VAR 'value-parameter x: Int' type=kotlin.Int origin=null
WHEN type=kotlin.Unit origin=WHEN
BRANCH
if: CALL 'EQEQ(Any?, Any?): Boolean' type=kotlin.Boolean origin=EQEQ
arg0: GET_VAR 'tmp0_subject: Int' type=kotlin.Int origin=null
arg1: CONST Int type=kotlin.Int value='0'
then: TYPE_OP origin=IMPLICIT_COERCION_TO_UNIT typeOperand=kotlin.Unit
CONST Int type=kotlin.Int value='0'
+2 -2
View File
@@ -1,10 +1,10 @@
FILE /whenReturn.kt
FUN public fun toString(grade: kotlin.String): kotlin.String
BLOCK_BODY
BLOCK type=kotlin.Nothing origin=WHEN
BLOCK type=kotlin.Unit origin=WHEN
VAR IR_TEMPORARY_VARIABLE val tmp0_subject: kotlin.String
GET_VAR 'value-parameter grade: String' type=kotlin.String origin=null
WHEN type=kotlin.Nothing origin=WHEN
WHEN type=kotlin.Unit origin=WHEN
BRANCH
if: CALL 'EQEQ(Any?, Any?): Boolean' type=kotlin.Boolean origin=EQEQ
arg0: GET_VAR 'tmp0_subject: String' type=kotlin.String origin=null
@@ -689,6 +689,12 @@ public class IrTextTestCaseGenerated extends AbstractIrTextTestCase {
doTest(fileName);
}
@TestMetadata("whenCoercedToUnit.kt")
public void testWhenCoercedToUnit() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/ir/irText/expressions/whenCoercedToUnit.kt");
doTest(fileName);
}
@TestMetadata("whenElse.kt")
public void testWhenElse() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/ir/irText/expressions/whenElse.kt");