Skip to content

Commit 9f2c8fd

Browse files
committed
Avoid the use of #[cfg(doc)] in std_detect
This doesn't work when building the standard library docs since that cfg flag is not passed to dependencies.
1 parent 863d31b commit 9f2c8fd

File tree

12 files changed

+111
-271
lines changed

12 files changed

+111
-271
lines changed

crates/std_detect/src/detect/arch/aarch64.rs

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
33
features! {
44
@TARGET: aarch64;
5+
@CFG: target_arch = "aarch64";
56
@MACRO_NAME: is_aarch64_feature_detected;
67
@MACRO_ATTRS:
78
/// This macro tests, at runtime, whether an `aarch64` feature is enabled on aarch64 platforms.

crates/std_detect/src/detect/arch/arm.rs

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
33
features! {
44
@TARGET: arm;
5+
@CFG: target_arch = "arm";
56
@MACRO_NAME: is_arm_feature_detected;
67
@MACRO_ATTRS:
78
/// Checks if `arm` feature is enabled.

crates/std_detect/src/detect/arch/mips.rs

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
33
features! {
44
@TARGET: mips;
5+
@CFG: target_arch = "mips";
56
@MACRO_NAME: is_mips_feature_detected;
67
@MACRO_ATTRS:
78
/// Checks if `mips` feature is enabled.

crates/std_detect/src/detect/arch/mips64.rs

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
33
features! {
44
@TARGET: mips64;
5+
@CFG: target_arch = "mips64";
56
@MACRO_NAME: is_mips64_feature_detected;
67
@MACRO_ATTRS:
78
/// Checks if `mips64` feature is enabled.
+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#![allow(dead_code)]
2+
3+
use cfg_if::cfg_if;
4+
5+
// Export the macros for all supported architectures.
6+
#[macro_use]
7+
mod x86;
8+
#[macro_use]
9+
mod arm;
10+
#[macro_use]
11+
mod aarch64;
12+
#[macro_use]
13+
mod riscv;
14+
#[macro_use]
15+
mod powerpc;
16+
#[macro_use]
17+
mod powerpc64;
18+
#[macro_use]
19+
mod mips;
20+
#[macro_use]
21+
mod mips64;
22+
23+
cfg_if! {
24+
if #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] {
25+
pub use x86::*;
26+
} else if #[cfg(target_arch = "arm")] {
27+
pub use arm::*;
28+
} else if #[cfg(target_arch = "aarch64")] {
29+
pub use aarch64::*;
30+
} else if #[cfg(any(target_arch = "riscv32", target_arch = "riscv64"))] {
31+
pub use riscv::*;
32+
} else if #[cfg(target_arch = "powerpc")] {
33+
pub use powerpc::*;
34+
} else if #[cfg(target_arch = "powerpc64")] {
35+
pub use powerpc64::*;
36+
} else if #[cfg(target_arch = "mips")] {
37+
pub use mips::*;
38+
} else if #[cfg(target_arch = "mips64")] {
39+
pub use mips64::*;
40+
} else {
41+
// Unimplemented architecture:
42+
#[doc(hidden)]
43+
pub(crate) enum Feature {
44+
Null
45+
}
46+
#[doc(hidden)]
47+
pub mod __is_feature_detected {}
48+
49+
impl Feature {
50+
#[doc(hidden)]
51+
pub(crate) fn from_str(_s: &str) -> Result<Feature, ()> { Err(()) }
52+
#[doc(hidden)]
53+
pub(crate) fn to_str(self) -> &'static str { "" }
54+
}
55+
}
56+
}

