diff --git a/gcc/cp/cp-gimplify.cc b/gcc/cp/cp-gimplify.cc index e6629dea5fd..6a5e4cf62ca 100644 --- a/gcc/cp/cp-gimplify.cc +++ b/gcc/cp/cp-gimplify.cc @@ -53,6 +53,10 @@ static GTY(()) hash_set *deferred_escalating_exprs; static void remember_escalating_expr (tree t) { + if (uses_template_parms (t)) + /* Templates don't escalate, and cp_fold_immediate can get confused by + other template trees in the function body (c++/115986). */ + return; if (!deferred_escalating_exprs) deferred_escalating_exprs = hash_set::create_ggc (37); deferred_escalating_exprs->add (t); diff --git a/gcc/testsuite/g++.dg/cpp2a/consteval-prop21.C b/gcc/testsuite/g++.dg/cpp2a/consteval-prop21.C new file mode 100644 index 00000000000..debbda4f425 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/consteval-prop21.C @@ -0,0 +1,17 @@ +// PR c++/115986 +// { dg-do compile { target c++20 } } + +template +constexpr int b(T) { + return 0; +} +consteval __uint128_t operator"" _c(const char*) { return 0; } +constexpr char e() { + long f = true ? 0 : b(long(1)); + return b(f); +} +template +void d() { + 0_c; + static_assert(e()); +}