提交 d9271b54 编写于 作者: D Dmitry Petrov

Fix IR generation: temporary variable in 'hashCode()' for data class should be 'var'

上级 514fc029
......@@ -48,6 +48,12 @@ fun <T : IrElement> IrStatementsBuilder<T>.defineTemporary(value: IrExpression,
return temporary.descriptor
}
fun <T : IrElement> IrStatementsBuilder<T>.defineTemporaryVar(value: IrExpression, nameHint: String? = null): VariableDescriptor {
val temporary = scope.createTemporaryVariable(value, nameHint, isMutable = true)
+temporary
return temporary.descriptor
}
fun IrBuilderWithScope.irReturn(value: IrExpression) =
IrReturnImpl(startOffset, endOffset, context.builtIns.nothingType, scope.assertCastOwner(), value)
......
......@@ -103,7 +103,7 @@ class DataClassMembersGenerator(
override fun generateHashCodeMethod(function: FunctionDescriptor, properties: List<PropertyDescriptor>) {
buildMember(function) {
val result = defineTemporary(irInt(0), "result")
val result = defineTemporaryVar(irInt(0), "result")
var first = true
for (property in properties) {
val hashCodeOfProperty = getHashCodeOfProperty(irThis(), property)
......
......@@ -30,18 +30,18 @@ class Scope(val scopeOwner: DeclarationDescriptor) {
private var lastTemporaryIndex: Int = 0
private fun nextTemporaryIndex(): Int = lastTemporaryIndex++
private fun createDescriptorForTemporaryVariable(type: KotlinType, nameHint: String? = null): IrTemporaryVariableDescriptor =
IrTemporaryVariableDescriptorImpl(scopeOwner, Name.identifier(getNameForTemporary(nameHint)), type)
private fun createDescriptorForTemporaryVariable(type: KotlinType, nameHint: String? = null, isMutable: Boolean = false): IrTemporaryVariableDescriptor =
IrTemporaryVariableDescriptorImpl(scopeOwner, Name.identifier(getNameForTemporary(nameHint)), type, isMutable)
private fun getNameForTemporary(nameHint: String?): String {
val index = nextTemporaryIndex()
return if (nameHint != null) "tmp${index}_$nameHint" else "tmp$index"
}
fun createTemporaryVariable(irExpression: IrExpression, nameHint: String? = null): IrVariable =
fun createTemporaryVariable(irExpression: IrExpression, nameHint: String? = null, isMutable: Boolean = false): IrVariable =
IrVariableImpl(
irExpression.startOffset, irExpression.endOffset, IrDeclarationOrigin.IR_TEMPORARY_VARIABLE,
createDescriptorForTemporaryVariable(irExpression.type, nameHint),
createDescriptorForTemporaryVariable(irExpression.type, nameHint, isMutable),
irExpression
)
}
......@@ -29,7 +29,8 @@ interface IrTemporaryVariableDescriptor : VariableDescriptor
class IrTemporaryVariableDescriptorImpl(
containingDeclaration: DeclarationDescriptor,
name: Name,
outType: KotlinType
outType: KotlinType,
private val isMutable: Boolean = false
): VariableDescriptorImpl(containingDeclaration, Annotations.EMPTY, name, outType, SourceElement.NO_SOURCE),
IrTemporaryVariableDescriptor
{
......@@ -41,7 +42,7 @@ class IrTemporaryVariableDescriptorImpl(
throw UnsupportedOperationException("Temporary variable descriptor shouldn't be substituted (so far): $this")
}
override fun isVar(): Boolean = false
override fun isVar(): Boolean = isMutable
override fun <R, D> accept(visitor: DeclarationDescriptorVisitor<R, D>, data: D): R =
visitor.visitVariableDescriptor(this, data)
......
......@@ -72,7 +72,7 @@ FILE /dataClasses.kt
CONST String type=kotlin.String value=')'
FUN GENERATED_DATA_CLASS_MEMBER public open override fun hashCode(): kotlin.Int
BLOCK_BODY
VAR IR_TEMPORARY_VARIABLE val tmp0_result: kotlin.Int
VAR IR_TEMPORARY_VARIABLE var tmp0_result: kotlin.Int
CONST Int type=kotlin.Int value='0'
SET_VAR 'tmp0_result: Int' type=kotlin.Unit origin=EQ
CALL 'hashCode(): Int' type=kotlin.Int origin=null
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册