diff --git a/analysis/analysis-api/testData/memberScopeByFqName/Int.txt b/analysis/analysis-api/testData/memberScopeByFqName/Int.txt index 2f8fff8903b..aa29e648865 100644 --- a/analysis/analysis-api/testData/memberScopeByFqName/Int.txt +++ b/analysis/analysis-api/testData/memberScopeByFqName/Int.txt @@ -1674,7 +1674,7 @@ KtConstructorSymbol: annotationClassIds: [] annotations: [] callableIdIfNonLocal: null - containingClassIdIfNonLocal: java/lang/Integer + containingClassIdIfNonLocal: kotlin/Int dispatchType: null hasStableParameterNames: false isExtension: false diff --git a/compiler/fir/java/src/org/jetbrains/kotlin/fir/scopes/jvm/JvmMappedScope.kt b/compiler/fir/java/src/org/jetbrains/kotlin/fir/scopes/jvm/JvmMappedScope.kt index c2d025e3b44..b8b57f8cbaa 100644 --- a/compiler/fir/java/src/org/jetbrains/kotlin/fir/scopes/jvm/JvmMappedScope.kt +++ b/compiler/fir/java/src/org/jetbrains/kotlin/fir/scopes/jvm/JvmMappedScope.kt @@ -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() + private val constructorsCache = mutableMapOf() + 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 diff --git a/compiler/fir/providers/src/org/jetbrains/kotlin/fir/scopes/impl/FirClassSubstitutionScope.kt b/compiler/fir/providers/src/org/jetbrains/kotlin/fir/scopes/impl/FirClassSubstitutionScope.kt index 0f40974a9d2..b8726d80ac6 100644 --- a/compiler/fir/providers/src/org/jetbrains/kotlin/fir/scopes/impl/FirClassSubstitutionScope.kt +++ b/compiler/fir/providers/src/org/jetbrains/kotlin/fir/scopes/impl/FirClassSubstitutionScope.kt @@ -189,10 +189,11 @@ class FirClassSubstitutionScope( return original } - return FirFakeOverrideGenerator.createSubstitutionOverrideConstructor( + return FirFakeOverrideGenerator.createCopyForFirConstructor( FirConstructorSymbol(original.callableId), session, constructor, + FirDeclarationOrigin.SubstitutionOverride, newDispatchReceiverType, newReturnType, newParameterTypes, diff --git a/compiler/fir/providers/src/org/jetbrains/kotlin/fir/scopes/impl/FirFakeOverrideGenerator.kt b/compiler/fir/providers/src/org/jetbrains/kotlin/fir/scopes/impl/FirFakeOverrideGenerator.kt index 58e067174be..dc8c1599fac 100644 --- a/compiler/fir/providers/src/org/jetbrains/kotlin/fir/scopes/impl/FirFakeOverrideGenerator.kt +++ b/compiler/fir/providers/src/org/jetbrains/kotlin/fir/scopes/impl/FirFakeOverrideGenerator.kt @@ -118,10 +118,11 @@ object FirFakeOverrideGenerator { } } - fun createSubstitutionOverrideConstructor( + fun createCopyForFirConstructor( fakeOverrideSymbol: FirConstructorSymbol, session: FirSession, baseConstructor: FirConstructor, + origin: FirDeclarationOrigin, newDispatchReceiverType: ConeKotlinType?, newReturnType: ConeKotlinType?, newParameterTypes: List?, @@ -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 diff --git a/compiler/testData/diagnostics/testsWithStdLib/targetedBuiltIns/unsupportedFeature.fir.kt b/compiler/testData/diagnostics/testsWithStdLib/targetedBuiltIns/unsupportedFeature.fir.kt index f17f3716cc2..d2258a28a22 100644 --- a/compiler/testData/diagnostics/testsWithStdLib/targetedBuiltIns/unsupportedFeature.fir.kt +++ b/compiler/testData/diagnostics/testsWithStdLib/targetedBuiltIns/unsupportedFeature.fir.kt @@ -27,7 +27,7 @@ interface A2 : List { override fun stream(): java.util.stream.Stream = null!! } -class B : 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