From d3670d4f5fd2453e335f28cbacc39b8e3b8b8e2c Mon Sep 17 00:00:00 2001 From: Dmitriy Novozhilov Date: Wed, 24 Jul 2019 12:06:58 +0300 Subject: [PATCH] [FIR] Add `Any?` as default bound for all type parameters --- .../src/org/jetbrains/kotlin/fir/builder/RawFirBuilder.kt | 8 ++++++++ .../kotlin/fir/declarations/impl/FirTypeParameterImpl.kt | 7 ++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/compiler/fir/psi2fir/src/org/jetbrains/kotlin/fir/builder/RawFirBuilder.kt b/compiler/fir/psi2fir/src/org/jetbrains/kotlin/fir/builder/RawFirBuilder.kt index 87518b20415..4b4a0022a03 100644 --- a/compiler/fir/psi2fir/src/org/jetbrains/kotlin/fir/builder/RawFirBuilder.kt +++ b/compiler/fir/psi2fir/src/org/jetbrains/kotlin/fir/builder/RawFirBuilder.kt @@ -444,6 +444,7 @@ class RawFirBuilder(val session: FirSession, val stubMode: Boolean) { val typeParameters = firClass.typeParameters.map { FirTypeParameterImpl(session, it.psi, FirTypeParameterSymbol(), it.name, Variance.INVARIANT, false).apply { this.bounds += it.bounds + addDefaultBoundIfNecessary() } } return FirResolvedTypeRefImpl( @@ -457,6 +458,12 @@ class RawFirBuilder(val session: FirSession, val stubMode: Boolean) { ) } + private fun FirTypeParameterImpl.addDefaultBoundIfNecessary() { + if (bounds.isEmpty()) { + bounds += FirImplicitNullableAnyTypeRef(session, null) + } + } + override fun visitEnumEntry(enumEntry: KtEnumEntry, data: Unit): FirElement { return withChildClassName(enumEntry.nameAsSafeName) { val firEnumEntry = FirEnumEntryImpl( @@ -917,6 +924,7 @@ class RawFirBuilder(val session: FirSession, val stubMode: Boolean) { firTypeParameter.bounds += typeConstraint.boundTypeReference.toFirOrErrorType() } } + firTypeParameter.addDefaultBoundIfNecessary() return firTypeParameter } diff --git a/compiler/fir/tree/src/org/jetbrains/kotlin/fir/declarations/impl/FirTypeParameterImpl.kt b/compiler/fir/tree/src/org/jetbrains/kotlin/fir/declarations/impl/FirTypeParameterImpl.kt index 552e72a73a3..30048a7f9e8 100644 --- a/compiler/fir/tree/src/org/jetbrains/kotlin/fir/declarations/impl/FirTypeParameterImpl.kt +++ b/compiler/fir/tree/src/org/jetbrains/kotlin/fir/declarations/impl/FirTypeParameterImpl.kt @@ -12,6 +12,7 @@ import org.jetbrains.kotlin.fir.declarations.FirTypeParameter import org.jetbrains.kotlin.fir.symbols.impl.FirTypeParameterSymbol import org.jetbrains.kotlin.fir.transformInplace import org.jetbrains.kotlin.fir.types.FirTypeRef +import org.jetbrains.kotlin.fir.types.impl.FirImplicitNullableAnyTypeRef import org.jetbrains.kotlin.fir.visitors.FirTransformer import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.types.Variance @@ -28,7 +29,11 @@ class FirTypeParameterImpl( symbol.bind(this) } - override val bounds = mutableListOf() + /* + * Note that each type parameter have to has at least one upper bound (Any? if there is no other bounds) + * so if you create FirTypeParameterImpl you need to guarantee this contract + */ + override val bounds: MutableList = mutableListOf() override fun transformChildren(transformer: FirTransformer, data: D): FirElement { bounds.transformInplace(transformer, data)