FIR IDE: forbid analysis session to be stored in a variable

This commit is contained in:
Ilya Kirillov
2020-08-06 11:17:51 +03:00
parent b41a5f9f34
commit ccf232eaab
2 changed files with 8 additions and 0 deletions

View File

@@ -9,6 +9,7 @@ import org.jetbrains.kotlin.diagnostics.Diagnostic
import org.jetbrains.kotlin.idea.frontend.api.scopes.KtScopeProvider
import org.jetbrains.kotlin.idea.frontend.api.symbols.*
import org.jetbrains.kotlin.idea.frontend.api.types.KtType
import org.jetbrains.kotlin.idea.frontend.api.types.KtTypeRenderer
import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.psi.*
@@ -17,6 +18,7 @@ import org.jetbrains.kotlin.psi.*
* - Should not be accessed from event dispatch thread
* - Should not be accessed outside read action
* - Should not be leaked outside read action it was created in
* - To be sure that session is not leaked it is forbidden to store it in a variable, consider working with it only in [analyze] context
* - All entities retrieved from analysis facade should not be leaked outside the read action KtAnalysisSession was created in
*
* To pass a symbol from one read action to another use [KtSymbolPointer] which can be created from a symbol by [KtSymbol.createPointer]

View File

@@ -13,9 +13,15 @@ abstract class KtAnalysisSessionProvider {
abstract fun getAnalysisSessionFor(contextElement: KtElement): KtAnalysisSession
}
@Suppress("DeprecatedCallableAddReplaceWith")
@Deprecated(
"Should not be directly used as it is not allowed to store KtAnalysisSession in a variable, consider using `analyze` instead",
level = DeprecationLevel.WARNING,
)
fun getAnalysisSessionFor(contextElement: KtElement): KtAnalysisSession =
contextElement.project.service<KtAnalysisSessionProvider>().getAnalysisSessionFor(contextElement)
@Suppress("DEPRECATION")
inline fun <R> analyze(contextElement: KtElement, action: KtAnalysisSession.() -> R): R =
getAnalysisSessionFor(contextElement).action()