[binutils-gdb] gdb/unittests: use one static_assert per check in span-selftests.c

Tom Tromey tromey@sourceware.org
Thu Nov 19 16:25:57 GMT 2026


https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=a2075bda73ca402f873a25a5a7a5a08c6d1c26d7

commit a2075bda73ca402f873a25a5a7a5a08c6d1c26d7
Author: Simon Marchi <simon.marchi@polymtl.ca>
Date:   Sat Nov 14 23:48:53 2026 -0500

    gdb/unittests: use one static_assert per check in span-selftests.c
    
    The conversion checks in span-selftests.c are written as long chains of
    "&&" inside constexpr functions, checked by a single static_assert.
    When one of the checks fails, the compiler only reports that the
    static_assert failed, which makes it hard to figure out which check is
    the culprit.
    
    Change these to use one static_assert per check, so that the compiler
    points at the failing one.  The checks themselves are unchanged.
    
    check_ctor_from_container is a template, so keep it as a function,
    with the static_asserts in its body.  The other checks are moved to
    namespace scope, in namespaces named after the functions they replace.
    
    Approved-By: Tom Tromey <tom@tromey.com>
    Change-Id: Iaaa19e3fab701bc672763d85ec29a2f2cd042e22

Diff:
---
 gdb/unittests/span-selftests.c | 142 ++++++++++++++++++-----------------------
 1 file changed, 62 insertions(+), 80 deletions(-)

diff --git a/gdb/unittests/span-selftests.c b/gdb/unittests/span-selftests.c
index 0012d3600d4..eb192bd49fd 100644
--- a/gdb/unittests/span-selftests.c
+++ b/gdb/unittests/span-selftests.c
@@ -48,33 +48,29 @@ is_convertible ()
 
 /* Check for implicit conversion to immutable and mutable views.  */
 
-static constexpr bool
-check_convertible ()
+namespace convertible
 {
-  using T = gdb_byte;
-  using gdb::span;
-
-  return (true
-	  /* immutable span */
-	  &&  is_convertible<const T (&) [1],	span<const T>> ()
-	  &&  is_convertible<T (&) [1], 	span<const T>> ()
-	  &&  is_convertible<const T, 		span<const T>> ()
-	  &&  is_convertible<T, 		span<const T>> ()
-
-	  /* mutable span */
-	  &&  is_convertible<T (&) [1], 	span<T>> ()
-	  && !is_convertible<const T (&) [1],	span<T>> ()
-	  &&  is_convertible<T, 		span<T>> ()
-	  && !is_convertible<const T,		span<T>> ()
-
-	  /* While float is implicitly convertible to gdb_byte, we
-	     don't want implicit float->span<gdb_byte>
-	     conversion.  */
-	  && !is_convertible<float, 		span<const T>> ()
-	  && !is_convertible<float, 		span<T>> ());
-}
+using T = gdb_byte;
+using gdb::span;
+
+/* immutable span */
+static_assert ( is_convertible<const T (&) [1],	span<const T>> ());
+static_assert ( is_convertible<T (&) [1], 	span<const T>> ());
+static_assert ( is_convertible<const T,		span<const T>> ());
+static_assert ( is_convertible<T,		span<const T>> ());
+
+/* mutable span */
+static_assert ( is_convertible<T (&) [1],	span<T>> ());
+static_assert (!is_convertible<const T (&) [1],	span<T>> ());
+static_assert ( is_convertible<T,		span<T>> ());
+static_assert (!is_convertible<const T,		span<T>> ());
 
