From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2122) id 4190F3858C5F; Wed, 1 Feb 2023 00:11:24 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4190F3858C5F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1675210284; bh=7H9O3PtY5d2wSNPU6X5YSe7athFb3GmeQmUJKaU0mV8=; h=From:To:Subject:Date:From; b=htrXsZhOoDRqEb0KGeMvxHET2L3bjprOXgD77hb1flk2yi+NQfobIG4oCGcCCC2i1 Ja+qkLGL//T3aifFLNzXTqhKWNEFVMveeuQqEyMicGYv31IgYccOeCK5sxu6TWIw5C HzVIiQbySmRVZxvbbyPNkmzWj+BltGo66IJaYglY= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jason Merrill To: gcc-cvs@gcc.gnu.org Subject: [gcc r13-5611] c++: aggregate base and TARGET_EXPR_ELIDING_P [PR108559] X-Act-Checkin: gcc X-Git-Author: Jason Merrill X-Git-Refname: refs/heads/trunk X-Git-Oldrev: a9fbc6687faa09bf045c0fcee7960b7fef796fcc X-Git-Newrev: b533084d756a2696a3eb6521810e0a0b2182a8e8 Message-Id: <20230201001124.4190F3858C5F@sourceware.org> Date: Wed, 1 Feb 2023 00:11:24 +0000 (GMT) List-Id: https://gcc.gnu.org/g:b533084d756a2696a3eb6521810e0a0b2182a8e8 commit r13-5611-gb533084d756a2696a3eb6521810e0a0b2182a8e8 Author: Jason Merrill Date: Mon Jan 30 18:18:54 2023 -0500 c++: aggregate base and TARGET_EXPR_ELIDING_P [PR108559] We also need to split up a CONSTRUCTOR in cp_genericize_init if we need to add extra copy constructor calls to deal with CWG2403. PR c++/108559 gcc/cp/ChangeLog: * cp-gimplify.cc (any_non_eliding_target_exprs): New. (cp_genericize_init): Check it. gcc/testsuite/ChangeLog: * g++.dg/cpp1z/aggr-base13.C: New test. Diff: --- gcc/cp/cp-gimplify.cc | 22 +++++++++++++++++++--- gcc/testsuite/g++.dg/cpp1z/aggr-base13.C | 19 +++++++++++++++++++ 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/gcc/cp/cp-gimplify.cc b/gcc/cp/cp-gimplify.cc index a35cedd05cc..9929d29981a 100644 --- a/gcc/cp/cp-gimplify.cc +++ b/gcc/cp/cp-gimplify.cc @@ -893,6 +893,21 @@ omp_cxx_notice_variable (struct cp_genericize_omp_taskreg *omp_ctx, tree decl) } } +/* True if any of the element initializers in CTOR are TARGET_EXPRs that are + not expected to elide, e.g. because unsafe_copy_elision_p is true. */ + +static bool +any_non_eliding_target_exprs (tree ctor) +{ + for (const constructor_elt &e : *CONSTRUCTOR_ELTS (ctor)) + { + if (TREE_CODE (e.value) == TARGET_EXPR + && !TARGET_EXPR_ELIDING_P (e.value)) + return true; + } + return false; +} + /* If we might need to clean up a partially constructed object, break down the CONSTRUCTOR with split_nonconstant_init. Also expand VEC_INIT_EXPR at this point. If initializing TO with FROM is non-trivial, overwrite *REPLACE with @@ -904,10 +919,11 @@ cp_genericize_init (tree *replace, tree from, tree to) tree init = NULL_TREE; if (TREE_CODE (from) == VEC_INIT_EXPR) init = expand_vec_init_expr (to, from, tf_warning_or_error); - else if (flag_exceptions - && TREE_CODE (from) == CONSTRUCTOR + else if (TREE_CODE (from) == CONSTRUCTOR && TREE_SIDE_EFFECTS (from) - && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (from))) + && ((flag_exceptions + && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (from))) + || any_non_eliding_target_exprs (from))) { to = cp_stabilize_reference (to); replace_placeholders (from, to); diff --git a/gcc/testsuite/g++.dg/cpp1z/aggr-base13.C b/gcc/testsuite/g++.dg/cpp1z/aggr-base13.C new file mode 100644 index 00000000000..c4c7ee0e8f0 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1z/aggr-base13.C @@ -0,0 +1,19 @@ +// PR c++/108559 +// { dg-do compile { target c++17 } } + +struct A { + int g = 0; + + A() {} + A(const A&) {} +}; + +struct B : A {}; + +A u() { return A{}; } + +int bug() { return B{u()}.g; } + +int main() { + return 0; +}