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

C++ contract assertions now have side effects.

Contract assertions in C++26 are now marked as having side effects, aligning with their behavior and fixing PR125904.

This commit modifies the C++ compiler’s handling of contract assertions for C++26, as specified in PR125904. The build_contract_check function in contracts.cc now correctly sets the TREE_SIDE_EFFECTS flag for contract checks. This ensures that compiler optimizations do not incorrectly discard these assertions, preserving their intended behavior.

In Details

The C++ frontend's contract assertion mechanism, introduced in C++26, relies on compiler internals to manage the checking of preconditions, postconditions, and assertions. Previously, the TREE_SIDE_EFFECTS flag was not set for contract assertion checks in build_contract_check. This commit remedies that by setting the flag, ensuring that the intermediate representation (IR) accurately reflects that these checks can have observable effects, thus preventing overly aggressive optimizations that might remove them.

For Context
C++26 contracts
A proposed feature for C++26 that allows developers to specify and check contracts (preconditions, postconditions, assertions) for functions, improving code correctness and robustness.
TREE_SIDE_EFFECTS
A flag in GCC's internal representation (tree) that indicates whether a particular expression or statement has side effects (e.g., modifying a variable, performing I/O). This flag is crucial for optimization passes to correctly reorder or eliminate code.
PR125904
A bug report filed against GCC. This specific report highlighted an issue with C++ contract assertions not being correctly marked as having side effects.
Filed Under: c++contractscompiler