From ea1edc2bd910b1f1f4762d81daca9fbe60a98839 Mon Sep 17 00:00:00 2001 From: Ilya Chernikov Date: Tue, 12 Oct 2021 16:41:53 +0200 Subject: [PATCH] Add equality to diagnostic context, simplifying deduplication --- .../org/jetbrains/kotlin/KtSourceElement.kt | 15 +++++++++++++++ .../kotlin/diagnostics/DiagnosticReporter.kt | 18 ++++++++++++++++++ .../jvm/codegen/JvmSignatureClashDetector.kt | 5 +++-- 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/compiler/frontend.common/src/org/jetbrains/kotlin/KtSourceElement.kt b/compiler/frontend.common/src/org/jetbrains/kotlin/KtSourceElement.kt index fd605c83a89..9fd69b8d64c 100644 --- a/compiler/frontend.common/src/org/jetbrains/kotlin/KtSourceElement.kt +++ b/compiler/frontend.common/src/org/jetbrains/kotlin/KtSourceElement.kt @@ -193,6 +193,21 @@ sealed class KtFakeSourceElementKind : KtSourceElementKind() { sealed class AbstractKtSourceElement { abstract val startOffset: Int abstract val endOffset: Int + override fun equals(other: Any?): Boolean { + if (this === other) return true + if (other !is AbstractKtSourceElement) return false + + if (startOffset != other.startOffset) return false + if (endOffset != other.endOffset) return false + + return true + } + + override fun hashCode(): Int { + var result = startOffset + result = 31 * result + endOffset + return result + } } class KtOffsetsOnlySourceElement( diff --git a/compiler/frontend.common/src/org/jetbrains/kotlin/diagnostics/DiagnosticReporter.kt b/compiler/frontend.common/src/org/jetbrains/kotlin/diagnostics/DiagnosticReporter.kt index e846e75b6ec..fcb00f50f0a 100644 --- a/compiler/frontend.common/src/org/jetbrains/kotlin/diagnostics/DiagnosticReporter.kt +++ b/compiler/frontend.common/src/org/jetbrains/kotlin/diagnostics/DiagnosticReporter.kt @@ -67,5 +67,23 @@ open class KtDiagnosticReporterWithContext( ) { sourceElement?.let { report(factory.on(it, a, positioningStrategy), this) } } + + override fun equals(other: Any?): Boolean { + if (this === other) return true + if (other !is DiagnosticContextImpl) return false + + if (sourceElement != other.sourceElement) return false + if (containingFilePath != other.containingFilePath) return false + if (languageVersionSettings != other.languageVersionSettings) return false + + return true + } + + override fun hashCode(): Int { + var result = sourceElement?.hashCode() ?: 0 + result = 31 * result + containingFilePath.hashCode() + result = 31 * result + languageVersionSettings.hashCode() + return result + } } } \ No newline at end of file diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/JvmSignatureClashDetector.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/JvmSignatureClashDetector.kt index 0ac3b7b8fa2..2213ed93155 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/JvmSignatureClashDetector.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/JvmSignatureClashDetector.kt @@ -147,9 +147,10 @@ class JvmSignatureClashDetector( irDeclarations: Collection, conflictingJvmDeclarationsData: ConflictingJvmDeclarationsData ) { - for (irDeclaration in irDeclarations) { + irDeclarations.mapNotNullTo(LinkedHashSet()) { irDeclaration -> context.ktDiagnosticReporter.atFirstValidFrom(irDeclaration, irClass, containingIrFile = irDeclaration.file) - .report(diagnosticFactory1, conflictingJvmDeclarationsData) + }.forEach { + it.report(diagnosticFactory1, conflictingJvmDeclarationsData) } }