diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/range/forLoop/AbstractForLoopGenerator.kt b/compiler/backend/src/org/jetbrains/kotlin/codegen/range/forLoop/AbstractForLoopGenerator.kt index 8f98dc86f06..1dd487b0182 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/range/forLoop/AbstractForLoopGenerator.kt +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/range/forLoop/AbstractForLoopGenerator.kt @@ -101,13 +101,17 @@ abstract class AbstractForLoopGenerator( }) } - v.visitLabel(destructuringStartLabel) - codegen.initializeDestructuringDeclarationVariables( destructuringDeclaration, TransientReceiver(elementType), StackValue.local(loopParameterVar, asmElementType) ) + + // Start the scope for the destructuring variables only after a values + // has been written to their locals slot. Otherwise, we can generate + // type-incorrect locals information because the local could have previously + // been used for a value of incompatible type. + v.visitLabel(destructuringStartLabel) } protected abstract fun assignToLoopParameter()