Skip to content

Commit bb0826a

Browse files
committed
rustc: target_features: allow for cfg-only stable target_features
1 parent 03c609a commit bb0826a

2 files changed

Lines changed: 28 additions & 6 deletions

File tree

compiler/rustc_codegen_ssa/src/target_features.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ pub(crate) fn from_target_feature_attr(
6262
feature: feature_str,
6363
reason,
6464
});
65-
} else if let Some(nightly_feature) = stability.requires_nightly()
65+
} else if let Some(nightly_feature) = stability.requires_nightly(false)
6666
&& !rust_features.enabled(nightly_feature)
6767
{
6868
feature_err(
@@ -315,7 +315,7 @@ pub fn cfg_target_feature<'a, const N: usize>(
315315
enabled: if enable { "enabled" } else { "disabled" },
316316
reason,
317317
});
318-
} else if stability.requires_nightly().is_some() {
318+
} else if stability.requires_nightly(true).is_some() {
319319
// An unstable feature. Warn about using it. It makes little sense
320320
// to hard-error here since we just warn about fully unknown
321321
// features above.
@@ -346,7 +346,7 @@ pub fn cfg_target_feature<'a, const N: usize>(
346346
// "forbidden" features.
347347
if allow_unstable
348348
|| (gate.in_cfg()
349-
&& (sess.is_nightly_build() || gate.requires_nightly().is_none()))
349+
&& (sess.is_nightly_build() || gate.requires_nightly(true).is_none()))
350350
{
351351
Some(Symbol::intern(feature))
352352
} else {

compiler/rustc_target/src/target_features.rs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ pub enum Stability {
1717
/// This target feature is stable, it can be used in `#[target_feature]` and
1818
/// `#[cfg(target_feature)]`.
1919
Stable,
20+
/// This target feature is cfg-stable. It can be used for `#[cfg(target_feature)]` on stable,
21+
/// but using it in `#[target_feature]` requires the given nightly feature.
22+
CfgOnlyStable(
23+
/// This must be a *language* feature, or else rustc will ICE when reporting a missing
24+
/// feature gate!
25+
Symbol,
26+
),
2027
/// This target feature is unstable. It is only present in `#[cfg(target_feature)]` on
2128
/// nightly and using it in `#[target_feature]` requires enabling the given nightly feature.
2229
Unstable(
@@ -37,7 +44,10 @@ impl Stability {
3744
/// (It might still be nightly-only even if this returns `true`, so make sure to also check
3845
/// `requires_nightly`.)
3946
pub fn in_cfg(&self) -> bool {
40-
matches!(self, Stability::Stable | Stability::Unstable { .. })
47+
matches!(
48+
self,
49+
Stability::Stable | Stability::CfgOnlyStable { .. } | Stability::Unstable { .. }
50+
)
4151
}
4252

4353
/// Returns the nightly feature that is required to toggle this target feature via
@@ -48,9 +58,19 @@ impl Stability {
4858
/// Before calling this, ensure the feature is even permitted for this use:
4959
/// - for `#[target_feature]`/`-Ctarget-feature`, check `toggle_allowed()`
5060
/// - for `cfg(target_feature)`, check `in_cfg()`
51-
pub fn requires_nightly(&self) -> Option<Symbol> {
61+
///
62+
/// The `is_cfg` parameter is used to determine whether it will be used in
63+
/// `cfg(target_feature)` (true) or `#[target_feature]`/`-Ctarget-feature` (false)
64+
pub fn requires_nightly(&self, is_cfg: bool) -> Option<Symbol> {
5265
match *self {
5366
Stability::Unstable(nightly_feature) => Some(nightly_feature),
67+
Stability::CfgOnlyStable(nightly_feature) => {
68+
if is_cfg {
69+
None
70+
} else {
71+
Some(nightly_feature)
72+
}
73+
}
5474
Stability::Stable { .. } => None,
5575
Stability::Forbidden { .. } => panic!("forbidden features should not reach this far"),
5676
}
@@ -61,7 +81,9 @@ impl Stability {
6181
/// `requires_nightly`.)
6282
pub fn toggle_allowed(&self) -> Result<(), &'static str> {
6383
match self {
64-
Stability::Unstable(_) | Stability::Stable { .. } => Ok(()),
84+
Stability::Unstable(_) | Stability::CfgOnlyStable(_) | Stability::Stable { .. } => {
85+
Ok(())
86+
}
6587
Stability::Forbidden { reason } => Err(reason),
6688
}
6789
}

0 commit comments

Comments
 (0)