[FIR2IR, IR] Fix for-loop withIndex lowering for name-based destructuring
#KT-80243 Fixed
diff --git a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/generators/CallAndReferenceGenerator.kt b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/generators/CallAndReferenceGenerator.kt
index 3f28727..4c90332 100644
--- a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/generators/CallAndReferenceGenerator.kt
+++ b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/generators/CallAndReferenceGenerator.kt
@@ -639,9 +639,7 @@
contextParameterCount = property.contextParameters.size,
hasDispatchReceiver = property.dispatchReceiverType != null,
hasExtensionReceiver = property.isExtension,
- origin = incOrDecSourceKindToIrStatementOrigin[qualifiedAccess.source?.kind]
- ?: augmentedAssignSourceKindToIrStatementOrigin[qualifiedAccess.source?.kind]
- ?: IrStatementOrigin.GET_PROPERTY,
+ origin = qualifiedAccess.source.propertyAccessOrigin(),
superQualifierSymbol = dispatchReceiver?.superQualifierSymbolForFunctionAndPropertyAccess()
)
}
diff --git a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/utils/OriginUtils.kt b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/utils/OriginUtils.kt
index a7561a3..105cf81 100644
--- a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/utils/OriginUtils.kt
+++ b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/utils/OriginUtils.kt
@@ -8,6 +8,7 @@
import com.intellij.psi.tree.IElementType
import org.jetbrains.kotlin.KtFakeSourceElementKind
import org.jetbrains.kotlin.KtNodeTypes
+import org.jetbrains.kotlin.KtSourceElement
import org.jetbrains.kotlin.builtins.StandardNames.DATA_CLASS_COMPONENT_PREFIX
import org.jetbrains.kotlin.fir.backend.Fir2IrComponents
import org.jetbrains.kotlin.fir.containingClassLookupTag
@@ -55,7 +56,7 @@
fun FirDeclaration?.computeIrOrigin(
predefinedOrigin: IrDeclarationOrigin? = null,
parentOrigin: IrDeclarationOrigin? = null,
- fakeOverrideOwnerLookupTag: ConeClassLikeLookupTag? = null
+ fakeOverrideOwnerLookupTag: ConeClassLikeLookupTag? = null,
): IrDeclarationOrigin {
if (this == null) {
return predefinedOrigin ?: parentOrigin ?: IrDeclarationOrigin.DEFINED
@@ -208,3 +209,16 @@
KtFakeSourceElementKind.DesugaredPrefixIncSecondGetReference to IrStatementOrigin.PREFIX_INCR,
KtFakeSourceElementKind.DesugaredPrefixDecSecondGetReference to IrStatementOrigin.PREFIX_DECR
)
+
+
+fun KtSourceElement?.propertyAccessOrigin(): IrStatementOrigin {
+ val kind = this?.kind
+
+ if (kind == KtFakeSourceElementKind.DesugaredNameBasedDestructuring) {
+ return IrStatementOrigin.NAME_BASED_DESTRUCTURING_ACCESS
+ }
+
+ return incOrDecSourceKindToIrStatementOrigin[kind]
+ ?: augmentedAssignSourceKindToIrStatementOrigin[kind]
+ ?: IrStatementOrigin.GET_PROPERTY
+}
diff --git a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/loops/ForLoopsLowering.kt b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/loops/ForLoopsLowering.kt
index 6c2201a..bb9d155 100644
--- a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/loops/ForLoopsLowering.kt
+++ b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/loops/ForLoopsLowering.kt
@@ -357,7 +357,7 @@
override fun visitCall(expression: IrCall) {
val candidateCall = when (expression.origin) {
IrStatementOrigin.FOR_LOOP_NEXT -> expression
- is IrStatementOrigin.COMPONENT_N ->
+ is IrStatementOrigin.COMPONENT_N, IrStatementOrigin.NAME_BASED_DESTRUCTURING_ACCESS ->
if (mainLoopVariable != null && (expression.dispatchReceiver as? IrGetValue)?.symbol == mainLoopVariable.symbol) {
expression
} else {
@@ -448,6 +448,10 @@
loopVariableComponents[origin.index] = stmt
loopVariableComponentIndices.add(i)
}
+ IrStatementOrigin.NAME_BASED_DESTRUCTURING_ACCESS -> {
+ loopVariableComponents[i] = stmt
+ loopVariableComponentIndices.add(i)
+ }
}
}
diff --git a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/expressions/IrStatementOrigin.kt b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/expressions/IrStatementOrigin.kt
index effc1f8..41f6ac0 100644
--- a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/expressions/IrStatementOrigin.kt
+++ b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/expressions/IrStatementOrigin.kt
@@ -130,6 +130,7 @@
val INLINED_FUNCTION_REFERENCE by IrStatementOriginImpl
val INLINE_LAMBDA by IrStatementOriginImpl
val INLINE_ARGS_CONTAINER by IrStatementOriginImpl
+ val NAME_BASED_DESTRUCTURING_ACCESS by IrStatementOriginImpl
}
data class COMPONENT_N private constructor(val index: Int) : IrStatementOrigin {
diff --git a/compiler/testData/codegen/box/multiDecl/nameBasedDestructuringShortFormWithIndex.kt b/compiler/testData/codegen/box/multiDecl/nameBasedDestructuringShortFormWithIndex.kt
index 9afa178..c39c0ad 100644
--- a/compiler/testData/codegen/box/multiDecl/nameBasedDestructuringShortFormWithIndex.kt
+++ b/compiler/testData/codegen/box/multiDecl/nameBasedDestructuringShortFormWithIndex.kt
@@ -1,5 +1,4 @@
// WITH_STDLIB
-// IGNORE_BACKEND_K2: ANY
// ISSUE: KT-80243
// LANGUAGE: +NameBasedDestructuring, +EnableNameBasedDestructuringShortForm