PR libstdc++/87855 fix optional for types with non-trivial copy/move
When the contained value is not trivially copy (or move) constructible the union's copy (or move) constructor will be deleted, and so the _Optional_payload delegating constructors are invalid. G++ fails to diagnose this because it incorrectly performs copy elision in the delegating constructors. Clang does diagnose it (llvm.org/PR40245). The solution is to avoid performing any copy (or move) when the contained value's copy (or move) constructor isn't trivial. Instead the contained value can be constructed by calling _M_construct. This is OK, because the relevant constructor doesn't need to be constexpr when the contained value isn't trivially copy (or move) constructible. Additionally, this patch removes a lot of code duplication in the _Optional_payload partial specializations and the _Optional_base partial specialization, by hoisting it into common base classes. The Python pretty printer for std::optional needs to be adjusted to support the new layout. Retain support for the old layout, and add a test to verify that the support still works. PR libstdc++/87855 * include/std/optional (_Optional_payload_base): New class template for common code hoisted from _Optional_payload specializations. Use a template for the union, to allow a partial specialization for types with non-trivial destructors. Add constructors for in-place initialization to the union. (_Optional_payload(bool, const _Optional_payload&)): Use _M_construct to perform non-trivial copy construction, instead of relying on non-standard copy elision in a delegating constructor. (_Optional_payload(bool, _Optional_payload&&)): Likewise for non-trivial move construction. (_Optional_payload): Derive from _Optional_payload_base and use it for everything except the non-trivial assignment operators, which are defined as needed. (_Optional_payload<false, C, M>): Derive from the specialization _Optional_payload<true, false, false> and add a destructor. (_Optional_base_impl::_M_destruct, _Optional_base_impl::_M_reset): Forward to corresponding members of _Optional_payload. (_Optional_base_impl::_M_is_engaged, _Optional_base_impl::_M_get): Hoist common members from _Optional_base. (_Optional_base): Make all members and base class public. (_Optional_base::_M_get, _Optional_base::_M_is_engaged): Move to _Optional_base_impl. * python/libstdcxx/v6/printers.py (StdExpOptionalPrinter): Add support for new std::optional layout. * testsuite/libstdc++-prettyprinters/compat.cc: New test. From-SVN: r267742
This commit is contained in:
parent
96e768c3fe
commit
d942bc80e4
@ -1,5 +1,32 @@
|
||||
2019-01-08 Jonathan Wakely <jwakely@redhat.com>
|
||||
|
||||
PR libstdc++/87855
|
||||
* include/std/optional (_Optional_payload_base): New class template
|
||||
for common code hoisted from _Optional_payload specializations. Use
|
||||
a template for the union, to allow a partial specialization for
|
||||
types with non-trivial destructors. Add constructors for in-place
|
||||
initialization to the union.
|
||||
(_Optional_payload(bool, const _Optional_payload&)): Use _M_construct
|
||||
to perform non-trivial copy construction, instead of relying on
|
||||
non-standard copy elision in a delegating constructor.
|
||||
(_Optional_payload(bool, _Optional_payload&&)): Likewise for
|
||||
non-trivial move construction.
|
||||
(_Optional_payload): Derive from _Optional_payload_base and use it
|
||||
for everything except the non-trivial assignment operators, which are
|
||||
defined as needed.
|
||||
(_Optional_payload<false, C, M>): Derive from the specialization
|
||||
_Optional_payload<true, false, false> and add a destructor.
|
||||
(_Optional_base_impl::_M_destruct, _Optional_base_impl::_M_reset):
|
||||
Forward to corresponding members of _Optional_payload.
|
||||
(_Optional_base_impl::_M_is_engaged, _Optional_base_impl::_M_get):
|
||||
Hoist common members from _Optional_base.
|
||||
(_Optional_base): Make all members and base class public.
|
||||
(_Optional_base::_M_get, _Optional_base::_M_is_engaged): Move to
|
||||
_Optional_base_impl.
|
||||
* python/libstdcxx/v6/printers.py (StdExpOptionalPrinter): Add
|
||||
support for new std::optional layout.
|
||||
* testsuite/libstdc++-prettyprinters/compat.cc: New test.
|
||||
|
||||
PR libstdc++/88066
|
||||
* include/bits/locale_conv.h: Use <> for includes not "".
|
||||
* include/ext/random: Likewise.
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -1093,13 +1093,23 @@ class StdExpOptionalPrinter(SingleObjContainerPrinter):
|
||||
|
||||
def __init__ (self, typename, val):
|
||||
valtype = self._recognize (val.type.template_argument(0))
|
||||
self.typename = strip_versioned_namespace(typename)
|
||||
self.typename = re.sub('^std::(experimental::|)(fundamentals_v\d::|)(.*)', r'std::\1\3<%s>' % valtype, self.typename, 1)
|
||||
if not self.typename.startswith('std::experimental'):
|
||||
val = val['_M_payload']
|
||||
self.val = val
|
||||
contained_value = val['_M_payload'] if self.val['_M_engaged'] else None
|
||||
visualizer = gdb.default_visualizer (val['_M_payload'])
|
||||
typename = strip_versioned_namespace(typename)
|
||||
self.typename = re.sub('^std::(experimental::|)(fundamentals_v\d::|)(.*)', r'std::\1\3<%s>' % valtype, typename, 1)
|
||||
payload = val['_M_payload']
|
||||
if self.typename.startswith('std::experimental'):
|
||||
engaged = val['_M_engaged']
|
||||
contained_value = payload
|
||||
else:
|
||||
engaged = payload['_M_engaged']
|
||||
contained_value = payload['_M_payload']
|
||||
try:
|
||||
# Since GCC 9
|
||||
contained_value = contained_value['_M_value']
|
||||
except:
|
||||
pass
|
||||
visualizer = gdb.default_visualizer (contained_value)
|
||||
if not engaged:
|
||||
contained_value = None
|
||||
super (StdExpOptionalPrinter, self).__init__ (contained_value, visualizer)
|
||||
|
||||
def to_string (self):
|
||||
|
76
libstdc++-v3/testsuite/libstdc++-prettyprinters/compat.cc
Normal file
76
libstdc++-v3/testsuite/libstdc++-prettyprinters/compat.cc
Normal file
@ -0,0 +1,76 @@
|
||||
// { dg-options "-g -O0" }
|
||||
// { dg-do run }
|
||||
// { dg-skip-if "" { *-*-* } { "-D_GLIBCXX_PROFILE" } }
|
||||
|
||||
// Copyright (C) 2014-2019 Free Software Foundation, Inc.
|
||||
//
|
||||
// This file is part of the GNU ISO C++ Library. This library is free
|
||||
// software; you can redistribute it and/or modify it under the
|
||||
// terms of the GNU General Public License as published by the
|
||||
// Free Software Foundation; either version 3, or (at your option)
|
||||
// any later version.
|
||||
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License along
|
||||
// with this library; see the file COPYING3. If not see
|
||||
// <http://www.gnu.org/licenses/>.
|
||||
|
||||
// Test that current printers still support old definitions of types.
|
||||
|
||||
namespace std
|
||||
{
|
||||
// Old representation of std::optional, before GCC 9
|
||||
template<typename T>
|
||||
struct _Optional_payload
|
||||
{
|
||||
_Optional_payload() : _M_empty(), _M_engaged(false) { }
|
||||
struct _Empty_byte { };
|
||||
union {
|
||||
_Empty_byte _M_empty;
|
||||
T _M_payload;
|
||||
};
|
||||
bool _M_engaged;
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
struct _Optional_base
|
||||
{
|
||||
_Optional_payload<T> _M_payload;
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
struct optional : _Optional_base<T>
|
||||
{
|
||||
optional() { }
|
||||
|
||||
optional(T t)
|
||||
{
|
||||
this->_M_payload._M_payload = t;
|
||||
this->_M_payload._M_engaged = true;
|
||||
}
|
||||
};
|
||||
} // namespace std
|
||||
|
||||
int
|
||||
main()
|
||||
{
|
||||
using std::optional;
|
||||
|
||||
optional<int> o;
|
||||
// { dg-final { note-test o {std::optional<int> [no contained value]} } }
|
||||
optional<bool> ob{false};
|
||||
// { dg-final { note-test ob {std::optional<bool> = {[contained value] = false}} } }
|
||||
optional<int> oi{5};
|
||||
// { dg-final { note-test oi {std::optional<int> = {[contained value] = 5}} } }
|
||||
optional<void*> op{nullptr};
|
||||
// { dg-final { note-test op {std::optional<void *> = {[contained value] = 0x0}} } }
|
||||
|
||||
__builtin_puts("");
|
||||
return 0; // Mark SPOT
|
||||
}
|
||||
|
||||
// { dg-final { gdb-test SPOT } }
|
Loading…
x
Reference in New Issue
Block a user