Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Tom Tromey <tromey@adacore.com>
To: gdb-patches@sourceware.org
Cc: Tom Tromey <tromey@adacore.com>, Simon Marchi <simon.marchi@polymtl.ca>
Subject: [PATCH v4 10/16] gdb/unittests: use one static_assert per check in span-selftests.c
Date: Wed, 18 Nov 2026 15:56:11 -0700	[thread overview]
Message-ID: <20261118-span-part-1-v4-10-5ecf901e570b@adacore.com> (raw)
In-Reply-To: <20261118-span-part-1-v4-0-5ecf901e570b@adacore.com>

From: Simon Marchi <simon.marchi@polymtl.ca>

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.


Change-Id: Iaaa19e3fab701bc672763d85ec29a2f2cd042e22
---
 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.  */

-- 
2.56.0


  parent reply	other threads:[~2026-11-18 22:58 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-18 22:56 [PATCH v4 00/16] Rename gdb::array_view to gdb::span Tom Tromey
2026-11-18 22:56 ` [PATCH v4 01/16] Rename array_view files Tom Tromey
2026-11-18 22:56 ` [PATCH v4 02/16] Rename gdb::array_view to gdb::span Tom Tromey
2026-11-18 22:56 ` [PATCH v4 03/16] Rename gdb::make_array_view to gdb::make_span Tom Tromey
2026-11-18 22:56 ` [PATCH v4 04/16] Rename gdb_argv::as_array_view Tom Tromey
2026-11-18 22:56 ` [PATCH v4 05/16] Remove operator== and operator!= from gdb::span Tom Tromey
2026-11-18 22:56 ` [PATCH v4 06/16] Rename span::slice to span::subspan Tom Tromey
2026-11-18 22:56 ` [PATCH v4 07/16] Qualify calls to gdb::copy Tom Tromey
2026-11-18 22:56 ` [PATCH v4 08/16] Add single-argument overloads of process_options et al Tom Tromey
2026-11-18 22:56 ` [PATCH v4 09/16] Remove span from cli-option.c:build_help_option Tom Tromey
2026-11-18 22:56 ` Tom Tromey [this message]
2026-11-18 22:56 ` [PATCH v4 11/16] Don't use the single-argument gdb::span constructor Tom Tromey
2026-11-19  3:34   ` Simon Marchi
2026-11-19  3:42   ` Simon Marchi
2026-11-18 22:56 ` [PATCH v4 12/16] Don't turn rvalue reference into a span Tom Tromey
2026-11-19  3:50   ` Simon Marchi
2026-11-19 14:56     ` Tom Tromey
2026-11-18 22:56 ` [PATCH v4 13/16] Make gdb::span conform more closely to C++20 Tom Tromey
2026-11-18 22:56 ` [PATCH v4 14/16] Allow the use of C++20 std::ranges in gdbsupport/ranges.h Tom Tromey
2026-11-18 22:56 ` [PATCH v4 15/16] Allow the use of C++20 std::span in gdbsupport/span.h Tom Tromey
2026-11-19  3:52   ` Simon Marchi
2026-11-20 15:52   ` Jan Vrany
2026-11-20 20:15     ` Simon Marchi
2026-11-18 22:56 ` [PATCH v4 16/16] Move gdb::ranges::equal to gdbsupport/ranges.h Tom Tromey
2026-11-19  4:21 ` [PATCH v4 00/16] Rename gdb::array_view to gdb::span Simon Marchi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261118-span-part-1-v4-10-5ecf901e570b@adacore.com \
    --to=tromey@adacore.com \
    --cc=gdb-patches@sourceware.org \
    --cc=simon.marchi@polymtl.ca \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox