Message ID | 20240915152430.28787-1-jwakely@redhat.com |
---|---|
State | New |
Headers | show |
Series | libstdc++: Avoid forming T* in unique_ptr(auto_ptr<U>&&) constraints [PR116529] | expand |
On Sun, 15 Sept 2024 at 16:24, Jonathan Wakely wrote: > > Tested x86_64-linux. > > -- >8 -- > > PR 116529 shows that std::unique_ptr<X&, D> is currently unusable > because the constructor taking std::auto_ptr (which is a non-standard > extension since C++17) tries to form the invalid type X&* during > overload resolution. We can use the `pointer` type in the constructor > constraints, instead of trying to form an invalid type. The > std::auto_ptr constructor can never actually match for the case where > element_type is a reference, so we just need it to produce a > substitution failure instead of being ill-formed. > > LWG 4144 might make std::unique_ptr<X&, D> ill-formed, which would > invalidate this new test. We would have to remove this test in that > case. Using `pointer` in the constructor from std::auto_ptr would not be > needed to support the std::unique_ptr<X&, D> case, but would not cause > any harm either. Pushed to trunk now. > > libstdc++-v3/ChangeLog: > > PR libstdc++/116529 > * include/bits/unique_ptr.h (unique_ptr(auto_ptr<U>&&)): > Use pointer instead of T*. > * testsuite/20_util/unique_ptr/creation/116529.cc: New test. > --- > libstdc++-v3/include/bits/unique_ptr.h | 5 +-- > .../20_util/unique_ptr/creation/116529.cc | 35 +++++++++++++++++++ > 2 files changed, 38 insertions(+), 2 deletions(-) > create mode 100644 libstdc++-v3/testsuite/20_util/unique_ptr/creation/116529.cc > > diff --git a/libstdc++-v3/include/bits/unique_ptr.h b/libstdc++-v3/include/bits/unique_ptr.h > index 0f600db32f9..edcff78bff9 100644 > --- a/libstdc++-v3/include/bits/unique_ptr.h > +++ b/libstdc++-v3/include/bits/unique_ptr.h > @@ -379,8 +379,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION > #pragma GCC diagnostic push > #pragma GCC diagnostic ignored "-Wdeprecated-declarations" > /// Converting constructor from @c auto_ptr > - template<typename _Up, typename = _Require< > - is_convertible<_Up*, _Tp*>, is_same<_Dp, default_delete<_Tp>>>> > + template<typename _Up, > + typename = _Require<is_convertible<_Up*, pointer>, > + is_same<_Dp, default_delete<_Tp>>>> > unique_ptr(auto_ptr<_Up>&& __u) noexcept; > #pragma GCC diagnostic pop > #endif > diff --git a/libstdc++-v3/testsuite/20_util/unique_ptr/creation/116529.cc b/libstdc++-v3/testsuite/20_util/unique_ptr/creation/116529.cc > new file mode 100644 > index 00000000000..323fc7cb27c > --- /dev/null > +++ b/libstdc++-v3/testsuite/20_util/unique_ptr/creation/116529.cc > @@ -0,0 +1,35 @@ > +// { dg-do run { target c++11 } } > + > +// Bug libstdc++/116529 - Construction of unique_ptr with reference type > +// is rejected because of auto_ptr constructor > + > +#include <memory> > +#include <testsuite_hooks.h> > + > +int count = 0; > + > +struct X > +{ > + ~X() { ++count; } > +}; > + > +struct deleter : std::default_delete<X> > +{ > + using pointer = X*; > +}; > + > +void > +test01() > +{ > + { > + std::unique_ptr<X&, deleter> up(new X); > + // { dg-bogus "forming pointer to reference" "" { target *-*-* } 0 } > + VERIFY( count == 0 ); > + } > + VERIFY( count == 1 ); > +} > + > +int main() > +{ > + test01(); > +} > -- > 2.46.0 >
diff --git a/libstdc++-v3/include/bits/unique_ptr.h b/libstdc++-v3/include/bits/unique_ptr.h index 0f600db32f9..edcff78bff9 100644 --- a/libstdc++-v3/include/bits/unique_ptr.h +++ b/libstdc++-v3/include/bits/unique_ptr.h @@ -379,8 +379,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wdeprecated-declarations" /// Converting constructor from @c auto_ptr - template<typename _Up, typename = _Require< - is_convertible<_Up*, _Tp*>, is_same<_Dp, default_delete<_Tp>>>> + template<typename _Up, + typename = _Require<is_convertible<_Up*, pointer>, + is_same<_Dp, default_delete<_Tp>>>> unique_ptr(auto_ptr<_Up>&& __u) noexcept; #pragma GCC diagnostic pop #endif diff --git a/libstdc++-v3/testsuite/20_util/unique_ptr/creation/116529.cc b/libstdc++-v3/testsuite/20_util/unique_ptr/creation/116529.cc new file mode 100644 index 00000000000..323fc7cb27c --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/unique_ptr/creation/116529.cc @@ -0,0 +1,35 @@ +// { dg-do run { target c++11 } } + +// Bug libstdc++/116529 - Construction of unique_ptr with reference type +// is rejected because of auto_ptr constructor + +#include <memory> +#include <testsuite_hooks.h> + +int count = 0; + +struct X +{ + ~X() { ++count; } +}; + +struct deleter : std::default_delete<X> +{ + using pointer = X*; +}; + +void +test01() +{ + { + std::unique_ptr<X&, deleter> up(new X); + // { dg-bogus "forming pointer to reference" "" { target *-*-* } 0 } + VERIFY( count == 0 ); + } + VERIFY( count == 1 ); +} + +int main() +{ + test01(); +}