crates/std_detect/src/detect/arch/powerpc.rs

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
33
features! {
44
@TARGET: powerpc;
5+
@CFG: target_arch = "powerpc";
56
@MACRO_NAME: is_powerpc_feature_detected;
67
@MACRO_ATTRS:
78
/// Checks if `powerpc` feature is enabled.

crates/std_detect/src/detect/arch/powerpc64.rs

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
33
features! {
44
@TARGET: powerpc64;
5+
@CFG: target_arch = "powerpc64";
56
@MACRO_NAME: is_powerpc64_feature_detected;
67
@MACRO_ATTRS:
78
/// Checks if `powerpc` feature is enabled.

crates/std_detect/src/detect/arch/riscv.rs

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
33
features! {
44
@TARGET: riscv;
5+
@CFG: any(target_arch = "riscv32", target_arch = "riscv64");
56
@MACRO_NAME: is_riscv_feature_detected;
67
@MACRO_ATTRS:
78
/// A macro to test at *runtime* whether instruction sets are available on

crates/std_detect/src/detect/arch/x86.rs

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
1818
features! {
1919
@TARGET: x86;
20+
@CFG: any(target_arch = "x86", target_arch = "x86_64");
2021
@MACRO_NAME: is_x86_feature_detected;
2122
@MACRO_ATTRS:
2223
/// A macro to test at *runtime* whether a CPU feature is available on

crates/std_detect/src/detect/error_macros.rs

-171
This file was deleted.

crates/std_detect/src/detect/macros.rs

+46
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
macro_rules! features {
33
(
44
@TARGET: $target:ident;
5+
@CFG: $cfg:meta;
56
@MACRO_NAME: $macro_name:ident;
67
@MACRO_ATTRS: $(#[$macro_attrs:meta])*
78
$(@BIND_FEATURE_NAME: $bind_feature:tt; $feature_impl:tt; )*
@@ -11,6 +12,7 @@ macro_rules! features {
1112
#[macro_export]
1213
$(#[$macro_attrs])*
1314
#[allow_internal_unstable(stdsimd_internal)]
15+
#[cfg($cfg)]
1416
macro_rules! $macro_name {
1517
$(
1618
($feature_lit) => {
@@ -44,6 +46,47 @@ macro_rules! features {
4446
};
4547
}
4648

49+
$(#[$macro_attrs])*
50+
#[macro_export]
51+
#[cfg(not($cfg))]
52+
macro_rules! $macro_name {
53+
$(
54+
($feature_lit) => {
55+
compile_error!(
56+
r#"
57+
This macro cannot be used on the current target.
58+
You can prevent it from being used in other architectures by
59+
guarding it behind a cfg(target_arch).
60+
"#
61+
)
62+
};
63+
)*
64+
$(
65+
($bind_feature) => { $macro_name!($feature_impl) };
66+
)*
67+
$(
68+
($nort_feature) => {
69+
compile_error!(
70+
concat!(
71+
stringify!($nort_feature),
72+
" feature cannot be detected at run-time"
73+
)
74+
)
75+
};
76+
)*
77+
($t:tt,) => {
78+
$macro_name!($t);
79+
};
80+
($t:tt) => {
81+
compile_error!(
82+
concat!(
83+
concat!("unknown ", stringify!($target)),
84+
concat!(" target feature: ", $t)
85+
)
86+
)
87+
};
88+
}
89+
4790
/// Each variant denotes a position in a bitset for a particular feature.
4891
///
4992
/// PLEASE: do not use this, it is an implementation detail subject
@@ -53,6 +96,7 @@ macro_rules! features {
5396
#[derive(Copy, Clone)]
5497
#[repr(u8)]
5598
#[unstable(feature = "stdsimd_internal", issue = "none")]
99+
#[cfg($cfg)]
56100
pub(crate) enum Feature {
57101
$(
58102
$(#[$feature_comment])*
@@ -63,6 +107,7 @@ macro_rules! features {
63107
_last
64108
}
65109

110+
#[cfg($cfg)]
66111
impl Feature {
67112
pub(crate) fn to_str(self) -> &'static str {
68113
match self {
@@ -86,6 +131,7 @@ macro_rules! features {
86131
/// PLEASE: do not use this, it is an implementation detail subject
87132
/// to change.
88133
#[doc(hidden)]
134+
#[cfg($cfg)]
89135
pub mod __is_feature_detected {
90136
$(
91137

0 commit comments

Comments
 (0)