[FIR] Let DeprecationsProvider.getDeprecationsInfo accept LanguageVersionSettings

This commit is contained in:
Kirill Rakhman
2023-08-04 14:56:52 +02:00
committed by Space Team
parent 62b7760c92
commit 65ea9697ab
6 changed files with 31 additions and 30 deletions
@@ -8,6 +8,7 @@ package org.jetbrains.kotlin.fir.analysis.checkers.config
import org.jetbrains.kotlin.config.AnalysisFlags
import org.jetbrains.kotlin.fir.analysis.checkers.context.CheckerContext
import org.jetbrains.kotlin.fir.declarations.getAnnotationByClassId
import org.jetbrains.kotlin.fir.declarations.getOwnDeprecation
import org.jetbrains.kotlin.fir.resolve.providers.symbolProvider
import org.jetbrains.kotlin.fir.resolve.transformers.PackageResolutionResult
import org.jetbrains.kotlin.fir.resolve.transformers.resolveToPackageOrClass
@@ -40,7 +41,7 @@ object FirOptInLanguageVersionSettingsChecker : FirLanguageVersionSettingsChecke
rawReport(false, "Class $fqNameAsString is not an opt-in requirement marker")
return
}
val deprecationInfo = symbol.getOwnDeprecation(context.languageVersionSettings.apiVersion)?.all ?: return
val deprecationInfo = symbol.getOwnDeprecation(context.languageVersionSettings)?.all ?: return
rawReport(
deprecationInfo.deprecationLevel != DeprecationLevelValue.WARNING,
"Opt-in requirement marker $fqNameAsString is deprecated" + deprecationInfo.message?.let { ". $it" }.orEmpty()
@@ -172,10 +172,10 @@ object FirOverrideChecker : FirClassChecker() {
overriddenSymbols: List<FirCallableSymbol<*>>,
context: CheckerContext
) {
val ownDeprecation = this.getDeprecation(context.session.languageVersionSettings.apiVersion)
val ownDeprecation = this.getDeprecation(context.session.languageVersionSettings)
if (ownDeprecation == null || ownDeprecation.isNotEmpty()) return
for (overriddenSymbol in overriddenSymbols) {
val deprecationInfoFromOverridden = overriddenSymbol.getDeprecation(context.session.languageVersionSettings.apiVersion)
val deprecationInfoFromOverridden = overriddenSymbol.getDeprecation(context.session.languageVersionSettings)
?: continue
val deprecationFromOverriddenSymbol = deprecationInfoFromOverridden.all
?: deprecationInfoFromOverridden.bySpecificSite?.values?.firstOrNull()
@@ -6,6 +6,7 @@
package org.jetbrains.kotlin.fir.declarations
import org.jetbrains.kotlin.config.ApiVersion
import org.jetbrains.kotlin.config.LanguageVersionSettings
import org.jetbrains.kotlin.descriptors.annotations.AnnotationUseSiteTarget
import org.jetbrains.kotlin.fir.FirAnnotationContainer
import org.jetbrains.kotlin.fir.FirElement
@@ -91,7 +92,7 @@ private fun FirBasedSymbol<*>.getUseSitesForCallSite(callSite: FirElement?): Arr
* corresponding declaration.
*/
fun FirBasedSymbol<*>.getOwnDeprecation(session: FirSession, callSite: FirElement?): DeprecationInfo? {
return getOwnDeprecationForCallSite(session.languageVersionSettings.apiVersion, *getUseSitesForCallSite(callSite))
return getOwnDeprecationForCallSite(session.languageVersionSettings, *getUseSitesForCallSite(callSite))
}
/**
@@ -180,12 +181,12 @@ fun List<FirAnnotation>.getDeprecationsProviderFromAnnotations(
* corresponding declaration.
*/
private fun FirBasedSymbol<*>.getOwnDeprecationForCallSite(
apiVersion: ApiVersion,
languageVersionSettings: LanguageVersionSettings,
vararg sites: AnnotationUseSiteTarget
): DeprecationInfo? {
val deprecations = when (this) {
is FirCallableSymbol<*> -> getDeprecation(apiVersion)
is FirClassLikeSymbol<*> -> getOwnDeprecation(apiVersion)
is FirCallableSymbol<*> -> getDeprecation(languageVersionSettings)
is FirClassLikeSymbol<*> -> getOwnDeprecation(languageVersionSettings)
else -> null
}
return (deprecations ?: EmptyDeprecationsPerUseSite).forUseSite(*sites)
@@ -201,12 +202,10 @@ fun FirBasedSymbol<*>.getDeprecationForCallSite(
session: FirSession,
vararg sites: AnnotationUseSiteTarget,
): DeprecationInfo? {
val apiVersion = session.languageVersionSettings.apiVersion
return when (this) {
!is FirTypeAliasSymbol -> getOwnDeprecationForCallSite(apiVersion, *sites)
!is FirTypeAliasSymbol -> getOwnDeprecationForCallSite(session.languageVersionSettings, *sites)
else -> {
var worstDeprecationInfo = getOwnDeprecationForCallSite(apiVersion, *sites)
var worstDeprecationInfo = getOwnDeprecationForCallSite(session.languageVersionSettings, *sites)
val visited = mutableMapOf<ConeKotlinType, DeprecationInfo?>()
resolvedExpandedTypeRef.type.forEachType {
@@ -6,6 +6,7 @@
package org.jetbrains.kotlin.fir.declarations
import org.jetbrains.kotlin.config.ApiVersion
import org.jetbrains.kotlin.config.LanguageVersionSettings
import org.jetbrains.kotlin.descriptors.annotations.AnnotationUseSiteTarget
import org.jetbrains.kotlin.fir.caches.FirCache
import org.jetbrains.kotlin.fir.caches.FirCachesFactory
@@ -16,7 +17,7 @@ import org.jetbrains.kotlin.resolve.deprecation.SimpleDeprecationInfo
import org.jetbrains.kotlin.utils.addToStdlib.runUnless
abstract class DeprecationsProvider {
abstract fun getDeprecationsInfo(version: ApiVersion): DeprecationsPerUseSite?
abstract fun getDeprecationsInfo(languageVersionSettings: LanguageVersionSettings): DeprecationsPerUseSite?
}
class DeprecationsProviderImpl(
@@ -24,7 +25,7 @@ class DeprecationsProviderImpl(
private val all: List<DeprecationAnnotationInfo>?,
private val bySpecificSite: Map<AnnotationUseSiteTarget, List<DeprecationAnnotationInfo>>?
) : DeprecationsProvider() {
private val cache: FirCache<ApiVersion, DeprecationsPerUseSite, Nothing?> = firCachesFactory.createCache { version ->
private val cache: FirCache<LanguageVersionSettings, DeprecationsPerUseSite, Nothing?> = firCachesFactory.createCache { version ->
@Suppress("UNCHECKED_CAST")
DeprecationsPerUseSite(
all?.computeDeprecationInfoOrNull(version),
@@ -33,29 +34,29 @@ class DeprecationsProviderImpl(
)
}
override fun getDeprecationsInfo(version: ApiVersion): DeprecationsPerUseSite {
return cache.getValue(version, null)
override fun getDeprecationsInfo(languageVersionSettings: LanguageVersionSettings): DeprecationsPerUseSite {
return cache.getValue(languageVersionSettings, null)
}
private fun List<DeprecationAnnotationInfo>.computeDeprecationInfoOrNull(version: ApiVersion): DeprecationInfo? {
private fun List<DeprecationAnnotationInfo>.computeDeprecationInfoOrNull(version: LanguageVersionSettings): DeprecationInfo? {
return firstNotNullOfOrNull { it.computeDeprecationInfo(version) }
}
}
object EmptyDeprecationsProvider : DeprecationsProvider() {
override fun getDeprecationsInfo(version: ApiVersion): DeprecationsPerUseSite {
override fun getDeprecationsInfo(languageVersionSettings: LanguageVersionSettings): DeprecationsPerUseSite {
return EmptyDeprecationsPerUseSite
}
}
object UnresolvedDeprecationProvider : DeprecationsProvider() {
override fun getDeprecationsInfo(version: ApiVersion): DeprecationsPerUseSite? {
override fun getDeprecationsInfo(languageVersionSettings: LanguageVersionSettings): DeprecationsPerUseSite? {
return null
}
}
sealed interface DeprecationAnnotationInfo {
fun computeDeprecationInfo(apiVersion: ApiVersion): DeprecationInfo?
fun computeDeprecationInfo(languageVersionSettings: LanguageVersionSettings): DeprecationInfo?
}
data class FutureApiDeprecationInfo(
@@ -67,8 +68,8 @@ data class FutureApiDeprecationInfo(
}
class SinceKotlinInfo(val sinceVersion: ApiVersion) : DeprecationAnnotationInfo {
override fun computeDeprecationInfo(apiVersion: ApiVersion): DeprecationInfo? {
return runUnless(sinceVersion <= apiVersion) {
override fun computeDeprecationInfo(languageVersionSettings: LanguageVersionSettings): DeprecationInfo? {
return runUnless(sinceVersion <= languageVersionSettings.apiVersion) {
FutureApiDeprecationInfo(
deprecationLevel = DeprecationLevelValue.HIDDEN,
propagatesToOverrides = true,
@@ -83,7 +84,7 @@ class DeprecatedInfo(
val propagatesToOverride: Boolean,
val message: String?
) : DeprecationAnnotationInfo {
override fun computeDeprecationInfo(apiVersion: ApiVersion): DeprecationInfo {
override fun computeDeprecationInfo(languageVersionSettings: LanguageVersionSettings): DeprecationInfo {
return SimpleDeprecationInfo(
level,
propagatesToOverride,
@@ -99,8 +100,8 @@ class DeprecatedSinceKotlinInfo(
val message: String?,
val propagatesToOverride: Boolean
) : DeprecationAnnotationInfo {
override fun computeDeprecationInfo(apiVersion: ApiVersion): DeprecationInfo? {
fun ApiVersion.takeLevelIfDeprecated(level: DeprecationLevelValue) = level.takeIf { this <= apiVersion }
override fun computeDeprecationInfo(languageVersionSettings: LanguageVersionSettings): DeprecationInfo? {
fun ApiVersion.takeLevelIfDeprecated(level: DeprecationLevelValue) = level.takeIf { this <= languageVersionSettings.apiVersion }
val appliedLevel = hiddenVersion?.takeLevelIfDeprecated(DeprecationLevelValue.HIDDEN)
?: errorVersion?.takeLevelIfDeprecated(DeprecationLevelValue.ERROR)
@@ -5,7 +5,7 @@
package org.jetbrains.kotlin.fir.symbols.impl
import org.jetbrains.kotlin.config.ApiVersion
import org.jetbrains.kotlin.config.LanguageVersionSettings
import org.jetbrains.kotlin.fir.declarations.*
import org.jetbrains.kotlin.fir.symbols.FirBasedSymbol
import org.jetbrains.kotlin.fir.symbols.lazyResolveToPhase
@@ -76,9 +76,9 @@ abstract class FirCallableSymbol<D : FirCallableDeclaration> : FirBasedSymbol<D>
val name: Name
get() = callableId.callableName
fun getDeprecation(apiVersion: ApiVersion): DeprecationsPerUseSite? {
fun getDeprecation(languageVersionSettings: LanguageVersionSettings): DeprecationsPerUseSite? {
lazyResolveToPhase(FirResolvePhase.COMPILER_REQUIRED_ANNOTATIONS)
return fir.deprecationsProvider.getDeprecationsInfo(apiVersion)
return fir.deprecationsProvider.getDeprecationsInfo(languageVersionSettings)
}
private fun ensureType(typeRef: FirTypeRef?) {
@@ -5,7 +5,7 @@
package org.jetbrains.kotlin.fir.symbols.impl
import org.jetbrains.kotlin.config.ApiVersion
import org.jetbrains.kotlin.config.LanguageVersionSettings
import org.jetbrains.kotlin.descriptors.ClassKind
import org.jetbrains.kotlin.fir.declarations.*
import org.jetbrains.kotlin.fir.symbols.ConeClassLikeLookupTag
@@ -29,10 +29,10 @@ sealed class FirClassLikeSymbol<D : FirClassLikeDeclaration>(
val name get() = classId.shortClassName
fun getOwnDeprecation(apiVersion: ApiVersion): DeprecationsPerUseSite? {
fun getOwnDeprecation(languageVersionSettings: LanguageVersionSettings): DeprecationsPerUseSite? {
if (annotations.isEmpty()) return null
lazyResolveToPhase(FirResolvePhase.COMPILER_REQUIRED_ANNOTATIONS)
return fir.deprecationsProvider.getDeprecationsInfo(apiVersion)
return fir.deprecationsProvider.getDeprecationsInfo(languageVersionSettings)
}
val rawStatus: FirDeclarationStatus