[libc-commits] [libc] 9e13940 - [libc] Add missing const in cpp::span and scanf_core::FormatSection (#240610)
via libc-commits
libc-commits at lists.llvm.org
Mon Nov 9 19:55:38 PST 2026
Author: Roland McGrath
Date: 2026-11-09T19:55:34-08:00
New Revision: 9e1394090187cd592d679b4396afdf8445eb85a4
URL: https://github.com/llvm/llvm-project/commit/9e1394090187cd592d679b4396afdf8445eb85a4
DIFF: https://github.com/llvm/llvm-project/commit/9e1394090187cd592d679b4396afdf8445eb85a4.diff
LOG: [libc] Add missing const in cpp::span and scanf_core::FormatSection (#240610)
cpp::span provided a constructor from non-const cpp::array<U, N> & but
missed the corresponding const cpp::array<U, N> & overload from C++20
std::span, and scanf_core::FormatSection::operator== lacked a const
qualifier. Add both so const span-based and scanf format-section
comparisons work without const_cast or mutable copies.
Part of #91630
Assisted-by: Gemini
Added:
Modified:
libc/src/__support/CPP/span.h
libc/src/stdio/scanf_core/core_structs.h
libc/test/src/__support/CPP/span_test.cpp
Removed:
################################################################################
diff --git a/libc/src/__support/CPP/span.h b/libc/src/__support/CPP/span.h
index 859ba24d1442e7..cbf15d160feef1 100644
--- a/libc/src/__support/CPP/span.h
+++ b/libc/src/__support/CPP/span.h
@@ -77,6 +77,11 @@ template <typename T> class span {
LIBC_INLINE constexpr span(array<U, N> &arr)
: span_data(arr.data()), span_size(arr.size()) {}
+ template <typename U, size_t N,
+ cpp::enable_if_t<is_compatible_v<const U>, bool> = true>
+ LIBC_INLINE constexpr span(const array<U, N> &arr)
+ : span_data(arr.data()), span_size(arr.size()) {}
+
template <typename U, cpp::enable_if_t<is_compatible_v<U>, bool> = true>
LIBC_INLINE constexpr span(const span<U> &s)
: span_data(s.data()), span_size(s.size()) {}
diff --git a/libc/src/stdio/scanf_core/core_structs.h b/libc/src/stdio/scanf_core/core_structs.h
index 87b2429ae550e6..7a80dc1166eb7f 100644
--- a/libc/src/stdio/scanf_core/core_structs.h
+++ b/libc/src/stdio/scanf_core/core_structs.h
@@ -46,7 +46,7 @@ struct FormatSection {
cpp::bitset<256> scan_set;
- LIBC_INLINE bool operator==(const FormatSection &other) {
+ LIBC_INLINE bool operator==(const FormatSection &other) const {
if (has_conv != other.has_conv)
return false;
diff --git a/libc/test/src/__support/CPP/span_test.cpp b/libc/test/src/__support/CPP/span_test.cpp
index 9d83e722c69c64..e8686749f85606 100644
--- a/libc/test/src/__support/CPP/span_test.cpp
+++ b/libc/test/src/__support/CPP/span_test.cpp
@@ -86,6 +86,15 @@ TEST(LlvmLibcSpanTest, InitializeViewFormMutableArray) {
ASSERT_EQ(s[2], 3);
}
+TEST(LlvmLibcSpanTest, InitializeViewFromConstArray) {
+ const array<int, 3> a = {1, 2, 3};
+ span<const int> s(a);
+ ASSERT_EQ(s.size(), size_t(3));
+ ASSERT_EQ(s[0], 1);
+ ASSERT_EQ(s[1], 2);
+ ASSERT_EQ(s[2], 3);
+}
+
TEST(LlvmLibcSpanTest, InitializeFromMutable) {
int a[] = {1, 2, 3};
span<int> s(a);
More information about the libc-commits
mailing list