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 1/2] rust: make unsafe_precondition_assert! const compatible
Date: Tue, 8 Sep 2026 16:06:02 +0800 [thread overview]
Message-ID: <tencent_DD1EF416B64C6088DB85E0AE3F9F0FCCEB07@qq.com> (raw)
In-Reply-To: <20260908080604.34070-1-1239989762@qq.com>
The no-message form of unsafe_precondition_assert! routes the
stringified condition through the formatting machinery. This prevents
the macro from being used in const unsafe functions.
Build its static diagnostic with concat! instead. This preserves the
message while allowing the no-message form in const contexts. 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 | 10 +++++++++-
1 file changed, 9 insertions(+), 1 deletion(-)
diff --git a/rust/kernel/safety.rs b/rust/kernel/safety.rs
index c1c6bd0fa2..a45a574709 100644
--- a/rust/kernel/safety.rs
+++ b/rust/kernel/safety.rs
@@ -7,6 +7,8 @@
/// 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.
+///
/// # Examples
///
/// ```no_run
@@ -40,7 +42,13 @@
#[macro_export]
macro_rules! unsafe_precondition_assert {
($cond:expr $(,)?) => {
- $crate::unsafe_precondition_assert!(@inner $cond, ::core::stringify!($cond))
+ ::core::debug_assert!(
+ $cond,
+ ::core::concat!(
+ "unsafe precondition violated: ",
+ ::core::stringify!($cond),
+ ),
+ )
};
($cond:expr, $($arg:tt)+) => {
--
2.54.0
next parent reply other threads:[~2026-09-08 8:06 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 ` JX [this message]
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 ` [PATCH v2 1/2] rust: make unsafe_precondition_assert! const compatible JX
2026-09-08 19:59 ` 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_DD1EF416B64C6088DB85E0AE3F9F0FCCEB07@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®