Skip to content

Commit b4a0e07

Browse files
Amanieubjorn3
andauthored
Avoid the use of #[cfg(doc)] in std_detect (#1283)
Co-authored-by: bjorn3 <[email protected]>
1 parent 863d31b commit b4a0e07

File tree

12 files changed

+115
-271
lines changed

12 files changed

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

+50
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,50 @@ 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+
concat!(
59+
r#"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("#,
62+
stringify!($cfg),
63+
")."
64+
)
65+
)
66+
};
67+
)*
68+
$(
69+
($bind_feature) => { $macro_name!($feature_impl) };
70+
)*
71+
$(
72+
($nort_feature) => {
73+
compile_error!(
74+
concat!(
75+
stringify!($nort_feature),
76+
" feature cannot be detected at run-time"
77+
)
78+
)
79+
};
80+
)*
81+
($t:tt,) => {
82+
$macro_name!($t);
83+
};
84+
($t:tt) => {
85+
compile_error!(
86+
concat!(
87+
concat!("unknown ", stringify!($target)),
88+
concat!(" target feature: ", $t)
89+
)
90+
)
91+
};
92+
}
93+
4794
/// Each variant denotes a position in a bitset for a particular feature.
4895
///
4996
/// PLEASE: do not use this, it is an implementation detail subject
@@ -53,6 +100,7 @@ macro_rules! features {
53100
#[derive(Copy, Clone)]
54101
#[repr(u8)]
55102
#[unstable(feature = "stdsimd_internal", issue = "none")]
103+
#[cfg($cfg)]
56104
pub(crate) enum Feature {
57105
$(
58106
$(#[$feature_comment])*
@@ -63,6 +111,7 @@ macro_rules! features {
63111
_last
64112
}
65113

114+
#[cfg($cfg)]
66115
impl Feature {
67116
pub(crate) fn to_str(self) -> &'static str {
68117
match self {
@@ -86,6 +135,7 @@ macro_rules! features {
86135
/// PLEASE: do not use this, it is an implementation detail subject
87136
/// to change.
88137
#[doc(hidden)]
138+
#[cfg($cfg)]
89139
pub mod __is_feature_detected {
90140
$(
91141

0 commit comments

Comments
 (0)