From 9adf24950a91e8a7439555464784123c85d7c732 Mon Sep 17 00:00:00 2001 From: Ilya Chernikov Date: Thu, 27 Apr 2023 17:53:33 +0200 Subject: [PATCH] Fir2Ir scripts: fix declarations registration and processing --- .../kotlin/fir/backend/Fir2IrConverter.kt | 13 +++++++++++- .../resolve/providers/impl/FirProviderImpl.kt | 1 + .../codegen/script/localDelegatedProperty.kts | 1 - .../localDelegatedPropertyNoExplicitType.kts | 1 - .../testData/codegen/script/simpleClass.kts | 1 - .../topLevelPropertyWithProvideDelegate.kts | 1 - .../tests/script/NestedInnerClass.fir.kts | 8 ++++---- .../destructuringDeclarationsScript.fir.kts | 8 ++++---- ...izerOfDestructuringDeclarationOnce.fir.kts | 7 ------- ...tializerOfDestructuringDeclarationOnce.kts | 1 + .../tests/script/topLevelVariable.fir.kts | 20 ------------------- .../tests/script/topLevelVariable.kts | 1 + 12 files changed, 23 insertions(+), 40 deletions(-) delete mode 100644 compiler/testData/diagnostics/tests/script/resolveInitializerOfDestructuringDeclarationOnce.fir.kts delete mode 100644 compiler/testData/diagnostics/tests/script/topLevelVariable.fir.kts diff --git a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrConverter.kt b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrConverter.kt index 4ef12afd782..71e3678540f 100644 --- a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrConverter.kt +++ b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrConverter.kt @@ -217,7 +217,8 @@ class Fir2IrConverter( internal fun processRegularClassMembers( regularClass: FirRegularClass, - irClass: IrClass = classifierStorage.getCachedIrClass(regularClass)!! + irClass: IrClass = + classifierStorage.getCachedIrClass(regularClass) ?: error("Expecting existing IrClass for class ${regularClass.name}") ): IrClass { val allDeclarations = mutableListOf().apply { addAll(regularClass.declarations) @@ -361,6 +362,16 @@ class Fir2IrConverter( assert(parent is IrFile) declarationStorage.getOrCreateIrScript(declaration).also { irScript -> declarationStorage.enterScope(irScript) + irScript.parent = parent + for (scriptStatement in declaration.statements) { + if (scriptStatement is FirDeclaration) { + if (scriptStatement is FirRegularClass) { + registerClassAndNestedClasses(scriptStatement, irScript) + processClassAndNestedClassHeaders(scriptStatement) + } + + } + } for (scriptStatement in declaration.statements) { if (scriptStatement is FirDeclaration) { processMemberDeclaration(scriptStatement, null, irScript) diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/providers/impl/FirProviderImpl.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/providers/impl/FirProviderImpl.kt index 12f74d92d9f..d9f4dbcd0a5 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/providers/impl/FirProviderImpl.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/providers/impl/FirProviderImpl.kt @@ -184,6 +184,7 @@ class FirProviderImpl(val session: FirSession, val kotlinScopeProvider: FirKotli override fun visitScript(script: FirScript, data: FirRecorderData) { val symbol = script.symbol data.state.scriptContainerMap[symbol] = data.file + script.acceptChildren(this, data) } } diff --git a/compiler/testData/codegen/script/localDelegatedProperty.kts b/compiler/testData/codegen/script/localDelegatedProperty.kts index 140e622599b..80f8e07f277 100644 --- a/compiler/testData/codegen/script/localDelegatedProperty.kts +++ b/compiler/testData/codegen/script/localDelegatedProperty.kts @@ -1,4 +1,3 @@ -// IGNORE_BACKEND_K2: JVM_IR import kotlin.reflect.KProperty diff --git a/compiler/testData/codegen/script/localDelegatedPropertyNoExplicitType.kts b/compiler/testData/codegen/script/localDelegatedPropertyNoExplicitType.kts index d5e1310cd61..b0fed5ffeb3 100644 --- a/compiler/testData/codegen/script/localDelegatedPropertyNoExplicitType.kts +++ b/compiler/testData/codegen/script/localDelegatedPropertyNoExplicitType.kts @@ -1,4 +1,3 @@ -// IGNORE_BACKEND_K2: JVM_IR import kotlin.reflect.KProperty diff --git a/compiler/testData/codegen/script/simpleClass.kts b/compiler/testData/codegen/script/simpleClass.kts index 99481df28de..252dca39708 100644 --- a/compiler/testData/codegen/script/simpleClass.kts +++ b/compiler/testData/codegen/script/simpleClass.kts @@ -1,4 +1,3 @@ -// IGNORE_BACKEND_K2: JVM_IR class SimpleClass(val s: String) { fun foo() = s diff --git a/compiler/testData/codegen/script/topLevelPropertyWithProvideDelegate.kts b/compiler/testData/codegen/script/topLevelPropertyWithProvideDelegate.kts index 72e907fce4a..a18f402ce63 100644 --- a/compiler/testData/codegen/script/topLevelPropertyWithProvideDelegate.kts +++ b/compiler/testData/codegen/script/topLevelPropertyWithProvideDelegate.kts @@ -1,4 +1,3 @@ -// IGNORE_BACKEND_K2: JVM_IR import kotlin.reflect.KProperty diff --git a/compiler/testData/diagnostics/tests/script/NestedInnerClass.fir.kts b/compiler/testData/diagnostics/tests/script/NestedInnerClass.fir.kts index 4aa39cba93c..d20c27c2f57 100644 --- a/compiler/testData/diagnostics/tests/script/NestedInnerClass.fir.kts +++ b/compiler/testData/diagnostics/tests/script/NestedInnerClass.fir.kts @@ -17,9 +17,9 @@ class Nested { val innerThisProp = this@NestedInnerClass.property inner class InnerInner { - fun f() = innerFun() - fun g() = innerProp - fun h() = this@Inner.innerFun() - fun i() = this@Inner.innerProp + fun f() = innerFun() + fun g() = innerProp + fun h() = this@Inner.innerFun() + fun i() = this@Inner.innerProp } } diff --git a/compiler/testData/diagnostics/tests/script/destructuringDeclarationsScript.fir.kts b/compiler/testData/diagnostics/tests/script/destructuringDeclarationsScript.fir.kts index 55517075d94..76d850f0ad9 100644 --- a/compiler/testData/diagnostics/tests/script/destructuringDeclarationsScript.fir.kts +++ b/compiler/testData/diagnostics/tests/script/destructuringDeclarationsScript.fir.kts @@ -1,11 +1,11 @@ // !WITH_NEW_INFERENCE -val (a1, a2) = A() -val (b1: Int, b2: Int) = A() +val (a1, a2) = A() +val (b1: Int, b2: Int) = A() val (c1) = unresolved -private val (d1) = A() +private val (d1) = A() -val (e1, _) = A() +val (e1, _) = A() a1 a2 diff --git a/compiler/testData/diagnostics/tests/script/resolveInitializerOfDestructuringDeclarationOnce.fir.kts b/compiler/testData/diagnostics/tests/script/resolveInitializerOfDestructuringDeclarationOnce.fir.kts deleted file mode 100644 index 2cfe812862a..00000000000 --- a/compiler/testData/diagnostics/tests/script/resolveInitializerOfDestructuringDeclarationOnce.fir.kts +++ /dev/null @@ -1,7 +0,0 @@ -val (a, b, c) = A() - -class A(val a: Int) { - operator fun component1() {} - operator fun component2() {} - operator fun component3() {} -} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/script/resolveInitializerOfDestructuringDeclarationOnce.kts b/compiler/testData/diagnostics/tests/script/resolveInitializerOfDestructuringDeclarationOnce.kts index c127e4e6133..9fb24456c2f 100644 --- a/compiler/testData/diagnostics/tests/script/resolveInitializerOfDestructuringDeclarationOnce.kts +++ b/compiler/testData/diagnostics/tests/script/resolveInitializerOfDestructuringDeclarationOnce.kts @@ -1,3 +1,4 @@ +// FIR_IDENTICAL val (a, b, c) = A() class A(val a: Int) { diff --git a/compiler/testData/diagnostics/tests/script/topLevelVariable.fir.kts b/compiler/testData/diagnostics/tests/script/topLevelVariable.fir.kts deleted file mode 100644 index b4477b9dfd7..00000000000 --- a/compiler/testData/diagnostics/tests/script/topLevelVariable.fir.kts +++ /dev/null @@ -1,20 +0,0 @@ -// !DIAGNOSTICS: -UNUSED_PARAMETER -UNUSED_VARIABLE - -import kotlin.reflect.KProperty - -class Delegate { - operator fun getValue(t: Any?, p: KProperty<*>): Int { - return 1 - } -} - -val a: Int by Delegate() - -class Foo { - val a: Int by Delegate() -} - -fun foo() { - val a: Int by Delegate() -} - diff --git a/compiler/testData/diagnostics/tests/script/topLevelVariable.kts b/compiler/testData/diagnostics/tests/script/topLevelVariable.kts index 4ae32022375..3e1a37633e7 100644 --- a/compiler/testData/diagnostics/tests/script/topLevelVariable.kts +++ b/compiler/testData/diagnostics/tests/script/topLevelVariable.kts @@ -1,3 +1,4 @@ +// FIR_IDENTICAL // !DIAGNOSTICS: -UNUSED_PARAMETER -UNUSED_VARIABLE import kotlin.reflect.KProperty