[PATCH RFC] libstdc++: use contract_assert for __glibcxx_assert

Jonathan Wakely jwakely@redhat.com
Mon Nov 16 10:17:44 GMT 2026


On Fri, 13 Nov 2026 at 15:39 -0500, Jason Merrill wrote:
>Tested x86_64-pc-linux-gnu.
>
>I assume you don't want to condition this on only __cpp_contracts as in the
>current patch.  And I don't know how you want to handle the view_interface
>empty() issue.  But I think the rest of the patch could go in as is.  Thoughts?

Maybe just __cpp_contracts && _GLIBCXX_USE_CONTRACTS_FOR_ASSERTIONS ?

>-- 8< --

[...]

+// { dg-prune-output "contract predicate is false" }
>diff --git a/libstdc++-v3/testsuite/23_containers/vector/element_access/constexpr.cc b/libstdc++-v3/testsuite/23_containers/vector/element_access/constexpr.cc
>index 358ded47ad9..00029ebee39 100644
>--- a/libstdc++-v3/testsuite/23_containers/vector/element_access/constexpr.cc
>+++ b/libstdc++-v3/testsuite/23_containers/vector/element_access/constexpr.cc
>@@ -121,3 +121,5 @@ template<typename T = int>
>   access_past_the_end() { return {}; }
>
> static_assert( ! access_past_the_end() );
>+
>+// { dg-prune-output "contract predicate is false" }
>diff --git a/libstdc++-v3/testsuite/25_algorithms/clamp/118185.cc b/libstdc++-v3/testsuite/25_algorithms/clamp/118185.cc
>index e9cf896d355..4482141b6c1 100644
>--- a/libstdc++-v3/testsuite/25_algorithms/clamp/118185.cc
>+++ b/libstdc++-v3/testsuite/25_algorithms/clamp/118185.cc
>@@ -5,7 +5,7 @@
>
> struct Comp
> {
>-  constexpr bool operator()(const int&& x, const int&& y) { return x < y; }
>+  constexpr bool operator()(const int&& x, const int&& y) const { return x < y; }

I looked into whether this was intentional, and although PR118185 is
about perfect-forwarding (where cv-quals are relevant) the comp
parameter isn't forwarded, and doesn't need to be non-const invocable.
So this is a good change.

> };
>
> struct Proj
>diff --git a/libstdc++-v3/testsuite/26_numerics/lcm/105844.cc b/libstdc++-v3/testsuite/26_numerics/lcm/105844.cc
>index 65a999c3e05..98534262a29 100644
>--- a/libstdc++-v3/testsuite/26_numerics/lcm/105844.cc
>+++ b/libstdc++-v3/testsuite/26_numerics/lcm/105844.cc
>@@ -21,4 +21,4 @@ constexpr int e = std::lcm(500000u, 499999); // { dg-error "in .constexpr." }
> constexpr int f = std::lcm(499999u, 500000); // { dg-error "in .constexpr." }
>
> // { dg-error "overflow" "" { target *-*-* } 0 }
>-// { dg-error "assert_fail" "" { target *-*-* } 0 }
>+// { dg-error "assert_fail|contract" "" { target *-*-* } 0 }
>diff --git a/libstdc++-v3/testsuite/26_numerics/random/piecewise_constant_distribution/cons/input_type_fallback_neg.cc b/libstdc++-v3/testsuite/26_numerics/random/piecewise_constant_distribution/cons/input_type_fallback_neg.cc
>index 3dfba49d7f9..f74a893247d 100644
>--- a/libstdc++-v3/testsuite/26_numerics/random/piecewise_constant_distribution/cons/input_type_fallback_neg.cc
>+++ b/libstdc++-v3/testsuite/26_numerics/random/piecewise_constant_distribution/cons/input_type_fallback_neg.cc
>@@ -52,4 +52,5 @@ int main()
>   test<long double>(); // { dg-error "here" }
> }
>
>+// { dg-prune-output "contract predicate is false" }
> // { dg-prune-output "static assertion failed" }
>diff --git a/libstdc++-v3/testsuite/26_numerics/random/piecewise_linear_distribution/cons/input_type_fallback_neg.cc b/libstdc++-v3/testsuite/26_numerics/random/piecewise_linear_distribution/cons/input_type_fallback_neg.cc
>index 4bc9ca2704e..8b2833c1437 100644
>--- a/libstdc++-v3/testsuite/26_numerics/random/piecewise_linear_distribution/cons/input_type_fallback_neg.cc
>+++ b/libstdc++-v3/testsuite/26_numerics/random/piecewise_linear_distribution/cons/input_type_fallback_neg.cc
>@@ -52,4 +52,5 @@ int main()
>   test<long double>(); // { dg-error "here" }
> }
>
>+// { dg-prune-output "contract predicate is false" }
> // { dg-prune-output "static assertion failed" }
>diff --git a/libstdc++-v3/testsuite/26_numerics/stdckdint/2_neg.cc b/libstdc++-v3/testsuite/26_numerics/stdckdint/2_neg.cc
>index 7954da2d0d9..297ca48b8bf 100644
>--- a/libstdc++-v3/testsuite/26_numerics/stdckdint/2_neg.cc
>+++ b/libstdc++-v3/testsuite/26_numerics/stdckdint/2_neg.cc
>@@ -36,4 +36,5 @@ test_mul(int i, char c, bool b)
> }
>
> // { dg-prune-output "static assertion failed" }
>+// { dg-prune-output "contract predicate is false" }
> // { dg-prune-output "pointer to 'const'" }
>diff --git a/libstdc++-v3/testsuite/30_threads/semaphore/cons_neg.cc b/libstdc++-v3/testsuite/30_threads/semaphore/cons_neg.cc
>index 56e27d7ddc4..13b1c5dcfff 100644
>--- a/libstdc++-v3/testsuite/30_threads/semaphore/cons_neg.cc
>+++ b/libstdc++-v3/testsuite/30_threads/semaphore/cons_neg.cc
>@@ -10,3 +10,5 @@ int main()
>   // Preconditions: desired >= 0 is true, and desired <= max() is true.
>   std::binary_semaphore b(2);
> }
>+
>+// { dg-prune-output "contract predicate is false" }
>diff --git a/libstdc++-v3/testsuite/std/memory/start_lifetime_as/neg.cc b/libstdc++-v3/testsuite/std/memory/start_lifetime_as/neg.cc
>index 0eeed67be48..9335a0db6f0 100644
>--- a/libstdc++-v3/testsuite/std/memory/start_lifetime_as/neg.cc
>+++ b/libstdc++-v3/testsuite/std/memory/start_lifetime_as/neg.cc
>@@ -42,4 +42,5 @@ void test(void* p)
>
> // { dg-prune-output "incomplete type" }
> // { dg-prune-output "template argument must be a complete type" }
>+// { dg-prune-output "contract predicate is false" }
> // { dg-prune-output "static assertion" }
>diff --git a/libstdc++-v3/include/ext/pb_ds/detail/bin_search_tree_/rotate_fn_imps.hpp b/libstdc++-v3/include/ext/pb_ds/detail/bin_search_tree_/rotate_fn_imps.hpp
>index 90151b07b69..ba9846ed7db 100644
>--- a/libstdc++-v3/include/ext/pb_ds/detail/bin_search_tree_/rotate_fn_imps.hpp
>+++ b/libstdc++-v3/include/ext/pb_ds/detail/bin_search_tree_/rotate_fn_imps.hpp
>@@ -114,7 +114,7 @@ rotate_parent(node_pointer p_nd)
>   else
>     rotate_left(p_parent);
>
>-  _GLIBCXX_DEBUG_ASSERT(p_parent->m_p_parent = p_nd);
>+  _GLIBCXX_DEBUG_ASSERT(p_parent->m_p_parent == p_nd);
>   _GLIBCXX_DEBUG_ASSERT(p_nd->m_p_left == p_parent ||
> 		   p_nd->m_p_right == p_parent);
> }
>diff --git a/libstdc++-v3/include/ext/pb_ds/detail/gp_hash_table_map_/erase_fn_imps.hpp b/libstdc++-v3/include/ext/pb_ds/detail/gp_hash_table_map_/erase_fn_imps.hpp
>index 769a35231f1..463692f6aca 100644
>--- a/libstdc++-v3/include/ext/pb_ds/detail/gp_hash_table_map_/erase_fn_imps.hpp
>+++ b/libstdc++-v3/include/ext/pb_ds/detail/gp_hash_table_map_/erase_fn_imps.hpp
>@@ -45,7 +45,7 @@ inline void
> PB_DS_CLASS_C_DEC::
> erase_entry(entry_pointer p_e)
> {
>-  _GLIBCXX_DEBUG_ASSERT(p_e->m_stat = valid_entry_status);
>+  _GLIBCXX_DEBUG_ASSERT(p_e->m_stat == valid_entry_status);
>   _GLIBCXX_DEBUG_ONLY(debug_base::erase_existing(PB_DS_V2F(p_e->m_value));)
>   p_e->m_value.~value_type();
>   p_e->m_stat = erased_entry_status;
>diff --git a/libstdc++-v3/include/ext/pb_ds/detail/pat_trie_/rotate_fn_imps.hpp b/libstdc++-v3/include/ext/pb_ds/detail/pat_trie_/rotate_fn_imps.hpp
>index ffe5c22d34d..404cce6a163 100644
>--- a/libstdc++-v3/include/ext/pb_ds/detail/pat_trie_/rotate_fn_imps.hpp
>+++ b/libstdc++-v3/include/ext/pb_ds/detail/pat_trie_/rotate_fn_imps.hpp
>@@ -108,7 +108,7 @@ rotate_parent(node_pointer p_nd)
>     rotate_right(p_parent);
>   else
>     rotate_left(p_parent);
>-  _GLIBCXX_DEBUG_ASSERT(p_parent->m_p_parent = p_nd);
>+  _GLIBCXX_DEBUG_ASSERT(p_parent->m_p_parent == p_nd);
>   _GLIBCXX_DEBUG_ASSERT(p_nd->m_p_left == p_parent || p_nd->m_p_right == p_parent);
> }

These fixes for the pb_ds tests could be pushed separately, the
assertions are just wrong and constification did us a favour here.

That will simplify backporting the fix to the pb_ds tests on the
branches.



More information about the Libstdc++ mailing list