rustc: target_features: adapt compile errors and warnings for cfg-only stable features

This commit is contained in:
Román Cárdenas Rodríguez
2026-05-04 17:28:35 +02:00
parent df8d029014
commit ee9a01aecf
3 changed files with 23 additions and 10 deletions
+2 -1
View File
@@ -1194,9 +1194,10 @@ pub(crate) struct UnknownCTargetFeature<'a> {
#[derive(Diagnostic)]
#[diag("unstable feature specified for `-Ctarget-feature`: `{$feature}`")]
#[note("this feature is not stably supported; its behavior can change in the future")]
#[note("{$note}; its behavior can change in the future")]
pub(crate) struct UnstableCTargetFeature<'a> {
pub feature: &'a str,
pub note: &'a str,
}
#[derive(Diagnostic)]
@@ -65,13 +65,12 @@ pub(crate) fn from_target_feature_attr(
} else if let Some(nightly_feature) = stability.requires_nightly(/* in_cfg */ false)
&& !rust_features.enabled(nightly_feature)
{
feature_err(
&tcx.sess,
nightly_feature,
feature_span,
format!("the target feature `{feature}` is currently unstable"),
)
.emit();
let explain = if stability.is_cfg_stable_toggle_unstable() {
format!("the target feature `{feature}` is allowed in cfg but unstable otherwise")
} else {
format!("the target feature `{feature}` is currently unstable")
};
feature_err(&tcx.sess, nightly_feature, feature_span, explain).emit();
} else {
// Add this and the implied features.
for &name in tcx.implied_target_features(feature) {
@@ -319,8 +318,15 @@ pub fn cfg_target_feature<'a, const N: usize>(
// An unstable feature. Warn about using it. It makes little sense
// to hard-error here since we just warn about fully unknown
// features above.
sess.dcx()
.emit_warn(errors::UnstableCTargetFeature { feature: base_feature });
let note = if stability.is_cfg_stable_toggle_unstable() {
"this feature is allowed in cfg but unstable otherwise"
} else {
"this feature is not stably supported"
};
sess.dcx().emit_warn(errors::UnstableCTargetFeature {
feature: base_feature,
note,
});
}
}
}
@@ -78,6 +78,12 @@ impl Stability {
}
}
/// Returns whether the feature is cfg-stable but still requires a nightly feature gate to
/// be used in `#[target_feature]`/`-Ctarget-feature`.
pub fn is_cfg_stable_toggle_unstable(&self) -> bool {
matches!(self, Stability::CfgStableToggleUnstable { .. })
}
/// Returns whether the feature may be toggled via `#[target_feature]` or `-Ctarget-feature`.
/// (It might still be nightly-only even if this returns `true`, so make sure to also check
/// `requires_nightly`.)