diff --git a/libstdc++-v3/include/bits/unique_ptr.h b/libstdc++-v3/include/bits/unique_ptr.h index 6e55375..53a68f5 100644 --- a/libstdc++-v3/include/bits/unique_ptr.h +++ b/libstdc++-v3/include/bits/unique_ptr.h @@ -339,7 +339,13 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION is_convertible<_Ep, _Dp>>::type>> unique_ptr(unique_ptr<_Up, _Ep>&& __u) noexcept : _M_t(__u.release(), std::forward<_Ep>(__u.get_deleter())) - { } + { + static_assert(!is_same<_Dp, default_delete<_Tp>>::value + || has_virtual_destructor::type>::value + || sizeof(_Tp) == sizeof(_Up), + "type of pointer owned by __u must be similar to the type of pointer " + "owned by this object or the latter must have a virtual destructor"); + } #if _GLIBCXX_USE_DEPRECATED #pragma GCC diagnostic push @@ -385,6 +391,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION unique_ptr&>::type operator=(unique_ptr<_Up, _Ep>&& __u) noexcept { + static_assert(!is_same<_Dp, default_delete<_Tp>>::value + || has_virtual_destructor::type>::value + || sizeof(_Tp) == sizeof(_Up), + "type of pointer owned by __u must be similar to the type of pointer " + "owned by this object or the latter must have a virtual destructor"); + reset(__u.release()); get_deleter() = std::forward<_Ep>(__u.get_deleter()); return *this; diff --git a/libstdc++-v3/testsuite/20_util/unique_ptr/assign/slicing_neg.cc b/libstdc++-v3/testsuite/20_util/unique_ptr/assign/slicing_neg.cc new file mode 100644 index 0000000..e93483a --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/unique_ptr/assign/slicing_neg.cc @@ -0,0 +1,86 @@ +// { dg-do compile { target c++11 } } +// { dg-prune-output "virtual destructor" } + +// Copyright (C) 2021 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 +// . + +#include + +struct A { }; +struct B : A { }; +struct C : B { int i; }; + +struct Ac { char c; }; +struct Bc : Ac { }; +struct Cc : Bc { short s; }; + + +void test01() +{ + std::unique_ptr upB; + + std::unique_ptr cA; + cA = std::move(upB); + + std::unique_ptr vA; + vA = std::move(upB); + + std::unique_ptr cvA; + cvA = std::move(upB); +} + +void test02() +{ + std::unique_ptr upC; + + std::unique_ptr cA{std::move(upC)}; // { dg-error "required from here" } + cA = std::move(upC); // { dg-error "required from here" } + + std::unique_ptr vA{std::move(upC)}; // { dg-error "required from here" } + vA = std::move(upC); // { dg-error "required from here" } + + std::unique_ptr cvA{std::move(upC)}; // { dg-error "required from here" } + cvA = std::move(upC); // { dg-error "required from here" } +} + +void test03() +{ + std::unique_ptr upB; + + std::unique_ptr cA; + cA = std::move(upB); + + std::unique_ptr vA; + vA = std::move(upB); + + std::unique_ptr cvA; + cvA = std::move(upB); +} + +void test04() +{ + std::unique_ptr upC; + + std::unique_ptr cA{std::move(upC)}; // { dg-error "required from here" } + cA = std::move(upC); // { dg-error "required from here" } + + std::unique_ptr vA{std::move(upC)}; // { dg-error "required from here" } + vA = std::move(upC); // { dg-error "required from here" } + + std::unique_ptr cvA{std::move(upC)}; // { dg-error "required from here" } + cvA = std::move(upC); // { dg-error "required from here" } +}