[FIR] Don't remove subsumed members from intersection overrides's overriddens

This fixes a bunch of missing overridden symbols in IR.
This is also required for fixing KT-59921 in the following commit
where we need to keep all overridden symbols of intersection overrides
so that we can enhance them properly.

#KT-57300 Fixed
#KT-57299 Fixed
#KT-59921
#KT-57300
#KT-62788
#KT-64271
#KT-64382
This commit is contained in:
Kirill Rakhman
2024-01-17 09:59:17 +01:00
committed by Space Team
parent d80dee6e1c
commit 3b841dcb98
58 changed files with 899 additions and 530 deletions
@@ -17,14 +17,12 @@ import org.jetbrains.kotlin.analysis.api.symbols.KtClassOrObjectSymbol
import org.jetbrains.kotlin.analysis.api.symbols.KtSymbol
import org.jetbrains.kotlin.analysis.api.symbols.KtSymbolOrigin
import org.jetbrains.kotlin.analysis.api.symbols.KtValueParameterSymbol
import org.jetbrains.kotlin.fir.FirSession
import org.jetbrains.kotlin.fir.declarations.*
import org.jetbrains.kotlin.fir.declarations.utils.superConeTypes
import org.jetbrains.kotlin.fir.scopes.*
import org.jetbrains.kotlin.fir.symbols.FirBasedSymbol
import org.jetbrains.kotlin.fir.symbols.impl.FirCallableSymbol
import org.jetbrains.kotlin.fir.symbols.impl.FirClassSymbol
import org.jetbrains.kotlin.fir.symbols.impl.FirIntersectionOverrideFunctionSymbol
import org.jetbrains.kotlin.fir.symbols.impl.FirIntersectionOverridePropertySymbol
import org.jetbrains.kotlin.fir.symbols.impl.*
import org.jetbrains.kotlin.fir.symbols.lazyResolveToPhase
import org.jetbrains.kotlin.fir.types.toRegularClassSymbol
import org.jetbrains.kotlin.fir.unwrapFakeOverrides
@@ -36,15 +34,24 @@ internal class KtFirSymbolDeclarationOverridesProvider(
override fun <T : KtSymbol> getAllOverriddenSymbols(
callableSymbol: T,
): List<KtCallableSymbol> {
require(callableSymbol is KtFirSymbol<*>)
if (callableSymbol is KtFirBackingFieldSymbol) return emptyList()
if (callableSymbol is KtValueParameterSymbol) {
return callableSymbol.getAllOverriddenSymbols()
}
(callableSymbol.firSymbol as? FirIntersectionCallableSymbol)?.let { intersectionSymbol ->
return intersectionSymbol.intersections.flatMap {
getAllOverriddenSymbols(analysisSession.firSymbolBuilder.callableBuilder.buildCallableSymbol(it))
}
}
val overriddenElement = mutableSetOf<FirCallableSymbol<*>>()
processOverrides(callableSymbol) { firTypeScope, firCallableDeclaration ->
firTypeScope.processAllOverriddenDeclarations(firCallableDeclaration) { overriddenDeclaration ->
overriddenDeclaration.symbol.collectIntersectionOverridesSymbolsTo(overriddenElement)
overriddenDeclaration.symbol.collectIntersectionOverridesSymbolsTo(
overriddenElement,
callableSymbol.analysisSession.useSiteSession
)
}
}
@@ -52,15 +59,22 @@ internal class KtFirSymbolDeclarationOverridesProvider(
}
override fun <T : KtSymbol> getDirectlyOverriddenSymbols(callableSymbol: T): List<KtCallableSymbol> {
require(callableSymbol is KtFirSymbol<*>)
if (callableSymbol is KtFirBackingFieldSymbol) return emptyList()
if (callableSymbol is KtValueParameterSymbol) {
return callableSymbol.getDirectlyOverriddenSymbols()
}
if (callableSymbol is KtCallableSymbol && callableSymbol.firSymbol is FirIntersectionCallableSymbol) {
return getIntersectionOverriddenSymbols(callableSymbol)
}
val overriddenElement = mutableSetOf<FirCallableSymbol<*>>()
processOverrides(callableSymbol) { firTypeScope, firCallableDeclaration ->
firTypeScope.processDirectOverriddenDeclarations(firCallableDeclaration) { overriddenDeclaration ->
overriddenDeclaration.symbol.collectIntersectionOverridesSymbolsTo(overriddenElement)
overriddenDeclaration.symbol.collectIntersectionOverridesSymbolsTo(
overriddenElement,
callableSymbol.analysisSession.useSiteSession
)
}
}
@@ -139,13 +153,13 @@ internal class KtFirSymbolDeclarationOverridesProvider(
process(firTypeScope, firCallableDeclaration)
}
private fun FirCallableSymbol<*>.collectIntersectionOverridesSymbolsTo(to: MutableCollection<FirCallableSymbol<*>>) {
private fun FirCallableSymbol<*>.collectIntersectionOverridesSymbolsTo(
to: MutableCollection<FirCallableSymbol<*>>,
useSiteSession: FirSession,
) {
when (this) {
is FirIntersectionOverrideFunctionSymbol -> {
intersections.forEach { it.collectIntersectionOverridesSymbolsTo(to) }
}
is FirIntersectionOverridePropertySymbol -> {
intersections.forEach { it.collectIntersectionOverridesSymbolsTo(to) }
is FirIntersectionCallableSymbol -> {
getIntersectionOverriddenSymbols(useSiteSession).forEach { it.collectIntersectionOverridesSymbolsTo(to, useSiteSession) }
}
else -> {
to += this.fir.unwrapFakeOverrides().symbol
@@ -187,22 +201,21 @@ internal class KtFirSymbolDeclarationOverridesProvider(
return false
}
override fun getIntersectionOverriddenSymbols(symbol: KtCallableSymbol): Collection<KtCallableSymbol> {
override fun getIntersectionOverriddenSymbols(symbol: KtCallableSymbol): List<KtCallableSymbol> {
require(symbol is KtFirSymbol<*>)
if (symbol.origin != KtSymbolOrigin.INTERSECTION_OVERRIDE) return emptyList()
return symbol.firSymbol
.getIntersectionOverriddenSymbols()
.getIntersectionOverriddenSymbols(symbol.analysisSession.useSiteSession)
.map { analysisSession.firSymbolBuilder.callableBuilder.buildCallableSymbol(it) }
}
private fun FirBasedSymbol<*>.getIntersectionOverriddenSymbols(): Collection<FirCallableSymbol<*>> {
private fun FirBasedSymbol<*>.getIntersectionOverriddenSymbols(useSiteSession: FirSession): Collection<FirCallableSymbol<*>> {
require(this is FirCallableSymbol<*>) {
"Required FirCallableSymbol but ${this::class} found"
}
return when (this) {
is FirIntersectionOverrideFunctionSymbol -> intersections
is FirIntersectionOverridePropertySymbol -> intersections
is FirIntersectionCallableSymbol -> getNonSubsumedOverriddenSymbols(useSiteSession, analysisSession.getScopeSessionFor(useSiteSession))
else -> listOf(this)
}
}
@@ -939,62 +939,12 @@ KtFunctionSymbol:
KtKotlinPropertySymbol:
annotationsList: []
backingFieldSymbol: KtBackingFieldSymbol:
annotationsList: []
callableIdIfNonLocal: null
contextReceivers: []
isExtension: false
name: field
origin: PROPERTY_BACKING_FIELD
owningProperty: KtKotlinPropertySymbol(kotlin/collections/List.size)
receiverParameter: null
returnType: KtUsualClassType:
annotationsList: []
ownTypeArguments: []
type: kotlin/Int
symbolKind: LOCAL
typeParameters: []
getContainingFileSymbol: null
getContainingJvmClassName: kotlin.collections.List
getContainingModule: KtBinaryModule "Builtins for JVM (1.8)"
deprecationStatus: null
callableIdIfNonLocal: kotlin/collections/List.size
backingFieldSymbol: null
callableIdIfNonLocal: kotlin/collections/MutableList.size
contextReceivers: []
getter: KtPropertyGetterSymbol:
annotationsList: []
callableIdIfNonLocal: null
contextReceivers: []
hasBody: false
hasStableParameterNames: true
isDefault: true
isExtension: false
isInline: false
isOverride: false
modality: ABSTRACT
origin: LIBRARY
receiverParameter: null
returnType: KtUsualClassType:
annotationsList: []
ownTypeArguments: []
type: kotlin/Int
symbolKind: ACCESSOR
typeParameters: []
valueParameters: []
visibility: Public
getDispatchReceiver(): KtUsualClassType:
annotationsList: []
ownTypeArguments: [
KtTypeParameterType:
annotationsList: []
type: E
]
type: kotlin/collections/List<E>
getContainingFileSymbol: null
getContainingJvmClassName: kotlin.collections.List
getContainingModule: KtBinaryModule "Builtins for JVM (1.8)"
deprecationStatus: null
getter: null
hasBackingField: false
hasGetter: true
hasGetter: false
hasSetter: false
initializer: null
isActual: false
@@ -1009,7 +959,7 @@ KtKotlinPropertySymbol:
isVal: true
modality: ABSTRACT
name: size
origin: LIBRARY
origin: INTERSECTION_OVERRIDE
receiverParameter: null
returnType: KtUsualClassType:
annotationsList: []
@@ -1026,9 +976,9 @@ KtKotlinPropertySymbol:
annotationsList: []
type: E
]
type: kotlin/collections/List<E>
type: kotlin/collections/MutableList<E>
getContainingFileSymbol: null
getContainingJvmClassName: kotlin.collections.List
getContainingJvmClassName: kotlin.collections.MutableList
getContainingModule: KtBinaryModule "Builtins for JVM (1.8)"
deprecationStatus: null
getterDeprecationStatus: null
@@ -1038,7 +988,7 @@ KtKotlinPropertySymbol:
KtFunctionSymbol:
annotationsList: []
callableIdIfNonLocal: kotlin/collections/List.isEmpty
callableIdIfNonLocal: kotlin/collections/MutableList.isEmpty
contextReceivers: []
contractEffects: []
hasStableParameterNames: true
@@ -1055,7 +1005,7 @@ KtFunctionSymbol:
isSuspend: false
modality: ABSTRACT
name: isEmpty
origin: LIBRARY
origin: INTERSECTION_OVERRIDE
receiverParameter: null
returnType: KtUsualClassType:
annotationsList: []
@@ -1072,9 +1022,9 @@ KtFunctionSymbol:
annotationsList: []
type: E
]
type: kotlin/collections/List<E>
type: kotlin/collections/MutableList<E>
getContainingFileSymbol: null
getContainingJvmClassName: kotlin.collections.List
getContainingJvmClassName: kotlin.collections.MutableList
getContainingModule: KtBinaryModule "Builtins for JVM (1.8)"
deprecationStatus: null
@@ -1097,7 +1047,7 @@ KtFunctionSymbol:
isSuspend: false
modality: ABSTRACT
name: contains
origin: SUBSTITUTION_OVERRIDE
origin: INTERSECTION_OVERRIDE
receiverParameter: null
returnType: KtUsualClassType:
annotationsList: []
@@ -1118,7 +1068,7 @@ KtFunctionSymbol:
isNoinline: false
isVararg: false
name: element
origin: SUBSTITUTION_OVERRIDE
origin: INTERSECTION_OVERRIDE
receiverParameter: null
returnType: KtTypeParameterType:
annotationsList: []
@@ -1209,7 +1159,7 @@ KtFunctionSymbol:
isSuspend: false
modality: ABSTRACT
name: containsAll
origin: SUBSTITUTION_OVERRIDE
origin: INTERSECTION_OVERRIDE
receiverParameter: null
returnType: KtUsualClassType:
annotationsList: []
@@ -1230,7 +1180,7 @@ KtFunctionSymbol:
isNoinline: false
isVararg: false
name: elements
origin: SUBSTITUTION_OVERRIDE
origin: INTERSECTION_OVERRIDE
receiverParameter: null
returnType: KtUsualClassType:
annotationsList: []
@@ -345,6 +345,12 @@ public class DiagnosticCompilerTestFE10TestdataTestGenerated extends AbstractDia
runTest("compiler/testData/diagnostics/tests/Dollar.kt");
}
@Test
@TestMetadata("duplicateDefaultValuesSubsumedIntersection.kt")
public void testDuplicateDefaultValuesSubsumedIntersection() throws Exception {
runTest("compiler/testData/diagnostics/tests/duplicateDefaultValuesSubsumedIntersection.kt");
}
@Test
@TestMetadata("duplicateDirrectOverriddenCallables.kt")
public void testDuplicateDirrectOverriddenCallables() throws Exception {
@@ -28698,6 +28704,36 @@ public class DiagnosticCompilerTestFE10TestdataTestGenerated extends AbstractDia
runTest("compiler/testData/diagnostics/tests/override/InternalPotentialOverride.kt");
}
@Test
@TestMetadata("intersectionOfAbstractAndOpen.kt")
public void testIntersectionOfAbstractAndOpen() throws Exception {
runTest("compiler/testData/diagnostics/tests/override/intersectionOfAbstractAndOpen.kt");
}
@Test
@TestMetadata("intersectionOfSubstitutedProperties.kt")
public void testIntersectionOfSubstitutedProperties() throws Exception {
runTest("compiler/testData/diagnostics/tests/override/intersectionOfSubstitutedProperties.kt");
}
@Test
@TestMetadata("intersectionOverrideWithSubsumedDifferentType.kt")
public void testIntersectionOverrideWithSubsumedDifferentType() throws Exception {
runTest("compiler/testData/diagnostics/tests/override/intersectionOverrideWithSubsumedDifferentType.kt");
}
@Test
@TestMetadata("intersectionOverridesIntersection.kt")
public void testIntersectionOverridesIntersection() throws Exception {
runTest("compiler/testData/diagnostics/tests/override/intersectionOverridesIntersection.kt");
}
@Test
@TestMetadata("intersectionWithSubsumedWithSubstitution.kt")
public void testIntersectionWithSubsumedWithSubstitution() throws Exception {
runTest("compiler/testData/diagnostics/tests/override/intersectionWithSubsumedWithSubstitution.kt");
}
@Test
@TestMetadata("InvisiblePotentialOverride.kt")
public void testInvisiblePotentialOverride() throws Exception {
@@ -28965,6 +29001,12 @@ public class DiagnosticCompilerTestFE10TestdataTestGenerated extends AbstractDia
runTest("compiler/testData/diagnostics/tests/override/clashesOnInheritance/genericWithUpperBound.kt");
}
@Test
@TestMetadata("intersectionReturnTypeMismatchSubsumed.kt")
public void testIntersectionReturnTypeMismatchSubsumed() throws Exception {
runTest("compiler/testData/diagnostics/tests/override/clashesOnInheritance/intersectionReturnTypeMismatchSubsumed.kt");
}
@Test
@TestMetadata("kt13355.kt")
public void testKt13355() throws Exception {
@@ -345,6 +345,12 @@ public class LLFirPreresolvedReversedDiagnosticCompilerFE10TestDataTestGenerated
runTest("compiler/testData/diagnostics/tests/Dollar.kt");
}
@Test
@TestMetadata("duplicateDefaultValuesSubsumedIntersection.kt")
public void testDuplicateDefaultValuesSubsumedIntersection() throws Exception {
runTest("compiler/testData/diagnostics/tests/duplicateDefaultValuesSubsumedIntersection.kt");
}
@Test
@TestMetadata("duplicateDirrectOverriddenCallables.kt")
public void testDuplicateDirrectOverriddenCallables() throws Exception {
@@ -28698,6 +28704,36 @@ public class LLFirPreresolvedReversedDiagnosticCompilerFE10TestDataTestGenerated
runTest("compiler/testData/diagnostics/tests/override/InternalPotentialOverride.kt");
}
@Test
@TestMetadata("intersectionOfAbstractAndOpen.kt")
public void testIntersectionOfAbstractAndOpen() throws Exception {
runTest("compiler/testData/diagnostics/tests/override/intersectionOfAbstractAndOpen.kt");
}
@Test
@TestMetadata("intersectionOfSubstitutedProperties.kt")
public void testIntersectionOfSubstitutedProperties() throws Exception {
runTest("compiler/testData/diagnostics/tests/override/intersectionOfSubstitutedProperties.kt");
}
@Test
@TestMetadata("intersectionOverrideWithSubsumedDifferentType.kt")
public void testIntersectionOverrideWithSubsumedDifferentType() throws Exception {
runTest("compiler/testData/diagnostics/tests/override/intersectionOverrideWithSubsumedDifferentType.kt");
}
@Test
@TestMetadata("intersectionOverridesIntersection.kt")
public void testIntersectionOverridesIntersection() throws Exception {
runTest("compiler/testData/diagnostics/tests/override/intersectionOverridesIntersection.kt");
}
@Test
@TestMetadata("intersectionWithSubsumedWithSubstitution.kt")
public void testIntersectionWithSubsumedWithSubstitution() throws Exception {
runTest("compiler/testData/diagnostics/tests/override/intersectionWithSubsumedWithSubstitution.kt");
}
@Test
@TestMetadata("InvisiblePotentialOverride.kt")
public void testInvisiblePotentialOverride() throws Exception {
@@ -28965,6 +29001,12 @@ public class LLFirPreresolvedReversedDiagnosticCompilerFE10TestDataTestGenerated
runTest("compiler/testData/diagnostics/tests/override/clashesOnInheritance/genericWithUpperBound.kt");
}
@Test
@TestMetadata("intersectionReturnTypeMismatchSubsumed.kt")
public void testIntersectionReturnTypeMismatchSubsumed() throws Exception {
runTest("compiler/testData/diagnostics/tests/override/clashesOnInheritance/intersectionReturnTypeMismatchSubsumed.kt");
}
@Test
@TestMetadata("kt13355.kt")
public void testKt13355() throws Exception {