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>
Subject: [PATCH v4 12/16] Don't turn rvalue reference into a span
Date: Wed, 18 Nov 2026 15:56:13 -0700	[thread overview]
Message-ID: <20261118-span-part-1-v4-12-5ecf901e570b@adacore.com> (raw)
In-Reply-To: <20261118-span-part-1-v4-0-5ecf901e570b@adacore.com>

Compiling with C++20's std::span failed here, because implicitly
converting an rvalue reference to a std::span isn't possible.

This patch changes gdb::span to follow C++20 here, and fixes up the
fallout: mostly the test cases, but also one spot in the code.

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=31422
---
 gdb/rust-lang.c                |  5 ++++-
 gdb/unittests/span-selftests.c | 27 ++++++++++++++-------------
 gdbsupport/span.h              | 14 +++++++++++++-
 3 files changed, 31 insertions(+), 15 deletions(-)

diff --git a/gdb/rust-lang.c b/gdb/rust-lang.c
index 1df3740d39d..79ef7701cb0 100644
--- a/gdb/rust-lang.c
+++ b/gdb/rust-lang.c
@@ -1536,7 +1536,10 @@ eval_op_rust_array (struct type *expect_type, struct expression *exp,
     error (_("Array with negative number of elements"));
 
   if (noside == EVAL_NORMAL && copies > 0)
-    return value_array (0, std::vector<value *> (copies, elt));
+    {
+      std::vector<value *> elements (copies, elt);
+      return value_array (0, elements);
+    }
   else
     {
       struct type *arraytype
diff --git a/gdb/unittests/span-selftests.c b/gdb/unittests/span-selftests.c
index 3e3a7f59ace..2cf875d187c 100644
--- a/gdb/unittests/span-selftests.c
+++ b/gdb/unittests/span-selftests.c
@@ -108,13 +108,13 @@ template<template<typename ...> class Container>
 static constexpr bool
 check_ctor_from_container ()
 {
-  static_assert ( is_convertible <Container<A>, span<A>> ());
-  static_assert (!is_convertible <Container<B>, span<A>> ());
-  static_assert (!is_convertible <Container<C>, span<A>> ());
+  static_assert ( is_convertible <Container<A> &, span<A>> ());
+  static_assert (!is_convertible <Container<B> &, span<A>> ());
+  static_assert (!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>> ());
+  static_assert (!is_convertible <Container<A> &, span<B>> ());
+  static_assert ( is_convertible <Container<B> &, span<B>> ());
+  static_assert (!is_convertible <Container<C> &, span<B>> ());
 
   return true;
 }
@@ -138,9 +138,14 @@ using T = gdb_byte;
 
 /* Note there's no such thing as std::vector<const T>.  */
 
-static_assert (is_convertible <std::vector<T>, span<T>> ());
+static_assert (is_convertible <std::vector<T> &, span<T>> ());
+static_assert (is_convertible <std::vector<T> &, span<const T>> ());
 static_assert (is_convertible <std::vector<T>, span<const T>> ());
 
+/* It's not ok to convert from an rvalue vector.  */
+
+static_assert (!is_convertible <std::vector<T>, span<T>> ());
+
 } /* namespace convertible_from_std_vector */
 
 /* Check that span implicitly converts from std::array.  */
@@ -152,8 +157,8 @@ using T = gdb_byte;
 
 /* Note: a non-const T view can't refer to a const T array.  */
 
-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<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>> ());
 
@@ -422,13 +427,9 @@ run_tests ()
 
     view = vec;
     SELF_CHECK (check_container_view (view, vec));
-    view = std::move (vec);
-    SELF_CHECK (check_container_view (view, vec));
 
     view = array;
     SELF_CHECK (check_container_view (view, array));
-    view = std::move (array);
-    SELF_CHECK (check_container_view (view, array));
 
     gdb_byte elem = 0;
     view = gdb::make_span (&elem, 1);
diff --git a/gdbsupport/span.h b/gdbsupport/span.h
index c3d7d97d380..483fffb30fc 100644
--- a/gdbsupport/span.h
+++ b/gdbsupport/span.h
@@ -138,6 +138,12 @@ class span
     : m_array (array), m_size (Size)
   {}
 
+  template<typename U,
+	   typename = Requires<DecayedConvertible<U>>>
+  constexpr span (const span<U> &other) noexcept
+    : m_array (other.data ()), m_size (other.size ())
+  {}
+
   /* Create a span from a contiguous container.  E.g.,
      std::vector and std::array.  */
   template<typename Container,
@@ -150,7 +156,13 @@ class span
 	   typename
 	     = Requires<std::is_convertible
 			<decltype (std::declval<Container> ().size ()),
-			 size_type>>>
+			 size_type>>,
+	   /* A span can only be constructed from an lvalue container;
+	      however a span with const element type can be
+	      constructed from an rvalue container.  */
+	   typename
+	     = Requires<Or<std::is_lvalue_reference<Container>,
+			   std::is_const<T>>>>
   constexpr span (Container &&c) noexcept
     : m_array (c.data ()), m_size (c.size ())
   {}

-- 
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 ` [PATCH v4 10/16] gdb/unittests: use one static_assert per check in span-selftests.c Tom Tromey
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 ` Tom Tromey [this message]
2026-11-19  3:50   ` [PATCH v4 12/16] Don't turn rvalue reference into a span 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-12-5ecf901e570b@adacore.com \
    --to=tromey@adacore.com \
    --cc=gdb-patches@sourceware.org \
    /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