]> git.ipfire.org Git - thirdparty/gcc.git/commit
c++: wrong-code with consteval constructor [PR117501]
authorMarek Polacek <polacek@redhat.com>
Wed, 14 May 2025 14:38:47 +0000 (10:38 -0400)
committerMarek Polacek <polacek@redhat.com>
Wed, 14 May 2025 14:38:47 +0000 (10:38 -0400)
commit1dc1c1e7f0bb3a295eff1bc8c5d4f4d4b2898d50
treeac6d53c9e424dd6b9f2c982eeac28aeeaf646771
parent52202e42919d0d95c13889bed9e69b954e4376af
c++: wrong-code with consteval constructor [PR117501]

We've had a wrong-code problem since r14-4140, due to which we
forget to initialize a variable.

In consteval39.C, we evaluate

    struct QQQ q;
  <<cleanup_point <<< Unknown tree: expr_stmt
    QQQ::QQQ (&q, TARGET_EXPR <D.2687, <<< Unknown tree: aggr_init_expr
      5
      __ct_comp
      D.2687
      (struct basic_string_view *) <<< Unknown tree: void_cst >>>
      (const char *) "" >>>>) >>>>>;

into

    struct QQQ q;
  <<cleanup_point <<< Unknown tree: expr_stmt
    {.data={._M_len=42, ._M_str=0}} >>>>>;

and then the useless expr_stmt is dropped on the floor, so q isn't
initialized.  As pre-r14-4140, we need to handle constructors specially.

With this patch, we generate:

    struct QQQ q;
  <<cleanup_point <<< Unknown tree: expr_stmt
    q = {.data={._M_len=42, ._M_str=0}} >>>>>;

initializing q properly.

PR c++/117501

gcc/cp/ChangeLog:

* cp-gimplify.cc (cp_build_init_expr_for_ctor): New.
(cp_fold_immediate_r): Call it.
(cp_fold): Break out code into cp_build_init_expr_for_ctor.

gcc/testsuite/ChangeLog:

* g++.dg/cpp2a/consteval39.C: New test.
* g++.dg/cpp2a/consteval40.C: New test.

Reviewed-by: Jason Merrill <jason@redhat.com>
gcc/cp/cp-gimplify.cc
gcc/testsuite/g++.dg/cpp2a/consteval39.C [new file with mode: 0644]
gcc/testsuite/g++.dg/cpp2a/consteval40.C [new file with mode: 0644]