c++: if consteval and consteval propagation [PR115583]

During speculative constant folding of an if consteval, we take the false
branch, but the true branch is an immediate function context, so we don't
want to to cp_fold_immediate it.  So we could check IF_STMT_CONSTEVAL_P
here.  But beyond that, we don't want to do this inside a call, only when
first parsing a function.

	PR c++/115583

gcc/cp/ChangeLog:

	* constexpr.cc (cxx_eval_conditional_expression): Don't
	cp_fold_immediate for if consteval.

gcc/testsuite/ChangeLog:

	* g++.dg/cpp23/consteval-if13.C: New test.

(cherry picked from commit d5f1948640815a554d106542c2e91e4e117aa3bc)
This commit is contained in:
Jason Merrill 2024-07-27 16:40:02 -04:00
parent 56d5f8a605
commit 9662299593
2 changed files with 22 additions and 2 deletions

View file

@ -3974,10 +3974,13 @@ cxx_eval_conditional_expression (const constexpr_ctx *ctx, tree t,
if (TREE_CODE (t) == IF_STMT && !val)
val = void_node;
/* P2564: a subexpression of a manifestly constant-evaluated expression
or conversion is an immediate function context. */
/* P2564: If we aren't in immediate function context (including a manifestly
constant-evaluated expression), check any uses of immediate functions in
the arm we're discarding. But don't do this inside a call; we already
checked when parsing the function. */
if (ctx->manifestly_const_eval != mce_true
&& !in_immediate_context ()
&& !ctx->call
&& cp_fold_immediate (&TREE_OPERAND (t, zero_p ? 1 : 2),
ctx->manifestly_const_eval))
{

View file

@ -0,0 +1,17 @@
// PR c++/115583
// { dg-do compile { target c++23 } }
consteval int f(int i) {
return i;
}
const bool b = 0;
constexpr int g(int i) {
if consteval {
return f(i);
} else {
return i;
}
}
int main() {
return g(1);
}