Skip to content

Commit ae20d16

Browse files
committed
build: simplify use of nightly features
We use some features that are already stable in later version of Rust, but only available as unstable features in older Rust versions that the kernel needs to support. Instead of check if a feature is already stable, simply enable them and allow the warning if the feature is already stable. This avoids the need of the hardcoding whether a feature has been stabilized at a given version. `#[feature(...)]` is used when cfg `USE_RUSTC_FEATURES` is enabled, which will be enabled when nightly compiler is detected or `RUSTC_BOOTSTRAP` is detected. Signed-off-by: Gary Guo <gary@garyguo.net>
1 parent e7a5b50 commit ae20d16

File tree

19 files changed

+38
-36
lines changed

19 files changed

+38
-36
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ macrotest = "1.0"
3737
prettyplease = { version = "0.2.37", features = ["verbatim"] }
3838

3939
[lints.rust]
40+
stable_features = "allow"
4041
non_ascii_idents = "deny"
4142
unexpected_cfgs = { level = "warn", check-cfg = [
4243
'cfg(UI_TESTS)',

build.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
1-
use rustc_version::{version, Version};
1+
use rustc_version::{version_meta, Channel, Version};
22

33
fn main() {
4-
println!("cargo::rustc-check-cfg=cfg(RUSTC_LINT_REASONS_IS_STABLE)");
5-
println!("cargo::rustc-check-cfg=cfg(RUSTC_NEW_UNINIT_IS_STABLE)");
4+
println!("cargo::rustc-check-cfg=cfg(USE_RUSTC_FEATURES)");
65
println!("cargo::rustc-check-cfg=cfg(CONFIG_RUSTC_HAS_UNSAFE_PINNED)");
7-
if version().unwrap() >= Version::parse("1.81.0").unwrap()
8-
|| version().unwrap() >= Version::parse("1.81.0-nightly").unwrap()
9-
{
10-
println!("cargo:rustc-cfg=RUSTC_LINT_REASONS_IS_STABLE");
11-
}
12-
if version().unwrap() >= Version::parse("1.82.0").unwrap() {
13-
println!("cargo:rustc-cfg=RUSTC_NEW_UNINIT_IS_STABLE");
6+
7+
let meta = version_meta().unwrap();
8+
9+
let use_feature = meta.channel == Channel::Nightly || std::env::var("RUSTC_BOOTSTRAP").is_ok();
10+
if use_feature {
11+
// Use this cfg option to control whether we should enable features that are already stable
12+
// in some new Rust versions, but are available as unstable features in older Rust versions
13+
// that needs to be supported by the Linux kernel.
14+
println!("cargo:rustc-cfg=USE_RUSTC_FEATURES");
1415
}
15-
if version().unwrap() >= Version::parse("1.89.0-nightly").unwrap() {
16+
17+
if meta.semver >= Version::parse("1.89.0-nightly").unwrap() && use_feature {
1618
println!("cargo:rustc-cfg=CONFIG_RUSTC_HAS_UNSAFE_PINNED");
1719
}
1820
}

examples/linked_list.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
#![allow(clippy::undocumented_unsafe_blocks)]
44
#![cfg_attr(feature = "alloc", feature(allocator_api))]
5-
#![cfg_attr(not(RUSTC_LINT_REASONS_IS_STABLE), feature(lint_reasons))]
5+
#![cfg_attr(USE_RUSTC_FEATURES, feature(lint_reasons))]
66

77
use core::{
88
cell::Cell,

examples/mutex.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
#![allow(clippy::undocumented_unsafe_blocks)]
44
#![cfg_attr(feature = "alloc", feature(allocator_api))]
5-
#![cfg_attr(not(RUSTC_LINT_REASONS_IS_STABLE), feature(lint_reasons))]
5+
#![cfg_attr(USE_RUSTC_FEATURES, feature(lint_reasons))]
66
#![allow(clippy::missing_safety_doc)]
77

88
use core::{

examples/pthread_mutex.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// inspired by <https://github.com/nbdd0121/pin-init/blob/trunk/examples/pthread_mutex.rs>
44
#![allow(clippy::undocumented_unsafe_blocks)]
55
#![cfg_attr(feature = "alloc", feature(allocator_api))]
6-
#![cfg_attr(not(RUSTC_LINT_REASONS_IS_STABLE), feature(lint_reasons))]
6+
#![cfg_attr(USE_RUSTC_FEATURES, feature(lint_reasons))]
77

88
#[cfg(not(windows))]
99
mod pthread_mtx {

examples/static_init.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
#![allow(clippy::undocumented_unsafe_blocks)]
44
#![cfg_attr(feature = "alloc", feature(allocator_api))]
5-
#![cfg_attr(not(RUSTC_LINT_REASONS_IS_STABLE), feature(lint_reasons))]
5+
#![cfg_attr(USE_RUSTC_FEATURES, feature(lint_reasons))]
66
#![allow(unused_imports)]
77

88
use core::{

internal/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,5 @@ syn = { version = "2.0.86", features = ["full", "parsing", "visit-mut"] }
2121
rustc_version = "0.4"
2222

2323
[lints.rust]
24+
stable_features = "allow"
2425
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(kernel)'] }

internal/build.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
use rustc_version::{version, Version};
1+
use rustc_version::{version_meta, Channel};
22

33
fn main() {
4-
println!("cargo::rustc-check-cfg=cfg(RUSTC_LINT_REASONS_IS_STABLE)");
5-
if version().unwrap() >= Version::parse("1.81.0").unwrap()
6-
|| version().unwrap() >= Version::parse("1.81.0-nightly").unwrap()
7-
{
8-
println!("cargo:rustc-cfg=RUSTC_LINT_REASONS_IS_STABLE");
4+
println!("cargo::rustc-check-cfg=cfg(USE_RUSTC_FEATURES)");
5+
6+
let meta = version_meta().unwrap();
7+
8+
let use_feature = meta.channel == Channel::Nightly || std::env::var("RUSTC_BOOTSTRAP").is_ok();
9+
if use_feature {
10+
println!("cargo:rustc-cfg=USE_RUSTC_FEATURES");
911
}
1012
}

internal/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
//! `pin-init` proc macros.
88
9-
#![cfg_attr(not(RUSTC_LINT_REASONS_IS_STABLE), feature(lint_reasons))]
9+
#![cfg_attr(USE_RUSTC_FEATURES, feature(lint_reasons))]
1010
// Documentation is done in the pin-init crate instead.
1111
#![allow(missing_docs)]
1212

src/lib.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -264,12 +264,9 @@
264264
//! [`impl Init<T, E>`]: crate::Init
265265
//! [Rust-for-Linux]: https://rust-for-linux.com/
266266
267-
#![cfg_attr(not(RUSTC_LINT_REASONS_IS_STABLE), feature(lint_reasons))]
267+
#![cfg_attr(USE_RUSTC_FEATURES, feature(lint_reasons))]
268268
#![cfg_attr(
269-
all(
270-
any(feature = "alloc", feature = "std"),
271-
not(RUSTC_NEW_UNINIT_IS_STABLE)
272-
),
269+
all(any(feature = "alloc", feature = "std"), USE_RUSTC_FEATURES),
273270
feature(new_uninit)
274271
)]
275272
#![forbid(missing_docs, unsafe_op_in_unsafe_fn)]

0 commit comments

Comments
 (0)