-static_assert (check_convertible (), "");
+/* While float is implicitly convertible to gdb_byte, we don't want
+   implicit float->span<gdb_byte> conversion.  */
+static_assert (!is_convertible<float,		span<const T>> ());
+static_assert (!is_convertible<float,		span<T>> ());
+
+} /* namespace convertible */
 
 namespace no_slicing
 {
@@ -82,35 +78,28 @@ struct A { int i; };
 struct B : A { int j; };
 struct C : A { int l; };
 
+using gdb::span;
+
 /* Check that there's no array->view conversion for arrays of derived types or
    subclasses.  */
-static constexpr bool
-check ()
-{
-  using gdb::span;
 
-  return (true
+static_assert ( is_convertible <A (&)[1], span<A>> ());
+static_assert (!is_convertible <B (&)[1], span<A>> ());
+static_assert (!is_convertible <C (&)[1], span<A>> ());
 
-	  /* array->view  */
+static_assert (!is_convertible <A (&)[1], span<B>> ());
+static_assert ( is_convertible <B (&)[1], span<B>> ());
+static_assert (!is_convertible <C (&)[1], span<B>> ());
 
-	  &&  is_convertible <A (&)[1], span<A>> ()
-	  && !is_convertible <B (&)[1], span<A>> ()
-	  && !is_convertible <C (&)[1], span<A>> ()
+/* Same, for elem->view.  */
 
-	  && !is_convertible <A (&)[1], span<B>> ()
-	  &&  is_convertible <B (&)[1], span<B>> ()
-	  && !is_convertible <C (&)[1], span<B>> ()
+static_assert ( is_convertible <A, span<A>> ());
+static_assert (!is_convertible <B, span<A>> ());
+static_assert (!is_convertible <C, span<A>> ());
 
-	  /* elem->view  */
-
-	  &&  is_convertible <A, span<A>> ()
-	  && !is_convertible <B, span<A>> ()
-	  && !is_convertible <C, span<A>> ()
-
-	  && !is_convertible <A, span<B>> ()
-	  &&  is_convertible <B, span<B>> ()
-	  && !is_convertible <C, span<B>> ());
-}
+static_assert (!is_convertible <A, span<B>> ());
+static_assert ( is_convertible <B, span<B>> ());
+static_assert (!is_convertible <C, span<B>> ());
 
 /* Check that there's no container->view conversion for containers of derived
    types or subclasses.  */
@@ -119,15 +108,15 @@ template<template<typename ...> class Container>
 static constexpr bool
 check_ctor_from_container ()
 {
-  using gdb::span;
+  static_assert ( is_convertible <Container<A>, span<A>> ());
+  static_assert (!is_convertible <Container<B>, span<A>> ());
+  static_assert (!is_convertible <Container<C>, span<A>> ());
 
-  return (    is_convertible <Container<A>, span<A>> ()
-	  && !is_convertible <Container<B>, span<A>> ()
-	  && !is_convertible <Container<C>, span<A>> ()
+  static_assert (!is_convertible <Container<A>, span<B>> ());
+  static_assert ( is_convertible <Container<B>, span<B>> ());
+  static_assert (!is_convertible <Container<C>, span<B>> ());
 
-	  && !is_convertible <Container<A>, span<B>> ()
-	  &&  is_convertible <Container<B>, span<B>> ()
-	  && !is_convertible <Container<C>, span<B>> ());
+  return true;
 }
 
 } /* namespace no_slicing */
@@ -136,46 +125,39 @@ check_ctor_from_container ()
    check_ctor_from_container.  */
 template<typename T> using StdArray1 = std::array<T, 1>;
 
-static_assert (no_slicing::check (), "");
-static_assert (no_slicing::check_ctor_from_container<std::vector> (), "");
-static_assert (no_slicing::check_ctor_from_container<StdArray1> (), "");
-static_assert (no_slicing::check_ctor_from_container<gdb::span> (), "");
+static_assert (no_slicing::check_ctor_from_container<std::vector> ());
+static_assert (no_slicing::check_ctor_from_container<StdArray1> ());
+static_assert (no_slicing::check_ctor_from_container<gdb::span> ());
 
 /* Check that span implicitly converts from std::vector.  */
 
-static constexpr bool
-check_convertible_from_std_vector ()
+namespace convertible_from_std_vector
 {
-  using gdb::span;
-  using T = gdb_byte;
+using gdb::span;
+using T = gdb_byte;
 
-  /* Note there's no such thing as std::vector<const T>.  */
+/* Note there's no such thing as std::vector<const T>.  */
 
-  return (true
-	  &&  is_convertible <std::vector<T>, span<T>> ()
-	  &&  is_convertible <std::vector<T>, span<const T>> ());
-}
+static_assert (is_convertible <std::vector<T>, span<T>> ());
+static_assert (is_convertible <std::vector<T>, span<const T>> ());
 
-static_assert (check_convertible_from_std_vector (), "");
+} /* namespace convertible_from_std_vector */
 
 /* Check that span implicitly converts from std::array.  */
 
-static constexpr bool
-check_convertible_from_std_array ()
+namespace convertible_from_std_array
 {
-  using gdb::span;
-  using T = gdb_byte;
+using gdb::span;
+using T = gdb_byte;
 
-  /* Note: a non-const T view can't refer to a const T array.  */
+/* Note: a non-const T view can't refer to a const T array.  */
 
-  return (true
-	  &&  is_convertible <std::array<T, 1>,		span<T>> ()
-	  &&  is_convertible <std::array<T, 1>,		span<const T>> ()
-	  && !is_convertible <std::array<const T, 1>,	span<T>> ()
-	  &&  is_convertible <std::array<const T, 1>,	span<const T>> ());
-}
+static_assert ( is_convertible <std::array<T, 1>,	span<T>> ());
+static_assert ( is_convertible <std::array<T, 1>,	span<const T>> ());
+static_assert (!is_convertible <std::array<const T, 1>,	span<T>> ());
+static_assert ( is_convertible <std::array<const T, 1>,	span<const T>> ());
 
-static_assert (check_convertible_from_std_array (), "");
+} /* namespace convertible_from_std_array */
 
 /* Check that VIEW views C (a container like std::vector/std::array)
    correctly.  */


More information about the Gdb-cvs mailing list