From: JX <1239989762@qq.com>
To: Miguel Ojeda <ojeda@kernel.org>, Danilo Krummrich <dakr@kernel.org>
Cc: "Boqun Feng" <boqun@kernel.org>, "Gary Guo" <gary@garyguo.net>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Benno Lossin" <lossin@kernel.org>,
"Andreas Hindborg" <a.hindborg@kernel.org>,
"Alice Ryhl" <aliceryhl@google.com>,
"Trevor Gross" <tmgross@umich.edu>,
"Daniel Almeida" <daniel.almeida@collabora.com>,
"Tamir Duberstein" <tamird@kernel.org>,
"Alexandre Courbot" <acourbot@nvidia.com>,
"Onur Özkan" <work@onurozkan.dev>,
"Lorenzo Stoakes" <ljs@kernel.org>,
"Vlastimil Babka" <vbabka@kernel.org>,
"Liam R . Howlett" <liam@infradead.org>,
"Uladzislau Rezki" <urezki@gmail.com>,
rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH v2 1/2] rust: make unsafe_precondition_assert! const compatible
Date: Tue, 8 Sep 2026 17:26:23 +0800 [thread overview]
Message-ID: <tencent_DEBD9FED1CD8FC1625D8EBEFEEC3EE7A0405@qq.com> (raw)
In-Reply-To: <20260908092624.63350-1-1239989762@qq.com>
The no-message form of unsafe_precondition_assert! routes the
condition through the formatting machinery. This prevents the macro from
being used in const unsafe functions.
Use a fixed diagnostic literal instead. This allows the no-message form
in const contexts without interpreting tokens from the condition as
format string syntax. Keep the custom-message form unchanged since it
intentionally supports runtime formatting.
Suggested-by: Miguel Ojeda <ojeda@kernel.org>
Link: https://github.com/Rust-for-Linux/linux/issues/1232
Signed-off-by: JX <1239989762@qq.com>
---
rust/kernel/safety.rs | 11 ++++++++++-
1 file changed, 10 insertions(+), 1 deletion(-)
diff --git a/rust/kernel/safety.rs b/rust/kernel/safety.rs
index c1c6bd0fa2..f3c498c242 100644
--- a/rust/kernel/safety.rs
+++ b/rust/kernel/safety.rs
@@ -7,6 +7,15 @@
/// The check is enabled at runtime if debug assertions (`CONFIG_RUST_DEBUG_ASSERTIONS`)
/// are enabled. Otherwise, this macro is a no-op.
///
+/// The form without a custom message can be used in const contexts.
+///
+/// ```
+/// # use kernel::unsafe_precondition_assert;
+/// const fn check(value: usize) {
+/// unsafe_precondition_assert!({ value < 4 });
+/// }
+/// ```
+///
/// # Examples
///
/// ```no_run
@@ -40,7 +49,7 @@
#[macro_export]
macro_rules! unsafe_precondition_assert {
($cond:expr $(,)?) => {
- $crate::unsafe_precondition_assert!(@inner $cond, ::core::stringify!($cond))
+ ::core::debug_assert!($cond, "unsafe precondition violated")
};
($cond:expr, $($arg:tt)+) => {
--
2.54.0
next prev parent reply other threads:[~2026-09-08 9:26 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <20260908080604.34070-1-1239989762@qq.com>
2026-09-08 8:06 ` [PATCH " JX
2026-09-08 8:06 ` [PATCH 2/2] rust: alloc: use unsafe_precondition_assert! in Vec length helpers JX
2026-09-08 9:26 ` [PATCH v2 0/2] rust: add unsafe precondition assertions to " JX
[not found] ` <20260908092624.63350-1-1239989762@qq.com>
2026-09-08 9:26 ` JX [this message]
2026-09-08 19:59 ` [PATCH v2 1/2] rust: make unsafe_precondition_assert! const compatible Miguel Ojeda
2026-09-08 9:26 ` [PATCH v2 2/2] rust: alloc: use unsafe_precondition_assert! in Vec length helpers JX
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=tencent_DEBD9FED1CD8FC1625D8EBEFEEC3EE7A0405@qq.com \
--to=1239989762@qq.com \
--cc=a.hindborg@kernel.org \
--cc=acourbot@nvidia.com \
--cc=aliceryhl@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun@kernel.org \
--cc=dakr@kernel.org \
--cc=daniel.almeida@collabora.com \
--cc=gary@garyguo.net \
--cc=liam@infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=ljs@kernel.org \
--cc=lossin@kernel.org \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=tamird@kernel.org \
--cc=tmgross@umich.edu \
--cc=urezki@gmail.com \
--cc=vbabka@kernel.org \
--cc=work@onurozkan.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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®