]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: Make sure fold_sizeof_expr returns the correct type [PR117775]
authorSimon Martin <simon@nasilyan.com>
Thu, 16 Jan 2025 15:27:06 +0000 (16:27 +0100)
committerSimon Martin <simartin@gcc.gnu.org>
Fri, 17 Jan 2025 05:39:41 +0000 (06:39 +0100)
We currently ICE upon the following code, that is valid under
-Wno-pointer-arith:

=== cut here ===
int main() {
  decltype( [](auto) { return sizeof(void); } ) x;
  return x.operator()(0);
}
=== cut here ===

The problem is that "fold_sizeof_expr (sizeof(void))" returns
size_one_node, that has a different TREE_TYPE from that of the sizeof
expression, which later triggers an assert in cxx_eval_store_expression.

This patch makes sure that fold_sizeof_expr always returns a tree with
the size_type_node type.

PR c++/117775

gcc/cp/ChangeLog:

* decl.cc (fold_sizeof_expr): Make sure the folded result has
type size_type_node.

gcc/testsuite/ChangeLog:

* g++.dg/cpp2a/constexpr-117775.C: New test.

(cherry picked from commit 37f38b0f97374476a4818b68c8df991886428787)

gcc/cp/decl.cc
gcc/testsuite/g++.dg/cpp2a/constexpr-117775.C [new file with mode: 0644]

index 02119105058e2d677a9a18a92f654634785e016e..529bfadf2e7d7d9a4bc0a0c98c2987636015dab5 100644 (file)
@@ -11502,6 +11502,7 @@ fold_sizeof_expr (tree t)
                                    false, false);
   if (r == error_mark_node)
     r = size_one_node;
+  r = cp_fold_convert (size_type_node, r);
   return r;
 }
 
diff --git a/gcc/testsuite/g++.dg/cpp2a/constexpr-117775.C b/gcc/testsuite/g++.dg/cpp2a/constexpr-117775.C
new file mode 100644 (file)
index 0000000..59fc0d3
--- /dev/null
@@ -0,0 +1,13 @@
+// PR c++/117775
+// Check that we don't ICE and have sizeof(void)==1 under -Wno-pointer-arith
+// { dg-do run { target c++20 } }
+// { dg-additional-options "-Wno-pointer-arith" }
+
+int main() {
+  struct why :
+    decltype( [](auto) {
+               return sizeof(void);
+             })
+  {} x;
+  return 1 - x.operator()(0);
+}