c++: Avoid redundant copy in {} init [PR98642]
authorJason Merrill <jason@redhat.com>
Wed, 13 Jan 2021 18:27:06 +0000 (13:27 -0500)
committerJason Merrill <jason@redhat.com>
Fri, 15 Jan 2021 18:57:01 +0000 (13:57 -0500)
commitd75199f782348bfc401f925b60f33ffc9822b7cc
treea1f67748fa7024d5816a2133f7e9310101d70f0a
parent424deca72b63e644cbc975cbc2fdda5248449bcb
c++: Avoid redundant copy in {} init [PR98642]

Here, initializing from { } implies a call to the default constructor for
base.  We were then seeing that we're initializing a base subobject, so we
tried to copy the result of that call.  This is clearly wrong; we should
initialize the base directly from its default constructor.

This patch does a lot of refactoring of unsafe_copy_elision_p and adds
make_safe_copy_elision that will also try to do the base constructor
rewriting from the last patch.

gcc/cp/ChangeLog:

PR c++/98642
* call.c (unsafe_return_slot_p): Return int.
(init_by_return_slot_p): Split out from...
(unsafe_copy_elision_p): ...here.
(unsafe_copy_elision_p_opt): New name for old meaning.
(build_over_call): Adjust.
(make_safe_copy_elision): New.
* typeck2.c (split_nonconstant_init_1): Elide copy from safe
list-initialization.
* cp-tree.h: Adjust.

gcc/testsuite/ChangeLog:

PR c++/98642
* g++.dg/cpp1z/elide5.C: New test.
gcc/cp/call.c
gcc/cp/cp-tree.h
gcc/cp/typeck2.c
gcc/testsuite/g++.dg/cpp1z/elide5.C [new file with mode: 0644]