Skip to content

Commit 614724e

Browse files
committed
rust: kernel: move build_error hidden function to prevent mistakes
Users were using the hidden exported `kernel::build_error` function instead of the intended `kernel::build_error!` macro, e.g. see the previous commit. To force to use the macro, move it into the `build_assert` module, thus making it a compilation error and avoiding a collision in the same "namespace". Using the function now would require typing the module name (which is hidden), not just a single character. Now attempting to use the function will trigger this error with the right suggestion by the compiler: error[E0423]: expected function, found macro `kernel::build_error` --> samples/rust/rust_minimal.rs:29:9 | 29 | kernel::build_error(); | ^^^^^^^^^^^^^^^^^^^ not a function | help: use `!` to invoke the macro | 29 | kernel::build_error!(); | + An alternative would be using an alias, but it would be more complex and moving it into the module seems right since it belongs there and reduces the amount of code at the crate root. Keep the `#[doc(hidden)]` inside `build_assert` in case the module is not hidden in the future. Reviewed-by: Alice Ryhl <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Miguel Ojeda <[email protected]>
1 parent 15f2f93 commit 614724e

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed

rust/kernel/build_assert.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
//! Build-time assert.
44
5+
#[doc(hidden)]
6+
pub use build_error::build_error;
7+
58
/// Fails the build if the code path calling `build_error!` can possibly be executed.
69
///
710
/// If the macro is executed in const context, `build_error!` will panic.
@@ -23,10 +26,10 @@
2326
#[macro_export]
2427
macro_rules! build_error {
2528
() => {{
26-
$crate::build_error("")
29+
$crate::build_assert::build_error("")
2730
}};
2831
($msg:expr) => {{
29-
$crate::build_error($msg)
32+
$crate::build_assert::build_error($msg)
3033
}};
3134
}
3235

@@ -73,12 +76,12 @@ macro_rules! build_error {
7376
macro_rules! build_assert {
7477
($cond:expr $(,)?) => {{
7578
if !$cond {
76-
$crate::build_error(concat!("assertion failed: ", stringify!($cond)));
79+
$crate::build_assert::build_error(concat!("assertion failed: ", stringify!($cond)));
7780
}
7881
}};
7982
($cond:expr, $msg:expr) => {{
8083
if !$cond {
81-
$crate::build_error($msg);
84+
$crate::build_assert::build_error($msg);
8285
}
8386
}};
8487
}

rust/kernel/lib.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ pub use ffi;
3232
pub mod alloc;
3333
#[cfg(CONFIG_BLOCK)]
3434
pub mod block;
35-
mod build_assert;
35+
#[doc(hidden)]
36+
pub mod build_assert;
3637
pub mod cred;
3738
pub mod device;
3839
pub mod error;
@@ -74,9 +75,6 @@ pub use bindings;
7475
pub use macros;
7576
pub use uapi;
7677

77-
#[doc(hidden)]
78-
pub use build_error::build_error;
79-
8078
/// Prefix to appear before log messages printed from within the `kernel` crate.
8179
const __LOG_PREFIX: &[u8] = b"rust_kernel\0";
8280

0 commit comments

Comments
 (0)