Generate IMPLICIT_NOTNULL for (nullability flexible type) -> (non-null type) implicit conversion.
This commit is contained in:
committed by
Dmitry Petrov
parent
2b6171057e
commit
3614b0a06c
@@ -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
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
fun test(): String =
|
||||
System.getProperty("test")
|
||||
@@ -0,0 +1,8 @@
|
||||
IrFile /implicitCastOnPlatformType.kt
|
||||
IrFunction public fun test(): kotlin.String
|
||||
IrExpressionBody
|
||||
BLOCK type=<no-type> hasResult=false operator=null
|
||||
RETURN type=<no-type>
|
||||
UNARY_OP operator=IMPLICIT_NOTNULL type=kotlin.String related=null
|
||||
CALL .getProperty type=kotlin.String! operator=null
|
||||
p0: LITERAL String type=kotlin.String value='test'
|
||||
@@ -131,6 +131,12 @@ public class IrTextTestCaseGenerated extends AbstractIrTextTestCase {
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("implicitCastOnPlatformType.kt")
|
||||
public void testImplicitCastOnPlatformType() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/ir/irText/implicitCastOnPlatformType.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("in.kt")
|
||||
public void testIn() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/ir/irText/in.kt");
|
||||
|
||||
Reference in New Issue
Block a user