On 11/29/24 9:33 AM, Simon Martin wrote:
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 type requested.

Successfully tested on x86_64-pc-linux-gnu.

        PR c++/117775

gcc/cp/ChangeLog:

        * decl.cc (fold_sizeof_expr): Make sure the folded result has
        the requested TREE_TYPE.

gcc/testsuite/ChangeLog:

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

---
  gcc/cp/decl.cc                                |  1 +
  gcc/testsuite/g++.dg/cpp2a/constexpr-117775.C | 13 +++++++++++++
  2 files changed, 14 insertions(+)
  create mode 100644 gcc/testsuite/g++.dg/cpp2a/constexpr-117775.C

diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc
index 80485f0a428..fbe1407a2d2 100644
--- a/gcc/cp/decl.cc
+++ b/gcc/cp/decl.cc
@@ -11686,6 +11686,7 @@ fold_sizeof_expr (tree t)
                                    false, false);
    if (r == error_mark_node)
      r = size_one_node;
+  r = cp_fold_convert (TREE_TYPE (t), r);

Instead of adding this conversion in all cases, let's change size_one_node to

build_int_cst (size_type_node, 1)

in the error case.  OK with that change.

Jason

Reply via email to