diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc index afd8d5731a1..aab10aa7125 100644 --- a/gcc/cp/constexpr.cc +++ b/gcc/cp/constexpr.cc @@ -9486,11 +9486,13 @@ cxx_eval_constant_expression (const constexpr_ctx *ctx, tree t, /* Pass vc_prvalue because this indicates initialization of a temporary. */ - r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), vc_prvalue, - non_constant_p, overflow_p, - jump_target); + r = cxx_eval_constant_expression (ctx, TARGET_EXPR_INITIAL (t), + vc_prvalue, non_constant_p, + overflow_p, jump_target); if (*non_constant_p) break; + if (ctx->save_exprs) + ctx->save_exprs->safe_push (slot); if (*jump_target) return NULL_TREE; if (!is_complex) @@ -9509,8 +9511,6 @@ cxx_eval_constant_expression (const constexpr_ctx *ctx, tree t, if (CLEANUP_EH_ONLY (t)) ctx->global->cleanups->safe_push (NULL_TREE); } - if (ctx->save_exprs) - ctx->save_exprs->safe_push (slot); if (lval) return slot; if (is_complex) diff --git a/gcc/testsuite/g++.dg/cpp26/constexpr-eh18.C b/gcc/testsuite/g++.dg/cpp26/constexpr-eh18.C new file mode 100644 index 00000000000..a19cf4a3c58 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp26/constexpr-eh18.C @@ -0,0 +1,42 @@ +// PR c++/124145 +// { dg-do compile { target c++26 } } + +struct S {}; + +constexpr S +foo (S x) +{ + throw 123; + return x; +} + +constexpr void +bar () +{ + foo (S {}); +} + +constexpr bool +baz () +{ + try + { + bar (); + asm (""); + } + catch (int) + { + } + + try + { + bar (); + asm (""); + } + catch (int) + { + } + return true; +} + +static_assert (baz ());