[IR] Fix mutable shared reference of MFVC type

Signed-off-by: Evgeniy.Zhelenskiy <Evgeniy.Zhelenskiy@jetbrains.com>

#KT-1179
This commit is contained in:
Evgeniy.Zhelenskiy
2023-01-28 01:44:01 +01:00
committed by Space Team
parent 752d6505e9
commit 19424702e0
8 changed files with 78 additions and 10 deletions
@@ -894,7 +894,11 @@ internal class JvmMultiFieldValueClassLowering(
context.createJvmIrBuilder(currentScope.symbol, expression).irBlock {
val rootNode = replacements.getRootMfvcNode(function.constructedClass)
val instance = rootNode.createInstanceFromValueDeclarationsAndBoxType(
this, function.constructedClassType as IrSimpleType, Name.identifier("constructor_tmp"), ::variablesSaver
scope = this,
type = function.constructedClassType as IrSimpleType,
name = Name.identifier("constructor_tmp"),
saveVariable = ::variablesSaver,
isVar = false,
)
for (valueDeclaration in instance.valueDeclarations) {
valueDeclaration.origin = JvmLoweredDeclarationOrigin.TEMPORARY_MULTI_FIELD_VALUE_CLASS_VARIABLE
@@ -1175,7 +1179,7 @@ internal class JvmMultiFieldValueClassLowering(
val rootNode = replacements.getRootMfvcNode(irClass)
return context.createJvmIrBuilder(getCurrentScopeSymbol(), declaration).irBlock {
val instance = rootNode.createInstanceFromValueDeclarationsAndBoxType(
this, declaration.type as IrSimpleType, declaration.name, ::variablesSaver
this, declaration.type as IrSimpleType, declaration.name, ::variablesSaver, declaration.isVar
)
valueDeclarationsRemapper.registerReplacement(declaration, instance)
initializer?.let {
@@ -1201,7 +1205,8 @@ internal class JvmMultiFieldValueClassLowering(
savableStandaloneVariable(
type = it.type.substitute(typeArguments),
origin = IrDeclarationOrigin.IR_TEMPORARY_VARIABLE,
saveVariable = ::variablesSaver
saveVariable = ::variablesSaver,
isVar = false,
)
}
val instance = ValueDeclarationMfvcNodeInstance(rootMfvcNode, typeArguments, variables)
@@ -48,18 +48,19 @@ fun MfvcNode.createInstanceFromBox(
createInstanceFromBox(scope, makeTypeArgumentsFromType(receiver.type as IrSimpleType), receiver, accessType, saveVariable)
fun MfvcNode.createInstanceFromValueDeclarationsAndBoxType(
scope: IrBuilderWithScope, type: IrSimpleType, name: Name, saveVariable: (IrVariable) -> Unit,
): ValueDeclarationMfvcNodeInstance = createInstanceFromValueDeclarations(scope, makeTypeArgumentsFromType(type), name, saveVariable)
scope: IrBuilderWithScope, type: IrSimpleType, name: Name, saveVariable: (IrVariable) -> Unit, isVar: Boolean
): ValueDeclarationMfvcNodeInstance = createInstanceFromValueDeclarations(scope, makeTypeArgumentsFromType(type), name, saveVariable, isVar)
fun MfvcNode.createInstanceFromValueDeclarations(
scope: IrBuilderWithScope, typeArguments: TypeArguments, name: Name, saveVariable: (IrVariable) -> Unit,
scope: IrBuilderWithScope, typeArguments: TypeArguments, name: Name, saveVariable: (IrVariable) -> Unit, isVar: Boolean
): ValueDeclarationMfvcNodeInstance {
val valueDeclarations = mapLeaves {
scope.savableStandaloneVariable(
type = it.type,
name = listOf(name, it.fullFieldName).joinToString("-"),
origin = JvmLoweredDeclarationOrigin.MULTI_FIELD_VALUE_CLASS_REPRESENTATION_VARIABLE,
saveVariable = saveVariable
saveVariable = saveVariable,
isVar = isVar,
)
}
return ValueDeclarationMfvcNodeInstance(this, typeArguments, valueDeclarations)
@@ -241,7 +241,7 @@ fun IrContainerExpression.unwrapBlock(): IrExpression = statements.singleOrNull(
fun IrBuilderWithScope.savableStandaloneVariable(
type: IrType,
name: String? = null,
isMutable: Boolean = false,
isVar: Boolean,
origin: IrDeclarationOrigin,
isTemporary: Boolean = origin == IrDeclarationOrigin.IR_TEMPORARY_VARIABLE
|| origin == JvmLoweredDeclarationOrigin.TEMPORARY_MULTI_FIELD_VALUE_CLASS_VARIABLE
@@ -249,7 +249,7 @@ fun IrBuilderWithScope.savableStandaloneVariable(
saveVariable: (IrVariable) -> Unit,
): IrVariable {
val variable = if (isTemporary || name == null) scope.createTemporaryVariableDeclaration(
type, name, isMutable,
type, name, isVar,
startOffset = startOffset,
endOffset = endOffset,
origin = origin,
@@ -260,7 +260,7 @@ fun IrBuilderWithScope.savableStandaloneVariable(
symbol = IrVariableSymbolImpl(),
name = Name.identifier(name),
type = type,
isVar = isMutable,
isVar = isVar,
isConst = false,
isLateinit = false
).apply {