Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Simon Marchi <simark@simark.ca>
To: Tom Tromey <tromey@adacore.com>, gdb-patches@sourceware.org
Subject: Re: [PATCH v2 11/15] Change span-selftests.c for C++20
Date: Sun, 15 Nov 2026 00:15:49 -0500	[thread overview]
Message-ID: <228feb69-232d-4a09-ac25-a014441405e6@simark.ca> (raw)
In-Reply-To: <3b7df843-4bde-43dc-a8dd-86cf6a3c3ca8@simark.ca>



On 2026-11-14 23:01, Simon Marchi wrote:
> 
> 
> On 2026-11-11 10:58, Tom Tromey wrote:
>> span-selftests.c has a number of tests that aren't valid for C++20
>> std::span.  This patch removes these.
> 
> Could you maybe talk in the commit message about what the general
> classes of changes are?  My understanding (maybe incomplete):
> 
>  - Some were about converting a single element to a span, which
>    std::span doesn't support.
> 
>  - Other changes are about adding a reference (&), not sure what those
>    are about.
> 
>  - Others are about not being able to convert an rvalue-reference to a
>    span.
> 
> Simon

Actually, instead of removing those tests, you could just flip their
expected results, although that would probably mean squashing it with
the next patch.  I think that would be fine though, as the diff in the
test would help illustrate the change in behavior brought by the patch.
It would also help ensure that gdb::span matches std::span in those
regards.

The runtime tests:

    gdb_byte elem = 0;
    view = elem;
    SELF_CHECK (check_elem_view (view, elem));
    view = std::move (elem);
    SELF_CHECK (check_elem_view (view, elem));

can become some static_assert to verify it's not possible anymore:

    static_assert (!std::is_assignable_v<gdb::span<gdb_byte> &, gdb_byte &>);
    static_assert (!std::is_assignable_v<gdb::span<gdb_byte> &, gdb_byte &&>);

I gave that a try, but in its current form it's difficult to see exactly
which static test fails, because they are just big conjunctions.
Switching them to be individual static_asserts would be much clearer.
See this patch that I think you could insert just before this one to
improve the test.


From d9f641f392d96038e776664803fdf2430b39b944 Mon Sep 17 00:00:00 2001
From: Simon Marchi <simon.marchi@polymtl.ca>
Date: Sat, 14 Nov 2026 23:48:53 -0500
Subject: [PATCH] gdb/unittests: use one static_assert per check in
 span-selftests.c
To: gdb-patches@sourceware.org

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 0012d3600d46..eb192bd49fd0 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


  reply	other threads:[~2026-11-15  5:16 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-11 15:58 [PATCH v2 00/15] Rename gdb::array_view to gdb::span Tom Tromey
2026-11-11 15:58 ` [PATCH v2 01/15] Rename array_view files Tom Tromey
2026-11-15  4:09   ` Simon Marchi
2026-11-16 15:55     ` Tom Tromey
2026-11-11 15:58 ` [PATCH v2 02/15] Rename gdb::array_view to gdb::span Tom Tromey
2026-11-15  4:18   ` Simon Marchi
2026-11-11 15:58 ` [PATCH v2 03/15] Rename gdb::make_array_view to gdb::make_span Tom Tromey
2026-11-11 15:58 ` [PATCH v2 04/15] Remove operator== and operator!= from gdb::span Tom Tromey
2026-11-11 15:58 ` [PATCH v2 05/15] Rename span::slice to span::subspan Tom Tromey
2026-11-11 15:58 ` [PATCH v2 06/15] Qualify calls to gdb::copy Tom Tromey
2026-11-11 15:58 ` [PATCH v2 07/15] Add single-argument overloads of process_options et al Tom Tromey
2026-11-11 15:58 ` [PATCH v2 08/15] Remove span from cli-option.c:build_help_option Tom Tromey
2026-11-15  3:40   ` Simon Marchi
2026-11-11 15:58 ` [PATCH v2 09/15] Add gdb::make_span overload and add necessary calls Tom Tromey
2026-11-15  3:53   ` Simon Marchi
2026-11-15  4:35     ` Simon Marchi
2026-11-16 16:55       ` Tom Tromey
2026-11-11 15:58 ` [PATCH v2 10/15] Don't turn rvalue reference into a span Tom Tromey
2026-11-11 15:58 ` [PATCH v2 11/15] Change span-selftests.c for C++20 Tom Tromey
2026-11-15  4:01   ` Simon Marchi
2026-11-15  5:15     ` Simon Marchi [this message]
2026-11-11 15:59 ` [PATCH v2 12/15] Make gdb::span conform more closely to C++20 Tom Tromey
2026-11-11 15:59 ` [PATCH v2 13/15] Allow the use of C++20 std::ranges in gdbsupport/ranges.h Tom Tromey
2026-11-15  5:18   ` Simon Marchi
2026-11-11 15:59 ` [PATCH v2 14/15] Allow the use of C++20 std::span in gdbsupport/span.h Tom Tromey
2026-11-15  4:05   ` Simon Marchi
2026-11-11 15:59 ` [PATCH v2 15/15] Move gdb::ranges::equal to gdbsupport/ranges.h Tom Tromey
2026-11-15  5:21 ` [PATCH v2 00/15] Rename gdb::array_view to gdb::span Simon Marchi
2026-11-17 21:42   ` 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=228feb69-232d-4a09-ac25-a014441405e6@simark.ca \
    --to=simark@simark.ca \
    --cc=gdb-patches@sourceware.org \
    --cc=tromey@adacore.com \
    /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