Skip to contentSkip to conversation

kbuild: rust: force `alloc` extern to allow "empty" Rust files

PATCH In review Miguel Ojeda 4 participants 4 messages Apr 22, 2024 – Apr 25, 2024

Subsystems (3)
lore
Share & tools
lore

Miguel Ojeda

author maintainer
#0 lore raw

PATCH 1/1 kbuild: rust: force `alloc` extern to allow "empty" Rust files

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, useing 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.

---
 scripts/Makefile.build | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
scripts/Makefile.build +1 -1
index baf86c0880b6..533a7799fdfe 100644
@@ -273,7 +273,7 @@ rust_common_cmd = \
 	-Zallow-features=$(rust_allowed_features) \
 	-Zcrate-attr=no_std \
 	-Zcrate-attr='feature($(rust_allowed_features))' \
-	--extern alloc --extern kernel \
+	-Zunstable-options --extern force:alloc --extern kernel \
 	--crate-type rlib -L $(objtree)/rust/ \
 	--crate-name $(basename $(notdir $@)) \
 	--sysroot=/dev/null \

base-commit: 4cece764965020c22cff7665b18a012006359095

Signature · 1 line
2.44.0
reviewer Reviewed-by on v1 1/1
#1 lore raw
Quoted 32 lines · Miguel Ojeda

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, useing 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.

Quoted 8 lines
#2 lore raw
In reply to Miguel Ojeda
Quoted 32 lines · Miguel Ojeda

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, useing 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.

Quoted 8 lines

Applied to rust-fixes early to start getting some testing in -next. Please feel free to send tags for this one.

Cheers,
Miguel

reviewer Reviewed-by on v1 1/1
#3 lore raw
In reply to Miguel Ojeda
Quoted 39 lines · On Mon, 22 Apr 2024 11:06:44 +0200 Miguel Ojeda <[email protected]> wrote:

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, useing 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.

Quoted 1 line

---

Quoted 18 lines
 scripts/Makefile.build | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
scripts/Makefile.build +1 -1
index baf86c0880b6..533a7799fdfe 100644
@@ -273,7 +273,7 @@ rust_common_cmd = \
 	-Zallow-features=$(rust_allowed_features) \
 	-Zcrate-attr=no_std \
 	-Zcrate-attr='feature($(rust_allowed_features))' \
-	--extern alloc --extern kernel \
+	-Zunstable-options --extern force:alloc --extern kernel \
 	--crate-type rlib -L $(objtree)/rust/ \
 	--crate-name $(basename $(notdir $@)) \
 	--sysroot=/dev/null \

base-commit: 4cece764965020c22cff7665b18a012006359095