K2: introduce & use MutableOrEmptyList.EMPTY singleton

This commit is contained in:
Mikhail Glukhikh
2022-12-15 11:40:52 +01:00
committed by Space Team
parent ddc786364f
commit a48610000d
7 changed files with 18 additions and 11 deletions
@@ -32,7 +32,7 @@ internal class FirErrorLoopImpl(
override val diagnostic: ConeDiagnostic,
) : FirErrorLoop() {
override var block: FirBlock = FirEmptyExpressionBlock()
override var condition: FirExpression = FirErrorExpressionImpl(source, MutableOrEmptyList(), ConeStubDiagnostic(diagnostic), null, null)
override var condition: FirExpression = FirErrorExpressionImpl(source, MutableOrEmptyList.empty(), ConeStubDiagnostic(diagnostic), null, null)
override fun <R, D> acceptChildren(visitor: FirVisitor<R, D>, data: D) {
annotations.forEach { it.accept(visitor, data) }
@@ -14,10 +14,7 @@ import org.jetbrains.kotlin.fir.declarations.impl.FirDeclarationStatusImpl
import org.jetbrains.kotlin.fir.declarations.impl.FirResolvedDeclarationStatusImpl
import org.jetbrains.kotlin.fir.expressions.FirBlock
import org.jetbrains.kotlin.fir.expressions.FirExpression
import org.jetbrains.kotlin.fir.references.FirReference
import org.jetbrains.kotlin.fir.references.FirResolvedNamedReference
import org.jetbrains.kotlin.fir.renderer.FirRenderer
import org.jetbrains.kotlin.fir.symbols.FirBasedSymbol
import org.jetbrains.kotlin.fir.types.*
import org.jetbrains.kotlin.fir.types.builder.*
import org.jetbrains.kotlin.fir.types.impl.*
@@ -146,7 +143,10 @@ inline fun <R> withFileAnalysisExceptionWrapping(file: FirFile, block: () -> R):
}
@JvmInline
value class MutableOrEmptyList<T>(val list: MutableList<T>? = null) : List<T> {
value class MutableOrEmptyList<T>(val list: MutableList<T>?) : List<T> {
private constructor(list: Nothing?) : this(list as MutableList<T>?)
override val size: Int
get() = list?.size ?: 0
@@ -189,4 +189,11 @@ value class MutableOrEmptyList<T>(val list: MutableList<T>? = null) : List<T> {
override fun contains(element: T): Boolean {
return list?.contains(element) ?: false
}
companion object {
private val EMPTY = MutableOrEmptyList<Nothing>(null)
@Suppress("UNCHECKED_CAST")
fun <T> empty(): MutableOrEmptyList<T> = EMPTY as MutableOrEmptyList<T>
}
}
@@ -11,8 +11,8 @@ import org.jetbrains.kotlin.fir.MutableOrEmptyList
annotation class FirBuilderDsl
fun <T> MutableList<T>.toMutableOrEmpty(): MutableOrEmptyList<T> =
if (isEmpty()) MutableOrEmptyList() else MutableOrEmptyList(this)
if (isEmpty()) MutableOrEmptyList.empty() else MutableOrEmptyList(this)
@JvmName("toMutableOrEmptyForImmutable")
fun <T> List<T>.toMutableOrEmpty(): MutableOrEmptyList<T> =
if (isEmpty()) MutableOrEmptyList() else MutableOrEmptyList(this.toMutableList())
if (isEmpty()) MutableOrEmptyList.empty() else MutableOrEmptyList(this.toMutableList())
@@ -61,7 +61,7 @@ abstract class FirDefaultPropertyAccessor(
symbol,
propertySymbol,
isGetter,
annotations = MutableOrEmptyList(),
annotations = MutableOrEmptyList.empty(),
typeParameters = mutableListOf(),
) {
override val dispatchReceiverType: ConeSimpleKotlinType?
@@ -25,7 +25,7 @@ class FirSingleExpressionBlock(
) : FirBlock() {
override val source: KtSourceElement?
get() = statement.source?.fakeElement(KtFakeSourceElementKind.SingleExpressionBlock)
override var annotations: MutableOrEmptyList<FirAnnotation> = MutableOrEmptyList()
override var annotations: MutableOrEmptyList<FirAnnotation> = MutableOrEmptyList.empty()
override val statements: List<FirStatement> get() = listOf(statement)
override var typeRef: FirTypeRef = FirImplicitTypeRefImpl(null)
@@ -32,7 +32,7 @@ internal class FirErrorTypeRefImpl(
isFromStubType
)
override val annotations: MutableOrEmptyList<FirAnnotation> = MutableOrEmptyList()
override val annotations: MutableOrEmptyList<FirAnnotation> = MutableOrEmptyList.empty()
override fun <R, D> acceptChildren(visitor: FirVisitor<R, D>, data: D) {
annotations.forEach { it.accept(visitor, data) }
@@ -195,7 +195,7 @@ object ImplementationConfigurator : AbstractFirTreeImplementationConfigurator()
impl(errorLoop) {
default("block", "FirEmptyExpressionBlock()")
default("condition", "FirErrorExpressionImpl(source, MutableOrEmptyList(), ConeStubDiagnostic(diagnostic), null, null)")
default("condition", "FirErrorExpressionImpl(source, MutableOrEmptyList.empty(), ConeStubDiagnostic(diagnostic), null, null)")
useTypes(emptyExpressionBlock, coneStubDiagnosticType)
}