Skip to content

Commit fffd285

Browse files
committed
Auto merge of #165011 - tgross35:std_detect-updates, r=<try>
std: Clean up and improve errors for `is_*_feature_detected` macros try-job: test-i686-gnu-1
2 parents 16d1ffa + d9777f8 commit fffd285

7 files changed

Lines changed: 344 additions & 47 deletions

File tree

‎library/std_detect/src/detect/arch/mod.rs‎

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,40 +22,40 @@ mod s390x;
2222

2323
cfg_select! {
2424
any(target_arch = "x86", target_arch = "x86_64") => {
25-
#[stable(feature = "simd_x86", since = "1.27.0")]
26-
pub use x86::*;
25+
pub use x86::__is_feature_detected;
26+
pub(crate) use x86::Feature;
2727
}
2828
target_arch = "arm" => {
29-
#[unstable(feature = "stdarch_arm_feature_detection", issue = "111190")]
30-
pub use arm::*;
29+
pub use arm::__is_feature_detected;
30+
pub(crate) use arm::Feature;
3131
}
3232
any(target_arch = "aarch64", target_arch = "arm64ec") => {
33-
#[stable(feature = "simd_aarch64", since = "1.60.0")]
34-
pub use aarch64::*;
33+
pub use aarch64::__is_feature_detected;
34+
pub(crate) use aarch64::Feature;
3535
}
3636
any(target_arch = "riscv32", target_arch = "riscv64") => {
37-
#[unstable(feature = "stdarch_riscv_feature_detection", issue = "111192")]
38-
pub use riscv::*;
37+
pub use riscv::__is_feature_detected;
38+
pub(crate) use riscv::Feature;
3939
}
4040
any(target_arch = "powerpc", target_arch = "powerpc64") => {
41-
#[unstable(feature = "stdarch_powerpc_feature_detection", issue = "111191")]
42-
pub use powerpc::*;
41+
pub use powerpc::__is_feature_detected;
42+
pub(crate) use powerpc::Feature;
4343
}
4444
target_arch = "mips" => {
45-
#[unstable(feature = "stdarch_mips_feature_detection", issue = "111188")]
46-
pub use mips::*;
45+
pub use mips::__is_feature_detected;
46+
pub(crate) use mips::Feature;
4747
}
4848
target_arch = "mips64" => {
49-
#[unstable(feature = "stdarch_mips_feature_detection", issue = "111188")]
50-
pub use mips64::*;
49+
pub use mips64::__is_feature_detected;
50+
pub(crate) use mips64::Feature;
5151
}
5252
any(target_arch = "loongarch32", target_arch = "loongarch64") => {
53-
#[stable(feature = "stdarch_loongarch_feature", since = "1.89.0")]
54-
pub use loongarch::*;
53+
pub use loongarch::__is_feature_detected;
54+
pub(crate) use loongarch::Feature;
5555
}
5656
target_arch = "s390x" => {
57-
#[stable(feature = "stdarch_s390x_feature_detection", since = "1.93.0")]
58-
pub use s390x::*;
57+
pub use s390x::__is_feature_detected;
58+
pub(crate) use s390x::Feature;
5959
}
6060
_ => {
6161
// Unimplemented architecture:
@@ -64,7 +64,6 @@ cfg_select! {
6464
Null,
6565
}
6666
#[doc(hidden)]
67-
#[unstable(feature = "stdarch_internal", issue = "none")]
6867
pub mod __is_feature_detected {}
6968

7069
impl Feature {

‎library/std_detect/src/detect/macros.rs‎

Lines changed: 58 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/// Forward a literal to the relevant `__is_feature_detected` function.
12
#[macro_export]
23
#[allow_internal_unstable(stdarch_internal)]
34
#[unstable(feature = "stdarch_internal", issue = "none")]
@@ -6,6 +7,7 @@ macro_rules! detect_feature {
67
$crate::detect_feature!($feature, $feature_lit : $feature_lit)
78
};
89
($feature:tt, $feature_lit:tt : $($target_feature_lit:tt),*) => {
10+
// Emit `true` if enabled at compile time
911
$(cfg!(target_feature = $target_feature_lit) ||)*
1012
$crate::detect::__is_feature_detected::$feature()
1113
};
@@ -14,6 +16,7 @@ macro_rules! detect_feature {
1416
};
1517
}
1618

19+
/// Verify that all features are known by `cfg!`.
1720
#[allow(unused_macros, reason = "it's used in the features! macro below")]
1821
macro_rules! check_cfg_feature {
1922
($feature:tt, $feature_lit:tt) => {
@@ -28,31 +31,54 @@ macro_rules! check_cfg_feature {
2831
};
2932
}
3033

34+
/// Create the `is_*_feature_detected` macro for an architecture.
35+
// Make sure any additions to this macro are tested in
36+
// `tests/ui/std/is_arch_feature_detected.AARCH64.stderr`.
3137
#[allow(unused)]
3238
macro_rules! features {
3339
(
34-
@TARGET: $target:ident;
35-
@CFG: $cfg:meta;
36-
@MACRO_NAME: $macro_name:ident;
37-
@MACRO_ATTRS: $(#[$macro_attrs:meta])*
38-
$(@BIND_FEATURE_NAME: $bind_feature:tt; $feature_impl:tt; $(#[$deprecate_attr:meta];)?)*
39-
$(@NO_RUNTIME_DETECTION: $nort_feature:tt; )*
40-
$(@FEATURE: #[$stability_attr:meta] $feature:ident: $feature_lit:tt;
41-
$(without cfg check: $feature_cfg_check:tt;)?
42-
$(implied by target_features: [$($target_feature_lit:tt),*];)?
43-
$(#[$feature_comment:meta])*)*
40+
// Target or target family name, for use in diagnostic messages
41+
@TARGET: $target:ident;
42+
// Cfg for which this macro should be allowed
43+
@CFG: $cfg:meta;
44+
// Name like `is_*_feature_detected`
45+
@MACRO_NAME: $macro_name:ident;
46+
// Docs and stability attributes
47+
@MACRO_ATTRS: $(#[$macro_attrs:meta])*
48+
// Synonyms, with possible deprecation
49+
$(@BIND_FEATURE_NAME: $bind_feature:tt; $feature_impl:tt; $(#[$deprecate_attr:meta];)?)*
50+
// Features that exist but can't be detected at runtime, so emit an error.
51+
$(@NO_RUNTIME_DETECTION: $nort_feature:tt; )*
52+
// List of features
53+
$(@FEATURE: #[$stability_attr:meta] $feature:ident: $feature_lit:tt;
54+
// Skip verifying that the feature is known by `cfg!`, which also skips emitting `true`
55+
// if the feature is enabled at compile time.
56+
$(without cfg check: $feature_cfg_check:tt;)?
57+
// If this feature is always enabled by some other feature, we can avoid the cost of
58+
// checking them separately by instead only checking the root features.
59+
$(implied by target_features: [$($target_feature_lit:tt),*];)?
60+
// Doc comment for the feature.
61+
$(#[$feature_comment:meta])*)*
4462
) => {
63+
// The current arch's version actually does the feature detection.
4564
#[macro_export]
4665
$(#[$macro_attrs])*
4766
#[allow_internal_unstable(stdarch_internal)]
4867
#[cfg($cfg)]
4968
#[doc(cfg($cfg))]
5069
macro_rules! $macro_name {
70+
// Emit all known features
5171
$(
5272
($feature_lit) => {
53-
$crate::detect_feature!($feature, $feature_lit $(, without cfg check: $feature_cfg_check)? $(: $($target_feature_lit),*)?)
73+
$crate::detect_feature!(
74+
$feature,
75+
$feature_lit
76+
$(, without cfg check: $feature_cfg_check)?
77+
$(: $($target_feature_lit),*)?
78+
)
5479
};
5580
)*
81+
// Emit all aliases, with a deprecation notice if applicable
5682
$(
5783
($bind_feature) => {
5884
{
@@ -64,6 +90,7 @@ macro_rules! features {
6490
}
6591
};
6692
)*
93+
// Emit an error for features that can't be detected at runtime
6794
$(
6895
($nort_feature) => {
6996
compile_error!(
@@ -74,40 +101,41 @@ macro_rules! features {
74101
)
75102
};
76103
)*
104+
// Allow a trailing comma
77105
($t:tt,) => {
78-
$crate::$macro_name!($t)
106+
$crate::$macro_name!($t)
79107
};
108+
// Fallback, error for unknown target features
80109
($t:tt) => {
81110
compile_error!(
82-
concat!(
83-
concat!("unknown ", stringify!($target)),
84-
concat!(" target feature: ", $t)
85-
)
111+
concat!("unknown ", stringify!($target), " target feature: ", $t)
86112
)
87113
};
88114
}
89115

116+
// All other arches still validate the features, but also
90117
$(#[$macro_attrs])*
91118
#[macro_export]
92119
#[cfg(not($cfg))]
93120
#[doc(cfg($cfg))]
94121
macro_rules! $macro_name {
122+
// All known features get an error
95123
$(
96124
($feature_lit) => {
97125
compile_error!(
98126
concat!(
99-
r#"This macro cannot be used on the current target.
100-
You can prevent it from being used in other architectures by
101-
guarding it behind a cfg("#,
102-
stringify!($cfg),
103-
")."
127+
"This macro cannot be used on the current target. You can prevent\n\
128+
it from being used in other architectures by guarding it behind\n\
129+
a `cfg(", stringify!($cfg), ")`."
104130
)
105131
)
106132
};
107133
)*
134+
// Forward deprecated features
108135
$(
109136
($bind_feature) => { $crate::$macro_name!($feature_impl) };
110137
)*
138+
// Emit an error for features that can't be detected at runtime
111139
$(
112140
($nort_feature) => {
113141
compile_error!(
@@ -118,15 +146,14 @@ macro_rules! features {
118146
)
119147
};
120148
)*
149+
// Allow a trailing comma
121150
($t:tt,) => {
122151
$crate::$macro_name!($t);
123152
};
153+
// Fallback, error for unknown target features
124154
($t:tt) => {
125155
compile_error!(
126-
concat!(
127-
concat!("unknown ", stringify!($target)),
128-
concat!(" target feature: ", $t)
129-
)
156+
concat!("unknown ", stringify!($target), " target feature: ", $t)
130157
)
131158
};
132159
}
@@ -135,7 +162,12 @@ macro_rules! features {
135162
#[deny(unfulfilled_lint_expectations)]
136163
const _: () = {
137164
$(
138-
check_cfg_feature!($feature, $feature_lit $(, without cfg check: $feature_cfg_check)? $(: $($target_feature_lit),*)?);
165+
check_cfg_feature!(
166+
$feature,
167+
$feature_lit
168+
$(, without cfg check: $feature_cfg_check)?
169+
$(: $($target_feature_lit),*)?)
170+
;
139171
)*
140172
};
141173

@@ -185,7 +217,6 @@ macro_rules! features {
185217
/// to change.
186218
#[doc(hidden)]
187219
#[cfg($cfg)]
188-
#[unstable(feature = "stdarch_internal", issue = "none")]
189220
pub mod __is_feature_detected {
190221
$(
191222

‎library/std_detect/src/detect/mod.rs‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ mod arch;
2424

2525
// This module needs to be public because the `is_{arch}_feature_detected!`
2626
// macros expand calls to items within it in user crates.
27-
#[doc(hidden)]
2827
#[unstable(feature = "stdarch_internal", issue = "none")]
2928
pub use self::arch::__is_feature_detected;
3029
pub(crate) use self::arch::Feature;
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
error: unknown x86 target feature: not-a-feature
2+
--> $DIR/is_arch_feature_detected.rs:22:13
3+
|
4+
LL | let _ = arch::is_x86_feature_detected!("not-a-feature");
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: this error originates in the macro `arch::is_x86_feature_detected` (in Nightly builds, run with -Z macro-backtrace for more info)
8+
9+
error: unknown aarch64 target feature: not-a-feature
10+
--> $DIR/is_arch_feature_detected.rs:24:13
11+
|
12+
LL | let _ = arch::is_aarch64_feature_detected!("not-a-feature");
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
14+
|
15+
= note: this error originates in the macro `arch::is_aarch64_feature_detected` (in Nightly builds, run with -Z macro-backtrace for more info)
16+
17+
error: This macro cannot be used on the current target. You can prevent
18+
it from being used in other architectures by guarding it behind
19+
a `cfg(any(target_arch = "x86", target_arch = "x86_64"))`.
20+
--> $DIR/is_arch_feature_detected.rs:29:13
21+
|
22+
LL | let _ = arch::is_x86_feature_detected!("sse");
23+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
24+
|
25+
= note: this error originates in the macro `arch::is_x86_feature_detected` (in Nightly builds, run with -Z macro-backtrace for more info)
26+
27+
error: This macro cannot be used on the current target. You can prevent
28+
it from being used in other architectures by guarding it behind
29+
a `cfg(any(target_arch = "x86", target_arch = "x86_64"))`.
30+
--> $DIR/is_arch_feature_detected.rs:36:13
31+
|
32+
LL | let _ = arch::is_x86_feature_detected!("avx512gfni");
33+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
34+
|
35+
= note: this error originates in the macro `$crate::is_x86_feature_detected` which comes from the expansion of the macro `arch::is_x86_feature_detected` (in Nightly builds, run with -Z macro-backtrace for more info)
36+
37+
error: "ras" feature cannot be detected at run-time
38+
--> $DIR/is_arch_feature_detected.rs:41:13
39+
|
40+
LL | let _ = arch::is_aarch64_feature_detected!("ras");
41+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
42+
|
43+
= note: this error originates in the macro `arch::is_aarch64_feature_detected` (in Nightly builds, run with -Z macro-backtrace for more info)
44+
45+
error[E0658]: use of unstable library feature `stdarch_mips_feature_detection`
46+
--> $DIR/is_arch_feature_detected.rs:45:13
47+
|
48+
LL | let _ = arch::is_mips_feature_detected!("msa");
49+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
50+
|
51+
= note: see issue #111188 <https://github.com/rust-lang/rust/issues/111188> for more information
52+
= help: add `#![feature(stdarch_mips_feature_detection)]` to the crate attributes to enable
53+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
54+
55+
error: This macro cannot be used on the current target. You can prevent
56+
it from being used in other architectures by guarding it behind
57+
a `cfg(target_arch = "mips")`.
58+
--> $DIR/is_arch_feature_detected.rs:45:13
59+
|
60+
LL | let _ = arch::is_mips_feature_detected!("msa");
61+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
62+
|
63+
= note: this error originates in the macro `arch::is_mips_feature_detected` (in Nightly builds, run with -Z macro-backtrace for more info)
64+
65+
error[E0658]: use of unstable library feature `stdarch_aarch64_feature_detection`
66+
--> $DIR/is_arch_feature_detected.rs:52:13
67+
|
68+
LL | let _ = arch::is_aarch64_feature_detected!("wfxt");
69+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
70+
|
71+
= note: see issue #127764 <https://github.com/rust-lang/rust/issues/127764> for more information
72+
= help: add `#![feature(stdarch_aarch64_feature_detection)]` to the crate attributes to enable
73+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
74+
= note: this error originates in the macro `$crate::detect_feature` which comes from the expansion of the macro `arch::is_aarch64_feature_detected` (in Nightly builds, run with -Z macro-backtrace for more info)
75+
76+
error: aborting due to 8 previous errors
77+
78+
For more information about this error, try `rustc --explain E0658`.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
error: unknown x86 target feature: not-a-feature
2+
--> $DIR/is_arch_feature_detected.rs:22:13
3+
|
4+
LL | let _ = arch::is_x86_feature_detected!("not-a-feature");
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: this error originates in the macro `arch::is_x86_feature_detected` (in Nightly builds, run with -Z macro-backtrace for more info)
8+
9+
error: unknown aarch64 target feature: not-a-feature
10+
--> $DIR/is_arch_feature_detected.rs:24:13
11+
|
12+
LL | let _ = arch::is_aarch64_feature_detected!("not-a-feature");
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
14+
|
15+
= note: this error originates in the macro `arch::is_aarch64_feature_detected` (in Nightly builds, run with -Z macro-backtrace for more info)
16+
17+
error: "ras" feature cannot be detected at run-time
18+
--> $DIR/is_arch_feature_detected.rs:41:13
19+
|
20+
LL | let _ = arch::is_aarch64_feature_detected!("ras");
21+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
22+
|
23+
= note: this error originates in the macro `arch::is_aarch64_feature_detected` (in Nightly builds, run with -Z macro-backtrace for more info)
24+
25+
error[E0658]: use of unstable library feature `stdarch_mips_feature_detection`
26+
--> $DIR/is_arch_feature_detected.rs:45:13
27+
|
28+
LL | let _ = arch::is_mips_feature_detected!("msa");
29+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
30+
|
31+
= note: see issue #111188 <https://github.com/rust-lang/rust/issues/111188> for more information
32+
= help: add `#![feature(stdarch_mips_feature_detection)]` to the crate attributes to enable
33+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
34+
35+
error: This macro cannot be used on the current target. You can prevent
36+
it from being used in other architectures by guarding it behind
37+
a `cfg(target_arch = "mips")`.
38+
--> $DIR/is_arch_feature_detected.rs:45:13
39+
|
40+
LL | let _ = arch::is_mips_feature_detected!("msa");
41+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
42+
|
43+
= note: this error originates in the macro `arch::is_mips_feature_detected` (in Nightly builds, run with -Z macro-backtrace for more info)
44+
45+
warning: use of deprecated macro `deprecated_feature`: the `avx512gfni` feature has been renamed to `gfni`
46+
--> $DIR/is_arch_feature_detected.rs:36:13
47+
|
48+
LL | let _ = arch::is_x86_feature_detected!("avx512gfni");
49+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
50+
|
51+
= note: `#[warn(deprecated)]` on by default
52+
= note: this warning originates in the macro `arch::is_x86_feature_detected` (in Nightly builds, run with -Z macro-backtrace for more info)
53+
54+
error: aborting due to 5 previous errors; 1 warning emitted
55+
56+
For more information about this error, try `rustc --explain E0658`.

0 commit comments

Comments
 (0)