FIR: create constructors copies with correct containers in JvmMappedScope

#KT-49133 Fixed
This commit is contained in:
Mikhail Glukhikh
2021-10-19 16:10:10 +03:00
committed by TeamCityServer
parent a2b9c2bd78
commit 7b9ac4c5f7
5 changed files with 42 additions and 10 deletions
+1 -1
View File
@@ -1674,7 +1674,7 @@ KtConstructorSymbol:
annotationClassIds: []
annotations: []
callableIdIfNonLocal: null
containingClassIdIfNonLocal: java/lang/Integer
containingClassIdIfNonLocal: kotlin/Int
dispatchType: null
hasStableParameterNames: false
isExtension: false
@@ -7,6 +7,7 @@ package org.jetbrains.kotlin.fir.scopes.jvm
import org.jetbrains.kotlin.builtins.jvm.JvmBuiltInsSignatures
import org.jetbrains.kotlin.fir.FirSession
import org.jetbrains.kotlin.fir.containingClassForStaticMemberAttr
import org.jetbrains.kotlin.fir.declarations.FirClass
import org.jetbrains.kotlin.fir.declarations.FirRegularClass
import org.jetbrains.kotlin.fir.declarations.utils.classId
@@ -27,17 +28,19 @@ import org.jetbrains.kotlin.name.Name
class JvmMappedScope(
private val session: FirSession,
private val firKotlinClass: FirClass,
private val firJavaClass: FirRegularClass,
firJavaClass: FirRegularClass,
private val declaredMemberScope: FirContainingNamesAwareScope,
private val javaMappedClassUseSiteScope: FirTypeScope,
private val signatures: Signatures
) : FirTypeScope() {
private val functionsCache = mutableMapOf<FirNamedFunctionSymbol, FirNamedFunctionSymbol>()
private val constructorsCache = mutableMapOf<FirConstructorSymbol, FirConstructorSymbol>()
private val substitutor = ConeSubstitutorByMap(
firJavaClass.typeParameters.zip(firKotlinClass.typeParameters).map { (javaParameter, kotlinParameter) ->
firJavaClass.typeParameters.zip(firKotlinClass.typeParameters).associate { (javaParameter, kotlinParameter) ->
javaParameter.symbol to ConeTypeParameterTypeImpl(ConeTypeParameterLookupTag(kotlinParameter.symbol), isNullable = false)
}.toMap(),
},
session
)
private val kotlinDispatchReceiverType = firKotlinClass.defaultType()
@@ -98,16 +101,43 @@ class JvmMappedScope(
javaMappedClassUseSiteScope.processDeclaredConstructors { symbol ->
val jvmSignature = symbol.fir.computeJvmDescriptor()
if (jvmSignature !in hiddenConstructors) {
processor(symbol)
val newSymbol = getOrCreateCopy(symbol)
processor(newSymbol)
}
}
} else {
javaMappedClassUseSiteScope.processDeclaredConstructors(processor)
javaMappedClassUseSiteScope.processDeclaredConstructors { symbol ->
val newSymbol = getOrCreateCopy(symbol)
processor(newSymbol)
}
}
declaredMemberScope.processDeclaredConstructors(processor)
}
private fun getOrCreateCopy(symbol: FirConstructorSymbol): FirConstructorSymbol {
return constructorsCache.getOrPut(symbol) {
val oldConstructor = symbol.fir
val classId = firKotlinClass.classId
val newSymbol = FirConstructorSymbol(CallableId(classId, classId.shortClassName))
FirFakeOverrideGenerator.createCopyForFirConstructor(
newSymbol,
session,
oldConstructor,
symbol.fir.origin,
newDispatchReceiverType = null,
newReturnType = substitutor.substituteOrSelf(oldConstructor.returnTypeRef.coneType),
newParameterTypes = oldConstructor.valueParameters.map { substitutor.substituteOrSelf(it.returnTypeRef.coneType) },
newTypeParameters = null,
isExpect = false,
fakeOverrideSubstitution = null
).apply {
containingClassForStaticMemberAttr = firKotlinClass.symbol.toLookupTag()
}
newSymbol
}
}
override fun processDirectOverriddenPropertiesWithBaseScope(
propertySymbol: FirPropertySymbol,
processor: (FirPropertySymbol, FirTypeScope) -> ProcessorAction
@@ -189,10 +189,11 @@ class FirClassSubstitutionScope(
return original
}
return FirFakeOverrideGenerator.createSubstitutionOverrideConstructor(
return FirFakeOverrideGenerator.createCopyForFirConstructor(
FirConstructorSymbol(original.callableId),
session,
constructor,
FirDeclarationOrigin.SubstitutionOverride,
newDispatchReceiverType,
newReturnType,
newParameterTypes,
@@ -118,10 +118,11 @@ object FirFakeOverrideGenerator {
}
}
fun createSubstitutionOverrideConstructor(
fun createCopyForFirConstructor(
fakeOverrideSymbol: FirConstructorSymbol,
session: FirSession,
baseConstructor: FirConstructor,
origin: FirDeclarationOrigin,
newDispatchReceiverType: ConeKotlinType?,
newReturnType: ConeKotlinType?,
newParameterTypes: List<ConeKotlinType?>?,
@@ -133,7 +134,7 @@ object FirFakeOverrideGenerator {
// As second alternative, we can invent some light-weight kind of FirRegularClass
return buildConstructor {
moduleData = session.moduleData
origin = FirDeclarationOrigin.SubstitutionOverride
this.origin = origin
receiverTypeRef = baseConstructor.receiverTypeRef?.withReplacedConeType(null)
status = baseConstructor.status.copy(isExpect)
symbol = fakeOverrideSymbol
@@ -27,7 +27,7 @@ interface A2 : List<String> {
override fun stream(): java.util.stream.Stream<String> = null!!
}
class B : <!NONE_APPLICABLE!>Throwable<!>("", null, false, false)
class B : Throwable("", null, false, false)
class B1 : RuntimeException() {
override fun fillInStackTrace(): Throwable { // 'override' keyword must be prohibited, as it was in 1.0.x