Add an implicit cast to Unit in returns from a constructor.

This commit is contained in:
Steven Schäfer
2019-03-25 10:39:00 +01:00
committed by Dmitry Petrov
parent 826f55ecb4
commit 59c5d8bdb8
6 changed files with 47 additions and 3 deletions
@@ -27,6 +27,7 @@ import org.jetbrains.kotlin.ir.declarations.IrVariable
import org.jetbrains.kotlin.ir.descriptors.IrBuiltIns
import org.jetbrains.kotlin.ir.expressions.*
import org.jetbrains.kotlin.ir.expressions.impl.IrTypeOperatorCallImpl
import org.jetbrains.kotlin.ir.symbols.IrConstructorSymbol
import org.jetbrains.kotlin.ir.types.IrType
import org.jetbrains.kotlin.ir.types.classifierOrFail
import org.jetbrains.kotlin.ir.types.impl.originalKotlinType
@@ -107,7 +108,11 @@ open class InsertImplicitCasts(
override fun visitReturn(expression: IrReturn): IrExpression =
expression.transformPostfix {
value = value.cast(expression.returnTarget.returnType)
value = if (expression.returnTargetSymbol is IrConstructorSymbol) {
value.coerceToUnit()
} else {
value.cast(expression.returnTarget.returnType)
}
}
override fun visitSetVariable(expression: IrSetVariable): IrExpression =
@@ -1,4 +1,3 @@
// IGNORE_BACKEND: JVM_IR
class A {
val prop: Int
constructor(arg: Boolean) {
@@ -1,4 +1,3 @@
// IGNORE_BACKEND: JVM_IR
class A {
val prop: Int
constructor(arg: Boolean) {
@@ -0,0 +1,5 @@
class C() {
constructor(x: Any?) : this() {
if (x is Unit) return x
}
}
@@ -0,0 +1,31 @@
FILE fqName:<root> fileName:/implicitCastInReturnFromConstructor.kt
CLASS CLASS name:C modality:FINAL visibility:public superTypes:[kotlin.Any]
$this: VALUE_PARAMETER INSTANCE_RECEIVER name:<this> type:<root>.C
CONSTRUCTOR visibility:public <> () returnType:<root>.C [primary]
BLOCK_BODY
DELEGATING_CONSTRUCTOR_CALL 'public constructor <init> () [primary] declared in kotlin.Any'
INSTANCE_INITIALIZER_CALL classDescriptor='CLASS CLASS name:C modality:FINAL visibility:public superTypes:[kotlin.Any]'
CONSTRUCTOR visibility:public <> (x:kotlin.Any?) returnType:<root>.C
VALUE_PARAMETER name:x index:0 type:kotlin.Any?
BLOCK_BODY
DELEGATING_CONSTRUCTOR_CALL 'public constructor <init> () [primary] declared in <root>.C'
WHEN type=kotlin.Unit origin=IF
BRANCH
if: TYPE_OP type=kotlin.Boolean origin=INSTANCEOF typeOperand=kotlin.Unit
GET_VAR 'x: kotlin.Any? declared in <root>.C.<init>' type=kotlin.Any? origin=null
then: RETURN type=kotlin.Nothing from='public constructor <init> (x: kotlin.Any?) declared in <root>.C'
TYPE_OP type=kotlin.Unit origin=IMPLICIT_COERCION_TO_UNIT typeOperand=kotlin.Unit
GET_VAR 'x: kotlin.Any? declared in <root>.C.<init>' type=kotlin.Any? origin=null
FUN FAKE_OVERRIDE name:equals visibility:public modality:OPEN <> ($this:kotlin.Any, other:kotlin.Any?) returnType:kotlin.Boolean
overridden:
public open fun equals (other: kotlin.Any?): kotlin.Boolean declared in kotlin.Any
$this: VALUE_PARAMETER name:<this> type:kotlin.Any
VALUE_PARAMETER name:other index:0 type:kotlin.Any?
FUN FAKE_OVERRIDE name:hashCode visibility:public modality:OPEN <> ($this:kotlin.Any) returnType:kotlin.Int
overridden:
public open fun hashCode (): kotlin.Int declared in kotlin.Any
$this: VALUE_PARAMETER name:<this> type:kotlin.Any
FUN FAKE_OVERRIDE name:toString visibility:public modality:OPEN <> ($this:kotlin.Any) returnType:kotlin.String
overridden:
public open fun toString (): kotlin.String declared in kotlin.Any
$this: VALUE_PARAMETER name:<this> type:kotlin.Any
@@ -907,6 +907,11 @@ public class IrTextTestCaseGenerated extends AbstractIrTextTestCase {
runTest("compiler/testData/ir/irText/expressions/ifElseIf.kt");
}
@TestMetadata("implicitCastInReturnFromConstructor.kt")
public void testImplicitCastInReturnFromConstructor() throws Exception {
runTest("compiler/testData/ir/irText/expressions/implicitCastInReturnFromConstructor.kt");
}
@TestMetadata("implicitCastOnPlatformType.kt")
public void testImplicitCastOnPlatformType() throws Exception {
runTest("compiler/testData/ir/irText/expressions/implicitCastOnPlatformType.kt");