Skip to content

Commit f987375

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 f987375

File tree

12 files changed

+113
-271
lines changed

12 files changed

+113
-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

+48
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,8 @@ macro_rules! features {
1112
#[macro_export]
1213
$(#[$macro_attrs])*
1314
#[allow_internal_unstable(stdsimd_internal)]
15+
#[cfg($cfg)]
16+
#[doc(cfg($cfg))]
1417
macro_rules! $macro_name {
1518
$(
1619
($feature_lit) => {
@@ -44,6 +47,48 @@ macro_rules! features {
4447
};
4548
}
4649

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

112+
#[cfg($cfg)]
66113
impl Feature {
67114
pub(crate) fn to_str(self) -> &'static str {
68115
match self {
@@ -86,6 +133,7 @@ macro_rules! features {
86133
/// PLEASE: do not use this, it is an implementation detail subject
87134
/// to change.
88135
#[doc(hidden)]
136+
#[cfg($cfg)]
89137
pub mod __is_feature_detected {
90138
$(
91139

0 commit comments

Comments
 (0)