From 885c5d83d7eb778a796d4a17380a0898b0d0a571 Mon Sep 17 00:00:00 2001 From: Gary Guo Date: Mon, 19 Jan 2026 17:36:08 +0000 Subject: [PATCH] build: simplify use of nightly features We use some features that are already stable in later versions of Rust, but only available as unstable features in older Rust versions that the kernel needs to support. Instead of checking if a feature is already stable, simply enable them and allow the warning if the feature is already stable. This avoids the need of hardcoding whether a feature has been stabilized at a given version. `#[feature(...)]` is used when cfg `USE_RUSTC_FEATURES` is enabled. The build script automatically does this when a nightly compiler is detected or `RUSTC_BOOTSTRAP` is set. Signed-off-by: Gary Guo --- Cargo.toml | 1 + build.rs | 24 ++++++++++++----------- examples/big_struct_in_place.rs | 2 +- examples/linked_list.rs | 2 +- examples/mutex.rs | 2 +- examples/pthread_mutex.rs | 2 +- examples/static_init.rs | 2 +- internal/Cargo.toml | 1 + internal/build.rs | 14 +++++++------ internal/src/lib.rs | 2 +- src/lib.rs | 7 ++----- tests/alloc_fail.rs | 2 +- tests/cfgs.rs | 2 +- tests/const-generic-default.rs | 2 +- tests/default_error.rs | 2 +- tests/init-scope.rs | 2 +- tests/many_generics.rs | 2 +- tests/ring_buf.rs | 2 +- tests/ui.rs | 2 +- tests/ui/expand/many_generics.expanded.rs | 1 - tests/underscore.rs | 2 +- tests/zeroing.rs | 2 +- 22 files changed, 41 insertions(+), 39 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index cee2a4d1..e189b6d6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -37,6 +37,7 @@ macrotest = "1.0" prettyplease = { version = "0.2.37", features = ["verbatim"] } [lints.rust] +stable_features = "allow" non_ascii_idents = "deny" unexpected_cfgs = { level = "warn", check-cfg = [ 'cfg(UI_TESTS)', diff --git a/build.rs b/build.rs index fddbb5dc..6465fcc0 100644 --- a/build.rs +++ b/build.rs @@ -1,18 +1,20 @@ -use rustc_version::{version, Version}; +use rustc_version::{version_meta, Channel, Version}; fn main() { - println!("cargo::rustc-check-cfg=cfg(RUSTC_LINT_REASONS_IS_STABLE)"); - println!("cargo::rustc-check-cfg=cfg(RUSTC_NEW_UNINIT_IS_STABLE)"); + println!("cargo::rustc-check-cfg=cfg(USE_RUSTC_FEATURES)"); println!("cargo::rustc-check-cfg=cfg(CONFIG_RUSTC_HAS_UNSAFE_PINNED)"); - if version().unwrap() >= Version::parse("1.81.0").unwrap() - || version().unwrap() >= Version::parse("1.81.0-nightly").unwrap() - { - println!("cargo:rustc-cfg=RUSTC_LINT_REASONS_IS_STABLE"); - } - if version().unwrap() >= Version::parse("1.82.0").unwrap() { - println!("cargo:rustc-cfg=RUSTC_NEW_UNINIT_IS_STABLE"); + + let meta = version_meta().unwrap(); + + let use_feature = meta.channel == Channel::Nightly || std::env::var("RUSTC_BOOTSTRAP").is_ok(); + if use_feature { + // Use this cfg option to control whether we should enable features that are already stable + // in some new Rust versions, but are available as unstable features in older Rust versions + // that needs to be supported by the Linux kernel. + println!("cargo:rustc-cfg=USE_RUSTC_FEATURES"); } - if version().unwrap() >= Version::parse("1.89.0-nightly").unwrap() { + + if meta.semver >= Version::parse("1.89.0-nightly").unwrap() && use_feature { println!("cargo:rustc-cfg=CONFIG_RUSTC_HAS_UNSAFE_PINNED"); } } diff --git a/examples/big_struct_in_place.rs b/examples/big_struct_in_place.rs index 0792d927..de8612a5 100644 --- a/examples/big_struct_in_place.rs +++ b/examples/big_struct_in_place.rs @@ -1,6 +1,6 @@ // SPDX-License-Identifier: Apache-2.0 OR MIT -#![cfg_attr(not(RUSTC_LINT_REASONS_IS_STABLE), feature(lint_reasons))] +#![cfg_attr(USE_RUSTC_FEATURES, feature(lint_reasons))] use pin_init::*; diff --git a/examples/linked_list.rs b/examples/linked_list.rs index 8445a589..226e33e4 100644 --- a/examples/linked_list.rs +++ b/examples/linked_list.rs @@ -2,7 +2,7 @@ #![allow(clippy::undocumented_unsafe_blocks)] #![cfg_attr(feature = "alloc", feature(allocator_api))] -#![cfg_attr(not(RUSTC_LINT_REASONS_IS_STABLE), feature(lint_reasons))] +#![cfg_attr(USE_RUSTC_FEATURES, feature(lint_reasons))] use core::{ cell::Cell, diff --git a/examples/mutex.rs b/examples/mutex.rs index 9f295226..e534c367 100644 --- a/examples/mutex.rs +++ b/examples/mutex.rs @@ -2,7 +2,7 @@ #![allow(clippy::undocumented_unsafe_blocks)] #![cfg_attr(feature = "alloc", feature(allocator_api))] -#![cfg_attr(not(RUSTC_LINT_REASONS_IS_STABLE), feature(lint_reasons))] +#![cfg_attr(USE_RUSTC_FEATURES, feature(lint_reasons))] #![allow(clippy::missing_safety_doc)] use core::{ diff --git a/examples/pthread_mutex.rs b/examples/pthread_mutex.rs index 4e082ec7..562ca5d3 100644 --- a/examples/pthread_mutex.rs +++ b/examples/pthread_mutex.rs @@ -3,7 +3,7 @@ // inspired by #![allow(clippy::undocumented_unsafe_blocks)] #![cfg_attr(feature = "alloc", feature(allocator_api))] -#![cfg_attr(not(RUSTC_LINT_REASONS_IS_STABLE), feature(lint_reasons))] +#![cfg_attr(USE_RUSTC_FEATURES, feature(lint_reasons))] #[cfg(not(windows))] mod pthread_mtx { diff --git a/examples/static_init.rs b/examples/static_init.rs index 0e165daa..df562134 100644 --- a/examples/static_init.rs +++ b/examples/static_init.rs @@ -2,7 +2,7 @@ #![allow(clippy::undocumented_unsafe_blocks)] #![cfg_attr(feature = "alloc", feature(allocator_api))] -#![cfg_attr(not(RUSTC_LINT_REASONS_IS_STABLE), feature(lint_reasons))] +#![cfg_attr(USE_RUSTC_FEATURES, feature(lint_reasons))] #![allow(unused_imports)] use core::{ diff --git a/internal/Cargo.toml b/internal/Cargo.toml index d3af5351..c8810106 100644 --- a/internal/Cargo.toml +++ b/internal/Cargo.toml @@ -21,4 +21,5 @@ syn = { version = "2.0.86", features = ["full", "parsing", "visit-mut"] } rustc_version = "0.4" [lints.rust] +stable_features = "allow" unexpected_cfgs = { level = "warn", check-cfg = ['cfg(kernel)'] } diff --git a/internal/build.rs b/internal/build.rs index 2fdb56a9..b8faf9f6 100644 --- a/internal/build.rs +++ b/internal/build.rs @@ -1,10 +1,12 @@ -use rustc_version::{version, Version}; +use rustc_version::{version_meta, Channel}; fn main() { - println!("cargo::rustc-check-cfg=cfg(RUSTC_LINT_REASONS_IS_STABLE)"); - if version().unwrap() >= Version::parse("1.81.0").unwrap() - || version().unwrap() >= Version::parse("1.81.0-nightly").unwrap() - { - println!("cargo:rustc-cfg=RUSTC_LINT_REASONS_IS_STABLE"); + println!("cargo::rustc-check-cfg=cfg(USE_RUSTC_FEATURES)"); + + let meta = version_meta().unwrap(); + + let use_feature = meta.channel == Channel::Nightly || std::env::var("RUSTC_BOOTSTRAP").is_ok(); + if use_feature { + println!("cargo:rustc-cfg=USE_RUSTC_FEATURES"); } } diff --git a/internal/src/lib.rs b/internal/src/lib.rs index 08372c8f..b08dfe00 100644 --- a/internal/src/lib.rs +++ b/internal/src/lib.rs @@ -6,7 +6,7 @@ //! `pin-init` proc macros. -#![cfg_attr(not(RUSTC_LINT_REASONS_IS_STABLE), feature(lint_reasons))] +#![cfg_attr(USE_RUSTC_FEATURES, feature(lint_reasons))] // Documentation is done in the pin-init crate instead. #![allow(missing_docs)] diff --git a/src/lib.rs b/src/lib.rs index 73177147..a513930e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -264,12 +264,9 @@ //! [`impl Init`]: crate::Init //! [Rust-for-Linux]: https://rust-for-linux.com/ -#![cfg_attr(not(RUSTC_LINT_REASONS_IS_STABLE), feature(lint_reasons))] +#![cfg_attr(USE_RUSTC_FEATURES, feature(lint_reasons))] #![cfg_attr( - all( - any(feature = "alloc", feature = "std"), - not(RUSTC_NEW_UNINIT_IS_STABLE) - ), + all(any(feature = "alloc", feature = "std"), USE_RUSTC_FEATURES), feature(new_uninit) )] #![forbid(missing_docs, unsafe_op_in_unsafe_fn)] diff --git a/tests/alloc_fail.rs b/tests/alloc_fail.rs index bc7937bf..fea9f058 100644 --- a/tests/alloc_fail.rs +++ b/tests/alloc_fail.rs @@ -1,5 +1,5 @@ #![cfg_attr(feature = "alloc", feature(allocator_api))] -#![cfg_attr(not(RUSTC_LINT_REASONS_IS_STABLE), feature(lint_reasons))] +#![cfg_attr(USE_RUSTC_FEATURES, feature(lint_reasons))] #[test] #[cfg(feature = "alloc")] diff --git a/tests/cfgs.rs b/tests/cfgs.rs index a28d0db4..6e8fcf7e 100644 --- a/tests/cfgs.rs +++ b/tests/cfgs.rs @@ -1,4 +1,4 @@ -#![cfg_attr(not(RUSTC_LINT_REASONS_IS_STABLE), feature(lint_reasons))] +#![cfg_attr(USE_RUSTC_FEATURES, feature(lint_reasons))] use pin_init::{pin_data, pin_init, PinInit}; diff --git a/tests/const-generic-default.rs b/tests/const-generic-default.rs index 7660272c..c30045d5 100644 --- a/tests/const-generic-default.rs +++ b/tests/const-generic-default.rs @@ -1,4 +1,4 @@ -#![cfg_attr(not(RUSTC_LINT_REASONS_IS_STABLE), feature(lint_reasons))] +#![cfg_attr(USE_RUSTC_FEATURES, feature(lint_reasons))] use pin_init::*; diff --git a/tests/default_error.rs b/tests/default_error.rs index c2ca60d2..a0e3d795 100644 --- a/tests/default_error.rs +++ b/tests/default_error.rs @@ -1,5 +1,5 @@ #![allow(dead_code)] -#![cfg_attr(not(RUSTC_LINT_REASONS_IS_STABLE), feature(lint_reasons))] +#![cfg_attr(USE_RUSTC_FEATURES, feature(lint_reasons))] use pin_init::{init, Init}; diff --git a/tests/init-scope.rs b/tests/init-scope.rs index 2ea700ab..85b4b793 100644 --- a/tests/init-scope.rs +++ b/tests/init-scope.rs @@ -1,5 +1,5 @@ #![allow(dead_code)] -#![cfg_attr(not(RUSTC_LINT_REASONS_IS_STABLE), feature(lint_reasons))] +#![cfg_attr(USE_RUSTC_FEATURES, feature(lint_reasons))] use pin_init::*; diff --git a/tests/many_generics.rs b/tests/many_generics.rs index d45ac287..fdc8eb28 100644 --- a/tests/many_generics.rs +++ b/tests/many_generics.rs @@ -1,4 +1,4 @@ -#![cfg_attr(not(RUSTC_LINT_REASONS_IS_STABLE), feature(lint_reasons))] +#![cfg_attr(USE_RUSTC_FEATURES, feature(lint_reasons))] #![allow(dead_code)] use core::{marker::PhantomPinned, pin::Pin}; diff --git a/tests/ring_buf.rs b/tests/ring_buf.rs index 9029f511..36d9cc3d 100644 --- a/tests/ring_buf.rs +++ b/tests/ring_buf.rs @@ -1,5 +1,5 @@ #![allow(clippy::undocumented_unsafe_blocks)] -#![cfg_attr(not(RUSTC_LINT_REASONS_IS_STABLE), feature(lint_reasons))] +#![cfg_attr(USE_RUSTC_FEATURES, feature(lint_reasons))] #![cfg_attr(feature = "alloc", feature(allocator_api))] #[cfg(all(not(feature = "std"), feature = "alloc"))] diff --git a/tests/ui.rs b/tests/ui.rs index 4eb03f85..edaa720a 100644 --- a/tests/ui.rs +++ b/tests/ui.rs @@ -1,4 +1,4 @@ -#![cfg_attr(not(RUSTC_LINT_REASONS_IS_STABLE), feature(lint_reasons))] +#![cfg_attr(USE_RUSTC_FEATURES, feature(lint_reasons))] #[test] #[cfg_attr(not(UI_TESTS), ignore)] diff --git a/tests/ui/expand/many_generics.expanded.rs b/tests/ui/expand/many_generics.expanded.rs index 44a4efa1..35f99102 100644 --- a/tests/ui/expand/many_generics.expanded.rs +++ b/tests/ui/expand/many_generics.expanded.rs @@ -1,4 +1,3 @@ -#![feature(lint_reasons)] #![allow(dead_code)] use core::{marker::PhantomPinned, pin::Pin}; use pin_init::*; diff --git a/tests/underscore.rs b/tests/underscore.rs index bdf7a9d0..626b4629 100644 --- a/tests/underscore.rs +++ b/tests/underscore.rs @@ -1,4 +1,4 @@ -#![cfg_attr(not(RUSTC_LINT_REASONS_IS_STABLE), feature(lint_reasons))] +#![cfg_attr(USE_RUSTC_FEATURES, feature(lint_reasons))] use pin_init::{init, Init}; diff --git a/tests/zeroing.rs b/tests/zeroing.rs index 6d10ad77..ead495d8 100644 --- a/tests/zeroing.rs +++ b/tests/zeroing.rs @@ -1,4 +1,4 @@ -#![cfg_attr(not(RUSTC_LINT_REASONS_IS_STABLE), feature(lint_reasons))] +#![cfg_attr(USE_RUSTC_FEATURES, feature(lint_reasons))] use core::{marker::PhantomPinned, ptr::addr_of_mut};