From 5741ff5d86e48edda0bfeaf81870bbb9f503d2b7 Mon Sep 17 00:00:00 2001 From: Mikhail Glukhikh Date: Thu, 29 Aug 2019 17:51:19 +0300 Subject: [PATCH] FIR: split default importing scopes into low/high priority ones This fixes resolve of StringBuilder(), AssertionError(), etc. --- .../org/jetbrains/kotlin/fir/scopes/Scopes.kt | 6 ++-- .../fir/scopes/impl/DefaultImportPriority.kt | 32 +++++++++++++++++++ .../impl/FirDefaultSimpleImportingScope.kt | 6 ++-- .../impl/FirDefaultStarImportingScope.kt | 24 ++++++++------ .../testData/resolve/cfg/initBlock.txt | 2 +- .../resolve/cfg/propertiesAndInitBlocks.dot | 22 ++++++------- .../resolve/cfg/propertiesAndInitBlocks.txt | 8 ++--- .../testData/resolve/stdlib/exception.txt | 2 +- .../testData/resolve/stdlib/problems.txt | 6 ++-- .../ir/irText/expressions/kt30796.fir.txt | 2 +- 10 files changed, 75 insertions(+), 35 deletions(-) create mode 100644 compiler/fir/resolve/src/org/jetbrains/kotlin/fir/scopes/impl/DefaultImportPriority.kt diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/scopes/Scopes.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/scopes/Scopes.kt index 2f3f5f75f08..e2ac11a4caf 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/scopes/Scopes.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/scopes/Scopes.kt @@ -24,9 +24,11 @@ import org.jetbrains.kotlin.utils.addToStdlib.safeAs fun MutableList.addImportingScopes(file: FirFile, session: FirSession) { this += listOf( // from low priority to high priority - FirDefaultStarImportingScope(session), + FirDefaultStarImportingScope(session, priority = DefaultImportPriority.LOW), + FirDefaultStarImportingScope(session, priority = DefaultImportPriority.HIGH), FirExplicitStarImportingScope(file.imports, session), - FirDefaultSimpleImportingScope(session), + FirDefaultSimpleImportingScope(session, priority = DefaultImportPriority.LOW), + FirDefaultSimpleImportingScope(session, priority = DefaultImportPriority.HIGH), FirSelfImportingScope(file.packageFqName, session), // TODO: explicit simple importing scope should have highest priority (higher than inner scopes added in process) FirExplicitSimpleImportingScope(file.imports, session) diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/scopes/impl/DefaultImportPriority.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/scopes/impl/DefaultImportPriority.kt new file mode 100644 index 00000000000..ba78e74292c --- /dev/null +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/scopes/impl/DefaultImportPriority.kt @@ -0,0 +1,32 @@ +/* + * Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. + */ + +package org.jetbrains.kotlin.fir.scopes.impl + +import org.jetbrains.kotlin.config.LanguageVersionSettings +import org.jetbrains.kotlin.resolve.ImportPath +import org.jetbrains.kotlin.resolve.PlatformDependentAnalyzerServices + +enum class DefaultImportPriority { + HIGH { + override fun getAllDefaultImports( + platformDependentAnalyzerServices: PlatformDependentAnalyzerServices?, + languageVersionSettings: LanguageVersionSettings + ): List? = + platformDependentAnalyzerServices?.getDefaultImports(languageVersionSettings, includeLowPriorityImports = false) + }, + LOW { + override fun getAllDefaultImports( + platformDependentAnalyzerServices: PlatformDependentAnalyzerServices?, + languageVersionSettings: LanguageVersionSettings + ): List? = + platformDependentAnalyzerServices?.defaultLowPriorityImports + }; + + abstract fun getAllDefaultImports( + platformDependentAnalyzerServices: PlatformDependentAnalyzerServices?, + languageVersionSettings: LanguageVersionSettings + ): List? +} \ No newline at end of file diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/scopes/impl/FirDefaultSimpleImportingScope.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/scopes/impl/FirDefaultSimpleImportingScope.kt index f9feab78751..0ed6f9b5070 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/scopes/impl/FirDefaultSimpleImportingScope.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/scopes/impl/FirDefaultSimpleImportingScope.kt @@ -11,14 +11,16 @@ import org.jetbrains.kotlin.fir.declarations.impl.FirImportImpl import org.jetbrains.kotlin.fir.declarations.impl.FirResolvedImportImpl import org.jetbrains.kotlin.fir.resolve.transformers.FirImportResolveTransformer -class FirDefaultSimpleImportingScope(session: FirSession) : FirAbstractSimpleImportingScope(session) { +class FirDefaultSimpleImportingScope(session: FirSession, priority: DefaultImportPriority) : FirAbstractSimpleImportingScope(session) { private fun FirImportImpl.resolve(importResolveTransformer: FirImportResolveTransformer) = importResolveTransformer.transformImport(this, null).single as FirResolvedImportImpl override val simpleImports = run { val importResolveTransformer = FirImportResolveTransformer(session) - session.moduleInfo?.analyzerServices?.getDefaultImports(LanguageVersionSettingsImpl.DEFAULT, true) + val analyzerServices = session.moduleInfo?.analyzerServices + val allDefaultImports = priority.getAllDefaultImports(analyzerServices, LanguageVersionSettingsImpl.DEFAULT) + allDefaultImports ?.filter { !it.isAllUnder } ?.map { FirImportImpl(null, it.fqName, isAllUnder = false, aliasName = null) diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/scopes/impl/FirDefaultStarImportingScope.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/scopes/impl/FirDefaultStarImportingScope.kt index cd2ffb89efb..94fcd53d85b 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/scopes/impl/FirDefaultStarImportingScope.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/scopes/impl/FirDefaultStarImportingScope.kt @@ -10,17 +10,21 @@ import org.jetbrains.kotlin.fir.FirSession import org.jetbrains.kotlin.fir.declarations.impl.FirImportImpl import org.jetbrains.kotlin.fir.declarations.impl.FirResolvedImportImpl -class FirDefaultStarImportingScope(session: FirSession, lookupInFir: Boolean = false) : +class FirDefaultStarImportingScope(session: FirSession, priority: DefaultImportPriority, lookupInFir: Boolean = false) : FirAbstractStarImportingScope(session, lookupInFir) { // TODO: put languageVersionSettings into FirSession? - override val starImports = session.moduleInfo?.analyzerServices?.getDefaultImports(LanguageVersionSettingsImpl.DEFAULT, true) - ?.filter { it.isAllUnder } - ?.map { - FirResolvedImportImpl( - FirImportImpl(null, it.fqName, isAllUnder = true, aliasName = null), - it.fqName, - null - ) - } ?: emptyList() + override val starImports = run { + val analyzerServices = session.moduleInfo?.analyzerServices + val allDefaultImports = priority.getAllDefaultImports(analyzerServices, LanguageVersionSettingsImpl.DEFAULT) + allDefaultImports + ?.filter { it.isAllUnder } + ?.map { + FirResolvedImportImpl( + FirImportImpl(null, it.fqName, isAllUnder = true, aliasName = null), + it.fqName, + null + ) + } ?: emptyList() + } } diff --git a/compiler/fir/resolve/testData/resolve/cfg/initBlock.txt b/compiler/fir/resolve/testData/resolve/cfg/initBlock.txt index 209507fb919..8f05d8c0fad 100644 --- a/compiler/fir/resolve/testData/resolve/cfg/initBlock.txt +++ b/compiler/fir/resolve/testData/resolve/cfg/initBlock.txt @@ -16,7 +16,7 @@ FILE: initBlock.kt init { lval x: R|kotlin/Int| = Int(1) - throw #() + throw R|java/lang/Exception.Exception|() lval y: R|kotlin/Int| = Int(2) } diff --git a/compiler/fir/resolve/testData/resolve/cfg/propertiesAndInitBlocks.dot b/compiler/fir/resolve/testData/resolve/cfg/propertiesAndInitBlocks.dot index ffd4341a1ec..0dd194da5a7 100644 --- a/compiler/fir/resolve/testData/resolve/cfg/propertiesAndInitBlocks.dot +++ b/compiler/fir/resolve/testData/resolve/cfg/propertiesAndInitBlocks.dot @@ -91,8 +91,8 @@ digraph propertiesAndInitBlocks_kt { 27 [label="Const: Int(1)"]; 28 [label="Function call: Int(1).R|kotlin/Int.plus|(Int(1))"]; 29 [label="Variable declaration: lval c: R|kotlin/Int|"]; - 30 [label="Function call: #()"]; - 31 [label="Throw: throw #()"]; + 30 [label="Function call: R|java/lang/Exception.Exception|()"]; + 31 [label="Throw: throw R|java/lang/Exception.Exception|()"]; 32 [label="Stub" style="filled" fillcolor=gray]; 33 [label="Exit block" style="filled" fillcolor=gray]; } @@ -125,8 +125,8 @@ digraph propertiesAndInitBlocks_kt { subgraph cluster_12 { color=blue 38 [label="Enter block"]; - 39 [label="Function call: #()"]; - 40 [label="Throw: throw #()"]; + 39 [label="Function call: R|java/lang/Exception.Exception|()"]; + 40 [label="Throw: throw R|java/lang/Exception.Exception|()"]; 41 [label="Stub" style="filled" fillcolor=gray]; 42 [label="Exit block" style="filled" fillcolor=gray]; } @@ -168,8 +168,8 @@ digraph propertiesAndInitBlocks_kt { subgraph cluster_17 { color=blue 51 [label="Enter block"]; - 52 [label="Function call: #()"]; - 53 [label="Throw: throw #()"]; + 52 [label="Function call: R|java/lang/Exception.Exception|()"]; + 53 [label="Throw: throw R|java/lang/Exception.Exception|()"]; 54 [label="Stub" style="filled" fillcolor=gray]; 55 [label="Const: Int(1)" style="filled" fillcolor=gray]; 56 [label="Exit block" style="filled" fillcolor=gray]; @@ -185,8 +185,8 @@ digraph propertiesAndInitBlocks_kt { subgraph cluster_20 { color=blue 60 [label="Enter block"]; - 61 [label="Function call: #()"]; - 62 [label="Throw: throw #()"]; + 61 [label="Function call: R|java/lang/Exception.Exception|()"]; + 62 [label="Throw: throw R|java/lang/Exception.Exception|()"]; 63 [label="Stub" style="filled" fillcolor=gray]; 64 [label="Exit block" style="filled" fillcolor=gray]; } @@ -195,7 +195,7 @@ digraph propertiesAndInitBlocks_kt { 66 [label="Function call: R|/run|( = run@fun (): R|kotlin/Unit| { local final fun foo(): R|kotlin/Unit| { lval c: R|kotlin/Int| = Int(1).R|kotlin/Int.plus|(Int(1)) - throw #() + throw R|java/lang/Exception.Exception|() } local final class LocalClass : R|kotlin/Any| { @@ -204,13 +204,13 @@ digraph propertiesAndInitBlocks_kt { } init { - throw #() + throw R|java/lang/Exception.Exception|() Int(1) } } - throw #() + throw R|java/lang/Exception.Exception|() } )" style="filled" fillcolor=gray]; 67 [label="Exit property" style="filled" fillcolor=red]; diff --git a/compiler/fir/resolve/testData/resolve/cfg/propertiesAndInitBlocks.txt b/compiler/fir/resolve/testData/resolve/cfg/propertiesAndInitBlocks.txt index c437a14645d..7761f4651f0 100644 --- a/compiler/fir/resolve/testData/resolve/cfg/propertiesAndInitBlocks.txt +++ b/compiler/fir/resolve/testData/resolve/cfg/propertiesAndInitBlocks.txt @@ -14,7 +14,7 @@ FILE: propertiesAndInitBlocks.kt public final val x3: R|kotlin/Unit| = R|/run|( = run@fun (): R|kotlin/Unit| { local final fun foo(): R|kotlin/Unit| { lval c: R|kotlin/Int| = Int(1).R|kotlin/Int.plus|(Int(1)) - throw #() + throw R|java/lang/Exception.Exception|() } local final class LocalClass : R|kotlin/Any| { @@ -23,13 +23,13 @@ FILE: propertiesAndInitBlocks.kt } init { - throw #() + throw R|java/lang/Exception.Exception|() Int(1) } } - throw #() + throw R|java/lang/Exception.Exception|() } ) public get(): R|kotlin/Unit| { @@ -39,7 +39,7 @@ FILE: propertiesAndInitBlocks.kt } init { - throw #() + throw R|java/lang/Exception.Exception|() } } diff --git a/compiler/fir/resolve/testData/resolve/stdlib/exception.txt b/compiler/fir/resolve/testData/resolve/stdlib/exception.txt index 26c7130b5ee..255993928fa 100644 --- a/compiler/fir/resolve/testData/resolve/stdlib/exception.txt +++ b/compiler/fir/resolve/testData/resolve/stdlib/exception.txt @@ -5,7 +5,7 @@ FILE: exception.kt public final fun main(args: R|kotlin/Array|): R|kotlin/Unit| { when () { ==(R|/box|(), String(OK)) -> { - throw #(String(Hello)) + throw R|java/lang/Exception.Exception|(String(Hello)) } else -> { } diff --git a/compiler/fir/resolve/testData/resolve/stdlib/problems.txt b/compiler/fir/resolve/testData/resolve/stdlib/problems.txt index fd2c3137998..7574a10cc11 100644 --- a/compiler/fir/resolve/testData/resolve/stdlib/problems.txt +++ b/compiler/fir/resolve/testData/resolve/stdlib/problems.txt @@ -1,6 +1,6 @@ FILE: problems.kt - public final val sb: = #() - public get(): + public final val sb: R|java/lang/StringBuilder| = R|java/lang/StringBuilder.StringBuilder|() + public get(): R|java/lang/StringBuilder| public final val o: R|kotlin/Any| = object : R|kotlin/Any| { private constructor(): R|kotlin/Any| { super() @@ -55,7 +55,7 @@ FILE: problems.kt } public final val xx: = R|/Derived.Derived|().R|/Base.x|.#(Int(1)) public get(): - public final val t: R|kotlin/Nothing| = throw #(String()) + public final val t: R|kotlin/Nothing| = throw R|java/lang/AssertionError.AssertionError|(String()) public get(): R|kotlin/Nothing| public abstract interface A : R|kotlin/Any| { } diff --git a/compiler/testData/ir/irText/expressions/kt30796.fir.txt b/compiler/testData/ir/irText/expressions/kt30796.fir.txt index 5749165de01..e0c2dab2e11 100644 --- a/compiler/testData/ir/irText/expressions/kt30796.fir.txt +++ b/compiler/testData/ir/irText/expressions/kt30796.fir.txt @@ -4,7 +4,7 @@ FILE fqName: fileName:/kt30796.kt BLOCK_BODY RETURN type=kotlin.Nothing from='public final fun magic (): T of .magic declared in ' THROW type=kotlin.Nothing - ERROR_CALL 'Unresolved reference: #' type=IrErrorType + CONSTRUCTOR_CALL 'public constructor () declared in java.lang.Exception' type=java.lang.Exception origin=null FUN name:test visibility:public modality:FINAL (value:T of .test, value2:T of .test) returnType:kotlin.Unit TYPE_PARAMETER name:T index:0 variance: superTypes:[] VALUE_PARAMETER name:value index:0 type:T of .test