public inbox for rust-for-linux@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] rust: fmt: reject {:p} format specifier for raw pointers
@ 2025-12-23  3:30 Ke Sun
  2025-12-23  6:41 ` Miguel Ojeda
  0 siblings, 1 reply; 6+ messages in thread
From: Ke Sun @ 2025-12-23  3:30 UTC (permalink / raw)
  To: Miguel Ojeda
  Cc: Boqun Feng, Gary Guo, Björn Roy Baron, Benno Lossin,
	Andreas Hindborg, Alice Ryhl, Trevor Gross, Danilo Krummrich,
	Tamir Duberstein, rust-for-linux, Ke Sun

Add compile-time check in fmt! macro to detect {:p} format specifier
and emit a compile error when it's used.

Signed-off-by: Ke Sun <sunke@kylinos.cn>
---
Changes: 
- According to Alice's suggestion, attempting to print raw pointers with
  {:p} format specifier should not compile at all.
- Link: https://lore.kernel.org/all/aUO1nRdaAISMjMTG@google.com/
---
 rust/macros/fmt.rs | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)

diff --git a/rust/macros/fmt.rs b/rust/macros/fmt.rs
index 2f4b9f6e22110..76dd3d1ee56ad 100644
--- a/rust/macros/fmt.rs
+++ b/rust/macros/fmt.rs
@@ -36,6 +36,23 @@ pub(crate) fn fmt(input: TokenStream) -> TokenStream {
             }
             if let Some((name, rest)) = first_str.split_once('}') {
                 first_str = rest;
+                // Check for {:p} format specifier and emit compile error
+                if name.contains(':') {
+                    let (_, format_spec) = name.split_once(':').unwrap_or(("", name));
+                    if format_spec.trim() == "p" {
+                        let error_msg = "{:p} formatting for raw pointers is not supported!";
+                        // Generate compile_error! that returns a format_args! placeholder
+                        // This ensures the macro expansion produces a valid expression
+                        // Use raw string literal for format! to avoid manual escaping
+                        // error_msg already contains escaped quotes, so regular string literal is sufficient
+                        return format!(
+                            r##"{{ ::core::compile_error!("{}"); ::core::format_args!("") }}"##,
+                            error_msg
+                        )
+                        .parse::<TokenStream>()
+                        .unwrap();
+                    }
+                }
                 let name = name.split_once(':').map_or(name, |(name, _)| name);
                 if !name.is_empty() && !name.chars().all(|c| c.is_ascii_digit()) {
                     names.insert(name);
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2025-12-23 15:48 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-12-23  3:30 [PATCH] rust: fmt: reject {:p} format specifier for raw pointers Ke Sun
2025-12-23  6:41 ` Miguel Ojeda
2025-12-23  8:03   ` Benno Lossin
2025-12-23  9:12     ` Ke Sun
2025-12-23 15:48     ` Timur Tabi
2025-12-23 11:27   ` Danilo Krummrich

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox