From b00858a886ea6c50f7acf97bbe5e8563cedd8fd5 Mon Sep 17 00:00:00 2001 From: Mads Ager Date: Mon, 23 Sep 2019 13:48:27 +0200 Subject: [PATCH] Start local var scope for destructuring variables after init. If the scope for a local variable is started before a value has been written, another value from a previous use of the local slot can be present. That value could have a different type which would lead to weird debugging situations and also leads to other tools (such as D8) rejecting the locals information as it is invalid. Fixes KT-33959. --- .../codegen/range/forLoop/AbstractForLoopGenerator.kt | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) 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()