Always check project.useCompositeAnalysis in findAnalyzerServices

The important things here are changes in
ResovlerElementCache/PerFileAnalysisCache. Previously, we'd always use
'CompositeAnalyzerServices' for common-platform, even with HMPP turned
off.

However, with HMPP turned off, common-platform is always coerced to
[JVM, JS, Native], no matter which platforms that project actually
targets.
So, even if project compiles only for JVM/Native, we'll detect
platform of common module as [JVM, JS, Native], build composite analyzer
services *which include all checkers from all platforms*, and then
report false-positive errors from JS checkers.

^KT-35031 Fixed
^KT-33573 Fixed
^KT-34925 Fixed
This commit is contained in:
Dmitry Savvinov
2020-02-21 15:56:32 +03:00
parent e56abcbb85
commit 423aeb9a08
8 changed files with 27 additions and 28 deletions
@@ -36,10 +36,7 @@ import org.jetbrains.kotlin.idea.configuration.BuildSystemType
import org.jetbrains.kotlin.idea.configuration.getBuildSystemType
import org.jetbrains.kotlin.idea.core.isInTestSourceContentKotlinAware
import org.jetbrains.kotlin.idea.framework.getLibraryPlatform
import org.jetbrains.kotlin.idea.project.TargetPlatformDetector
import org.jetbrains.kotlin.idea.project.findAnalyzerServices
import org.jetbrains.kotlin.idea.project.getStableName
import org.jetbrains.kotlin.idea.project.isHMPPEnabled
import org.jetbrains.kotlin.idea.project.*
import org.jetbrains.kotlin.idea.stubindex.KotlinSourceFilterScope
import org.jetbrains.kotlin.idea.util.isInSourceContentWithoutInjected
import org.jetbrains.kotlin.idea.util.rootManager
@@ -170,7 +167,7 @@ interface ModuleSourceInfo : IdeaModuleInfo, TrackableModuleInfo {
fun getPlatform(): org.jetbrains.kotlin.resolve.TargetPlatform = platform.toOldPlatform()
override val analyzerServices: PlatformDependentAnalyzerServices
get() = platform.findAnalyzerServices
get() = platform.findAnalyzerServices(module.project)
override fun createModificationTracker(): ModificationTracker =
KotlinModuleOutOfCodeBlockModificationTracker(module)
@@ -312,7 +309,7 @@ open class LibraryInfo(val project: Project, val library: Library) : IdeaModuleI
get() = getLibraryPlatform(project, library)
override val analyzerServices: PlatformDependentAnalyzerServices
get() = platform.findAnalyzerServices
get() = platform.findAnalyzerServices(project)
override val sourcesModuleInfo: SourceForBinaryModuleInfo
get() = LibrarySourceInfo(project, library, this)
@@ -388,7 +385,7 @@ object NotUnderContentRootModuleInfo : IdeaModuleInfo {
get() = DefaultIdeTargetPlatformKindProvider.defaultPlatform
override val analyzerServices: PlatformDependentAnalyzerServices
get() = platform.findAnalyzerServices
get() = platform.single().findAnalyzerServices()
}
private class LibraryWithoutSourceScope(project: Project, private val library: Library) :
@@ -486,7 +483,7 @@ data class PlatformModuleInfo(
get() = platformModule.moduleOrigin
override val analyzerServices: PlatformDependentAnalyzerServices
get() = platform.findAnalyzerServices
get() = platform.findAnalyzerServices(platformModule.module.project)
override fun dependencies() = platformModule.dependencies()
@@ -113,7 +113,7 @@ class IdeaResolverForProject(
commonPlatformParameters,
jvmPlatformParameters,
platform,
CompositeAnalyzerServices(platform.componentPlatforms.map { it.toTargetPlatform().findAnalyzerServices })
platform.findAnalyzerServices(projectContext.project) as CompositeAnalyzerServices
)
}
}
@@ -28,9 +28,7 @@ import org.jetbrains.kotlin.frontend.di.createContainerForLazyBodyResolve
import org.jetbrains.kotlin.idea.caches.project.getModuleInfo
import org.jetbrains.kotlin.idea.caches.trackers.clearInBlockModifications
import org.jetbrains.kotlin.idea.caches.trackers.inBlockModifications
import org.jetbrains.kotlin.idea.project.IdeaModuleStructureOracle
import org.jetbrains.kotlin.idea.project.findAnalyzerServices
import org.jetbrains.kotlin.idea.project.languageVersionSettings
import org.jetbrains.kotlin.idea.project.*
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.parentsWithSelf
import org.jetbrains.kotlin.resolve.*
@@ -404,7 +402,7 @@ private object KotlinResolveDataProvider {
trace,
targetPlatform,
bodyResolveCache,
targetPlatform.findAnalyzerServices,
targetPlatform.findAnalyzerServices(project),
analyzableElement.languageVersionSettings,
IdeaModuleStructureOracle()
).get<LazyTopDownAnalyzer>()
@@ -700,7 +700,7 @@ class ResolveElementCache(
trace,
targetPlatform,
statementFilter,
targetPlatform.findAnalyzerServices,
targetPlatform.findAnalyzerServices(file.project),
file.languageVersionSettings,
IdeaModuleStructureOracle()
).get()
@@ -5,6 +5,8 @@
package org.jetbrains.kotlin.idea.project
import com.intellij.openapi.project.Project
import org.jetbrains.kotlin.analyzer.common.CommonPlatformAnalyzerServices
import org.jetbrains.kotlin.caches.resolve.CompositeAnalyzerServices
import org.jetbrains.kotlin.js.resolve.JsPlatformAnalyzerServices
import org.jetbrains.kotlin.platform.SimplePlatform
@@ -18,17 +20,17 @@ import org.jetbrains.kotlin.resolve.jvm.platform.JvmPlatformAnalyzerServices
import org.jetbrains.kotlin.resolve.konan.platform.NativePlatformAnalyzerServices
import java.lang.IllegalStateException
val TargetPlatform.findAnalyzerServices: PlatformDependentAnalyzerServices
get() =
when {
isCommon() -> CompositeAnalyzerServices(this.componentPlatforms.map { it.findAnalyzerServices })
else -> single().findAnalyzerServices
}
fun TargetPlatform.findAnalyzerServices(project: Project): PlatformDependentAnalyzerServices = when {
isCommon() && project.useCompositeAnalysis -> CompositeAnalyzerServices(componentPlatforms.map { it.findAnalyzerServices() })
isCommon() && !project.useCompositeAnalysis -> CommonPlatformAnalyzerServices
else -> single().findAnalyzerServices()
}
val SimplePlatform.findAnalyzerServices: PlatformDependentAnalyzerServices
get() = when (this) {
fun SimplePlatform.findAnalyzerServices(): PlatformDependentAnalyzerServices {
return when (this) {
is JvmPlatform -> JvmPlatformAnalyzerServices
is JsPlatform -> JsPlatformAnalyzerServices
is KonanPlatform -> NativePlatformAnalyzerServices
else -> throw IllegalStateException("Unknown platform $this")
}
}
}
@@ -248,7 +248,7 @@ class PsiBasedClassResolver @TestOnly constructor(private val targetClassFqName:
private fun KtFile.getDefaultImports(): List<ImportPath> {
val moduleInfo = getNullableModuleInfo() ?: return emptyList()
return TargetPlatformDetector.getPlatform(this).findAnalyzerServices.getDefaultImports(
return TargetPlatformDetector.getPlatform(this).findAnalyzerServices(project).getDefaultImports(
IDELanguageSettingsProvider.getLanguageVersionSettings(moduleInfo, project),
includeLowPriorityImports = true
)
@@ -133,7 +133,7 @@ object ReplaceWithAnnotationAnalyzer {
languageVersionSettings: LanguageVersionSettings
): List<ImportingScope> {
val allDefaultImports =
resolutionFacade.frontendService<TargetPlatform>().findAnalyzerServices
resolutionFacade.frontendService<TargetPlatform>().findAnalyzerServices(resolutionFacade.project)
.getDefaultImports(languageVersionSettings, includeLowPriorityImports = true)
val (allUnderImports, aliasImports) = allDefaultImports.partition { it.isAllUnder }
// this solution doesn't support aliased default imports with a different alias
@@ -50,7 +50,8 @@ class ImportInsertHelperImpl(private val project: Project) : ImportInsertHelper(
override fun isImportedWithDefault(importPath: ImportPath, contextFile: KtFile): Boolean {
val languageVersionSettings = contextFile.getResolutionFacade().frontendService<LanguageVersionSettings>()
val platform = TargetPlatformDetector.getPlatform(contextFile)
val allDefaultImports = platform.findAnalyzerServices.getDefaultImports(languageVersionSettings, includeLowPriorityImports = true)
val analyzerServices = platform.findAnalyzerServices(contextFile.project)
val allDefaultImports = analyzerServices.getDefaultImports(languageVersionSettings, includeLowPriorityImports = true)
val scriptExtraImports = contextFile.takeIf { it.isScript() }?.let { ktFile ->
val scriptDependencies = ScriptDependenciesProvider.getInstance(ktFile.project)
@@ -58,12 +59,13 @@ class ImportInsertHelperImpl(private val project: Project) : ImportInsertHelper(
scriptDependencies?.defaultImports?.map { ImportPath.fromString(it) }
}.orEmpty()
return importPath.isImported(allDefaultImports + scriptExtraImports, platform.findAnalyzerServices.excludedImports)
return importPath.isImported(allDefaultImports + scriptExtraImports, analyzerServices.excludedImports)
}
override fun isImportedWithLowPriorityDefaultImport(importPath: ImportPath, contextFile: KtFile): Boolean {
val platform = TargetPlatformDetector.getPlatform(contextFile)
return importPath.isImported(platform.findAnalyzerServices.defaultLowPriorityImports, platform.findAnalyzerServices.excludedImports)
val analyzerServices = platform.findAnalyzerServices(contextFile.project)
return importPath.isImported(analyzerServices.defaultLowPriorityImports, analyzerServices.excludedImports)
}
override fun mayImportOnShortenReferences(descriptor: DeclarationDescriptor): Boolean {