diff --git a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/generators/DataClassMembersGenerator.kt b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/generators/DataClassMembersGenerator.kt index 54861c1e1dd..198b3846b9b 100644 --- a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/generators/DataClassMembersGenerator.kt +++ b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/generators/DataClassMembersGenerator.kt @@ -189,31 +189,27 @@ class DataClassMembersGenerator(val components: Fir2IrComponents) { } else null } + val scope = klass.unsubstitutedScope( + components.session, + components.scopeSession, + withForcedTypeCalculator = true + ) val contributedFunctionsInSupertypes = @OptIn(ExperimentalStdlibApi::class) buildMap { for (name in listOf(EQUALS, HASHCODE_NAME, TO_STRING)) { - klass.unsubstitutedScope( - components.session, - components.scopeSession, - withForcedTypeCalculator = true - ).processFunctionsByName(name) { + // We won't synthesize a function if there is a user-contributed one. + if (contributedFunctionsInThisType.contains(name)) continue + scope.processFunctionsByName(name) { val declaration = it.fir - if (declaration.matchesDataClassSyntheticMemberSignatures) { + if (declaration.matchesDataClassSyntheticMemberSignatures && declaration.modality != Modality.FINAL) { putIfAbsent(declaration.name, declaration) } } } } - fun isOverridableDeclaration(name: Name): Boolean { - val declaration = contributedFunctionsInSupertypes[name] ?: return false - return declaration.modality != Modality.FINAL - } - - if (!contributedFunctionsInThisType.contains(EQUALS) && - isOverridableDeclaration(EQUALS) - ) { + if (contributedFunctionsInSupertypes.containsKey(EQUALS)) { result.add(contributedFunctionsInSupertypes.getValue(EQUALS)) val equalsFunction = createSyntheticIrFunction( EQUALS, @@ -224,9 +220,7 @@ class DataClassMembersGenerator(val components: Fir2IrComponents) { irClass.declarations.add(equalsFunction) } - if (!contributedFunctionsInThisType.contains(HASHCODE_NAME) && - isOverridableDeclaration(HASHCODE_NAME) - ) { + if (contributedFunctionsInSupertypes.containsKey(HASHCODE_NAME)) { result.add(contributedFunctionsInSupertypes.getValue(HASHCODE_NAME)) val hashCodeFunction = createSyntheticIrFunction( HASHCODE_NAME, @@ -236,9 +230,7 @@ class DataClassMembersGenerator(val components: Fir2IrComponents) { irClass.declarations.add(hashCodeFunction) } - if (!contributedFunctionsInThisType.contains(TO_STRING) && - isOverridableDeclaration(TO_STRING) - ) { + if (contributedFunctionsInSupertypes.containsKey(TO_STRING)) { result.add(contributedFunctionsInSupertypes.getValue(TO_STRING)) val toStringFunction = createSyntheticIrFunction( TO_STRING,