From 7956142f9288cb3117b2b7a6924f9106fe57dace Mon Sep 17 00:00:00 2001 From: Urgau Date: Thu, 7 Nov 2024 00:28:43 +0100 Subject: [PATCH] Add compile-time tests against unexpected target features cfgs --- .../crates/std_detect/src/detect/macros.rs | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/library/stdarch/crates/std_detect/src/detect/macros.rs b/library/stdarch/crates/std_detect/src/detect/macros.rs index f2cc711812a0..38c8b8068fbf 100644 --- a/library/stdarch/crates/std_detect/src/detect/macros.rs +++ b/library/stdarch/crates/std_detect/src/detect/macros.rs @@ -14,6 +14,21 @@ macro_rules! detect_feature { }; } +#[allow(unused_macros, reason = "it's used in the features! macro below")] +macro_rules! check_cfg_feature { + ($feature:tt, $feature_lit:tt) => { + check_cfg_feature!($feature, $feature_lit : $feature_lit) + }; + ($feature:tt, $feature_lit:tt : $($target_feature_lit:tt),*) => { + $(cfg!(target_feature = $target_feature_lit);)* + }; + ($feature:tt, $feature_lit:tt, without cfg check: $feature_cfg_check:literal) => { + // FIXME: Enable once rust-lang/rust#132577 hit's nightly + // #[expect(unexpected_cfgs, reason = $feature_lit)] + // { cfg!(target_feature = $feature_lit) } + }; +} + #[allow(unused)] macro_rules! features { ( @@ -117,6 +132,15 @@ macro_rules! features { }; } + #[test] + #[deny(unexpected_cfgs)] + #[deny(unfulfilled_lint_expectations)] + fn unexpected_cfgs() { + $( + check_cfg_feature!($feature, $feature_lit $(, without cfg check: $feature_cfg_check)? $(: $($target_feature_lit),*)?); + )* + } + /// Each variant denotes a position in a bitset for a particular feature. /// /// PLEASE: do not use this, it is an implementation detail subject