Skip to content

Commit 1b18b37

Browse files
Gnurouojeda
authored andcommitted
rust: build_assert: add instructions for use with function arguments
`build_assert` relies on the compiler to optimize out its error path, lest build fails with the dreaded error: ERROR: modpost: "rust_build_error" [path/to/module.ko] undefined! It has been observed that very trivial code performing I/O accesses (sometimes even using an immediate value) would seemingly randomly fail with this error whenever `CLIPPY=1` was set. The same behavior was also observed until different, very similar conditions [1][2]. The cause appears to be that the failing function is eventually using `build_assert` with its argument, but is only annotated with `#[inline]`. This gives the compiler freedom to not inline the function, which it notably did when Clippy was active, triggering the error. The fix is to annotate functions passing their argument to `build_assert` with `#[inline(always)]`, telling the compiler to be as aggressive as possible with their inlining. This is also the correct behavior as inlining is mandatory for correct behavior in these cases. Add a paragraph instructing to annotate such functions with `#[inline(always)]` in `build_assert`'s documentation, and split its example to illustrate. Reviewed-by: Daniel Almeida <daniel.almeida@collabora.com> Signed-off-by: Alexandre Courbot <acourbot@nvidia.com> Link: https://patch.msgid.link/20251208-io-build-assert-v3-1-98aded02c1ea@nvidia.com Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
1 parent 9fa7153 commit 1b18b37

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

rust/kernel/build_assert.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,13 @@ macro_rules! build_error {
6161
/// build_assert!(N > 1); // Build-time check
6262
/// assert!(N > 1); // Run-time check
6363
/// }
64+
/// ```
6465
///
65-
/// #[inline]
66+
/// When a condition depends on a function argument, the function must be annotated with
67+
/// `#[inline(always)]`. Without this attribute, the compiler may choose to not inline the
68+
/// function, preventing it from optimizing out the error path.
69+
/// ```
70+
/// #[inline(always)]
6671
/// fn bar(n: usize) {
6772
/// // `static_assert!(n > 1);` is not allowed
6873
/// build_assert!(n > 1); // Build-time check

0 commit comments

Comments
 (0)