Generate IMPLICIT_NOTNULL for (nullability flexible type) -> (non-null type) implicit conversion.

This commit is contained in:
Dmitry Petrov
2016-08-18 19:00:32 +03:00
committed by Dmitry Petrov
parent 2b6171057e
commit 3614b0a06c
5 changed files with 30 additions and 13 deletions
@@ -17,25 +17,26 @@
package org.jetbrains.kotlin.psi2ir
import org.jetbrains.kotlin.ir.declarations.IrVariable
import org.jetbrains.kotlin.ir.expressions.IrExpression
import org.jetbrains.kotlin.ir.expressions.IrGetVariableExpressionImpl
import org.jetbrains.kotlin.ir.expressions.IrTypeOperator
import org.jetbrains.kotlin.ir.expressions.IrTypeOperatorExpressionImpl
import org.jetbrains.kotlin.ir.expressions.*
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.checker.KotlinTypeChecker
import org.jetbrains.kotlin.types.isNullabilityFlexible
fun IrExpression.toExpectedType(expectedType: KotlinType?): IrExpression {
if (expectedType == null) return this
val valueType = type ?: throw AssertionError("expectedType != null, valueType == null: $this")
if (KotlinTypeChecker.DEFAULT.isSubtypeOf(valueType, expectedType)) {
return this
if (valueType.isNullabilityFlexible() && !expectedType.isMarkedNullable) {
return IrUnaryOperatorExpressionImpl(startOffset, endOffset, expectedType,
IrOperator.IMPLICIT_NOTNULL, null, this)
}
return IrTypeOperatorExpressionImpl(
startOffset, endOffset, expectedType,
IrTypeOperator.IMPLICIT_CAST, expectedType,
this
)
if (!KotlinTypeChecker.DEFAULT.isSubtypeOf(valueType, expectedType)) {
return IrTypeOperatorExpressionImpl(startOffset, endOffset, expectedType,
IrTypeOperator.IMPLICIT_CAST, expectedType, this)
}
return this
}
fun IrVariable.load(): IrExpression =
@@ -21,6 +21,8 @@ interface IrOperator {
object EXCL : IrOperatorImpl("EXCL"), IrUnaryOperator
object EXCLEXCL : IrOperatorImpl("EXCLEXCL"), IrUnaryOperator
object IMPLICIT_NOTNULL : IrOperatorImpl("IMPLICIT_NOTNULL"), IrUnaryOperator
object ELVIS : IrOperatorImpl("ELVIS"), IrBinaryOperator
object LT : IrOperatorImpl("LT"), IrBinaryOperator
@@ -38,8 +40,6 @@ interface IrOperator {
object ANDAND : IrOperatorImpl("ANDAND"), IrBinaryOperator
object OROR : IrOperatorImpl("OROR"), IrBinaryOperator
object PLUS : IrOperatorImpl("PLUS"), IrBinaryOperator
object MINUS : IrOperatorImpl("MINUS"), IrBinaryOperator
object MUL : IrOperatorImpl("MUL"), IrBinaryOperator