diff mbox series

[2/2] libstdc++: Add debug assertions to std::vector<bool> [PR103191]

Message ID 20240627084821.95065-2-jwakely@redhat.com
State New
Headers show
Series [1/2] libstdc++: Enable more debug assertions during constant evaluation [PR111250] | expand

Commit Message

Jonathan Wakely June 27, 2024, 8:45 a.m. UTC
The previous commit allows these to work for constexpr too, which is
nice.

Tested x86_64-linux. Pushed to trunk.

-- >8 --

This adds debug assertions for std::vector<bool> element access.

libstdc++-v3/ChangeLog:

	PR libstdc++/103191
	* include/bits/stl_bvector.h (vector<bool>::operator[])
	(vector<bool>::front, vector<bool>::back): Add debug assertions.
	* testsuite/23_containers/vector/bool/element_access/constexpr.cc:
	Remove dg-error that no longer triggers.
---
 libstdc++-v3/include/bits/stl_bvector.h       | 30 +++++++++++++++----
 .../vector/bool/element_access/constexpr.cc   |  2 +-
 2 files changed, 25 insertions(+), 7 deletions(-)
diff mbox series

Patch

diff --git a/libstdc++-v3/include/bits/stl_bvector.h b/libstdc++-v3/include/bits/stl_bvector.h
index 52153cadf8f..8685cc64cc4 100644
--- a/libstdc++-v3/include/bits/stl_bvector.h
+++ b/libstdc++-v3/include/bits/stl_bvector.h
@@ -1084,12 +1084,18 @@  _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
       _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
       reference
       operator[](size_type __n)
-      { return begin()[__n]; }
+      {
+	__glibcxx_requires_subscript(__n);
+	return begin()[__n];
+      }
 
       _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
       const_reference
       operator[](size_type __n) const
-      { return begin()[__n]; }
+      {
+	__glibcxx_requires_subscript(__n);
+	return begin()[__n];
+      }
 
     protected:
       _GLIBCXX20_CONSTEXPR
@@ -1133,22 +1139,34 @@  _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
       _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
       reference
       front()
-      { return *begin(); }
+      {
+	__glibcxx_requires_nonempty();
+	return *begin();
+      }
 
       _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
       const_reference
       front() const
-      { return *begin(); }
+      {
+	__glibcxx_requires_nonempty();
+	return *begin();
+      }
 
       _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
       reference
       back()
-      { return *(end() - 1); }
+      {
+	__glibcxx_requires_nonempty();
+	return *(end() - 1);
+      }
 
       _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
       const_reference
       back() const
-      { return *(end() - 1); }
+      {
+	__glibcxx_requires_nonempty();
+	return *(end() - 1);
+      }
 
       _GLIBCXX20_CONSTEXPR
       void
diff --git a/libstdc++-v3/testsuite/23_containers/vector/bool/element_access/constexpr.cc b/libstdc++-v3/testsuite/23_containers/vector/bool/element_access/constexpr.cc
index bff9f7b4e0f..7c60e5db4d1 100644
--- a/libstdc++-v3/testsuite/23_containers/vector/bool/element_access/constexpr.cc
+++ b/libstdc++-v3/testsuite/23_containers/vector/bool/element_access/constexpr.cc
@@ -115,4 +115,4 @@  template<typename T = bool>
   constexpr std::true_type
   access_empty_front() { return {}; }
 
-static_assert( ! access_empty_front() ); // { dg-error "ambiguous" "PR 103191" { target { ! debug_mode } } }
+static_assert( ! access_empty_front() );