Avoid using containingKtFile in PerFileAnalysisCache

Relates to #EA-230153
This commit is contained in:
Vladimir Dolzhenko
2020-04-16 21:47:06 +02:00
committed by Vladimir Dolzhenko
parent a53a12b68a
commit 061bc9af07
@@ -43,6 +43,7 @@ import org.jetbrains.kotlin.storage.guarded
import org.jetbrains.kotlin.types.KotlinType import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.util.slicedMap.ReadOnlySlice import org.jetbrains.kotlin.util.slicedMap.ReadOnlySlice
import org.jetbrains.kotlin.util.slicedMap.WritableSlice import org.jetbrains.kotlin.util.slicedMap.WritableSlice
import org.jetbrains.kotlin.utils.checkWithAttachment
import java.util.* import java.util.*
import java.util.concurrent.locks.ReentrantLock import java.util.concurrent.locks.ReentrantLock
@@ -60,8 +61,18 @@ internal class PerFileAnalysisCache(val file: KtFile, componentProvider: Compone
ProgressIndicatorProvider.checkCanceled() ProgressIndicatorProvider.checkCanceled()
} }
private fun check(element: KtElement) {
checkWithAttachment(element.containingFile == file, {
"Expected $file, but was ${element.containingFile} for ${if (element.isValid) "valid" else "invalid"} $element "
}) {
it.withAttachment("element.kt", element.text)
it.withAttachment("file.kt", element.containingFile.text)
it.withAttachment("original.kt", file.text)
}
}
internal fun fetchAnalysisResults(element: KtElement): AnalysisResult? { internal fun fetchAnalysisResults(element: KtElement): AnalysisResult? {
assert(element.containingKtFile == file) { "Wrong file. Expected $file, but was ${element.containingKtFile}" } check(element)
if (lock.tryLock()) { if (lock.tryLock()) {
try { try {
@@ -76,9 +87,9 @@ internal class PerFileAnalysisCache(val file: KtFile, componentProvider: Compone
} }
internal fun getAnalysisResults(element: KtElement): AnalysisResult { internal fun getAnalysisResults(element: KtElement): AnalysisResult {
assert(element.containingKtFile == file) { "Wrong file. Expected $file, but was ${element.containingKtFile}" } check(element)
val analyzableParent = KotlinResolveDataProvider.findAnalyzableParent(element) val analyzableParent = KotlinResolveDataProvider.findAnalyzableParent(element) ?: return AnalysisResult.EMPTY
return guardLock.guarded { return guardLock.guarded {
// step 1: perform incremental analysis IF it is applicable // step 1: perform incremental analysis IF it is applicable
@@ -359,7 +370,7 @@ private object KotlinResolveDataProvider {
KtTypeAlias::class.java KtTypeAlias::class.java
) )
fun findAnalyzableParent(element: KtElement): KtElement { fun findAnalyzableParent(element: KtElement): KtElement? {
if (element is KtFile) return element if (element is KtFile) return element
val topmostElement = KtPsiUtil.getTopmostParentOfTypes(element, *topmostElementTypes) as KtElement? val topmostElement = KtPsiUtil.getTopmostParentOfTypes(element, *topmostElementTypes) as KtElement?
@@ -381,7 +392,7 @@ private object KotlinResolveDataProvider {
// if none of the above worked, take the outermost declaration // if none of the above worked, take the outermost declaration
?: PsiTreeUtil.getTopmostParentOfType(element, KtDeclaration::class.java) ?: PsiTreeUtil.getTopmostParentOfType(element, KtDeclaration::class.java)
// if even that didn't work, take the whole file // if even that didn't work, take the whole file
?: element.containingKtFile ?: element.containingFile as? KtFile
} }
fun analyze( fun analyze(
@@ -407,7 +418,7 @@ private object KotlinResolveDataProvider {
allowSliceRewrite = true allowSliceRewrite = true
) )
val moduleInfo = analyzableElement.containingKtFile.getModuleInfo() val moduleInfo = analyzableElement.containingFile.getModuleInfo()
val targetPlatform = moduleInfo.platform val targetPlatform = moduleInfo.platform