public inbox for libstdc++-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r14-11180] libstdc++: Skip redundant assertions in std::span construction [PR117966]
@ 2025-01-09 23:53 Jonathan Wakely
  0 siblings, 0 replies; only message in thread
From: Jonathan Wakely @ 2025-01-09 23:53 UTC (permalink / raw)
  To: gcc-cvs, libstdc++-cvs

https://gcc.gnu.org/g:83fa0822aeec8af2162825976209efb90ca40c87

commit r14-11180-g83fa0822aeec8af2162825976209efb90ca40c87
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Mon Dec 9 17:35:24 2024 +0000

    libstdc++: Skip redundant assertions in std::span construction [PR117966]
    
    As PR c++/117966 shows, the Debug Mode checks cause a compilation error
    for a global constexpr std::span. Those debug checks are redundant when
    constructing from an array or a range, because we already know we have a
    valid range and we know its size. Instead of delegating to the
    std::span(contiguous_iterator, contiguous_iterator) constructor, just
    initialize the data members directly.
    
    libstdc++-v3/ChangeLog:
    
            PR libstdc++/117966
            * include/std/span (span(T (&)[N])): Do not delegate to
            constructor that performs redundant checks.
            (span(array<T, N>&), span(const array<T, N>&)): Likewise.
            (span(Range&&), span(const span<T, N>&)): Likewise.
            * testsuite/23_containers/span/117966.cc: New test.
    
    (cherry picked from commit e95bda027e0b81922c1bf44770674190bdf787e8)

Diff:
---
 libstdc++-v3/include/std/span                       | 10 +++++-----
 libstdc++-v3/testsuite/23_containers/span/117966.cc | 13 +++++++++++++
 2 files changed, 18 insertions(+), 5 deletions(-)

diff --git a/libstdc++-v3/include/std/span b/libstdc++-v3/include/std/span
index 00fc52791526..726f0f339f14 100644
--- a/libstdc++-v3/include/std/span
+++ b/libstdc++-v3/include/std/span
@@ -185,21 +185,21 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 	requires (_Extent == dynamic_extent || _ArrayExtent == _Extent)
 	constexpr
 	span(type_identity_t<element_type> (&__arr)[_ArrayExtent]) noexcept
-	: span(static_cast<pointer>(__arr), _ArrayExtent)
+	: _M_ptr(__arr), _M_extent(_ArrayExtent)
 	{ }
 
       template<typename _Tp, size_t _ArrayExtent>
 	requires __is_compatible_array<_Tp, _ArrayExtent>::value
 	constexpr
 	span(array<_Tp, _ArrayExtent>& __arr) noexcept
-	: span(static_cast<pointer>(__arr.data()), _ArrayExtent)
+	: _M_ptr(__arr.data()), _M_extent(_ArrayExtent)
 	{ }
 
       template<typename _Tp, size_t _ArrayExtent>
 	requires __is_compatible_array<const _Tp, _ArrayExtent>::value
 	constexpr
 	span(const array<_Tp, _ArrayExtent>& __arr) noexcept
-	: span(static_cast<pointer>(__arr.data()), _ArrayExtent)
+	: _M_ptr(__arr.data()), _M_extent(_ArrayExtent)
 	{ }
 
       template<typename _Range>
@@ -213,7 +213,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 	span(_Range&& __range)
 	noexcept(noexcept(ranges::data(__range))
 		  && noexcept(ranges::size(__range)))
-	: span(ranges::data(__range), ranges::size(__range))
+	: _M_ptr(ranges::data(__range)), _M_extent(ranges::size(__range))
 	{
 	  if constexpr (extent != dynamic_extent)
 	    {
@@ -231,7 +231,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 	constexpr
 	explicit(extent != dynamic_extent && _OExtent == dynamic_extent)
 	span(const span<_OType, _OExtent>& __s) noexcept
-	: _M_extent(__s.size()), _M_ptr(__s.data())
+	: _M_ptr(__s.data()), _M_extent(__s.size())
 	{
 	  if constexpr (extent != dynamic_extent)
 	    {
diff --git a/libstdc++-v3/testsuite/23_containers/span/117966.cc b/libstdc++-v3/testsuite/23_containers/span/117966.cc
new file mode 100644
index 000000000000..8bbb5ca1e079
--- /dev/null
+++ b/libstdc++-v3/testsuite/23_containers/span/117966.cc
@@ -0,0 +1,13 @@
+// { dg-options "-D_GLIBCXX_DEBUG" }
+// { dg-do compile { target c++20 } }
+
+// Bug 117966
+// constexpr std::span construction fails to compile with D_GLIBCXX_DEBUG
+
+#include <array>
+#include <span>
+
+struct A {
+  constexpr A(std::span<const unsigned char>) {}
+};
+constexpr A val{std::array<unsigned char, 2>{0x11, 0x22}};

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2025-01-09 23:53 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-01-09 23:53 [gcc r14-11180] libstdc++: Skip redundant assertions in std::span construction [PR117966] Jonathan Wakely

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).