[FIR] make script destructuring declaration entry initializer lazy expression

This change is required to properly support such declarations in lazy
resolution mode as we usually replaced initializers with lazy
expressions during the body calculation phase

^KT-62840
This commit is contained in:
Dmitrii Gridin
2024-02-09 23:14:50 +01:00
committed by Space Team
parent c0f4f7fefd
commit 312feb3dca
9 changed files with 135 additions and 67 deletions
@@ -16,6 +16,7 @@ import org.jetbrains.kotlin.analysis.utils.errors.withPsiEntry
import org.jetbrains.kotlin.fir.*
import org.jetbrains.kotlin.fir.builder.BodyBuildingMode
import org.jetbrains.kotlin.fir.builder.PsiRawFirBuilder
import org.jetbrains.kotlin.fir.builder.buildDestructuringVariable
import org.jetbrains.kotlin.fir.declarations.*
import org.jetbrains.kotlin.fir.declarations.utils.isInner
import org.jetbrains.kotlin.fir.expressions.FirAnnotation
@@ -27,12 +28,15 @@ import org.jetbrains.kotlin.fir.symbols.impl.FirFunctionSymbol
import org.jetbrains.kotlin.fir.symbols.impl.FirPropertySymbol
import org.jetbrains.kotlin.fir.types.FirResolvedTypeRef
import org.jetbrains.kotlin.fir.types.FirTypeRef
import org.jetbrains.kotlin.fir.utils.exceptions.withFirEntry
import org.jetbrains.kotlin.name.NameUtils
import org.jetbrains.kotlin.psi
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.hasExpectModifier
import org.jetbrains.kotlin.util.PrivateForInline
import org.jetbrains.kotlin.utils.exceptions.errorWithAttachment
import org.jetbrains.kotlin.utils.exceptions.requireWithAttachment
import org.jetbrains.kotlin.utils.exceptions.withPsiEntry
internal class RawFirNonLocalDeclarationBuilder private constructor(
session: FirSession,
@@ -159,6 +163,34 @@ internal class RawFirNonLocalDeclarationBuilder private constructor(
}
}
fun convertDestructuringDeclarationEntry(element: KtDestructuringDeclarationEntry): FirVariable {
val replacementDeclaration = replacementApplier?.tryReplace(element) ?: element
requireIsInstance<KtDestructuringDeclarationEntry>(replacementDeclaration)
requireIsInstance<FirProperty>(originalDeclaration)
val container = originalDeclaration.destructuringDeclarationContainerVariable?.fir
requireWithAttachment(container != null, { "Container is not found"}) {
withFirEntry("originalDeclaration", originalDeclaration)
withPsiEntry("element", element)
}
return buildDestructuringVariable(
moduleData = baseModuleData,
container = container,
element,
isVar = false,
localEntries = false,
index = element.index(),
configure = { configureScriptDestructuringDeclarationEntry(it, container) },
)
}
private fun KtDestructuringDeclarationEntry.index(): Int {
val destructuringDeclaration = parent
requireIsInstance<KtDestructuringDeclaration>(destructuringDeclaration)
return destructuringDeclaration.entries.indexOf(this)
}
fun convertAnonymousInitializer(element: KtAnonymousInitializer, containingDeclaration: FirDeclaration?): FirAnonymousInitializer {
val replacementDeclaration = replacementApplier?.tryReplace(element) ?: element
requireIsInstance<KtAnonymousInitializer>(replacementDeclaration)
@@ -356,6 +388,7 @@ internal class RawFirNonLocalDeclarationBuilder private constructor(
}
}
is KtDestructuringDeclaration -> visitor.convertDestructuringDeclaration(declarationToBuild, containingDeclaration)
is KtDestructuringDeclarationEntry -> visitor.convertDestructuringDeclarationEntry(declarationToBuild)
is KtCodeFragment -> {
val firFile = visitor.convertElement(declarationToBuild, originalDeclaration) as FirFile
firFile.codeFragment
@@ -44,7 +44,7 @@ FILE: [ResolvedTo(IMPORTS)] destructuringAnnotation.kts
}
@R|DestrAnno|[Types](s = <strcat>(String(destr 1 ), R|/prop|)) [ResolvedTo(ANNOTATION_ARGUMENTS)] [DestructuringDeclarationContainerVariableMarkerKey=true] lval <destruct>: R|MyPair| = R|/MyPair.MyPair|(Int(1), Int(2))
public final [ResolvedTo(RAW_FIR)] [DestructuringDeclarationContainerVariableKey=<local>/<destruct>] val a: <implicit> = R|<local>/<destruct>|.component1#()
public final [ResolvedTo(RAW_FIR)] [DestructuringDeclarationContainerVariableKey=<local>/<destruct>] val a: <implicit> = LAZY_EXPRESSION
public final [ResolvedTo(RAW_FIR)] [DestructuringDeclarationContainerVariableKey=<local>/<destruct>] val b: <implicit> = R|<local>/<destruct>|.component2#()
public final [ResolvedTo(RAW_FIR)] [DestructuringDeclarationContainerVariableKey=<local>/<destruct>] val b: <implicit> = LAZY_EXPRESSION
@@ -12,7 +12,7 @@ FILE: [ResolvedTo(IMPORTS)] destructionWithNoRValueScript.kts
[ResolvedTo(RAW_FIR)] lval args: R|kotlin/Array<kotlin/String>|
[ResolvedTo(BODY_RESOLVE)] [DestructuringDeclarationContainerVariableMarkerKey=true] lval <destruct>: <ERROR TYPE REF: Initializer required for destructuring declaration> = ERROR_EXPR(Initializer required for destructuring declaration)
public final [ResolvedTo(RAW_FIR)] [DestructuringDeclarationContainerVariableKey=<local>/<destruct>] val o: <implicit> = R|<local>/<destruct>|.component1#()
public final [ResolvedTo(RAW_FIR)] [DestructuringDeclarationContainerVariableKey=<local>/<destruct>] val o: <implicit> = LAZY_EXPRESSION
public final [ResolvedTo(RAW_FIR)] [DestructuringDeclarationContainerVariableKey=<local>/<destruct>] val r: <implicit> = R|<local>/<destruct>|.component2#()
public final [ResolvedTo(RAW_FIR)] [DestructuringDeclarationContainerVariableKey=<local>/<destruct>] val r: <implicit> = LAZY_EXPRESSION
@@ -23,7 +23,7 @@ FILE: [ResolvedTo(IMPORTS)] destructuringScript.kts
public [ResolvedTo(IMPLICIT_TYPES_BODY_RESOLVE)] get(): R|kotlin/Pair<kotlin/Int, kotlin/Int>|
[ResolvedTo(BODY_RESOLVE)] [DestructuringDeclarationContainerVariableMarkerKey=true] lval <destruct>: R|kotlin/Pair<kotlin/Int, kotlin/Int>| = R|/pair|
public final [ResolvedTo(RAW_FIR)] [DestructuringDeclarationContainerVariableKey=<local>/<destruct>] val first: <implicit> = R|<local>/<destruct>|.component1#()
public final [ResolvedTo(RAW_FIR)] [DestructuringDeclarationContainerVariableKey=<local>/<destruct>] val first: <implicit> = LAZY_EXPRESSION
public final [ResolvedTo(RAW_FIR)] [DestructuringDeclarationContainerVariableKey=<local>/<destruct>] val last: <implicit> = R|<local>/<destruct>|.component2#()
public final [ResolvedTo(RAW_FIR)] [DestructuringDeclarationContainerVariableKey=<local>/<destruct>] val last: <implicit> = LAZY_EXPRESSION
@@ -42,11 +42,11 @@ FILE: [ResolvedTo(RAW_FIR)] scriptStatementLevelAsLastStatement.kts
public final [ResolvedTo(RAW_FIR)] val $$result: <implicit> = when () {
Boolean(true) -> {
@Anno[Unresolved](LAZY_EXPRESSION) [ResolvedTo(RAW_FIR)] lval <destruct>: <implicit> = Pair#(LAZY_EXPRESSION, LAZY_EXPRESSION)
@Anno[Unresolved](LAZY_EXPRESSION) [ResolvedTo(RAW_FIR)] lval a: <implicit> = R|<local>/<destruct>|.component1#()
@Anno[Unresolved](LAZY_EXPRESSION) [ResolvedTo(RAW_FIR)] lval b: <implicit> = R|<local>/<destruct>|.component2#()
@Anno[Unresolved](LAZY_EXPRESSION) [ResolvedTo(RAW_FIR)] lval a: <implicit> = LAZY_EXPRESSION
@Anno[Unresolved](LAZY_EXPRESSION) [ResolvedTo(RAW_FIR)] lval b: <implicit> = LAZY_EXPRESSION
@Anno[Unresolved](LAZY_EXPRESSION) [ResolvedTo(RAW_FIR)] lval <destruct>: <implicit> = Pair#(LAZY_EXPRESSION, LAZY_EXPRESSION)
@Anno[Unresolved](LAZY_EXPRESSION) [ResolvedTo(RAW_FIR)] lval c: <implicit> = R|<local>/<destruct>|.component1#()
@Anno[Unresolved](LAZY_EXPRESSION) [ResolvedTo(RAW_FIR)] lval d: <implicit> = R|<local>/<destruct>|.component2#()
@Anno[Unresolved](LAZY_EXPRESSION) [ResolvedTo(RAW_FIR)] lval c: <implicit> = LAZY_EXPRESSION
@Anno[Unresolved](LAZY_EXPRESSION) [ResolvedTo(RAW_FIR)] lval d: <implicit> = LAZY_EXPRESSION
}
}
@@ -97,11 +97,11 @@ FILE: [ResolvedTo(IMPORTS)] scriptStatementLevelAsLastStatement.kts
public final [ResolvedTo(RAW_FIR)] val $$result: <implicit> = when () {
Boolean(true) -> {
@Anno[Unresolved](LAZY_EXPRESSION) [ResolvedTo(RAW_FIR)] lval <destruct>: <implicit> = Pair#(LAZY_EXPRESSION, LAZY_EXPRESSION)
@Anno[Unresolved](LAZY_EXPRESSION) [ResolvedTo(RAW_FIR)] lval a: <implicit> = R|<local>/<destruct>|.component1#()
@Anno[Unresolved](LAZY_EXPRESSION) [ResolvedTo(RAW_FIR)] lval b: <implicit> = R|<local>/<destruct>|.component2#()
@Anno[Unresolved](LAZY_EXPRESSION) [ResolvedTo(RAW_FIR)] lval a: <implicit> = LAZY_EXPRESSION
@Anno[Unresolved](LAZY_EXPRESSION) [ResolvedTo(RAW_FIR)] lval b: <implicit> = LAZY_EXPRESSION
@Anno[Unresolved](LAZY_EXPRESSION) [ResolvedTo(RAW_FIR)] lval <destruct>: <implicit> = Pair#(LAZY_EXPRESSION, LAZY_EXPRESSION)
@Anno[Unresolved](LAZY_EXPRESSION) [ResolvedTo(RAW_FIR)] lval c: <implicit> = R|<local>/<destruct>|.component1#()
@Anno[Unresolved](LAZY_EXPRESSION) [ResolvedTo(RAW_FIR)] lval d: <implicit> = R|<local>/<destruct>|.component2#()
@Anno[Unresolved](LAZY_EXPRESSION) [ResolvedTo(RAW_FIR)] lval c: <implicit> = LAZY_EXPRESSION
@Anno[Unresolved](LAZY_EXPRESSION) [ResolvedTo(RAW_FIR)] lval d: <implicit> = LAZY_EXPRESSION
}
}
@@ -152,11 +152,11 @@ FILE: [ResolvedTo(IMPORTS)] scriptStatementLevelAsLastStatement.kts
public final [ResolvedTo(COMPILER_REQUIRED_ANNOTATIONS)] val $$result: <implicit> = when () {
Boolean(true) -> {
@Anno[Unresolved](LAZY_EXPRESSION) [ResolvedTo(RAW_FIR)] lval <destruct>: <implicit> = Pair#(LAZY_EXPRESSION, LAZY_EXPRESSION)
@Anno[Unresolved](LAZY_EXPRESSION) [ResolvedTo(RAW_FIR)] lval a: <implicit> = R|<local>/<destruct>|.component1#()
@Anno[Unresolved](LAZY_EXPRESSION) [ResolvedTo(RAW_FIR)] lval b: <implicit> = R|<local>/<destruct>|.component2#()
@Anno[Unresolved](LAZY_EXPRESSION) [ResolvedTo(RAW_FIR)] lval a: <implicit> = LAZY_EXPRESSION
@Anno[Unresolved](LAZY_EXPRESSION) [ResolvedTo(RAW_FIR)] lval b: <implicit> = LAZY_EXPRESSION
@Anno[Unresolved](LAZY_EXPRESSION) [ResolvedTo(RAW_FIR)] lval <destruct>: <implicit> = Pair#(LAZY_EXPRESSION, LAZY_EXPRESSION)
@Anno[Unresolved](LAZY_EXPRESSION) [ResolvedTo(RAW_FIR)] lval c: <implicit> = R|<local>/<destruct>|.component1#()
@Anno[Unresolved](LAZY_EXPRESSION) [ResolvedTo(RAW_FIR)] lval d: <implicit> = R|<local>/<destruct>|.component2#()
@Anno[Unresolved](LAZY_EXPRESSION) [ResolvedTo(RAW_FIR)] lval c: <implicit> = LAZY_EXPRESSION
@Anno[Unresolved](LAZY_EXPRESSION) [ResolvedTo(RAW_FIR)] lval d: <implicit> = LAZY_EXPRESSION
}
}
@@ -207,11 +207,11 @@ FILE: [ResolvedTo(IMPORTS)] scriptStatementLevelAsLastStatement.kts
public final [ResolvedTo(COMPANION_GENERATION)] val $$result: <implicit> = when () {
Boolean(true) -> {
@Anno[Unresolved](LAZY_EXPRESSION) [ResolvedTo(RAW_FIR)] lval <destruct>: <implicit> = Pair#(LAZY_EXPRESSION, LAZY_EXPRESSION)
@Anno[Unresolved](LAZY_EXPRESSION) [ResolvedTo(RAW_FIR)] lval a: <implicit> = R|<local>/<destruct>|.component1#()
@Anno[Unresolved](LAZY_EXPRESSION) [ResolvedTo(RAW_FIR)] lval b: <implicit> = R|<local>/<destruct>|.component2#()
@Anno[Unresolved](LAZY_EXPRESSION) [ResolvedTo(RAW_FIR)] lval a: <implicit> = LAZY_EXPRESSION
@Anno[Unresolved](LAZY_EXPRESSION) [ResolvedTo(RAW_FIR)] lval b: <implicit> = LAZY_EXPRESSION
@Anno[Unresolved](LAZY_EXPRESSION) [ResolvedTo(RAW_FIR)] lval <destruct>: <implicit> = Pair#(LAZY_EXPRESSION, LAZY_EXPRESSION)
@Anno[Unresolved](LAZY_EXPRESSION) [ResolvedTo(RAW_FIR)] lval c: <implicit> = R|<local>/<destruct>|.component1#()
@Anno[Unresolved](LAZY_EXPRESSION) [ResolvedTo(RAW_FIR)] lval d: <implicit> = R|<local>/<destruct>|.component2#()
@Anno[Unresolved](LAZY_EXPRESSION) [ResolvedTo(RAW_FIR)] lval c: <implicit> = LAZY_EXPRESSION
@Anno[Unresolved](LAZY_EXPRESSION) [ResolvedTo(RAW_FIR)] lval d: <implicit> = LAZY_EXPRESSION
}
}
@@ -262,11 +262,11 @@ FILE: [ResolvedTo(IMPORTS)] scriptStatementLevelAsLastStatement.kts
public final [ResolvedTo(SUPER_TYPES)] val $$result: <implicit> = when () {
Boolean(true) -> {
@Anno[Unresolved](LAZY_EXPRESSION) [ResolvedTo(RAW_FIR)] lval <destruct>: <implicit> = Pair#(LAZY_EXPRESSION, LAZY_EXPRESSION)
@Anno[Unresolved](LAZY_EXPRESSION) [ResolvedTo(RAW_FIR)] lval a: <implicit> = R|<local>/<destruct>|.component1#()
@Anno[Unresolved](LAZY_EXPRESSION) [ResolvedTo(RAW_FIR)] lval b: <implicit> = R|<local>/<destruct>|.component2#()
@Anno[Unresolved](LAZY_EXPRESSION) [ResolvedTo(RAW_FIR)] lval a: <implicit> = LAZY_EXPRESSION
@Anno[Unresolved](LAZY_EXPRESSION) [ResolvedTo(RAW_FIR)] lval b: <implicit> = LAZY_EXPRESSION
@Anno[Unresolved](LAZY_EXPRESSION) [ResolvedTo(RAW_FIR)] lval <destruct>: <implicit> = Pair#(LAZY_EXPRESSION, LAZY_EXPRESSION)
@Anno[Unresolved](LAZY_EXPRESSION) [ResolvedTo(RAW_FIR)] lval c: <implicit> = R|<local>/<destruct>|.component1#()
@Anno[Unresolved](LAZY_EXPRESSION) [ResolvedTo(RAW_FIR)] lval d: <implicit> = R|<local>/<destruct>|.component2#()
@Anno[Unresolved](LAZY_EXPRESSION) [ResolvedTo(RAW_FIR)] lval c: <implicit> = LAZY_EXPRESSION
@Anno[Unresolved](LAZY_EXPRESSION) [ResolvedTo(RAW_FIR)] lval d: <implicit> = LAZY_EXPRESSION
}
}
@@ -317,11 +317,11 @@ FILE: [ResolvedTo(IMPORTS)] scriptStatementLevelAsLastStatement.kts
public final [ResolvedTo(TYPES)] val $$result: <implicit> = when () {
Boolean(true) -> {
@Anno[Unresolved](LAZY_EXPRESSION) [ResolvedTo(RAW_FIR)] lval <destruct>: <implicit> = Pair#(LAZY_EXPRESSION, LAZY_EXPRESSION)
@Anno[Unresolved](LAZY_EXPRESSION) [ResolvedTo(RAW_FIR)] lval a: <implicit> = R|<local>/<destruct>|.component1#()
@Anno[Unresolved](LAZY_EXPRESSION) [ResolvedTo(RAW_FIR)] lval b: <implicit> = R|<local>/<destruct>|.component2#()
@Anno[Unresolved](LAZY_EXPRESSION) [ResolvedTo(RAW_FIR)] lval a: <implicit> = LAZY_EXPRESSION
@Anno[Unresolved](LAZY_EXPRESSION) [ResolvedTo(RAW_FIR)] lval b: <implicit> = LAZY_EXPRESSION
@Anno[Unresolved](LAZY_EXPRESSION) [ResolvedTo(RAW_FIR)] lval <destruct>: <implicit> = Pair#(LAZY_EXPRESSION, LAZY_EXPRESSION)
@Anno[Unresolved](LAZY_EXPRESSION) [ResolvedTo(RAW_FIR)] lval c: <implicit> = R|<local>/<destruct>|.component1#()
@Anno[Unresolved](LAZY_EXPRESSION) [ResolvedTo(RAW_FIR)] lval d: <implicit> = R|<local>/<destruct>|.component2#()
@Anno[Unresolved](LAZY_EXPRESSION) [ResolvedTo(RAW_FIR)] lval c: <implicit> = LAZY_EXPRESSION
@Anno[Unresolved](LAZY_EXPRESSION) [ResolvedTo(RAW_FIR)] lval d: <implicit> = LAZY_EXPRESSION
}
}
@@ -372,11 +372,11 @@ FILE: [ResolvedTo(IMPORTS)] scriptStatementLevelAsLastStatement.kts
public final [ResolvedTo(STATUS)] val $$result: <implicit> = when () {
Boolean(true) -> {
@Anno[Unresolved](LAZY_EXPRESSION) [ResolvedTo(RAW_FIR)] lval <destruct>: <implicit> = Pair#(LAZY_EXPRESSION, LAZY_EXPRESSION)
@Anno[Unresolved](LAZY_EXPRESSION) [ResolvedTo(RAW_FIR)] lval a: <implicit> = R|<local>/<destruct>|.component1#()
@Anno[Unresolved](LAZY_EXPRESSION) [ResolvedTo(RAW_FIR)] lval b: <implicit> = R|<local>/<destruct>|.component2#()
@Anno[Unresolved](LAZY_EXPRESSION) [ResolvedTo(RAW_FIR)] lval a: <implicit> = LAZY_EXPRESSION
@Anno[Unresolved](LAZY_EXPRESSION) [ResolvedTo(RAW_FIR)] lval b: <implicit> = LAZY_EXPRESSION
@Anno[Unresolved](LAZY_EXPRESSION) [ResolvedTo(RAW_FIR)] lval <destruct>: <implicit> = Pair#(LAZY_EXPRESSION, LAZY_EXPRESSION)
@Anno[Unresolved](LAZY_EXPRESSION) [ResolvedTo(RAW_FIR)] lval c: <implicit> = R|<local>/<destruct>|.component1#()
@Anno[Unresolved](LAZY_EXPRESSION) [ResolvedTo(RAW_FIR)] lval d: <implicit> = R|<local>/<destruct>|.component2#()
@Anno[Unresolved](LAZY_EXPRESSION) [ResolvedTo(RAW_FIR)] lval c: <implicit> = LAZY_EXPRESSION
@Anno[Unresolved](LAZY_EXPRESSION) [ResolvedTo(RAW_FIR)] lval d: <implicit> = LAZY_EXPRESSION
}
}
@@ -427,11 +427,11 @@ FILE: [ResolvedTo(IMPORTS)] scriptStatementLevelAsLastStatement.kts
public final [ResolvedTo(EXPECT_ACTUAL_MATCHING)] val $$result: <implicit> = when () {
Boolean(true) -> {
@Anno[Unresolved](LAZY_EXPRESSION) [ResolvedTo(RAW_FIR)] lval <destruct>: <implicit> = Pair#(LAZY_EXPRESSION, LAZY_EXPRESSION)
@Anno[Unresolved](LAZY_EXPRESSION) [ResolvedTo(RAW_FIR)] lval a: <implicit> = R|<local>/<destruct>|.component1#()
@Anno[Unresolved](LAZY_EXPRESSION) [ResolvedTo(RAW_FIR)] lval b: <implicit> = R|<local>/<destruct>|.component2#()
@Anno[Unresolved](LAZY_EXPRESSION) [ResolvedTo(RAW_FIR)] lval a: <implicit> = LAZY_EXPRESSION
@Anno[Unresolved](LAZY_EXPRESSION) [ResolvedTo(RAW_FIR)] lval b: <implicit> = LAZY_EXPRESSION
@Anno[Unresolved](LAZY_EXPRESSION) [ResolvedTo(RAW_FIR)] lval <destruct>: <implicit> = Pair#(LAZY_EXPRESSION, LAZY_EXPRESSION)
@Anno[Unresolved](LAZY_EXPRESSION) [ResolvedTo(RAW_FIR)] lval c: <implicit> = R|<local>/<destruct>|.component1#()
@Anno[Unresolved](LAZY_EXPRESSION) [ResolvedTo(RAW_FIR)] lval d: <implicit> = R|<local>/<destruct>|.component2#()
@Anno[Unresolved](LAZY_EXPRESSION) [ResolvedTo(RAW_FIR)] lval c: <implicit> = LAZY_EXPRESSION
@Anno[Unresolved](LAZY_EXPRESSION) [ResolvedTo(RAW_FIR)] lval d: <implicit> = LAZY_EXPRESSION
}
}
@@ -482,11 +482,11 @@ FILE: [ResolvedTo(IMPORTS)] scriptStatementLevelAsLastStatement.kts
public final [ResolvedTo(CONTRACTS)] val $$result: <implicit> = when () {
Boolean(true) -> {
@Anno[Unresolved](LAZY_EXPRESSION) [ResolvedTo(RAW_FIR)] lval <destruct>: <implicit> = Pair#(LAZY_EXPRESSION, LAZY_EXPRESSION)
@Anno[Unresolved](LAZY_EXPRESSION) [ResolvedTo(RAW_FIR)] lval a: <implicit> = R|<local>/<destruct>|.component1#()
@Anno[Unresolved](LAZY_EXPRESSION) [ResolvedTo(RAW_FIR)] lval b: <implicit> = R|<local>/<destruct>|.component2#()
@Anno[Unresolved](LAZY_EXPRESSION) [ResolvedTo(RAW_FIR)] lval a: <implicit> = LAZY_EXPRESSION
@Anno[Unresolved](LAZY_EXPRESSION) [ResolvedTo(RAW_FIR)] lval b: <implicit> = LAZY_EXPRESSION
@Anno[Unresolved](LAZY_EXPRESSION) [ResolvedTo(RAW_FIR)] lval <destruct>: <implicit> = Pair#(LAZY_EXPRESSION, LAZY_EXPRESSION)
@Anno[Unresolved](LAZY_EXPRESSION) [ResolvedTo(RAW_FIR)] lval c: <implicit> = R|<local>/<destruct>|.component1#()
@Anno[Unresolved](LAZY_EXPRESSION) [ResolvedTo(RAW_FIR)] lval d: <implicit> = R|<local>/<destruct>|.component2#()
@Anno[Unresolved](LAZY_EXPRESSION) [ResolvedTo(RAW_FIR)] lval c: <implicit> = LAZY_EXPRESSION
@Anno[Unresolved](LAZY_EXPRESSION) [ResolvedTo(RAW_FIR)] lval d: <implicit> = LAZY_EXPRESSION
}
}
@@ -198,6 +198,14 @@ open class PsiRawFirBuilder(
(this as KtAnnotated).extractAnnotationsTo(target)
}
override fun createComponentCall(
container: FirVariable,
entrySource: KtSourceElement?,
index: Int,
): FirExpression = buildOrLazyExpression(entrySource) {
super.createComponentCall(container, entrySource, index)
}
private inline fun <reified R : FirElement> KtElement?.convertSafe(): R? =
this?.let { convertElement(it, null) } as? R
@@ -1280,6 +1288,10 @@ open class PsiRawFirBuilder(
}
}
protected fun configureScriptDestructuringDeclarationEntry(declaration: FirVariable, container: FirVariable) {
(declaration as FirProperty).destructuringDeclarationContainerVariable = container.symbol
}
protected fun buildScriptDestructuringDeclaration(destructuringDeclaration: KtDestructuringDeclaration): FirVariable {
val initializer = destructuringDeclaration.initializer
val firInitializer = buildOrLazyExpression(initializer?.toFirSourceElement()) {
@@ -1344,7 +1356,7 @@ open class PsiRawFirBuilder(
tmpVariable = false,
localEntries = false,
) {
(it as FirProperty).destructuringDeclarationContainerVariable = destructuringContainerVar.symbol
configureScriptDestructuringDeclarationEntry(it, destructuringContainerVar)
}
}
else -> {
@@ -2,16 +2,16 @@ FILE: scriptLevelDestructuringWithAnnotation.kts
SCRIPT: <script-scriptLevelDestructuringWithAnnotation.kts>
@DestrAnno(LAZY_EXPRESSION) lval <destruct>: <implicit> = LAZY_EXPRESSION
@LeftAnno(LAZY_EXPRESSION) public final val a: <implicit> = R|<local>/<destruct>|.component1#()
@LeftAnno(LAZY_EXPRESSION) public final val a: <implicit> = LAZY_EXPRESSION
@RightAnno(LAZY_EXPRESSION) public final val b: <implicit> = R|<local>/<destruct>|.component2#()
@RightAnno(LAZY_EXPRESSION) public final val b: <implicit> = LAZY_EXPRESSION
@Destr2Anno(LAZY_EXPRESSION) lval <destruct>: <implicit> = LAZY_EXPRESSION
@SecondLeftAnno(LAZY_EXPRESSION) public final val c: <implicit> = R|<local>/<destruct>|.component1#()
@SecondLeftAnno(LAZY_EXPRESSION) public final val c: <implicit> = LAZY_EXPRESSION
@SecondRightAnno(LAZY_EXPRESSION) public final val d: <implicit> = R|<local>/<destruct>|.component2#()
@SecondRightAnno(LAZY_EXPRESSION) public final val d: <implicit> = LAZY_EXPRESSION
@@ -5,12 +5,12 @@ FILE: scriptStatementLevelDestructuringWithAnnotationAsLastStatement.kts
when () {
Boolean(true) -> {
@DestrAnno(LAZY_EXPRESSION) lval <destruct>: <implicit> = IntegerLiteral(0).to#(IntegerLiteral(1))
@LeftAnno(LAZY_EXPRESSION) lval a: <implicit> = R|<local>/<destruct>|.component1#()
@RightAnno(LAZY_EXPRESSION) lval b: <implicit> = R|<local>/<destruct>|.component2#()
@LeftAnno(LAZY_EXPRESSION) lval a: <implicit> = LAZY_EXPRESSION
@RightAnno(LAZY_EXPRESSION) lval b: <implicit> = LAZY_EXPRESSION
run#(<L> = LAZY_EXPRESSION)
@Destr2Anno(LAZY_EXPRESSION) lval <destruct>: <implicit> = IntegerLiteral(2).to#(IntegerLiteral(3))
@SecondLeftAnno(LAZY_EXPRESSION) lval c: <implicit> = R|<local>/<destruct>|.component1#()
@SecondRightAnno(LAZY_EXPRESSION) lval d: <implicit> = R|<local>/<destruct>|.component2#()
@SecondLeftAnno(LAZY_EXPRESSION) lval c: <implicit> = LAZY_EXPRESSION
@SecondRightAnno(LAZY_EXPRESSION) lval d: <implicit> = LAZY_EXPRESSION
}
}
@@ -13,6 +13,7 @@ import org.jetbrains.kotlin.fir.declarations.FirDeclarationOrigin
import org.jetbrains.kotlin.fir.declarations.FirVariable
import org.jetbrains.kotlin.fir.declarations.builder.buildProperty
import org.jetbrains.kotlin.fir.declarations.impl.FirDeclarationStatusImpl
import org.jetbrains.kotlin.fir.expressions.FirExpression
import org.jetbrains.kotlin.fir.symbols.FirBasedSymbol
import org.jetbrains.kotlin.fir.symbols.impl.FirPropertySymbol
import org.jetbrains.kotlin.fir.types.FirTypeRef
@@ -23,6 +24,9 @@ interface DestructuringContext<T> {
val T.name: Name
val T.source: KtSourceElement
fun T.extractAnnotationsTo(target: FirAnnotationContainerBuilder, containerSymbol: FirBasedSymbol<*>)
fun createComponentCall(container: FirVariable, entrySource: KtSourceElement?, index: Int): FirExpression {
return container.toComponentCall(entrySource, index)
}
}
context(AbstractRawFirBuilder<*>, DestructuringContext<T>)
@@ -39,20 +43,39 @@ fun <T> MutableList<in FirVariable>.addDestructuringVariables(
this += container
}
for ((index, entry) in entries.withIndex()) {
this += buildProperty {
symbol = FirPropertySymbol(entry.name)
withContainerSymbol(symbol, localEntries) {
this.moduleData = moduleData
origin = FirDeclarationOrigin.Source
returnTypeRef = entry.returnTypeRef
name = entry.name
initializer = container.toComponentCall(entry.source, index)
this.isVar = isVar
source = entry.source
isLocal = localEntries
status = FirDeclarationStatusImpl(if (localEntries) Visibilities.Local else Visibilities.Public, Modality.FINAL)
entry.extractAnnotationsTo(this, context.containerSymbol)
}
}.also(configure)
this += buildDestructuringVariable(
moduleData,
container,
entry,
isVar,
localEntries,
index,
configure,
)
}
}
}
context(AbstractRawFirBuilder<*>, DestructuringContext<T>)
fun <T> buildDestructuringVariable(
moduleData: FirModuleData,
container: FirVariable,
entry: T,
isVar: Boolean,
localEntries: Boolean,
index: Int,
configure: (FirVariable) -> Unit = {}
): FirVariable = buildProperty {
symbol = FirPropertySymbol(entry.name)
withContainerSymbol(symbol, localEntries) {
this.moduleData = moduleData
origin = FirDeclarationOrigin.Source
returnTypeRef = entry.returnTypeRef
name = entry.name
initializer = createComponentCall(container, entry.source, index)
this.isVar = isVar
source = entry.source
isLocal = localEntries
status = FirDeclarationStatusImpl(if (localEntries) Visibilities.Local else Visibilities.Public, Modality.FINAL)
entry.extractAnnotationsTo(this, context.containerSymbol)
}
}.also(configure)