public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] c++: fix ICE with sizeof in a template [PR112869]
@ 2023-12-05 20:31 Marek Polacek
  2023-12-08 17:09 ` Jason Merrill
  0 siblings, 1 reply; 8+ messages in thread
From: Marek Polacek @ 2023-12-05 20:31 UTC (permalink / raw)
  To: GCC Patches, Jason Merrill

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


^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2023-12-14 21:04 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-12-05 20:31 [PATCH] c++: fix ICE with sizeof in a template [PR112869] Marek Polacek
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

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).