Destroy locals at the end of blocks, not only on return (#7448)

Destroy local variables and temporaries at each `}`, and when branching
with `break` and `continue`. In `for` statements, destroy loop variables
along with anything created within the loop at the end of each loop
iteration, and destroy the cursor and range object when the loop
terminates.

Assisted-by: Gemini via Antigravity

---------

Co-authored-by: Geoff Romer <gromer@google.com>
This commit is contained in:
Richard Smith
2026-07-14 00:59:22 +00:00
committed by GitHub
co-authored by Geoff Romer
parent 8bae79f44a
commit 6e62a7d4a2
29 changed files with 1803 additions and 266 deletions
+3 -1
View File
@@ -3,6 +3,7 @@
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#include "toolchain/check/context.h"
#include "toolchain/check/control_flow.h"
#include "toolchain/check/handle.h"
#include "toolchain/check/unused.h"
@@ -11,11 +12,12 @@ namespace Carbon::Check {
auto HandleParseNode(Context& context, Parse::CodeBlockStartId node_id)
-> bool {
context.node_stack().Push(node_id);
context.scope_stack().PushForSameRegion();
context.scope_stack().PushForSameRegion(ScopeStack::CleanupScopeKind::Owned);
return true;
}
auto HandleParseNode(Context& context, Parse::CodeBlockId /*node_id*/) -> bool {
AddAndDiscardCleanups(context);
context.scope_stack().Pop(/*check_unused=*/true);
context.node_stack()
.PopAndDiscardSoloNodeId<Parse::NodeKind::CodeBlockStart>();