GCC Newspaper
OCTOBER 6, 2026
Date
/
Architectures
Components
Topics
News & Policy
Other
c++

Constexpr `break` in expansion statements corrected.

Fixes `constexpr` evaluation to correctly handle `break` statements within expansion statements.

This commit resolves an issue in constexpr evaluation where break statements within expansion statements were mishandled. The compiler incorrectly processed these breaks, leading to incorrect constant evaluation results or failures, such as misinterpreting a break as a return or causing outer loops to terminate prematurely. The fix involves wrapping the instantiated bodies of expansion statements in an artificial do...while(0) loop, ensuring that break statements correctly target the intended loop scope within the constexpr context.

In Details

The finish_expansion_stmt function converts BREAK_STMT/CONTINUE_STMT to GOTO_EXPRs with labeled LABEL_DECLs marked LABEL_DECL_BREAK/LABEL_DECL_CONTINUE. However, cxx_eval_statement_list correctly handles continue but fails to correctly resolve break targets, especially within expansion statements. This patch wraps the instantiated bodies of expansion statements in a do...while(0) construct to provide a proper loop context for break statements, ensuring they behave as expected during constexpr evaluation.

For Context
constexpr
A C++ keyword that allows certain variables and functions to be evaluated at compile time, improving performance and enabling compile-time computations.
expansion statement
A statement within a constexpr context that gets instantiated multiple times, typically within a loop or other construct, for each iteration or instantiation.
constant evaluation
The process by which the compiler evaluates expressions at compile time, typically for constexpr functions or variables.
break statement
A control flow statement used to exit the innermost enclosing loop (like for, while, do-while) or switch statement.
GOTO_EXPR
GCC's internal representation for a goto statement, used within the compiler's abstract syntax tree (AST) or intermediate representation (IR).
Filed Under: c++constexprcompiler bugcontrol flow