diff --git a/compiler/fir/raw-fir/light-tree2fir/src/org/jetbrains/kotlin/fir/lightTree/converter/DeclarationsConverter.kt b/compiler/fir/raw-fir/light-tree2fir/src/org/jetbrains/kotlin/fir/lightTree/converter/DeclarationsConverter.kt index be02c557e81..9a64c8a20b0 100644 --- a/compiler/fir/raw-fir/light-tree2fir/src/org/jetbrains/kotlin/fir/lightTree/converter/DeclarationsConverter.kt +++ b/compiler/fir/raw-fir/light-tree2fir/src/org/jetbrains/kotlin/fir/lightTree/converter/DeclarationsConverter.kt @@ -2021,12 +2021,7 @@ class DeclarationsConverter( } for (modifierList in allTypeModifiers) { - if (modifierList.annotations.isNotEmpty()) { - val replacementAnnotations = - if (calculatedFirType.annotations.isNotEmpty()) calculatedFirType.annotations + modifierList.annotations - else modifierList.annotations - calculatedFirType.replaceAnnotations(replacementAnnotations) - } + calculatedFirType.replaceAnnotations(calculatedFirType.annotations.smartPlus(modifierList.annotations)) } return calculatedFirType } diff --git a/compiler/fir/raw-fir/light-tree2fir/src/org/jetbrains/kotlin/fir/lightTree/converter/ExpressionsConverter.kt b/compiler/fir/raw-fir/light-tree2fir/src/org/jetbrains/kotlin/fir/lightTree/converter/ExpressionsConverter.kt index 7143e0dfc5e..c7a133410a6 100644 --- a/compiler/fir/raw-fir/light-tree2fir/src/org/jetbrains/kotlin/fir/lightTree/converter/ExpressionsConverter.kt +++ b/compiler/fir/raw-fir/light-tree2fir/src/org/jetbrains/kotlin/fir/lightTree/converter/ExpressionsConverter.kt @@ -451,10 +451,7 @@ class ExpressionsConverter( val result = firExpression ?: buildErrorExpression(null, ConeNotAnnotationContainer("???")) require(result is FirAnnotationContainer) - if (firAnnotationList.isNotEmpty()) { - val replacementAnnotations = if (result.annotations.isNotEmpty()) result.annotations + firAnnotationList else firAnnotationList - result.replaceAnnotations(replacementAnnotations) - } + result.replaceAnnotations(result.annotations.smartPlus(firAnnotationList)) return result } diff --git a/compiler/fir/raw-fir/psi2fir/src/org/jetbrains/kotlin/fir/builder/RawFirBuilder.kt b/compiler/fir/raw-fir/psi2fir/src/org/jetbrains/kotlin/fir/builder/RawFirBuilder.kt index feb12441b30..261c3a03876 100644 --- a/compiler/fir/raw-fir/psi2fir/src/org/jetbrains/kotlin/fir/builder/RawFirBuilder.kt +++ b/compiler/fir/raw-fir/psi2fir/src/org/jetbrains/kotlin/fir/builder/RawFirBuilder.kt @@ -499,12 +499,7 @@ open class RawFirBuilder( if (this != null) { it.extractAnnotationsFrom(this) } - val resultAnnotations = when { - accessorAnnotationsFromProperty.isEmpty() -> it.annotations - it.annotations.isEmpty() -> accessorAnnotationsFromProperty - else -> it.annotations + accessorAnnotationsFromProperty - } - it.replaceAnnotations(resultAnnotations) + it.replaceAnnotations(it.annotations.smartPlus(accessorAnnotationsFromProperty)) it.status = status it.initContainingClassAttr() } @@ -704,7 +699,7 @@ open class RawFirBuilder( val annotations = buildList { addAll(container.annotations) for (annotationEntry in annotationEntries) { - add(annotationEntry.convert()) + add(annotationEntry.convert()) } } container.replaceAnnotations(annotations) diff --git a/compiler/fir/raw-fir/raw-fir.common/src/org/jetbrains/kotlin/fir/builder/BaseFirBuilder.kt b/compiler/fir/raw-fir/raw-fir.common/src/org/jetbrains/kotlin/fir/builder/BaseFirBuilder.kt index 730978a2db6..0fd72a9ece6 100644 --- a/compiler/fir/raw-fir/raw-fir.common/src/org/jetbrains/kotlin/fir/builder/BaseFirBuilder.kt +++ b/compiler/fir/raw-fir/raw-fir.common/src/org/jetbrains/kotlin/fir/builder/BaseFirBuilder.kt @@ -958,13 +958,7 @@ abstract class BaseFirBuilder(val baseSession: FirSession, val context: Conte } return if (operation == FirOperation.ASSIGN) { val result = unwrappedLhs.convert() - if (annotations.isNotEmpty()) { - val annotationsToReplace = if (result.annotations.isEmpty()) annotations else mutableListOf().apply { - addAll(result.annotations) - addAll(annotations) - } - result.replaceAnnotations(annotationsToReplace) - } + result.replaceAnnotations(result.annotations.smartPlus(annotations)) result.pullUpSafeCallIfNecessary() } else { val receiver = unwrappedLhs.convert() diff --git a/compiler/fir/tree/src/org/jetbrains/kotlin/fir/Utils.kt b/compiler/fir/tree/src/org/jetbrains/kotlin/fir/Utils.kt index ac3c53e4a31..661c7d62b98 100644 --- a/compiler/fir/tree/src/org/jetbrains/kotlin/fir/Utils.kt +++ b/compiler/fir/tree/src/org/jetbrains/kotlin/fir/Utils.kt @@ -197,3 +197,14 @@ value class MutableOrEmptyList(val list: MutableList?) : List { fun empty(): MutableOrEmptyList = EMPTY as MutableOrEmptyList } } + +fun List.smartPlus(other: List): List = when { + other.isEmpty() -> this + this.isEmpty() -> other + else -> { + val result = ArrayList(this.size + other.size) + result.addAll(this) + result.addAll(other) + result + } +}