K2 Scripting: support script top-level destructuring declarations
This commit is contained in:
committed by
Space Team
parent
02e2438d37
commit
bf3a6f7678
+1
-4
@@ -22,10 +22,7 @@ object FirJvmNamesChecker {
|
||||
|
||||
|
||||
fun checkNameAndReport(name: Name, declarationSource: KtSourceElement?, context: CheckerContext, reporter: DiagnosticReporter) {
|
||||
if (declarationSource != null &&
|
||||
declarationSource.kind !is KtFakeSourceElementKind &&
|
||||
!name.isSpecial
|
||||
) {
|
||||
if (declarationSource != null && declarationSource.kind !is KtFakeSourceElementKind && !name.isSpecial) {
|
||||
val nameString = name.asString()
|
||||
if (nameString.any { it in INVALID_CHARS }) {
|
||||
reporter.reportOn(
|
||||
|
||||
+1
-1
@@ -1003,7 +1003,7 @@ class Fir2IrDeclarationStorage(
|
||||
delegate != null -> IrDeclarationOrigin.DELEGATED_PROPERTY_ACCESSOR
|
||||
origin == IrDeclarationOrigin.FAKE_OVERRIDE -> origin
|
||||
origin == IrDeclarationOrigin.DELEGATED_MEMBER -> origin
|
||||
getter is FirDefaultPropertyGetter -> IrDeclarationOrigin.DEFAULT_PROPERTY_ACCESSOR
|
||||
getter == null || getter is FirDefaultPropertyGetter -> IrDeclarationOrigin.DEFAULT_PROPERTY_ACCESSOR
|
||||
else -> origin
|
||||
},
|
||||
startOffset, endOffset,
|
||||
|
||||
@@ -227,9 +227,41 @@ class Fir2IrVisitor(
|
||||
}
|
||||
|
||||
conversionScope.withParent(irScript) {
|
||||
val destructComposites = mutableMapOf<FirVariableSymbol<*>, IrComposite>()
|
||||
for (statement in script.statements) {
|
||||
val irStatement = if (statement is FirDeclaration) {
|
||||
when {
|
||||
statement is FirVariable && statement.isDestructuringDeclarationContainerVariable == true -> {
|
||||
statement.convertWithOffsets { startOffset, endOffset ->
|
||||
IrCompositeImpl(
|
||||
startOffset, endOffset,
|
||||
irBuiltIns.unitType, IrStatementOrigin.DESTRUCTURING_DECLARATION
|
||||
).also {
|
||||
it.statements.add(
|
||||
declarationStorage.createIrVariable(statement, conversionScope.parentFromStack()).also {
|
||||
it.initializer = statement.initializer?.toIrStatement() as? IrExpression
|
||||
}
|
||||
)
|
||||
destructComposites[(statement).symbol] = it
|
||||
}
|
||||
}
|
||||
}
|
||||
statement is FirProperty && statement.destructuringDeclarationContainerVariable != null -> {
|
||||
(statement.accept(this@Fir2IrVisitor, null) as IrProperty).also {
|
||||
val irComponentInitializer = IrSetFieldImpl(
|
||||
it.startOffset, it.endOffset,
|
||||
it.backingField!!.symbol,
|
||||
irBuiltIns.unitType,
|
||||
origin = null, superQualifierSymbol = null
|
||||
).apply {
|
||||
value = it.backingField!!.initializer!!.expression
|
||||
receiver = null
|
||||
}
|
||||
val correspondingComposite = destructComposites[statement.destructuringDeclarationContainerVariable!!]!!
|
||||
correspondingComposite.statements.add(irComponentInitializer)
|
||||
it.backingField!!.initializer = null
|
||||
}
|
||||
}
|
||||
statement is FirClass -> {
|
||||
(statement.accept(this@Fir2IrVisitor, null) as IrClass).also {
|
||||
converter.bindFakeOverridesInClass(it)
|
||||
|
||||
+7
-2
@@ -5,6 +5,7 @@
|
||||
|
||||
package org.jetbrains.kotlin.fir.backend.generators
|
||||
|
||||
import org.jetbrains.kotlin.KtFakeSourceElementKind
|
||||
import org.jetbrains.kotlin.descriptors.ClassKind
|
||||
import org.jetbrains.kotlin.descriptors.Visibilities
|
||||
import org.jetbrains.kotlin.fir.backend.*
|
||||
@@ -190,7 +191,7 @@ internal class ClassMemberGenerator(
|
||||
}
|
||||
}
|
||||
}
|
||||
if (irFunction !is IrConstructor || !irFunction.isPrimary) {
|
||||
if (firFunction != null && (irFunction !is IrConstructor || !irFunction.isPrimary)) {
|
||||
// Scope for primary constructor should be left after class declaration
|
||||
declarationStorage.leaveScope(irFunction)
|
||||
}
|
||||
@@ -209,9 +210,13 @@ internal class ClassMemberGenerator(
|
||||
if (containingClass != null) {
|
||||
irProperty.overriddenSymbols = property.generateOverriddenPropertySymbols(containingClass)
|
||||
}
|
||||
val needGenerateDefaultGetter =
|
||||
property.getter is FirDefaultPropertyGetter ||
|
||||
(property.getter == null && irProperty.parent is IrScript && property.destructuringDeclarationContainerVariable != null)
|
||||
|
||||
irProperty.getter?.setPropertyAccessorContent(
|
||||
property, property.getter, irProperty, propertyType,
|
||||
property.getter is FirDefaultPropertyGetter,
|
||||
isDefault = needGenerateDefaultGetter,
|
||||
isGetter = true,
|
||||
containingClass = containingClass
|
||||
)
|
||||
|
||||
+6
@@ -217,6 +217,12 @@ public class FirScriptCodegenTestGenerated extends AbstractFirScriptCodegenTest
|
||||
runTest("compiler/testData/codegen/script/topLevelTypealias.kts");
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestMetadata("twoDestructuringDeclarations.kts")
|
||||
public void testTwoDestructuringDeclarations() throws Exception {
|
||||
runTest("compiler/testData/codegen/script/twoDestructuringDeclarations.kts");
|
||||
}
|
||||
|
||||
@Nested
|
||||
@TestMetadata("compiler/testData/codegen/script/scriptInstanceCapturing")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
|
||||
+2
-1
@@ -124,6 +124,7 @@ internal fun generateDestructuringBlock(
|
||||
multiDeclaration: KtDestructuringDeclaration,
|
||||
container: FirVariable,
|
||||
tmpVariable: Boolean,
|
||||
localEntries: Boolean,
|
||||
extractAnnotationsTo: KtAnnotated.(FirAnnotationContainerBuilder) -> Unit,
|
||||
toFirOrImplicitTypeRef: KtTypeReference?.() -> FirTypeRef,
|
||||
): FirBlock {
|
||||
@@ -150,7 +151,7 @@ internal fun generateDestructuringBlock(
|
||||
componentIndex = index + 1
|
||||
}
|
||||
this.isVar = isVar
|
||||
isLocal = true
|
||||
isLocal = localEntries
|
||||
status = FirDeclarationStatusImpl(Visibilities.Local, Modality.FINAL)
|
||||
symbol = FirPropertySymbol(name)
|
||||
entry.extractAnnotationsTo(this)
|
||||
|
||||
+32
-1
@@ -1121,6 +1121,33 @@ open class PsiRawFirBuilder(
|
||||
is KtScriptInitializer -> {
|
||||
declaration.body?.let { statements.add(it.toFirStatement()) }
|
||||
}
|
||||
is KtDestructuringDeclaration -> {
|
||||
val destructuringContainerVar = generateTemporaryVariable(
|
||||
baseModuleData,
|
||||
declaration.toFirSourceElement(),
|
||||
"destruct",
|
||||
declaration.initializer.toFirExpression { ConeSyntaxDiagnostic("Initializer required for destructuring declaration") },
|
||||
extractAnnotationsTo = { extractAnnotationsTo(it) }
|
||||
).apply {
|
||||
isDestructuringDeclarationContainerVariable = true
|
||||
}
|
||||
val destructuringBlock = generateDestructuringBlock(
|
||||
baseModuleData,
|
||||
declaration,
|
||||
destructuringContainerVar,
|
||||
tmpVariable = false,
|
||||
localEntries = false,
|
||||
extractAnnotationsTo = { extractAnnotationsTo(it) },
|
||||
) {
|
||||
toFirOrImplicitType()
|
||||
}.apply {
|
||||
statements.forEach {
|
||||
(it as FirProperty).destructuringDeclarationContainerVariable = destructuringContainerVar.symbol
|
||||
}
|
||||
}
|
||||
statements.add(destructuringContainerVar)
|
||||
statements.addAll(destructuringBlock.statements)
|
||||
}
|
||||
else -> {
|
||||
statements.add(declaration.toFirStatement())
|
||||
}
|
||||
@@ -1648,6 +1675,7 @@ open class PsiRawFirBuilder(
|
||||
multiDeclaration,
|
||||
multiParameter,
|
||||
tmpVariable = false,
|
||||
localEntries = true,
|
||||
extractAnnotationsTo = { extractAnnotationsTo(it) },
|
||||
) { toFirOrImplicitType() }.statements
|
||||
multiParameter
|
||||
@@ -2463,7 +2491,8 @@ open class PsiRawFirBuilder(
|
||||
if (ktParameter != null) {
|
||||
val multiDeclaration = ktParameter.destructuringDeclaration
|
||||
val firLoopParameter = generateTemporaryVariable(
|
||||
moduleData = baseModuleData, source = expression.loopParameter?.toFirSourceElement(),
|
||||
moduleData = baseModuleData,
|
||||
source = expression.loopParameter?.toFirSourceElement(),
|
||||
name = if (multiDeclaration != null) SpecialNames.DESTRUCT else ktParameter.nameAsSafeName,
|
||||
initializer = buildFunctionCall {
|
||||
source = fakeSource
|
||||
@@ -2481,6 +2510,7 @@ open class PsiRawFirBuilder(
|
||||
multiDeclaration = multiDeclaration,
|
||||
container = firLoopParameter,
|
||||
tmpVariable = true,
|
||||
localEntries = true,
|
||||
extractAnnotationsTo = { extractAnnotationsTo(it) },
|
||||
) { toFirOrImplicitType() }
|
||||
blockBuilder.statements.addAll(destructuringBlock.statements)
|
||||
@@ -2843,6 +2873,7 @@ open class PsiRawFirBuilder(
|
||||
multiDeclaration,
|
||||
baseVariable,
|
||||
tmpVariable = true,
|
||||
localEntries = true,
|
||||
extractAnnotationsTo = { extractAnnotationsTo(it) },
|
||||
) {
|
||||
toFirOrImplicitType()
|
||||
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
/*
|
||||
* Copyright 2010-2023 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.declarations
|
||||
|
||||
import org.jetbrains.kotlin.fir.symbols.impl.FirPropertySymbol
|
||||
import org.jetbrains.kotlin.fir.symbols.impl.FirVariableSymbol
|
||||
|
||||
private object DestructuringDeclarationContainerVariableKey : FirDeclarationDataKey()
|
||||
private object DestructuringDeclarationContainerVariableMarkerKey : FirDeclarationDataKey()
|
||||
|
||||
/*
|
||||
* For properties created from destructuring declaration entries, contains the symbol of the corresponding container variable
|
||||
* Currently used only for script top-level destructuring declarations, and therefore only set for them too
|
||||
*/
|
||||
var FirProperty.destructuringDeclarationContainerVariable: FirVariableSymbol<*>? by FirDeclarationDataRegistry.data(DestructuringDeclarationContainerVariableKey)
|
||||
|
||||
var FirVariable.isDestructuringDeclarationContainerVariable:
|
||||
Boolean? by FirDeclarationDataRegistry.data(DestructuringDeclarationContainerVariableMarkerKey)
|
||||
@@ -0,0 +1,67 @@
|
||||
FILE fqName:<root> fileName:/destructuringDeclaration.kts
|
||||
SCRIPT
|
||||
COMPOSITE type=kotlin.Unit origin=DESTRUCTURING_DECLARATION
|
||||
VAR IR_TEMPORARY_VARIABLE name:tmp_0 type:<root>.<script-destructuringDeclaration.kts>.A [val]
|
||||
CONSTRUCTOR_CALL 'public constructor <init> () declared in <root>.<script-destructuringDeclaration.kts>.A' type=<root>.<script-destructuringDeclaration.kts>.A origin=null
|
||||
SET_FIELD 'FIELD PROPERTY_BACKING_FIELD name:abc type:kotlin.Int visibility:private [final]' type=kotlin.Unit origin=null
|
||||
value: CALL 'public final fun component1 (): kotlin.Int declared in <root>.<script-destructuringDeclaration.kts>.A' type=kotlin.Int origin=COMPONENT_N(index=1)
|
||||
$this: GET_VAR 'val tmp_0: <root>.<script-destructuringDeclaration.kts>.A declared in <root>.<script-destructuringDeclaration.kts>' type=<root>.<script-destructuringDeclaration.kts>.A origin=null
|
||||
SET_FIELD 'FIELD PROPERTY_BACKING_FIELD name:def type:kotlin.Int visibility:private [final]' type=kotlin.Unit origin=null
|
||||
value: CALL 'public final fun component2 (): kotlin.Int declared in <root>.<script-destructuringDeclaration.kts>.A' type=kotlin.Int origin=COMPONENT_N(index=2)
|
||||
$this: GET_VAR 'val tmp_0: <root>.<script-destructuringDeclaration.kts>.A declared in <root>.<script-destructuringDeclaration.kts>' type=<root>.<script-destructuringDeclaration.kts>.A origin=null
|
||||
PROPERTY name:abc visibility:local modality:FINAL [val]
|
||||
FIELD PROPERTY_BACKING_FIELD name:abc type:kotlin.Int visibility:private [final]
|
||||
FUN DEFAULT_PROPERTY_ACCESSOR name:<get-abc> visibility:local modality:FINAL <> () returnType:kotlin.Int
|
||||
correspondingProperty: PROPERTY name:abc visibility:local modality:FINAL [val]
|
||||
BLOCK_BODY
|
||||
RETURN type=kotlin.Nothing from='local final fun <get-abc> (): kotlin.Int declared in <root>.<script-destructuringDeclaration.kts>'
|
||||
GET_FIELD 'FIELD PROPERTY_BACKING_FIELD name:abc type:kotlin.Int visibility:private [final]' type=kotlin.Int origin=null
|
||||
PROPERTY name:def visibility:local modality:FINAL [val]
|
||||
FIELD PROPERTY_BACKING_FIELD name:def type:kotlin.Int visibility:private [final]
|
||||
FUN DEFAULT_PROPERTY_ACCESSOR name:<get-def> visibility:local modality:FINAL <> () returnType:kotlin.Int
|
||||
correspondingProperty: PROPERTY name:def visibility:local modality:FINAL [val]
|
||||
BLOCK_BODY
|
||||
RETURN type=kotlin.Nothing from='local final fun <get-def> (): kotlin.Int declared in <root>.<script-destructuringDeclaration.kts>'
|
||||
GET_FIELD 'FIELD PROPERTY_BACKING_FIELD name:def type:kotlin.Int visibility:private [final]' type=kotlin.Int origin=null
|
||||
PROPERTY name:rv visibility:public modality:FINAL [val]
|
||||
FIELD PROPERTY_BACKING_FIELD name:rv type:kotlin.Int visibility:private [final]
|
||||
EXPRESSION_BODY
|
||||
CALL 'public final fun plus (other: kotlin.Int): kotlin.Int declared in kotlin.Int' type=kotlin.Int origin=PLUS
|
||||
$this: CALL 'local final fun <get-abc> (): kotlin.Int declared in <root>.<script-destructuringDeclaration.kts>' type=kotlin.Int origin=GET_PROPERTY
|
||||
other: CALL 'local final fun <get-def> (): kotlin.Int declared in <root>.<script-destructuringDeclaration.kts>' type=kotlin.Int origin=GET_PROPERTY
|
||||
FUN DEFAULT_PROPERTY_ACCESSOR name:<get-rv> visibility:public modality:FINAL <> () returnType:kotlin.Int
|
||||
correspondingProperty: PROPERTY name:rv visibility:public modality:FINAL [val]
|
||||
BLOCK_BODY
|
||||
RETURN type=kotlin.Nothing from='public final fun <get-rv> (): kotlin.Int declared in <root>.<script-destructuringDeclaration.kts>'
|
||||
GET_FIELD 'FIELD PROPERTY_BACKING_FIELD name:rv type:kotlin.Int visibility:private [final]' type=kotlin.Int origin=null
|
||||
CLASS CLASS name:A modality:FINAL visibility:public superTypes:[kotlin.Any]
|
||||
$this: VALUE_PARAMETER INSTANCE_RECEIVER name:<this> type:<root>.<script-destructuringDeclaration.kts>.A
|
||||
CONSTRUCTOR visibility:public <> () returnType:<root>.<script-destructuringDeclaration.kts>.A [primary]
|
||||
BLOCK_BODY
|
||||
DELEGATING_CONSTRUCTOR_CALL 'public constructor <init> () declared in kotlin.Any'
|
||||
INSTANCE_INITIALIZER_CALL classDescriptor='CLASS CLASS name:A modality:FINAL visibility:public superTypes:[kotlin.Any]'
|
||||
FUN name:component1 visibility:public modality:FINAL <> ($this:<root>.<script-destructuringDeclaration.kts>.A) returnType:kotlin.Int [operator]
|
||||
$this: VALUE_PARAMETER name:<this> type:<root>.<script-destructuringDeclaration.kts>.A
|
||||
BLOCK_BODY
|
||||
RETURN type=kotlin.Nothing from='public final fun component1 (): kotlin.Int declared in <root>.<script-destructuringDeclaration.kts>.A'
|
||||
CONST Int type=kotlin.Int value=123
|
||||
FUN name:component2 visibility:public modality:FINAL <> ($this:<root>.<script-destructuringDeclaration.kts>.A) returnType:kotlin.Int [operator]
|
||||
$this: VALUE_PARAMETER name:<this> type:<root>.<script-destructuringDeclaration.kts>.A
|
||||
BLOCK_BODY
|
||||
RETURN type=kotlin.Nothing from='public final fun component2 (): kotlin.Int declared in <root>.<script-destructuringDeclaration.kts>.A'
|
||||
CONST Int type=kotlin.Int value=2
|
||||
FUN FAKE_OVERRIDE name:equals visibility:public modality:OPEN <> ($this:kotlin.Any, other:kotlin.Any?) returnType:kotlin.Boolean [fake_override,operator]
|
||||
overridden:
|
||||
public open fun equals (other: kotlin.Any?): kotlin.Boolean declared in kotlin.Any
|
||||
$this: VALUE_PARAMETER name:<this> type:kotlin.Any
|
||||
VALUE_PARAMETER name:other index:0 type:kotlin.Any?
|
||||
FUN FAKE_OVERRIDE name:hashCode visibility:public modality:OPEN <> ($this:kotlin.Any) returnType:kotlin.Int [fake_override]
|
||||
overridden:
|
||||
public open fun hashCode (): kotlin.Int declared in kotlin.Any
|
||||
$this: VALUE_PARAMETER name:<this> type:kotlin.Any
|
||||
FUN FAKE_OVERRIDE name:toString visibility:public modality:OPEN <> ($this:kotlin.Any) returnType:kotlin.String [fake_override]
|
||||
overridden:
|
||||
public open fun toString (): kotlin.String declared in kotlin.Any
|
||||
$this: VALUE_PARAMETER name:<this> type:kotlin.Any
|
||||
VALUE_PARAMETER INSTANCE_RECEIVER name:<this> type:kotlin.script.templates.standard.ScriptTemplateWithArgs
|
||||
VAR SCRIPT_CALL_PARAMETER name:args type:kotlin.Array<kotlin.String> [val]
|
||||
@@ -1,4 +1,4 @@
|
||||
// IGNORE_BACKEND_K2: JVM_IR
|
||||
// DUMP_IR
|
||||
|
||||
val (abc, def) = A()
|
||||
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
// IGNORE_BACKEND_K2: JVM_IR
|
||||
|
||||
val (_, b, _) = A()
|
||||
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
// IGNORE_BACKEND_K2: JVM_IR
|
||||
|
||||
data class Pair(val first: Int, val second: Int)
|
||||
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
val (a, b) = A()
|
||||
val (c, d) = B()
|
||||
|
||||
val rv = (a + b) * (c + d)
|
||||
|
||||
class A {
|
||||
operator fun component1() = 1
|
||||
operator fun component2() = 5
|
||||
}
|
||||
|
||||
class B {
|
||||
operator fun component1() = 3
|
||||
operator fun component2() = 4
|
||||
}
|
||||
|
||||
// expected: rv: 42
|
||||
+3
-3
@@ -7,9 +7,9 @@ val (c1) = <!COMPONENT_FUNCTION_MISSING, UNRESOLVED_REFERENCE!>unresolved<!>
|
||||
|
||||
val (e1, _) = A()
|
||||
|
||||
<!UNRESOLVED_REFERENCE!>a1<!>
|
||||
<!UNRESOLVED_REFERENCE!>a2<!>
|
||||
<!UNRESOLVED_REFERENCE!>e1<!>
|
||||
a1
|
||||
a2
|
||||
e1
|
||||
|
||||
class A {
|
||||
operator fun component1() = 1
|
||||
|
||||
+5
@@ -190,6 +190,11 @@ public class ScriptCodegenTestGenerated extends AbstractScriptCodegenTest {
|
||||
runTest("compiler/testData/codegen/script/topLevelTypealias.kts");
|
||||
}
|
||||
|
||||
@TestMetadata("twoDestructuringDeclarations.kts")
|
||||
public void testTwoDestructuringDeclarations() throws Exception {
|
||||
runTest("compiler/testData/codegen/script/twoDestructuringDeclarations.kts");
|
||||
}
|
||||
|
||||
@TestMetadata("compiler/testData/codegen/script/scriptInstanceCapturing")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
|
||||
+5
@@ -190,6 +190,11 @@ public class IrScriptCodegenTestGenerated extends AbstractIrScriptCodegenTest {
|
||||
runTest("compiler/testData/codegen/script/topLevelTypealias.kts");
|
||||
}
|
||||
|
||||
@TestMetadata("twoDestructuringDeclarations.kts")
|
||||
public void testTwoDestructuringDeclarations() throws Exception {
|
||||
runTest("compiler/testData/codegen/script/twoDestructuringDeclarations.kts");
|
||||
}
|
||||
|
||||
@TestMetadata("compiler/testData/codegen/script/scriptInstanceCapturing")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
|
||||
Reference in New Issue
Block a user