FIR2IR: provide more precise conversion type context at some use-sites

This commit is contained in:
Mikhail Glukhikh
2022-06-29 14:48:43 +02:00
committed by Space
parent 517614a190
commit 2880cd8d5a
10 changed files with 38 additions and 132 deletions
@@ -123,6 +123,9 @@ class ConversionTypeContext internal constructor(
definitelyNotNull = false, origin = ConversionTypeOrigin.DEFAULT, invariantProjection = false
)
internal val WITH_INVARIANT = DEFAULT.withInvariantProjections()
internal val IN_SETTER = ConversionTypeContext(
definitelyNotNull = false, origin = ConversionTypeOrigin.SETTER, invariantProjection = false
)
}
}
@@ -11,6 +11,7 @@ import org.jetbrains.kotlin.fir.declarations.FirProperty
import org.jetbrains.kotlin.fir.declarations.FirPropertyAccessor
import org.jetbrains.kotlin.fir.expressions.FirReturnExpression
import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.util.isSetter
import org.jetbrains.kotlin.ir.util.parentClassOrNull
class Fir2IrConversionScope {
@@ -134,6 +135,9 @@ class Fir2IrConversionScope {
fun parent(): IrDeclarationParent? = parentStack.lastOrNull()
fun defaultConversionTypeContext(): ConversionTypeContext =
if ((parent() as? IrFunction)?.isSetter == true) ConversionTypeContext.IN_SETTER else ConversionTypeContext.DEFAULT
fun dispatchReceiverParameter(irClass: IrClass): IrValueParameter? {
for (function in functionStack.asReversed()) {
if (function.parentClassOrNull == irClass) {
@@ -30,9 +30,13 @@ class Fir2IrImplicitCastInserter(
private val components: Fir2IrComponents
) : Fir2IrComponents by components, FirDefaultVisitor<IrElement, IrElement>() {
private fun FirTypeRef.toIrType(): IrType = with(typeConverter) { toIrType() }
private fun FirTypeRef.toIrType(conversionTypeContext: ConversionTypeContext): IrType = with(typeConverter) {
toIrType(conversionTypeContext)
}
private fun ConeKotlinType.toIrType(): IrType = with(typeConverter) { toIrType() }
private fun ConeKotlinType.toIrType(conversionTypeContext: ConversionTypeContext): IrType = with(typeConverter) {
toIrType(conversionTypeContext)
}
override fun visitElement(element: FirElement, data: IrElement): IrElement {
TODO("Should not be here: ${element::class}: ${element.render()}")
@@ -99,7 +103,7 @@ class Fir2IrImplicitCastInserter(
override fun visitExpression(expression: FirExpression, data: IrElement): IrElement {
return when (expression) {
is FirBlock -> (data as IrContainerExpression).insertImplicitCasts()
is FirUnitExpression -> (data as IrExpression).let { coerceToUnitIfNeeded(it, irBuiltIns) }
is FirUnitExpression -> coerceToUnitIfNeeded(data as IrExpression, irBuiltIns)
else -> data
}
}
@@ -108,7 +112,7 @@ class Fir2IrImplicitCastInserter(
return when (statement) {
is FirTypeAlias -> data
FirStubStatement -> data
is FirUnitExpression -> (data as IrExpression).let { coerceToUnitIfNeeded(it, irBuiltIns) }
is FirUnitExpression -> coerceToUnitIfNeeded(data as IrExpression, irBuiltIns)
is FirBlock -> (data as IrContainerExpression).insertImplicitCasts()
else -> statement.accept(this, data)
}
@@ -207,7 +211,7 @@ class Fir2IrImplicitCastInserter(
}
valueType.coneTypeSafe<ConeDynamicType>() != null -> {
if (expectedType.coneType !is ConeDynamicType && !expectedType.isNullableAny) {
implicitCast(this, expectedType.toIrType())
implicitCast(this, expectedType.toIrType(ConversionTypeContext.DEFAULT))
} else {
this
}
@@ -298,6 +302,7 @@ class Fir2IrImplicitCastInserter(
original: IrExpression,
originalTypeRef: FirTypeRef,
calleeReference: FirReference,
conversionTypeContext: ConversionTypeContext,
): IrExpression {
val referencedDeclaration = (calleeReference.resolvedSymbol as? FirCallableSymbol<*>)?.unwrapCallRepresentative()?.fir
@@ -310,19 +315,23 @@ class Fir2IrImplicitCastInserter(
val castType = originalTypeRef.coneTypeSafe<ConeIntersectionType>()
castType?.intersectedTypes?.forEach { componentType ->
if (AbstractTypeChecker.isSubtypeOf(session.typeContext, componentType, starProjectedDispatchReceiver)) {
return implicitCastOrExpression(original, componentType)
return implicitCastOrExpression(original, componentType, conversionTypeContext)
}
}
return implicitCastOrExpression(original, originalTypeRef)
return implicitCastOrExpression(original, originalTypeRef, conversionTypeContext)
}
private fun implicitCastOrExpression(original: IrExpression, castType: ConeKotlinType): IrExpression {
return implicitCastOrExpression(original, castType.toIrType())
private fun implicitCastOrExpression(
original: IrExpression, castType: ConeKotlinType, conversionTypeContext: ConversionTypeContext = ConversionTypeContext.DEFAULT
): IrExpression {
return implicitCastOrExpression(original, castType.toIrType(conversionTypeContext))
}
private fun implicitCastOrExpression(original: IrExpression, castType: FirTypeRef): IrExpression {
return implicitCastOrExpression(original, castType.toIrType())
private fun implicitCastOrExpression(
original: IrExpression, castType: FirTypeRef, conversionTypeContext: ConversionTypeContext = ConversionTypeContext.DEFAULT
): IrExpression {
return implicitCastOrExpression(original, castType.toIrType(conversionTypeContext))
}
internal fun implicitCastOrExpression(original: IrExpression, castType: IrType): IrExpression {
@@ -672,7 +672,10 @@ class Fir2IrVisitor(
}?.run {
if (expression is FirQualifiedAccessExpression && expression.calleeReference is FirSuperReference) return@run this
implicitCastInserter.implicitCastFromDispatchReceiver(this, expression.typeRef, calleeReference)
implicitCastInserter.implicitCastFromDispatchReceiver(
this, expression.typeRef, calleeReference,
conversionScope.defaultConversionTypeContext()
)
}
}
@@ -59,10 +59,11 @@ class CallAndReferenceGenerator(
private val approximator = ConeTypeApproximator(session.typeContext, session.languageVersionSettings)
private val adapterGenerator = AdapterGenerator(components, conversionScope)
private fun FirTypeRef.toIrType(): IrType = with(typeConverter) { toIrType() }
private fun FirTypeRef.toIrType(): IrType =
with(typeConverter) { toIrType(conversionScope.defaultConversionTypeContext()) }
private fun ConeKotlinType.toIrType(conversionTypeContext: ConversionTypeContext = ConversionTypeContext.DEFAULT): IrType =
with(typeConverter) { toIrType(conversionTypeContext) }
private fun ConeKotlinType.toIrType(): IrType =
with(typeConverter) { toIrType(conversionScope.defaultConversionTypeContext()) }
fun convertToIrCallableReference(
callableReferenceAccess: FirCallableReferenceAccess,
@@ -75,6 +75,6 @@ FILE fqName:<root> fileName:/genericDelegatedProperty.kt
CALL 'public final fun setValue (thisRef: kotlin.Any?, kProp: kotlin.Any?, newValue: kotlin.Int): kotlin.Unit [operator] declared in <root>.Delegate' type=kotlin.Unit origin=null
$this: GET_FIELD 'FIELD PROPERTY_DELEGATE name:genericDelegatedProperty$delegate type:<root>.Delegate visibility:private [final,static]' type=<root>.Delegate origin=null
thisRef: GET_VAR '<this>: <root>.C<T of <root>.<set-genericDelegatedProperty>> declared in <root>.<set-genericDelegatedProperty>' type=<root>.C<T of <root>.<set-genericDelegatedProperty>> origin=null
kProp: PROPERTY_REFERENCE 'public final genericDelegatedProperty: kotlin.Int [delegated,var]' field=null getter='public final fun <get-genericDelegatedProperty> <T> (): kotlin.Int declared in <root>' setter='public final fun <set-genericDelegatedProperty> <T> (<set-?>: kotlin.Int): kotlin.Unit declared in <root>' type=kotlin.reflect.KMutableProperty1<<root>.C<T of <root>.<get-genericDelegatedProperty>>, kotlin.Int> origin=PROPERTY_REFERENCE_FOR_DELEGATE
kProp: PROPERTY_REFERENCE 'public final genericDelegatedProperty: kotlin.Int [delegated,var]' field=null getter='public final fun <get-genericDelegatedProperty> <T> (): kotlin.Int declared in <root>' setter='public final fun <set-genericDelegatedProperty> <T> (<set-?>: kotlin.Int): kotlin.Unit declared in <root>' type=kotlin.reflect.KMutableProperty1<<root>.C<T of <root>.<set-genericDelegatedProperty>>, kotlin.Int> origin=PROPERTY_REFERENCE_FOR_DELEGATE
<1>: <none>
newValue: GET_VAR '<set-?>: kotlin.Int declared in <root>.<set-genericDelegatedProperty>' type=kotlin.Int origin=null
@@ -181,7 +181,7 @@ FILE fqName:<root> fileName:/genericPropertyRef.kt
CALL 'public final fun <set-recivier> (<set-?>: kotlin.Any?): kotlin.Unit declared in <root>' type=kotlin.Unit origin=EQ
<set-?>: GET_VAR '<this>: T of <root>.<set-bar> declared in <root>.<set-bar>' type=T of <root>.<set-bar> origin=null
CALL 'public final fun <set-value2> (<set-?>: kotlin.Any?): kotlin.Unit declared in <root>' type=kotlin.Unit origin=EQ
<set-?>: GET_VAR 'value: T of <root>.<set-bar> declared in <root>.<set-bar>' type=T of <root>.<get-bar> origin=null
<set-?>: GET_VAR 'value: T of <root>.<set-bar> declared in <root>.<set-bar>' type=T of <root>.<set-bar> origin=null
PROPERTY name:barRef visibility:public modality:FINAL [val]
FIELD PROPERTY_BACKING_FIELD name:barRef type:kotlin.reflect.KMutableProperty1<kotlin.String?, kotlin.String?> visibility:private [final,static]
EXPRESSION_BODY
@@ -1,81 +0,0 @@
FILE fqName:<root> fileName:/genericPropertyReferenceType.kt
CLASS CLASS name:C modality:FINAL visibility:public superTypes:[kotlin.Any]
$this: VALUE_PARAMETER INSTANCE_RECEIVER name:<this> type:<root>.C<T of <root>.C>
TYPE_PARAMETER name:T index:0 variance: superTypes:[kotlin.Any?] reified:false
CONSTRUCTOR visibility:public <> (x:T of <root>.C) returnType:<root>.C<T of <root>.C> [primary]
VALUE_PARAMETER name:x index:0 type:T of <root>.C
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]'
PROPERTY name:x visibility:public modality:FINAL [var]
FIELD PROPERTY_BACKING_FIELD name:x type:T of <root>.C visibility:private
EXPRESSION_BODY
GET_VAR 'x: T of <root>.C declared in <root>.C.<init>' type=T of <root>.C origin=INITIALIZE_PROPERTY_FROM_PARAMETER
FUN DEFAULT_PROPERTY_ACCESSOR name:<get-x> visibility:public modality:FINAL <> ($this:<root>.C<T of <root>.C>) returnType:T of <root>.C
correspondingProperty: PROPERTY name:x visibility:public modality:FINAL [var]
$this: VALUE_PARAMETER name:<this> type:<root>.C<T of <root>.C>
BLOCK_BODY
RETURN type=kotlin.Nothing from='public final fun <get-x> (): T of <root>.C declared in <root>.C'
GET_FIELD 'FIELD PROPERTY_BACKING_FIELD name:x type:T of <root>.C visibility:private' type=T of <root>.C origin=null
receiver: GET_VAR '<this>: <root>.C<T of <root>.C> declared in <root>.C.<get-x>' type=<root>.C<T of <root>.C> origin=null
FUN DEFAULT_PROPERTY_ACCESSOR name:<set-x> visibility:public modality:FINAL <> ($this:<root>.C<T of <root>.C>, <set-?>:T of <root>.C) returnType:kotlin.Unit
correspondingProperty: PROPERTY name:x visibility:public modality:FINAL [var]
$this: VALUE_PARAMETER name:<this> type:<root>.C<T of <root>.C>
VALUE_PARAMETER name:<set-?> index:0 type:T of <root>.C
BLOCK_BODY
SET_FIELD 'FIELD PROPERTY_BACKING_FIELD name:x type:T of <root>.C visibility:private' type=kotlin.Unit origin=null
receiver: GET_VAR '<this>: <root>.C<T of <root>.C> declared in <root>.C.<set-x>' type=<root>.C<T of <root>.C> origin=null
value: GET_VAR '<set-?>: T of <root>.C declared in <root>.C.<set-x>' type=T of <root>.C origin=null
FUN FAKE_OVERRIDE name:equals visibility:public modality:OPEN <> ($this:kotlin.Any, other:kotlin.Any?) returnType:kotlin.Boolean [fake_override,operator]
overridden:
public open fun equals (other: kotlin.Any?): kotlin.Boolean [operator] 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 [fake_override]
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 [fake_override]
overridden:
public open fun toString (): kotlin.String declared in kotlin.Any
$this: VALUE_PARAMETER name:<this> type:kotlin.Any
PROPERTY name:y visibility:public modality:FINAL [var]
FUN name:<get-y> visibility:public modality:FINAL <T> ($receiver:<root>.C<T of <root>.<get-y>>) returnType:T of <root>.<get-y>
correspondingProperty: PROPERTY name:y visibility:public modality:FINAL [var]
TYPE_PARAMETER name:T index:0 variance: superTypes:[kotlin.Any?] reified:false
$receiver: VALUE_PARAMETER name:<this> type:<root>.C<T of <root>.<get-y>>
BLOCK_BODY
RETURN type=kotlin.Nothing from='public final fun <get-y> <T> (): T of <root>.<get-y> declared in <root>'
CALL 'public final fun <get-x> (): T of <root>.C declared in <root>.C' type=T of <root>.<get-y> origin=GET_PROPERTY
$this: GET_VAR '<this>: <root>.C<T of <root>.<get-y>> declared in <root>.<get-y>' type=<root>.C<T of <root>.<get-y>> origin=null
FUN name:<set-y> visibility:public modality:FINAL <T> ($receiver:<root>.C<T of <root>.<set-y>>, v:T of <root>.<set-y>) returnType:kotlin.Unit
correspondingProperty: PROPERTY name:y visibility:public modality:FINAL [var]
TYPE_PARAMETER name:T index:0 variance: superTypes:[kotlin.Any?] reified:false
$receiver: VALUE_PARAMETER name:<this> type:<root>.C<T of <root>.<set-y>>
VALUE_PARAMETER name:v index:0 type:T of <root>.<set-y>
BLOCK_BODY
CALL 'public final fun <set-x> (<set-?>: T of <root>.C): kotlin.Unit declared in <root>.C' type=kotlin.Unit origin=EQ
$this: GET_VAR '<this>: <root>.C<T of <root>.<set-y>> declared in <root>.<set-y>' type=<root>.C<T of <root>.<set-y>> origin=null
<set-?>: GET_VAR 'v: T of <root>.<set-y> declared in <root>.<set-y>' type=T of <root>.<get-y> origin=null
FUN name:use visibility:public modality:FINAL <> (p:kotlin.reflect.KMutableProperty<kotlin.String>) returnType:kotlin.Unit
VALUE_PARAMETER name:p index:0 type:kotlin.reflect.KMutableProperty<kotlin.String>
BLOCK_BODY
FUN name:test1 visibility:public modality:FINAL <> () returnType:kotlin.Unit
BLOCK_BODY
CALL 'public final fun use (p: kotlin.reflect.KMutableProperty<kotlin.String>): kotlin.Unit declared in <root>' type=kotlin.Unit origin=null
p: PROPERTY_REFERENCE 'public final y: T of <root>.<get-y> [var]' field=null getter='public final fun <get-y> <T> (): T of <root>.<get-y> declared in <root>' setter='public final fun <set-y> <T> (v: T of <root>.<set-y>): kotlin.Unit declared in <root>' type=kotlin.reflect.KMutableProperty0<kotlin.String> origin=null
<1>: kotlin.String
$receiver: CONSTRUCTOR_CALL 'public constructor <init> (x: T of <root>.C) [primary] declared in <root>.C' type=<root>.C<kotlin.String> origin=null
<class: T>: kotlin.String
x: CONST String type=kotlin.String value="abc"
FUN name:test2 visibility:public modality:FINAL <> (a:kotlin.Any) returnType:kotlin.Unit
VALUE_PARAMETER name:a index:0 type:kotlin.Any
BLOCK_BODY
TYPE_OP type=kotlin.Unit origin=IMPLICIT_COERCION_TO_UNIT typeOperand=kotlin.Unit
TYPE_OP type=<root>.C<kotlin.String> origin=CAST typeOperand=<root>.C<kotlin.String>
GET_VAR 'a: kotlin.Any declared in <root>.test2' type=kotlin.Any origin=null
CALL 'public final fun use (p: kotlin.reflect.KMutableProperty<kotlin.String>): kotlin.Unit declared in <root>' type=kotlin.Unit origin=null
p: PROPERTY_REFERENCE 'public final y: T of <root>.<get-y> [var]' field=null getter='public final fun <get-y> <T> (): T of <root>.<get-y> declared in <root>' setter='public final fun <set-y> <T> (v: T of <root>.<set-y>): kotlin.Unit declared in <root>' type=kotlin.reflect.KMutableProperty0<kotlin.String> origin=null
<1>: kotlin.String
$receiver: TYPE_OP type=<root>.C<kotlin.String> origin=IMPLICIT_CAST typeOperand=<root>.C<kotlin.String>
GET_VAR 'a: kotlin.Any declared in <root>.test2' type=kotlin.Any origin=null
@@ -1,34 +0,0 @@
class C<T : Any?> {
constructor(x: T) /* primary */ {
super/*Any*/()
/* <init>() */
}
var x: T
field = x
get
set
}
var <T : Any?> C<T>.y: T
get(): T {
return <this>.<get-x>()
}
set(v: T) {
<this>.<set-x>(<set-?> = v)
}
fun use(p: KMutableProperty<String>) {
}
fun test1() {
use(p = C<String>(x = "abc")::y/*<String>()*/)
}
fun test2(a: Any) {
a as C<String> /*~> Unit */
use(p = a /*as C<String> */::y/*<String>()*/)
}
@@ -1,3 +1,4 @@
// FIR_IDENTICAL
import kotlin.reflect.KMutableProperty
class C<T>(var x: T)