public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jason Merrill <jason@redhat.com>
To: gcc-patches@gcc.gnu.org
Cc: Marek Polacek <polacek@redhat.com>
Subject: [pushed] c++: if consteval and consteval propagation [PR115583]
Date: Sun, 28 Jul 2024 17:38:31 -0400	[thread overview]
Message-ID: <20240728213843.161062-1-jason@redhat.com> (raw)

Tested x86_64-pc-linux-gnu, applying to trunk.

-- 8< --

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.
---
 gcc/cp/constexpr.cc                         |  7 +++++--
 gcc/testsuite/g++.dg/cpp23/consteval-if13.C | 17 +++++++++++++++++
 2 files changed, 22 insertions(+), 2 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/cpp23/consteval-if13.C

diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc
index abd3b04ea7f..8277b3b79ba 100644
--- a/gcc/cp/constexpr.cc
+++ b/gcc/cp/constexpr.cc
@@ -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))
     {
diff --git a/gcc/testsuite/g++.dg/cpp23/consteval-if13.C b/gcc/testsuite/g++.dg/cpp23/consteval-if13.C
new file mode 100644
index 00000000000..b98bbc33d13
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp23/consteval-if13.C
@@ -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);
+}

base-commit: 027bde11d44d58e6ad473af3a69dc21b451bcbc0
-- 
2.45.2


                 reply	other threads:[~2024-07-28 21:38 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240728213843.161062-1-jason@redhat.com \
    --to=jason@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=polacek@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).