]> git.ipfire.org Git - thirdparty/gcc.git/commit
c++: init-list opt and lvalue initializers [PR118673]
authorJason Merrill <jason@redhat.com>
Mon, 27 Jan 2025 23:30:18 +0000 (18:30 -0500)
committerJason Merrill <jason@redhat.com>
Tue, 28 Jan 2025 17:31:00 +0000 (12:31 -0500)
commitd0f230adf0e888d9218a123ac620c9a5b3020c2d
treec657de3ee932d57a8fa58104c29ede7305be9674
parent0204dcf930b5093d0811a007b7f47aa42e55e787
c++: init-list opt and lvalue initializers [PR118673]

When fn returns {extension}, the ArrayRef in the initializer_list is
constructed to point to 'extension', the variable with static storage
duration.  The optimization was copying extension's value into a temporary
array and constructing the ArrayRef to point to that temporary copy instead,
resulting in a dangling pointer.  So suppress this optimization if the
element constructor takes a reference and the initializer is a non-mergeable
lvalue.

PR c++/118673

gcc/cp/ChangeLog:

* call.cc (maybe_init_list_as_array): Check for lvalue
initializers.
* cp-tree.h (enum cp_lvalue_kind_flags): Add clk_mergeable.
* tree.cc (lvalue_kind): Return it.
(non_mergeable_glvalue_p): New.
(test_lvalue_kind): Adjust.

gcc/testsuite/ChangeLog:

* g++.dg/cpp0x/initlist-opt6.C: New test.
gcc/cp/call.cc
gcc/cp/cp-tree.h
gcc/cp/tree.cc
gcc/testsuite/g++.dg/cpp0x/initlist-opt6.C [new file with mode: 0644]