mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-05 22:02:55 +01:00
Destroy temporaries at the end of expression statements. (#7513)
Instead of tracking the cleanup scope depth on entry to each scope, track an "ambient" cleanup scope depth that's *after* the destructors of local variables in that scope. This gets increased to include the destructors of local variables when we create a name-binding declaration. Then, when we reach a point where temporaries should be destroyed, run cleanups that are after the ambient cleanup scope depth on the stack. This happens: * At the `;` of a statement expression. * At the `)` of an `if` or `while` statement. * After performing the implied `HasValue()` call in a `for` statement. Per informal agreement with leads, this means we lifetime-extend all temporaries created in the initializer of a name-binding declaration to the full scope of that declaration, but that temporaries created in an expression statement are destroyed at the `;`.
This commit is contained in:
@@ -161,7 +161,13 @@ static auto AddCleanups(Context& context, ScopeStack::CleanupScopeDepth depth)
|
||||
}
|
||||
}
|
||||
|
||||
auto AddAndDiscardCleanups(Context& context) -> void {
|
||||
auto AddAndDiscardTemporaryCleanups(Context& context) -> void {
|
||||
auto depth = context.scope_stack().ambient_cleanup_scope_depth();
|
||||
AddCleanups(context, depth);
|
||||
context.scope_stack().DiscardCleanupsSince(depth);
|
||||
}
|
||||
|
||||
auto AddAndDiscardScopeCleanups(Context& context) -> void {
|
||||
auto depth = context.scope_stack().enclosing_cleanup_scope_depth();
|
||||
AddCleanups(context, depth);
|
||||
context.scope_stack().DiscardCleanupsSince(depth);
|
||||
|
||||
Reference in New Issue
Block a user