Skip to content

Commit ee9a01a

Browse files
committed
rustc: target_features: adapt compile errors and warnings for cfg-only stable features
1 parent df8d029 commit ee9a01a

3 files changed

Lines changed: 23 additions & 10 deletions

File tree

compiler/rustc_codegen_ssa/src/errors.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1194,9 +1194,10 @@ pub(crate) struct UnknownCTargetFeature<'a> {
11941194

11951195
#[derive(Diagnostic)]
11961196
#[diag("unstable feature specified for `-Ctarget-feature`: `{$feature}`")]
1197-
#[note("this feature is not stably supported; its behavior can change in the future")]
1197+
#[note("{$note}; its behavior can change in the future")]
11981198
pub(crate) struct UnstableCTargetFeature<'a> {
11991199
pub feature: &'a str,
1200+
pub note: &'a str,
12001201
}
12011202

12021203
#[derive(Diagnostic)]

compiler/rustc_codegen_ssa/src/target_features.rs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,12 @@ pub(crate) fn from_target_feature_attr(
6565
} else if let Some(nightly_feature) = stability.requires_nightly(/* in_cfg */ false)
6666
&& !rust_features.enabled(nightly_feature)
6767
{
68-
feature_err(
69-
&tcx.sess,
70-
nightly_feature,
71-
feature_span,
72-
format!("the target feature `{feature}` is currently unstable"),
73-
)
74-
.emit();
68+
let explain = if stability.is_cfg_stable_toggle_unstable() {
69+
format!("the target feature `{feature}` is allowed in cfg but unstable otherwise")
70+
} else {
71+
format!("the target feature `{feature}` is currently unstable")
72+
};
73+
feature_err(&tcx.sess, nightly_feature, feature_span, explain).emit();
7574
} else {
7675
// Add this and the implied features.
7776
for &name in tcx.implied_target_features(feature) {
@@ -319,8 +318,15 @@ pub fn cfg_target_feature<'a, const N: usize>(
319318
// An unstable feature. Warn about using it. It makes little sense
320319
// to hard-error here since we just warn about fully unknown
321320
// features above.
322-
sess.dcx()
323-
.emit_warn(errors::UnstableCTargetFeature { feature: base_feature });
321+
let note = if stability.is_cfg_stable_toggle_unstable() {
322+
"this feature is allowed in cfg but unstable otherwise"
323+
} else {
324+
"this feature is not stably supported"
325+
};
326+
sess.dcx().emit_warn(errors::UnstableCTargetFeature {
327+
feature: base_feature,
328+
note,
329+
});
324330
}
325331
}
326332
}

compiler/rustc_target/src/target_features.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,12 @@ impl Stability {
7878
}
7979
}
8080

81+
/// Returns whether the feature is cfg-stable but still requires a nightly feature gate to
82+
/// be used in `#[target_feature]`/`-Ctarget-feature`.
83+
pub fn is_cfg_stable_toggle_unstable(&self) -> bool {
84+
matches!(self, Stability::CfgStableToggleUnstable { .. })
85+
}
86+
8187
/// Returns whether the feature may be toggled via `#[target_feature]` or `-Ctarget-feature`.
8288
/// (It might still be nightly-only even if this returns `true`, so make sure to also check
8389
/// `requires_nightly`.)

0 commit comments

Comments
 (0)