Skip to contentSkip to conversation

rust: fix tests under Rust 1.89.0's `CStr` `Debug` impl

PATCH In review Tamir Duberstein 2 participants 2 messages Oct 20, 2025 – Oct 22, 2025 Rust

lore
Share & tools
lore

Tamir Duberstein

author reviewer
#0 lore raw

PATCH 1/1 rust: fix tests under Rust 1.89.0's `CStr` `Debug` impl

Starting with Rust 1.89.0, <CStr as Debug>::fmt prints UTF-8 sequences unescaped.

Thus update our test to expect the new behavior starting with Rust 1.89.0.

Fixes a1ec674cd709 ("rust: replace `CStr` with `core::ffi::CStr`")

---
Hi Miguel, please feel free to rebase this in, if easier.

---
 init/Kconfig       | 3 +++
 rust/kernel/str.rs | 6 +++++-
 2 files changed, 8 insertions(+), 1 deletion(-)
init/Kconfig +3 -0
index cab3ad28ca49..aa29eae7f14b 100644
@@ -156,6 +156,9 @@ config RUSTC_HAS_SPAN_FILE
 config RUSTC_HAS_UNNECESSARY_TRANSMUTES
 	def_bool RUSTC_VERSION >= 108800
 
+config RUSTC_HAS_CSTR_DEBUG_UTF8
+	def_bool RUSTC_VERSION >= 108900
+
 config RUSTC_HAS_FILE_WITH_NUL
 	def_bool RUSTC_VERSION >= 108900
 
rust/kernel/str.rs +5 -1
index da539e1f29d4..f2d60288eced 100644
@@ -478,7 +478,11 @@ fn test_cstr_debug() -> Result {
         let non_ascii = c"d\xe9j\xe0 vu";
         assert_eq!(format!("{non_ascii:?}"), "\"d\\xe9j\\xe0 vu\"");
         let good_bytes = c"\xf0\x9f\xa6\x80";
-        assert_eq!(format!("{good_bytes:?}"), "\"\\xf0\\x9f\\xa6\\x80\"");
+        if cfg!(CONFIG_RUSTC_HAS_CSTR_DEBUG_UTF8) {
+            assert_eq!(format!("{good_bytes:?}"), "\"🦀\"");
+        } else {
+            assert_eq!(format!("{good_bytes:?}"), "\"\\xf0\\x9f\\xa6\\x80\"");
+        }
         Ok(())
     }
 

---
base-commit: a1ec674cd709fec213acbb567e699c5f6f58cb60
change-id: 20251020-cstr-debug-utf-8-8e9c43a1b757

Best regards,

Signature · 1 line
Tamir Duberstein <[email protected]>
#1 lore raw
Quoted 4 lines · Tamir Duberstein

Starting with Rust 1.89.0, <CStr as Debug>::fmt prints UTF-8 sequences unescaped.

Quoted 8 lines

Thus update our test to expect the new behavior starting with Rust 1.89.0.

Fixes a1ec674cd709 ("rust: replace `CStr` with `core::ffi::CStr`")

---
Hi Miguel, please feel free to rebase this in, if easier.

Thanks Tamir, yeah, I saw the test failing in my CI.

To keep things simple, I just removed that assert for the moment -- if we care about the escaping behavior on debug formatting to the point of testing the behavior on different Rust versions, then we can re-introduce it.

[ Removed assert that would now depend on the Rust version. - Miguel ]

I took the chance to take the tags we got so far too.

I appreciate that you took the time to put the link to the relevant commit upstream (in this case, it was yourself, but still :)

Cheers,
Miguel