Skip to content

Commit ac26440

Browse files
ojedaSasha Levin
authored and
Sasha Levin
committed
kbuild: rust: force alloc extern to allow "empty" Rust files
commit ded103c upstream. If one attempts to build an essentially empty file somewhere in the kernel tree, it leads to a build error because the compiler does not recognize the `new_uninit` unstable feature: error[E0635]: unknown feature `new_uninit` --> <crate attribute>:1:9 | 1 | feature(new_uninit) | ^^^^^^^^^^ The reason is that we pass `-Zcrate-attr='feature(new_uninit)'` (together with `-Zallow-features=new_uninit`) to let non-`rust/` code use that unstable feature. However, the compiler only recognizes the feature if the `alloc` crate is resolved (the feature is an `alloc` one). `--extern alloc`, which we pass, is not enough to resolve the crate. Introducing a reference like `use alloc;` or `extern crate alloc;` solves the issue, thus this is not seen in normal files. For instance, `use`ing the `kernel` prelude introduces such a reference, since `alloc` is used inside. While normal use of the build system is not impacted by this, it can still be fairly confusing for kernel developers [1], thus use the unstable `force` option of `--extern` [2] (added in Rust 1.71 [3]) to force the compiler to resolve `alloc`. This new unstable feature is only needed meanwhile we use the other unstable feature, since then we will not need `-Zcrate-attr`. Cc: [email protected] # v6.6+ Reported-by: Daniel Almeida <[email protected]> Reported-by: Julian Stecklina <[email protected]> Closes: https://rust-for-linux.zulipchat.com/#narrow/stream/288089-General/topic/x/near/424096982 [1] Fixes: 2f7ab12 ("Kbuild: add Rust support") Link: rust-lang/rust#111302 [2] Link: rust-lang/rust#109421 [3] Reviewed-by: Alice Ryhl <[email protected]> Reviewed-by: Gary Guo <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Miguel Ojeda <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent d719dd9 commit ac26440

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

scripts/Makefile.build

+1-1
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ rust_common_cmd = \
272272
-Zallow-features=$(rust_allowed_features) \
273273
-Zcrate-attr=no_std \
274274
-Zcrate-attr='feature($(rust_allowed_features))' \
275-
--extern alloc --extern kernel \
275+
-Zunstable-options --extern force:alloc --extern kernel \
276276
--crate-type rlib -L $(objtree)/rust/ \
277277
--crate-name $(basename $(notdir $@)) \
278278
--out-dir $(dir $@) --emit=dep-info=$(depfile)

0 commit comments

Comments
 (0)