https://gcc.gnu.org/bugzilla/show_bug.cgi?id=129231

            Bug ID: 129231
           Summary: [C++26] ICE in maybe_apply_function_contracts with
                    -fno-exceptions on noexcept function
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Keywords: c++-contracts, c++26, ice-on-valid-code
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: adrian.herrmann.xyz at gmail dot com
  Target Milestone: ---

Compiling a noexcept function with pre or post contract specifiers under
-fno-exceptions ICEs in maybe_apply_function_contracts:

--- repro.cpp ---
int f(int x) noexcept pre (x > 0) {
    return x + 1;
}

int g(int x) noexcept post (r : r > 0) {
    return x + 1;
}

int h(const int x) noexcept pre (x > 0) post (r : r > x) {
    return x + 1;
}

$ g++ -std=c++26 -fno-exceptions -fcontract-evaluation-semantic=enforce -c
repro.cpp -o /dev/null
internal compiler error: in maybe_apply_function_contracts, at
cp/contracts.cc:1371

maybe_apply_function_contracts checks TYPE_NOEXCEPT_P (TREE_TYPE (fndecl)) and
asserts that expr_first (DECL_SAVED_TREE (fndecl)) is a MUST_NOT_THROW_EXPR.
Under -fno-exceptions, use_eh_spec_block (fndecl) is false, so no
MUST_NOT_THROW_EXPR is created (with release checking, TREE_OPERAND
(m_n_t_expr, 0) corrupts the AST and crashes later in cp_fold/gimplification).

In the same function, the else branch also misses pop_stmt_list on
DECL_SAVED_TREE (fndecl), and the early return on if (!do_post) skips popping
lambda_bind / m_n_t_expr statement lists.

I have a fix for this and plan to send it to gcc-patches.

Tested on master 48688cfeadf6ec3b2dd22783f5048db6fc444d04 (also reproduces on
5a61875b0cd983e57dd44b7d6e12b9eb11985bae):
Using built-in specs.
COLLECT_GCC=xg++
Target: x86_64-elf
Configured with: src/configure --target=x86_64-elf --enable-languages=c++
--disable-nls --disable-lto --disable-libssp --disable-multilib
--without-headers --without-isl --without-zstd --enable-checking=yes
Thread model: single
gcc version 17.0.0 20261126 (experimental) (GCC)

Reply via email to