* [PATCH v2] rust: update `kernel::c_str!` documentation
@ 2026-03-09 17:01 Tamir Duberstein
2026-03-10 17:57 ` Gary Guo
` (2 more replies)
0 siblings, 3 replies; 5+ messages in thread
From: Tamir Duberstein @ 2026-03-09 17:01 UTC (permalink / raw)
To: Miguel Ojeda, Boqun Feng, Gary Guo, Björn Roy Baron,
Benno Lossin, Andreas Hindborg, Alice Ryhl, Trevor Gross,
Danilo Krummrich, Breno Leitao, David Airlie, Simona Vetter,
Brendan Higgins, David Gow, Rae Moar, Peter Zijlstra,
Ingo Molnar, Will Deacon, Waiman Long
Cc: rust-for-linux, linux-kernel, dri-devel, linux-kselftest,
kunit-dev, Greg Kroah-Hartman, Tamir Duberstein
Now that all literals are C-Strings, update the documentation to explain
that use of this macro should be limited to non-literal strings.
Link: https://github.com/Rust-for-Linux/linux/issues/1075
Acked-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Reviewed-by: Alice Ryhl <aliceryhl@google.com>
Signed-off-by: Tamir Duberstein <tamird@kernel.org>
---
This patch completes the work of replacing our custom `CStr` with
upstream's.
---
Changes in v2:
- Drop rename, keep only documentation update. (Gary Guo)
- Add example of misuse to documentation. (Gary Guo)
- Link to v1: https://patch.msgid.link/20260302-cstr-rename-macro-v1-1-a269fe4dc3f0@kernel.org
---
rust/kernel/str.rs | 19 ++++++++++++++++---
1 file changed, 16 insertions(+), 3 deletions(-)
diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs
index fa87779d2253..6dae82e7d875 100644
--- a/rust/kernel/str.rs
+++ b/rust/kernel/str.rs
@@ -376,19 +376,32 @@ fn as_ref(&self) -> &BStr {
}
}
-/// Creates a new [`CStr`] from a string literal.
+/// Creates a new [`CStr`] at compile time.
///
-/// The string literal should not contain any `NUL` bytes.
+/// Rust supports C string literals since Rust 1.77, and they should be used instead of this macro
+/// where possible. This macro exists to allow static *non-literal* C strings to be created at
+/// compile time. This is most often used in other macros.
+///
+/// # Panics
+///
+/// This macro panics if the operand contains an interior `NUL` byte.
///
/// # Examples
///
/// ```
/// # use kernel::c_str;
/// # use kernel::str::CStr;
-/// const MY_CSTR: &CStr = c_str!("My awesome CStr!");
+/// // This is allowed, but `c"literal"` should be preferred for literals.
+/// const BAD: &CStr = c_str!("literal");
+///
+/// // `c_str!` is still needed for static non-literal C strings.
+/// const GOOD: &CStr = c_str!(concat!(file!(), ":", line!(), ": My CStr!"));
/// ```
#[macro_export]
macro_rules! c_str {
+ // NB: we could write `($str:lit) => compile_error!("use a C string literal instead");` here but
+ // that would trigger when the literal is at the top of several macro expansions. That would be
+ // too limiting to macro authors.
($str:expr) => {{
const S: &str = concat!($str, "\0");
const C: &$crate::str::CStr = match $crate::str::CStr::from_bytes_with_nul(S.as_bytes()) {
---
base-commit: 11439c4635edd669ae435eec308f4ab8a0804808
change-id: 20260302-cstr-rename-macro-64201be6c969
Best regards,
--
Tamir Duberstein <tamird@kernel.org>
^ permalink raw reply [flat|nested] 5+ messages in thread* Re: [PATCH v2] rust: update `kernel::c_str!` documentation
2026-03-09 17:01 [PATCH v2] rust: update `kernel::c_str!` documentation Tamir Duberstein
@ 2026-03-10 17:57 ` Gary Guo
2026-03-12 13:24 ` Tamir Duberstein
2026-03-10 22:39 ` Gary Guo
2026-03-15 21:23 ` Miguel Ojeda
2 siblings, 1 reply; 5+ messages in thread
From: Gary Guo @ 2026-03-10 17:57 UTC (permalink / raw)
To: Tamir Duberstein, Miguel Ojeda, Boqun Feng, Gary Guo,
Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl,
Trevor Gross, Danilo Krummrich, Breno Leitao, David Airlie,
Simona Vetter, Brendan Higgins, David Gow, Rae Moar,
Peter Zijlstra, Ingo Molnar, Will Deacon, Waiman Long
Cc: rust-for-linux, linux-kernel, dri-devel, linux-kselftest,
kunit-dev, Greg Kroah-Hartman
On Mon Mar 9, 2026 at 5:01 PM GMT, Tamir Duberstein wrote:
> Now that all literals are C-Strings, update the documentation to explain
> that use of this macro should be limited to non-literal strings.
>
> Link: https://github.com/Rust-for-Linux/linux/issues/1075
> Acked-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
> Reviewed-by: Alice Ryhl <aliceryhl@google.com>
> Signed-off-by: Tamir Duberstein <tamird@kernel.org>
> ---
> This patch completes the work of replacing our custom `CStr` with
> upstream's.
> ---
> Changes in v2:
> - Drop rename, keep only documentation update. (Gary Guo)
> - Add example of misuse to documentation. (Gary Guo)
> - Link to v1: https://patch.msgid.link/20260302-cstr-rename-macro-v1-1-a269fe4dc3f0@kernel.org
The lint for `c_str!("literal")` now lands in klint:
https://github.com/Rust-for-Linux/klint/commit/69fa3560e28031faa8c5a93e1ac95cc2b3689306
Best,
Gary
^ permalink raw reply [flat|nested] 5+ messages in thread* Re: [PATCH v2] rust: update `kernel::c_str!` documentation
2026-03-10 17:57 ` Gary Guo
@ 2026-03-12 13:24 ` Tamir Duberstein
0 siblings, 0 replies; 5+ messages in thread
From: Tamir Duberstein @ 2026-03-12 13:24 UTC (permalink / raw)
To: Gary Guo
Cc: Miguel Ojeda, Boqun Feng, Björn Roy Baron, Benno Lossin,
Andreas Hindborg, Alice Ryhl, Trevor Gross, Danilo Krummrich,
Breno Leitao, David Airlie, Simona Vetter, Brendan Higgins,
David Gow, Rae Moar, Peter Zijlstra, Ingo Molnar, Will Deacon,
Waiman Long, rust-for-linux, linux-kernel, dri-devel,
linux-kselftest, kunit-dev, Greg Kroah-Hartman
On Tue, Mar 10, 2026 at 1:57 PM Gary Guo <gary@garyguo.net> wrote:
>
> On Mon Mar 9, 2026 at 5:01 PM GMT, Tamir Duberstein wrote:
> > Now that all literals are C-Strings, update the documentation to explain
> > that use of this macro should be limited to non-literal strings.
> >
> > Link: https://github.com/Rust-for-Linux/linux/issues/1075
> > Acked-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
> > Reviewed-by: Alice Ryhl <aliceryhl@google.com>
> > Signed-off-by: Tamir Duberstein <tamird@kernel.org>
> > ---
> > This patch completes the work of replacing our custom `CStr` with
> > upstream's.
> > ---
> > Changes in v2:
> > - Drop rename, keep only documentation update. (Gary Guo)
> > - Add example of misuse to documentation. (Gary Guo)
> > - Link to v1: https://patch.msgid.link/20260302-cstr-rename-macro-v1-1-a269fe4dc3f0@kernel.org
>
> The lint for `c_str!("literal")` now lands in klint:
>
> https://github.com/Rust-for-Linux/klint/commit/69fa3560e28031faa8c5a93e1ac95cc2b3689306
>
> Best,
> Gary
Very cool, thanks!
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH v2] rust: update `kernel::c_str!` documentation
2026-03-09 17:01 [PATCH v2] rust: update `kernel::c_str!` documentation Tamir Duberstein
2026-03-10 17:57 ` Gary Guo
@ 2026-03-10 22:39 ` Gary Guo
2026-03-15 21:23 ` Miguel Ojeda
2 siblings, 0 replies; 5+ messages in thread
From: Gary Guo @ 2026-03-10 22:39 UTC (permalink / raw)
To: Tamir Duberstein, Miguel Ojeda, Boqun Feng, Gary Guo,
Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl,
Trevor Gross, Danilo Krummrich, Breno Leitao, David Airlie,
Simona Vetter, Brendan Higgins, David Gow, Rae Moar,
Peter Zijlstra, Ingo Molnar, Will Deacon, Waiman Long
Cc: rust-for-linux, linux-kernel, dri-devel, linux-kselftest,
kunit-dev, Greg Kroah-Hartman
On Mon Mar 9, 2026 at 5:01 PM GMT, Tamir Duberstein wrote:
> Now that all literals are C-Strings, update the documentation to explain
> that use of this macro should be limited to non-literal strings.
>
> Link: https://github.com/Rust-for-Linux/linux/issues/1075
> Acked-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
> Reviewed-by: Alice Ryhl <aliceryhl@google.com>
> Signed-off-by: Tamir Duberstein <tamird@kernel.org>
Reviewed-by: Gary Guo <gary@garyguo.net>
> ---
> This patch completes the work of replacing our custom `CStr` with
> upstream's.
> ---
> Changes in v2:
> - Drop rename, keep only documentation update. (Gary Guo)
> - Add example of misuse to documentation. (Gary Guo)
> - Link to v1: https://patch.msgid.link/20260302-cstr-rename-macro-v1-1-a269fe4dc3f0@kernel.org
> ---
> rust/kernel/str.rs | 19 ++++++++++++++++---
> 1 file changed, 16 insertions(+), 3 deletions(-)
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH v2] rust: update `kernel::c_str!` documentation
2026-03-09 17:01 [PATCH v2] rust: update `kernel::c_str!` documentation Tamir Duberstein
2026-03-10 17:57 ` Gary Guo
2026-03-10 22:39 ` Gary Guo
@ 2026-03-15 21:23 ` Miguel Ojeda
2 siblings, 0 replies; 5+ messages in thread
From: Miguel Ojeda @ 2026-03-15 21:23 UTC (permalink / raw)
To: Tamir Duberstein
Cc: Miguel Ojeda, Boqun Feng, Gary Guo, Björn Roy Baron,
Benno Lossin, Andreas Hindborg, Alice Ryhl, Trevor Gross,
Danilo Krummrich, Breno Leitao, David Airlie, Simona Vetter,
Brendan Higgins, David Gow, Rae Moar, Peter Zijlstra,
Ingo Molnar, Will Deacon, Waiman Long, rust-for-linux,
linux-kernel, dri-devel, linux-kselftest, kunit-dev,
Greg Kroah-Hartman
On Mon, Mar 9, 2026 at 6:02 PM Tamir Duberstein <tamird@kernel.org> wrote:
>
> Now that all literals are C-Strings, update the documentation to explain
> that use of this macro should be limited to non-literal strings.
>
> Link: https://github.com/Rust-for-Linux/linux/issues/1075
> Acked-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
> Reviewed-by: Alice Ryhl <aliceryhl@google.com>
> Signed-off-by: Tamir Duberstein <tamird@kernel.org>
Applied to `rust-next` -- thanks everyone!
[ Apply sentence case to comment. Reword title. - Miguel ]
Cheers,
Miguel
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-03-15 21:23 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-03-09 17:01 [PATCH v2] rust: update `kernel::c_str!` documentation Tamir Duberstein
2026-03-10 17:57 ` Gary Guo
2026-03-12 13:24 ` Tamir Duberstein
2026-03-10 22:39 ` Gary Guo
2026-03-15 21:23 ` Miguel Ojeda
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®