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

C++ contracts template postconditions rebuild logic improved.

Avoids unnecessary rebuilding of template postconditions if still processing a template declaration.

This commit refactors the rebuild_postconditions logic in GCC’s C++ contract handling to prevent unnecessary rebuilding when still processing a template declaration. Previously, the function used tsubst_expr to replace placeholder return variables, but passing an empty template argument vector could lead to an Internal Compiler Error (ICE) if template parameters were directly referenced. The fix ensures that if the compiler is currently processing a template declaration, the postcondition rebuilding is skipped, as it will be handled correctly during template instantiation.

In Details

The rebuild_postconditions function in gcc/cp/contracts.cc is intended to finalize postcondition expressions by substituting specific types or values. It relies on tsubst_expr for this substitution. However, when rebuild_postconditions is called during the processing of a function template declaration (indicated by processing_template_decl), and no explicit template arguments are provided for substitution, it can lead to an ICE if the postcondition refers to template parameters. This change adds a check to skip the rebuilding process entirely when processing_template_decl is true,…

For Context
C++ contracts
A C++ language feature (tentatively C++26) that allows developers to specify contract conditions (preconditions, postconditions, assertions) directly in the code. These contracts can be used for static analysis, runtime checking, and documentation.
template postconditions
The conditions that must hold true after a function template has been executed, specified as part of the contract mechanism.
tsubst_expr
A GCC internal function used for template substitution, which replaces template parameters within an expression with their corresponding arguments.
Internal Compiler Error (ICE)
An unrecoverable error that occurs within the compiler itself, usually due to a bug or an unexpected internal state, causing the compilation process to halt.
Filed Under: c++contractstemplatescompiler-error