[IR] Make IrFunction.returnType lateinit

to replace custom logic that would throw in case of IrUninitialized.

This is to remove custom logic from those classes,
so that source generator will be able to generate
them in a straight-forward way.
Otherwise, it would need to be thought about those special cases, which
is better to be avoided.

There are two consequences:
- The error message will be more generic, and won't contain reference
to the element.
- It becomes possible to reassign returnType
property with IrUninitializedType
and read it afterward.

^KT-65773 In Progress
This commit is contained in:
Wojciech Litewka
2024-02-21 15:06:56 +01:00
committed by Space Team
parent 92c56d0535
commit a005f2e75d
4 changed files with 24 additions and 30 deletions
@@ -26,7 +26,6 @@ class IrConstructorImpl @IrImplementationDetail constructor(
override val symbol: IrConstructorSymbol,
override var name: Name,
override var visibility: DescriptorVisibility,
returnType: IrType,
override var isInline: Boolean,
override var isExternal: Boolean,
override var isPrimary: Boolean,
@@ -41,13 +40,6 @@ class IrConstructorImpl @IrImplementationDetail constructor(
override lateinit var parent: IrDeclarationParent
override var annotations: List<IrConstructorCall> = emptyList()
override var returnType: IrType = returnType
get() = if (field === IrUninitializedType) {
throw ReturnTypeIsNotInitializedException(this)
} else {
field
}
override var typeParameters: List<IrTypeParameter> = emptyList()
override var dispatchReceiverParameter: IrValueParameter? = null
@@ -55,6 +47,8 @@ class IrConstructorImpl @IrImplementationDetail constructor(
override var contextReceiverParametersCount: Int = 0
override var valueParameters: List<IrValueParameter> = emptyList()
override lateinit var returnType: IrType
override var body: IrBody? = null
override var metadata: MetadataSource? = null
@@ -15,6 +15,7 @@ import org.jetbrains.kotlin.ir.expressions.impl.IrBlockBodyImpl
import org.jetbrains.kotlin.ir.expressions.impl.IrExpressionBodyImpl
import org.jetbrains.kotlin.ir.symbols.*
import org.jetbrains.kotlin.ir.types.IrType
import org.jetbrains.kotlin.ir.types.impl.IrUninitializedType
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedContainerSource
import org.jetbrains.kotlin.types.Variance
@@ -83,9 +84,13 @@ abstract class AbstractIrFactoryImpl : IrFactory {
containerSource: DeserializedContainerSource?,
): IrConstructor =
IrConstructorImpl(
startOffset, endOffset, origin, symbol, name, visibility, returnType, isInline, isExternal, isPrimary, isExpect,
startOffset, endOffset, origin, symbol, name, visibility, isInline, isExternal, isPrimary, isExpect,
containerSource, factory = this
)
).apply {
if (returnType != IrUninitializedType) {
this.returnType = returnType
}
}
override fun createEnumEntry(
startOffset: Int,
@@ -137,10 +142,14 @@ abstract class AbstractIrFactoryImpl : IrFactory {
isFakeOverride: Boolean,
): IrSimpleFunction =
IrFunctionImpl(
startOffset, endOffset, origin, symbol, name, visibility, modality, returnType,
startOffset, endOffset, origin, symbol, name, visibility, modality,
isInline, isExternal, isTailrec, isSuspend, isOperator, isInfix, isExpect, isFakeOverride,
containerSource, factory = this
)
).apply {
if (returnType != IrUninitializedType) {
this.returnType = returnType
}
}
override fun createFunctionWithLateBinding(
startOffset: Int,
@@ -166,7 +175,6 @@ abstract class AbstractIrFactoryImpl : IrFactory {
name = name,
visibility = visibility,
modality = modality,
returnType = returnType,
isInline = isInline,
isExternal = isExternal,
isTailrec = isTailrec,
@@ -176,7 +184,11 @@ abstract class AbstractIrFactoryImpl : IrFactory {
isExpect = isExpect,
isFakeOverride = isFakeOverride,
factory = this
)
).apply {
if (returnType != IrUninitializedType) {
this.returnType = returnType
}
}
override fun createLocalDelegatedProperty(
startOffset: Int,
@@ -32,7 +32,6 @@ class IrFunctionImpl @IrImplementationDetail constructor(
override var name: Name,
override var visibility: DescriptorVisibility,
override var modality: Modality,
returnType: IrType,
override var isInline: Boolean,
override var isExternal: Boolean,
override var isTailrec: Boolean,
@@ -55,13 +54,6 @@ class IrFunctionImpl @IrImplementationDetail constructor(
override lateinit var parent: IrDeclarationParent
override var annotations: List<IrConstructorCall> = emptyList()
override var returnType: IrType = returnType
get() = if (field === IrUninitializedType) {
throw ReturnTypeIsNotInitializedException(this)
} else {
field
}
override var typeParameters: List<IrTypeParameter> = emptyList()
override var dispatchReceiverParameter: IrValueParameter? = null
@@ -70,6 +62,8 @@ class IrFunctionImpl @IrImplementationDetail constructor(
override var contextReceiverParametersCount: Int = 0
override lateinit var returnType: IrType
override var body: IrBody? = null
override var metadata: MetadataSource? = null
@@ -28,7 +28,6 @@ class IrFunctionWithLateBindingImpl @IrImplementationDetail constructor(
override var name: Name,
override var visibility: DescriptorVisibility,
override var modality: Modality,
returnType: IrType,
override var isInline: Boolean,
override var isExternal: Boolean,
override var isTailrec: Boolean,
@@ -66,13 +65,6 @@ class IrFunctionWithLateBindingImpl @IrImplementationDetail constructor(
override var annotations: List<IrConstructorCall> = emptyList()
override var returnType: IrType = returnType
get() = if (field === IrUninitializedType) {
throw ReturnTypeIsNotInitializedException(this)
} else {
field
}
override var typeParameters: List<IrTypeParameter> = emptyList()
override var dispatchReceiverParameter: IrValueParameter? = null
@@ -81,6 +73,8 @@ class IrFunctionWithLateBindingImpl @IrImplementationDetail constructor(
override var contextReceiverParametersCount: Int = 0
override lateinit var returnType: IrType
override var body: IrBody? = null
override var metadata: MetadataSource? = null