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 v3 12/17] gdb/unittests: use one static_assert per check in span-selftests.c
Date: Tue, 17 Nov 2026 15:10:08 -0700 [thread overview]
Message-ID: <20261117-span-part-1-v3-12-40b8b11d2d8c@adacore.com> (raw)
In-Reply-To: <20261117-span-part-1-v3-0-40b8b11d2d8c@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
next prev parent reply other threads:[~2026-11-17 22:12 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-17 22:09 [PATCH v3 00/17] Rename gdb::array_view to gdb::span Tom Tromey
2026-11-17 22:09 ` [PATCH v3 01/17] Rename array_view files Tom Tromey
2026-11-17 22:09 ` [PATCH v3 02/17] Rename gdb::array_view to gdb::span Tom Tromey
2026-11-17 22:09 ` [PATCH v3 03/17] Rename gdb::make_array_view to gdb::make_span Tom Tromey
2026-11-17 22:10 ` [PATCH v3 04/17] Rename gdb_argv::as_array_view Tom Tromey
2026-11-17 22:10 ` [PATCH v3 05/17] Remove operator== and operator!= from gdb::span Tom Tromey
2026-11-17 22:10 ` [PATCH v3 06/17] Rename span::slice to span::subspan Tom Tromey
2026-11-17 22:10 ` [PATCH v3 07/17] Qualify calls to gdb::copy Tom Tromey
2026-11-17 22:10 ` [PATCH v3 08/17] Add single-argument overloads of process_options et al Tom Tromey
2026-11-17 22:10 ` [PATCH v3 09/17] Remove span from cli-option.c:build_help_option Tom Tromey
2026-11-17 22:10 ` [PATCH v3 10/17] Don't use the single-argument gdb::span constructor Tom Tromey
2026-11-17 22:10 ` [PATCH v3 11/17] Don't turn rvalue reference into a span Tom Tromey
2026-11-17 22:10 ` Tom Tromey [this message]
2026-11-17 22:10 ` [PATCH v3 13/17] Change span-selftests.c for C++20 Tom Tromey
2026-11-17 22:10 ` [PATCH v3 14/17] Make gdb::span conform more closely to C++20 Tom Tromey
2026-11-17 22:10 ` [PATCH v3 15/17] Allow the use of C++20 std::ranges in gdbsupport/ranges.h Tom Tromey
2026-11-17 22:10 ` [PATCH v3 16/17] Allow the use of C++20 std::span in gdbsupport/span.h Tom Tromey
2026-11-17 22:10 ` [PATCH v3 17/17] Move gdb::ranges::equal to gdbsupport/ranges.h Tom Tromey
2026-11-18 15:04 ` [PATCH v3 00/17] Rename gdb::array_view to gdb::span Tom Tromey
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=20261117-span-part-1-v3-12-40b8b11d2d8c@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