GCC Newspaper
JULY 29, 2026
Date
/
Architectures
Components
Topics
News & Policy
c++

C++: co_await and structured bindings: Fix guard variable lifetime.

Structured bindings used with `co_await` now correctly manage temporary guard variables.

This commit fixes an issue where temporary guard variables controlling the cleanup of structured binding variables were not surviving across co_await expressions. The problem stemmed from using TARGET_EXPR, which has a limited lifetime, to hold values that needed to persist as long as the structured binding itself. The solution uses get_temp_regvar for these guards and manually pushdecls them to ensure visibility and correct lifetime management, resolving PR124584.

In Details

The cp_finish_decl function now uses get_temp_regvar instead of TARGET_EXPR to manage the lifetime of internal guard variables used for structured bindings within coroutines. These guards were previously not promoted to the coroutine frame, causing them to be destroyed prematurely after co_await. Manually pushdecling the guard variable ensures it's visible to register_local_var_uses. This addresses PR124584.

For Context
Structured bindings
A C++17 language feature that allows variables to be declared and initialized from the elements of an array or the members of a struct/tuple. For example, auto [a, b] = my_pair;.
`co_await`
A C++20 coroutine keyword that suspends the execution of a coroutine until an asynchronous operation completes. It interacts with the coroutine's state machine and frame.
Coroutines
Functions that can suspend their execution and resume later, allowing for more flexible asynchronous programming patterns. C++20 introduced coroutine support.
`TARGET_EXPR`
An internal GCC representation for a temporary object created within a full-expression that has a lifetime tied to that expression. It's often used for intermediate results.
`get_temp_regvar`
An internal GCC function used to obtain a temporary variable that is suitable for holding values with a lifetime managed by the compiler, often related to register allocation or temporary storage.
Filed Under: c++coroutinesstructured bindings