diff --git a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrDeclarationStorage.kt b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrDeclarationStorage.kt index 04b57fb34d5..463a570505c 100644 --- a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrDeclarationStorage.kt +++ b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrDeclarationStorage.kt @@ -1210,6 +1210,9 @@ class Fir2IrDeclarationStorage( irParent: IrDeclarationParent, givenOrigin: IrDeclarationOrigin? = null ): IrVariable = convertCatching(variable) { + // Note: for components call, we have to change type here (to original component type) to keep compatibility with PSI2IR + // Some backend optimizations related to withIndex() probably depend on this type: index should always be Int + // See e.g. forInStringWithIndexWithExplicitlyTypedIndexVariable.kt from codegen box tests val type = ((variable.initializer as? FirComponentCall)?.typeRef ?: variable.returnTypeRef).toIrType() // Some temporary variables are produced in RawFirBuilder, but we consistently use special names for them. val origin = when { 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 9c470f05a0e..a92845e9ca9 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 @@ -458,7 +458,10 @@ class CallAndReferenceGenerator( is IrValueSymbol -> { IrGetValueImpl( - startOffset, endOffset, type, symbol, + // Note: sometimes we change an IR type of local variable + // (see component call case: Fir2IrDeclarationStorage.createIrVariable -> val type = ...) + // That's why we should use here v the IR variable type and not FIR converted type (to prevent IR inconsistency) + startOffset, endOffset, symbol.owner.type, symbol, origin = if (variableAsFunctionMode) IrStatementOrigin.VARIABLE_AS_FUNCTION else calleeReference.statementOrigin() ) diff --git a/compiler/testData/codegen/box/controlStructures/forInArrayWithIndex/forInArrayWithIndexWithExplicitlyTypedIndexVariable.kt b/compiler/testData/codegen/box/controlStructures/forInArrayWithIndex/forInArrayWithIndexWithExplicitlyTypedIndexVariable.kt index 1e3050e5bb0..18eac0ccf04 100644 --- a/compiler/testData/codegen/box/controlStructures/forInArrayWithIndex/forInArrayWithIndexWithExplicitlyTypedIndexVariable.kt +++ b/compiler/testData/codegen/box/controlStructures/forInArrayWithIndex/forInArrayWithIndexWithExplicitlyTypedIndexVariable.kt @@ -1,6 +1,5 @@ // WITH_STDLIB // KT-55458 -// IGNORE_BACKEND_K2: NATIVE val arr = arrayOf("a", "b", "c", "d") diff --git a/compiler/testData/codegen/box/controlStructures/forInCharSequenceWithIndex/forInStringWithIndexWithExplicitlyTypedIndexVariable.kt b/compiler/testData/codegen/box/controlStructures/forInCharSequenceWithIndex/forInStringWithIndexWithExplicitlyTypedIndexVariable.kt index 97667d9503b..f7be1c13935 100644 --- a/compiler/testData/codegen/box/controlStructures/forInCharSequenceWithIndex/forInStringWithIndexWithExplicitlyTypedIndexVariable.kt +++ b/compiler/testData/codegen/box/controlStructures/forInCharSequenceWithIndex/forInStringWithIndexWithExplicitlyTypedIndexVariable.kt @@ -1,6 +1,5 @@ // WITH_STDLIB // KT-55458 -// IGNORE_BACKEND_K2: NATIVE val xs = "abcd" diff --git a/compiler/testData/codegen/box/controlStructures/forInIterableWithIndex/forInListWithIndexWithExplicitlyTypedIndexVariable.kt b/compiler/testData/codegen/box/controlStructures/forInIterableWithIndex/forInListWithIndexWithExplicitlyTypedIndexVariable.kt index e80ba8e50a4..1e202c5a95f 100644 --- a/compiler/testData/codegen/box/controlStructures/forInIterableWithIndex/forInListWithIndexWithExplicitlyTypedIndexVariable.kt +++ b/compiler/testData/codegen/box/controlStructures/forInIterableWithIndex/forInListWithIndexWithExplicitlyTypedIndexVariable.kt @@ -1,6 +1,5 @@ // WITH_STDLIB // KT-55458 -// IGNORE_BACKEND_K2: NATIVE val xs = listOf("a", "b", "c", "d") diff --git a/compiler/testData/codegen/box/controlStructures/forInSequenceWithIndex/forInSequenceWithIndexWithExplicitlyTypedIndexVariable.kt b/compiler/testData/codegen/box/controlStructures/forInSequenceWithIndex/forInSequenceWithIndexWithExplicitlyTypedIndexVariable.kt index a228c693714..e79300e9ca4 100644 --- a/compiler/testData/codegen/box/controlStructures/forInSequenceWithIndex/forInSequenceWithIndexWithExplicitlyTypedIndexVariable.kt +++ b/compiler/testData/codegen/box/controlStructures/forInSequenceWithIndex/forInSequenceWithIndexWithExplicitlyTypedIndexVariable.kt @@ -1,6 +1,5 @@ // WITH_STDLIB // KT-55458 -// IGNORE_BACKEND_K2: NATIVE val xs = listOf("a", "b", "c", "d").asSequence() diff --git a/compiler/testData/ir/irText/firProblems/kt55458.fir.ir.txt b/compiler/testData/ir/irText/firProblems/kt55458.fir.ir.txt index 393b73ef70d..4d81a9af583 100644 --- a/compiler/testData/ir/irText/firProblems/kt55458.fir.ir.txt +++ b/compiler/testData/ir/irText/firProblems/kt55458.fir.ir.txt @@ -11,4 +11,4 @@ FILE fqName: fileName:/kt55458.kt CALL 'public final fun component1 (): A of kotlin.Pair [operator] declared in kotlin.Pair' type=kotlin.Int origin=COMPONENT_N(index=1) $this: GET_VAR 'val tmp_0: kotlin.Pair [val] declared in .main' type=kotlin.Pair origin=null CALL 'public final fun println (message: kotlin.Any?): kotlin.Unit [inline] declared in kotlin.io.ConsoleKt' type=kotlin.Unit origin=null - message: GET_VAR 'val a: kotlin.Int [val] declared in .main' type=kotlin.Any origin=null + message: GET_VAR 'val a: kotlin.Int [val] declared in .main' type=kotlin.Int origin=null diff --git a/compiler/testData/ir/irText/types/nullChecks/enhancedNullabilityInDestructuringAssignment.fir.ir.txt b/compiler/testData/ir/irText/types/nullChecks/enhancedNullabilityInDestructuringAssignment.fir.ir.txt index 1a2a0d5c4ef..3932fc39be9 100644 --- a/compiler/testData/ir/irText/types/nullChecks/enhancedNullabilityInDestructuringAssignment.fir.ir.txt +++ b/compiler/testData/ir/irText/types/nullChecks/enhancedNullabilityInDestructuringAssignment.fir.ir.txt @@ -142,8 +142,8 @@ FILE fqName: fileName:/enhancedNullabilityInDestructuringAssignment.kt CALL 'public final fun component2 (): T2 of .Q [operator] declared in .Q' type=@[EnhancedNullability] kotlin.String origin=COMPONENT_N(index=2) $this: GET_VAR 'val tmp_1: @[FlexibleNullability] .Q<@[EnhancedNullability] kotlin.String, @[EnhancedNullability] kotlin.String>? [val] declared in .test2' type=@[FlexibleNullability] .Q<@[EnhancedNullability] kotlin.String, @[EnhancedNullability] kotlin.String>? origin=null CALL 'public final fun use (x: kotlin.Any, y: kotlin.Any): kotlin.Unit declared in ' type=kotlin.Unit origin=null - x: GET_VAR 'val x: @[EnhancedNullability] kotlin.String [val] declared in .test2' type=kotlin.String origin=null - y: GET_VAR 'val y: @[EnhancedNullability] kotlin.String [val] declared in .test2' type=kotlin.String origin=null + x: GET_VAR 'val x: @[EnhancedNullability] kotlin.String [val] declared in .test2' type=@[EnhancedNullability] kotlin.String origin=null + y: GET_VAR 'val y: @[EnhancedNullability] kotlin.String [val] declared in .test2' type=@[EnhancedNullability] kotlin.String origin=null FUN name:test2Desugared visibility:public modality:FINAL <> () returnType:kotlin.Unit BLOCK_BODY VAR name:tmp type:@[FlexibleNullability] .Q<@[EnhancedNullability] kotlin.String, @[EnhancedNullability] kotlin.String>? [val] @@ -173,8 +173,8 @@ FILE fqName: fileName:/enhancedNullabilityInDestructuringAssignment.kt CALL 'public final fun component2 (): T2 of .Q [operator] declared in .Q' type=@[EnhancedNullability] kotlin.String origin=COMPONENT_N(index=2) $this: GET_VAR 'val tmp_2: .Q<@[EnhancedNullability] kotlin.String, @[EnhancedNullability] kotlin.String> [val] declared in .test3' type=.Q<@[EnhancedNullability] kotlin.String, @[EnhancedNullability] kotlin.String> origin=null CALL 'public final fun use (x: kotlin.Any, y: kotlin.Any): kotlin.Unit declared in ' type=kotlin.Unit origin=null - x: GET_VAR 'val x: @[EnhancedNullability] kotlin.String [val] declared in .test3' type=kotlin.String origin=null - y: GET_VAR 'val y: @[EnhancedNullability] kotlin.String [val] declared in .test3' type=kotlin.String origin=null + x: GET_VAR 'val x: @[EnhancedNullability] kotlin.String [val] declared in .test3' type=@[EnhancedNullability] kotlin.String origin=null + y: GET_VAR 'val y: @[EnhancedNullability] kotlin.String [val] declared in .test3' type=@[EnhancedNullability] kotlin.String origin=null FUN name:test4 visibility:public modality:FINAL <> () returnType:kotlin.Unit BLOCK_BODY VAR IR_TEMPORARY_VARIABLE name:tmp_3 type:kotlin.collections.IndexedValue<@[EnhancedNullability] .P> [val] @@ -193,4 +193,4 @@ FILE fqName: fileName:/enhancedNullabilityInDestructuringAssignment.kt $this: GET_VAR 'val tmp_3: kotlin.collections.IndexedValue<@[EnhancedNullability] .P> [val] declared in .test4' type=kotlin.collections.IndexedValue<@[EnhancedNullability] .P> origin=null CALL 'public final fun use (x: kotlin.Any, y: kotlin.Any): kotlin.Unit declared in ' type=kotlin.Unit origin=null x: GET_VAR 'val x: kotlin.Int [val] declared in .test4' type=kotlin.Int origin=null - y: GET_VAR 'val y: @[EnhancedNullability] .P [val] declared in .test4' type=.P origin=null + y: GET_VAR 'val y: @[EnhancedNullability] .P [val] declared in .test4' type=@[EnhancedNullability] .P origin=null