]> git.ipfire.org Git - thirdparty/gcc.git/commit
c++: ICE in TARGET_EXPR evaluation in cp_fold_r [PR117980]
authorMarek Polacek <polacek@redhat.com>
Tue, 10 Dec 2024 23:43:56 +0000 (18:43 -0500)
committerMarek Polacek <polacek@redhat.com>
Thu, 19 Dec 2024 16:07:18 +0000 (11:07 -0500)
commitfa99002538bc91c869f3b1fd9af7f14e410e1e1a
tree2e5eb34b71657d9c1c517c3da2a39be90bd8e5f8
parentfc95e8776cf4b11bbffc593167105883165b9e4c
c++: ICE in TARGET_EXPR evaluation in cp_fold_r [PR117980]

This ICE started with the recent prvalue optimization (r15-6052).  In
cp_fold_r we have:

      if (tree &init = TARGET_EXPR_INITIAL (stmt))
        {
          cp_walk_tree (&init, cp_fold_r, data, NULL);
          // ...
  tree folded = maybe_constant_init (init, TARGET_EXPR_SLOT (stmt));

What can happen here is that originally the TARGET_EXPR is:

    TARGET_EXPR <D.2747, <<< Unknown tree: aggr_init_expr
      5
      __ct_comp
      D.2747
      (struct subrange *) <<< Unknown tree: void_cst >>>
      &TARGET_EXPR <D.2707, {.it=TARGET_EXPR <D.2695, ...>}> >>>>

but after the first cp_walk_tree we fold the D.2707 TARGET_EXPR into:

    TARGET_EXPR <D.2707, <<< Unknown tree: expr_stmt
      D.2707.it = TARGET_EXPR <D.2695, ...> >>>>

and then we pass the EXPR_STMT to maybe_constant_init, with D.2707 as
the object.  But their types don't match anymore, so we crash.  We'd
have to pass D.2707.it as the object for it to work.

This patch adjusts cxx_eval_outermost_constant_expr to take the object's
type if available.

constexpr-prvalue3.C is reduced from a large std::ranges libstdc++ test.

PR c++/117980

gcc/cp/ChangeLog:

* constexpr.cc (cxx_eval_outermost_constant_expr): If there's
an object to initialize, take its type.  Don't set the type
in the constexpr dtor case.

gcc/testsuite/ChangeLog:

* g++.dg/cpp0x/constexpr-prvalue2.C: New test.
* g++.dg/cpp0x/constexpr-prvalue3.C: New test.

Co-authored-by: Jason Merrill <jason@redhat.com>
gcc/cp/constexpr.cc
gcc/testsuite/g++.dg/cpp0x/constexpr-prvalue2.C [new file with mode: 0644]
gcc/testsuite/g++.dg/cpp0x/constexpr-prvalue3.C [new file with mode: 0644]