[libc-commits] [libc] [libc] Make cpp::byte alias-safe (PR #194171)

Mikhail R. Gadelha via libc-commits libc-commits at lists.llvm.org
Sat Apr 25 12:14:42 PDT 2026


https://github.com/mikhailramalho created https://github.com/llvm/llvm-project/pull/194171

Change LIBC_NAMESPACE::cpp::byte from an enum-backed type to unsigned char so libc’s raw-memory utilities and sorting code can legally access object representations without violating C++ strict-aliasing rules.

>From 1440f4d9ed1b311862d7981656bff49da7b818c3 Mon Sep 17 00:00:00 2001
From: "Mikhail R. Gadelha" <mikhail at igalia.com>
Date: Sat, 25 Apr 2026 16:09:19 -0300
Subject: [PATCH] [libc] Make cpp::byte alias-safe
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Change LIBC_NAMESPACE::cpp::byte from an enum-backed type to unsigned
char so libc’s raw-memory utilities and sorting code can legally access
object representations without violating C++ strict-aliasing rules.
---
 libc/src/__support/CPP/cstddef.h             | 26 +-------------------
 libc/test/src/__support/CPP/cstddef_test.cpp |  4 +--
 2 files changed, 3 insertions(+), 27 deletions(-)

diff --git a/libc/src/__support/CPP/cstddef.h b/libc/src/__support/CPP/cstddef.h
index ed6c9d03362f4..873f4a37c5b36 100644
--- a/libc/src/__support/CPP/cstddef.h
+++ b/libc/src/__support/CPP/cstddef.h
@@ -16,7 +16,7 @@
 namespace LIBC_NAMESPACE_DECL {
 namespace cpp {
 
-enum class byte : unsigned char {};
+using byte = unsigned char;
 
 template <class IntegerType>
 LIBC_INLINE constexpr enable_if_t<is_integral_v<IntegerType>, byte>
@@ -38,30 +38,6 @@ LIBC_INLINE constexpr enable_if_t<is_integral_v<IntegerType>, byte &>
 operator<<=(byte &b, IntegerType shift) noexcept {
   return b = b << shift;
 }
-LIBC_INLINE constexpr byte operator|(byte l, byte r) noexcept {
-  return static_cast<byte>(static_cast<unsigned char>(l) |
-                           static_cast<unsigned char>(r));
-}
-LIBC_INLINE constexpr byte &operator|=(byte &l, byte r) noexcept {
-  return l = l | r;
-}
-LIBC_INLINE constexpr byte operator&(byte l, byte r) noexcept {
-  return static_cast<byte>(static_cast<unsigned char>(l) &
-                           static_cast<unsigned char>(r));
-}
-LIBC_INLINE constexpr byte &operator&=(byte &l, byte r) noexcept {
-  return l = l & r;
-}
-LIBC_INLINE constexpr byte operator^(byte l, byte r) noexcept {
-  return static_cast<byte>(static_cast<unsigned char>(l) ^
-                           static_cast<unsigned char>(r));
-}
-LIBC_INLINE constexpr byte &operator^=(byte &l, byte r) noexcept {
-  return l = l ^ r;
-}
-LIBC_INLINE constexpr byte operator~(byte b) noexcept {
-  return static_cast<byte>(~static_cast<unsigned char>(b));
-}
 template <typename IntegerType>
 LIBC_INLINE constexpr enable_if_t<is_integral_v<IntegerType>, IntegerType>
 to_integer(byte b) noexcept {
diff --git a/libc/test/src/__support/CPP/cstddef_test.cpp b/libc/test/src/__support/CPP/cstddef_test.cpp
index eceabaa815983..db24bcc705f50 100644
--- a/libc/test/src/__support/CPP/cstddef_test.cpp
+++ b/libc/test/src/__support/CPP/cstddef_test.cpp
@@ -30,8 +30,8 @@ TEST(LlvmLibcByteTest, bitwise) {
   ASSERT_EQ(b, byte{0b01010100});
   b >>= 1;
 
-  ASSERT_EQ((b << 1), byte{0b01010100});
-  ASSERT_EQ((b >> 1), byte{0b00010101});
+  ASSERT_EQ(static_cast<unsigned int>(b << 1), 0b01010100U);
+  ASSERT_EQ(static_cast<unsigned int>(b >> 1), 0b00010101U);
 
   b |= byte{0b11110000};
   ASSERT_EQ(b, byte{0b11111010});



More information about the libc-commits mailing list