]> gcc.gnu.org Git - gcc.git/commit
libstdc++: Diagnose broken allocator rebind members
authorJonathan Wakely <jwakely@redhat.com>
Wed, 14 Dec 2022 15:21:32 +0000 (15:21 +0000)
committerJonathan Wakely <jwakely@redhat.com>
Fri, 16 Dec 2022 20:58:09 +0000 (20:58 +0000)
commit64c986b49558a7c356b85bda85195216936c29a3
tree58f9ac6933d1445101f40b1e20f91599e81a9b3e
parent0b1d66658ecdcc3d9251641a0b902b4c73ace303
libstdc++: Diagnose broken allocator rebind members

This adds a static assertion to std::allocator_traits::rebind_alloc to
diagnose violations of the rule that rebinding an allocator to its own
value type yields the same allocator type.

This helps to catch the easy mistake of deriving from std::allocator but
forgetting to override the rebind behaviour (no longer an issue in C++20
as std::allocator doesn't have a rebind member that can be inherited).
It also catches bugs like in 23_containers/vector/52591.cc where a typo
means the rebound allocator is a completely different type.

I initially wanted to put this static assert into the body of
allocator_traits:

      static_assert(is_same<rebind_alloc<value_type>, _Alloc>::value,
    "rebind_alloc<value_type> must be Alloc");

However, this causes a regression in the test for PR libstdc++/72792.
It seems that instantiating std::allocator_traits should be allowed for
invalid allocator types as long as you don't try to rebind them. To
support that, only assert in the __allocator_traits_base::__rebind class
template (in both the primary template and the partial specialization).
As a result, the bug in 20_util/scoped_allocator/outermost.cc is not
diagnosed, because nothing in that test rebinds the allocator.

libstdc++-v3/ChangeLog:

* include/bits/alloc_traits.h (__allocator_traits_base::__rebind):
Add static assert for rebind requirement.
* testsuite/20_util/allocator_traits/members/rebind_alloc.cc:
Fix invalid rebind member in test allocator.
* testsuite/20_util/allocator_traits/requirements/rebind_neg.cc:
New test.
* testsuite/20_util/scoped_allocator/outermost.cc: Add rebind to
test allocator.
* testsuite/23_containers/forward_list/48101_neg.cc: Prune new
static assert error.
* testsuite/23_containers/unordered_multiset/48101_neg.cc:
Likewise.
* testsuite/23_containers/unordered_set/48101_neg.cc:
Likewise.
* testsuite/23_containers/vector/52591.cc: Fix typo in rebind.
libstdc++-v3/include/bits/alloc_traits.h
libstdc++-v3/testsuite/20_util/allocator_traits/members/rebind_alloc.cc
libstdc++-v3/testsuite/20_util/allocator_traits/requirements/rebind_neg.cc [new file with mode: 0644]
libstdc++-v3/testsuite/20_util/scoped_allocator/outermost.cc
libstdc++-v3/testsuite/23_containers/forward_list/48101_neg.cc
libstdc++-v3/testsuite/23_containers/unordered_multiset/48101_neg.cc
libstdc++-v3/testsuite/23_containers/unordered_set/48101_neg.cc
libstdc++-v3/testsuite/23_containers/vector/52591.cc
This page took 0.059792 seconds and 6 git commands to generate.