public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Marek Polacek <polacek@redhat.com>
To: GCC Patches <gcc-patches@gcc.gnu.org>, Jason Merrill <jason@redhat.com>
Subject: [PATCH] c++: fix ICE with sizeof in a template [PR112869]
Date: Tue,  5 Dec 2023 15:31:36 -0500	[thread overview]
Message-ID: <20231205203136.94832-1-polacek@redhat.com> (raw)

Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?

-- >8 --
This test shows that we cannot clear *walk_subtrees in
cp_fold_immediate_r when we're in_immediate_context, because that,
as the comment says, affects cp_fold_r as well.  Here we had an
expression with

  min ((long int) VIEW_CONVERT_EXPR<long unsigned int>(bytecount), (long int) <<< Unknown tree: sizeof_expr
    (int) <<< error >>> >>>)

as its sub-expression, and we never evaluated that into

  min ((long int) bytecount, 4)

so the SIZEOF_EXPR leaked into the middle end.

(There's still one *walk_subtrees = 0; in cp_fold_immediate_r, but that
one should be OK.)

	PR c++/112869

gcc/cp/ChangeLog:

	* cp-gimplify.cc (cp_fold_immediate_r): Don't clear *walk_subtrees
	for unevaluated operands.

gcc/testsuite/ChangeLog:

	* g++.dg/template/sizeof18.C: New test.
---
 gcc/cp/cp-gimplify.cc                    | 8 ++------
 gcc/testsuite/g++.dg/template/sizeof18.C | 8 ++++++++
 2 files changed, 10 insertions(+), 6 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/template/sizeof18.C

diff --git a/gcc/cp/cp-gimplify.cc b/gcc/cp/cp-gimplify.cc
index 5abb91bbdd3..46c3eb91853 100644
--- a/gcc/cp/cp-gimplify.cc
+++ b/gcc/cp/cp-gimplify.cc
@@ -1177,13 +1177,9 @@ cp_fold_immediate_r (tree *stmt_p, int *walk_subtrees, void *data_)
 				   ? tf_error : tf_none);
   const tree_code code = TREE_CODE (stmt);
 
-  /* No need to look into types or unevaluated operands.
-     NB: This affects cp_fold_r as well.  */
+  /* No need to look into types or unevaluated operands.  */
   if (TYPE_P (stmt) || unevaluated_p (code) || in_immediate_context ())
-    {
-      *walk_subtrees = 0;
-      return NULL_TREE;
-    }
+    return NULL_TREE;
 
   tree decl = NULL_TREE;
   bool call_p = false;
diff --git a/gcc/testsuite/g++.dg/template/sizeof18.C b/gcc/testsuite/g++.dg/template/sizeof18.C
new file mode 100644
index 00000000000..afba9946258
--- /dev/null
+++ b/gcc/testsuite/g++.dg/template/sizeof18.C
@@ -0,0 +1,8 @@
+// PR c++/112869
+// { dg-do compile }
+
+void min(long, long);
+template <class T> void Binaryread(int &, T, unsigned long);
+template <> void Binaryread(int &, float, unsigned long bytecount) {
+  min(bytecount, sizeof(int));
+}

base-commit: 9c3a880feecf81c310b4ade210fbd7004c9aece7
-- 
2.43.0


             reply	other threads:[~2023-12-05 20:31 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-05 20:31 Marek Polacek [this message]
2023-12-08 17:09 ` Jason Merrill
2023-12-08 21:15   ` [PATCH v2] " Marek Polacek
2023-12-09  4:09     ` Jason Merrill
2023-12-12 22:48       ` [PATCH v3] " Marek Polacek
2023-12-13 20:28         ` Jason Merrill
2023-12-14 21:01           ` [PATCH v4] " Marek Polacek
2023-12-14 21:04             ` Jason Merrill

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=20231205203136.94832-1-polacek@redhat.com \
    --to=polacek@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jason@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).