From 6e0eba7e001c94666345af5b962caee7c4331c16 Mon Sep 17 00:00:00 2001 From: Ilya Kirillov Date: Mon, 9 May 2022 21:42:01 +0200 Subject: [PATCH] [low level api] fix ISE: Expected FirResolvedTypeRef with ConeKotlinType but was FirImplicitTypeRefImpl for CheckDslScopeViolation --- .../fir/declarations/FirAnnotationUtils.kt | 4 ++++ .../fir/resolve/calls/ResolutionStages.kt | 17 +++++++---------- .../kotlin/fir/symbols/FirBasedSymbol.kt | 6 ++++++ 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/compiler/fir/providers/src/org/jetbrains/kotlin/fir/declarations/FirAnnotationUtils.kt b/compiler/fir/providers/src/org/jetbrains/kotlin/fir/declarations/FirAnnotationUtils.kt index 51cbc1a2784..f380f219ee5 100644 --- a/compiler/fir/providers/src/org/jetbrains/kotlin/fir/declarations/FirAnnotationUtils.kt +++ b/compiler/fir/providers/src/org/jetbrains/kotlin/fir/declarations/FirAnnotationUtils.kt @@ -102,6 +102,10 @@ fun FirDeclaration.hasAnnotation(classId: ClassId): Boolean { return annotations.any { it.toAnnotationClassId() == classId } } +fun FirBasedSymbol<*>.hasAnnotation(classId: ClassId): Boolean { + return resolvedAnnotationsWithClassIds.any { it.toAnnotationClassId() == classId } +} + fun FirBasedSymbol.getAnnotationByClassId(classId: ClassId): FirAnnotation? where D : FirAnnotationContainer, D : FirDeclaration { return fir.getAnnotationByClassId(classId) } diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/ResolutionStages.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/ResolutionStages.kt index 1b1c5661e7d..0da9dbee300 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/ResolutionStages.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/ResolutionStages.kt @@ -14,6 +14,7 @@ import org.jetbrains.kotlin.fir.expressions.* import org.jetbrains.kotlin.fir.languageVersionSettings import org.jetbrains.kotlin.fir.matchingParameterFunctionType import org.jetbrains.kotlin.fir.references.FirSuperReference +import org.jetbrains.kotlin.fir.resolve.dfa.symbol import org.jetbrains.kotlin.fir.resolve.directExpansionType import org.jetbrains.kotlin.fir.resolve.fullyExpandedType import org.jetbrains.kotlin.fir.resolve.inference.ConeTypeParameterBasedTypeVariable @@ -25,7 +26,6 @@ import org.jetbrains.kotlin.fir.scopes.FirTypeScope import org.jetbrains.kotlin.fir.scopes.FirUnstableSmartcastTypeScope import org.jetbrains.kotlin.fir.scopes.ProcessorAction import org.jetbrains.kotlin.fir.scopes.processOverriddenFunctions -import org.jetbrains.kotlin.fir.symbols.SymbolInternals import org.jetbrains.kotlin.fir.symbols.SyntheticSymbol import org.jetbrains.kotlin.fir.symbols.ensureResolved import org.jetbrains.kotlin.fir.symbols.impl.* @@ -152,7 +152,6 @@ private class ReceiverDescription( ) object CheckDispatchReceiver : ResolutionStage() { - @OptIn(SymbolInternals::class) override suspend fun check(candidate: Candidate, callInfo: CallInfo, sink: CheckerSink, context: ResolutionContext) { val explicitReceiverExpression = callInfo.explicitReceiver if (explicitReceiverExpression.isSuperCall()) { @@ -363,7 +362,6 @@ object CheckDslScopeViolation : ResolutionStage() { } } - @OptIn(SymbolInternals::class) private fun MutableSet.collectDslMarkerAnnotations(context: ResolutionContext, type: ConeKotlinType) { collectDslMarkerAnnotations(context, type.attributes.customAnnotations) when (type) { @@ -379,15 +377,15 @@ object CheckDslScopeViolation : ResolutionStage() { is ConeDefinitelyNotNullType -> collectDslMarkerAnnotations(context, type.original) is ConeIntersectionType -> type.intersectedTypes.forEach { collectDslMarkerAnnotations(context, it) } is ConeClassLikeType -> { - val classDeclaration = type.toSymbol(context.session)?.fir ?: return - collectDslMarkerAnnotations(context, classDeclaration.annotations) + val classDeclaration = type.toSymbol(context.session) ?: return + collectDslMarkerAnnotations(context, classDeclaration.resolvedAnnotationsWithClassIds) when (classDeclaration) { - is FirClass -> { - for (superType in classDeclaration.superConeTypes) { + is FirClassSymbol -> { + for (superType in classDeclaration.resolvedSuperTypes) { collectDslMarkerAnnotations(context, superType) } } - is FirTypeAlias -> { + is FirTypeAliasSymbol -> { type.directExpansionType(context.session)?.let { collectDslMarkerAnnotations(context, it) } @@ -398,11 +396,10 @@ object CheckDslScopeViolation : ResolutionStage() { } } - @OptIn(SymbolInternals::class) private fun MutableSet.collectDslMarkerAnnotations(context: ResolutionContext, annotations: Collection) { for (annotation in annotations) { val annotationClass = - annotation.annotationTypeRef.coneType.fullyExpandedType(context.session).toSymbol(context.session)?.fir as? FirClass + annotation.annotationTypeRef.coneType.fullyExpandedType(context.session).toSymbol(context.session) as? FirClassSymbol ?: continue if (annotationClass.hasAnnotation(dslMarkerClassId)) { add(annotationClass.classId) diff --git a/compiler/fir/tree/src/org/jetbrains/kotlin/fir/symbols/FirBasedSymbol.kt b/compiler/fir/tree/src/org/jetbrains/kotlin/fir/symbols/FirBasedSymbol.kt index 7f367ae27ee..781e5ff4c71 100644 --- a/compiler/fir/tree/src/org/jetbrains/kotlin/fir/symbols/FirBasedSymbol.kt +++ b/compiler/fir/tree/src/org/jetbrains/kotlin/fir/symbols/FirBasedSymbol.kt @@ -45,6 +45,12 @@ abstract class FirBasedSymbol { return fir.annotations } + val resolvedAnnotationsWithClassIds: List + get() { + ensureResolved(FirResolvePhase.TYPES) + return fir.annotations + } + val resolvedAnnotationClassIds: List get() { ensureResolved(FirResolvePhase.TYPES)