mark some target features as 'forbidden' so they cannot be (un)set
For now, this is just a warning, but should become a hard error in the future
This commit is contained in:
parent
2dece5bb62
commit
ffad9aac27
23 changed files with 372 additions and 158 deletions
|
@ -58,6 +58,9 @@ codegen_ssa_failed_to_write = failed to write {$path}: {$error}
|
|||
|
||||
codegen_ssa_field_associated_value_expected = associated value expected for `{$name}`
|
||||
|
||||
codegen_ssa_forbidden_target_feature_attr =
|
||||
target feature `{$feature}` cannot be toggled with `#[target_feature]`: {$reason}
|
||||
|
||||
codegen_ssa_ignoring_emit_path = ignoring emit path because multiple .{$extension} files were produced
|
||||
|
||||
codegen_ssa_ignoring_output = ignoring -o because multiple .{$extension} files were produced
|
||||
|
|
|
@ -20,8 +20,8 @@ use rustc_span::symbol::Ident;
|
|||
use rustc_span::{Span, sym};
|
||||
use rustc_target::spec::{SanitizerSet, abi};
|
||||
|
||||
use crate::errors::{self, MissingFeatures, TargetFeatureDisableOrEnable};
|
||||
use crate::target_features::{check_target_feature_trait_unsafe, from_target_feature};
|
||||
use crate::errors;
|
||||
use crate::target_features::{check_target_feature_trait_unsafe, from_target_feature_attr};
|
||||
|
||||
fn linkage_by_name(tcx: TyCtxt<'_>, def_id: LocalDefId, name: &str) -> Linkage {
|
||||
use rustc_middle::mir::mono::Linkage::*;
|
||||
|
@ -73,7 +73,7 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
|
|||
codegen_fn_attrs.flags |= CodegenFnAttrFlags::NO_BUILTINS;
|
||||
}
|
||||
|
||||
let supported_target_features = tcx.supported_target_features(LOCAL_CRATE);
|
||||
let rust_target_features = tcx.rust_target_features(LOCAL_CRATE);
|
||||
|
||||
let mut inline_span = None;
|
||||
let mut link_ordinal_span = None;
|
||||
|
@ -281,10 +281,10 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
|
|||
check_target_feature_trait_unsafe(tcx, did, attr.span);
|
||||
}
|
||||
}
|
||||
from_target_feature(
|
||||
from_target_feature_attr(
|
||||
tcx,
|
||||
attr,
|
||||
supported_target_features,
|
||||
rust_target_features,
|
||||
&mut codegen_fn_attrs.target_features,
|
||||
);
|
||||
}
|
||||
|
@ -676,10 +676,10 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
|
|||
.next()
|
||||
.map_or_else(|| tcx.def_span(did), |a| a.span);
|
||||
tcx.dcx()
|
||||
.create_err(TargetFeatureDisableOrEnable {
|
||||
.create_err(errors::TargetFeatureDisableOrEnable {
|
||||
features,
|
||||
span: Some(span),
|
||||
missing_features: Some(MissingFeatures),
|
||||
missing_features: Some(errors::MissingFeatures),
|
||||
})
|
||||
.emit();
|
||||
}
|
||||
|
|
|
@ -1018,6 +1018,15 @@ pub(crate) struct TargetFeatureSafeTrait {
|
|||
pub def: Span,
|
||||
}
|
||||
|
||||
#[derive(Diagnostic)]
|
||||
#[diag(codegen_ssa_forbidden_target_feature_attr)]
|
||||
pub struct ForbiddenTargetFeatureAttr<'a> {
|
||||
#[primary_span]
|
||||
pub span: Span,
|
||||
pub feature: &'a str,
|
||||
pub reason: &'a str,
|
||||
}
|
||||
|
||||
#[derive(Diagnostic)]
|
||||
#[diag(codegen_ssa_failed_to_get_layout)]
|
||||
pub struct FailedToGetLayout<'tcx> {
|
||||
|
|
|
@ -11,13 +11,16 @@ use rustc_middle::ty::TyCtxt;
|
|||
use rustc_session::parse::feature_err;
|
||||
use rustc_span::Span;
|
||||
use rustc_span::symbol::{Symbol, sym};
|
||||
use rustc_target::target_features::{self, Stability};
|
||||
|
||||
use crate::errors;
|
||||
|
||||
pub(crate) fn from_target_feature(
|
||||
/// Compute the enabled target features from the `#[target_feature]` function attribute.
|
||||
/// Enabled target features are added to `target_features`.
|
||||
pub(crate) fn from_target_feature_attr(
|
||||
tcx: TyCtxt<'_>,
|
||||
attr: &ast::Attribute,
|
||||
supported_target_features: &UnordMap<String, Option<Symbol>>,
|
||||
rust_target_features: &UnordMap<String, target_features::Stability>,
|
||||
target_features: &mut Vec<TargetFeature>,
|
||||
) {
|
||||
let Some(list) = attr.meta_item_list() else { return };
|
||||
|
@ -46,12 +49,12 @@ pub(crate) fn from_target_feature(
|
|||
|
||||
// We allow comma separation to enable multiple features.
|
||||
added_target_features.extend(value.as_str().split(',').filter_map(|feature| {
|
||||
let Some(feature_gate) = supported_target_features.get(feature) else {
|
||||
let Some(stability) = rust_target_features.get(feature) else {
|
||||
let msg = format!("the feature named `{feature}` is not valid for this target");
|
||||
let mut err = tcx.dcx().struct_span_err(item.span(), msg);
|
||||
err.span_label(item.span(), format!("`{feature}` is not valid for this target"));
|
||||
if let Some(stripped) = feature.strip_prefix('+') {
|
||||
let valid = supported_target_features.contains_key(stripped);
|
||||
let valid = rust_target_features.contains_key(stripped);
|
||||
if valid {
|
||||
err.help("consider removing the leading `+` in the feature name");
|
||||
}
|
||||
|
@ -61,18 +64,31 @@ pub(crate) fn from_target_feature(
|
|||
};
|
||||
|
||||
// Only allow target features whose feature gates have been enabled.
|
||||
let allowed = match feature_gate.as_ref().copied() {
|
||||
Some(name) => rust_features.enabled(name),
|
||||
None => true,
|
||||
let allowed = match stability {
|
||||
Stability::Forbidden { .. } => false,
|
||||
Stability::Stable => true,
|
||||
Stability::Unstable(name) => rust_features.enabled(*name),
|
||||
};
|
||||
if !allowed {
|
||||
feature_err(
|
||||
&tcx.sess,
|
||||
feature_gate.unwrap(),
|
||||
item.span(),
|
||||
format!("the target feature `{feature}` is currently unstable"),
|
||||
)
|
||||
.emit();
|
||||
match stability {
|
||||
Stability::Stable => unreachable!(),
|
||||
&Stability::Unstable(lang_feature_name) => {
|
||||
feature_err(
|
||||
&tcx.sess,
|
||||
lang_feature_name,
|
||||
item.span(),
|
||||
format!("the target feature `{feature}` is currently unstable"),
|
||||
)
|
||||
.emit();
|
||||
}
|
||||
Stability::Forbidden { reason } => {
|
||||
tcx.dcx().emit_err(errors::ForbiddenTargetFeatureAttr {
|
||||
span: item.span(),
|
||||
feature,
|
||||
reason,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
Some(Symbol::intern(feature))
|
||||
}));
|
||||
|
@ -138,20 +154,20 @@ pub(crate) fn check_target_feature_trait_unsafe(tcx: TyCtxt<'_>, id: LocalDefId,
|
|||
|
||||
pub(crate) fn provide(providers: &mut Providers) {
|
||||
*providers = Providers {
|
||||
supported_target_features: |tcx, cnum| {
|
||||
rust_target_features: |tcx, cnum| {
|
||||
assert_eq!(cnum, LOCAL_CRATE);
|
||||
if tcx.sess.opts.actually_rustdoc {
|
||||
// rustdoc needs to be able to document functions that use all the features, so
|
||||
// whitelist them all
|
||||
rustc_target::target_features::all_known_features()
|
||||
.map(|(a, b)| (a.to_string(), b.as_feature_name()))
|
||||
rustc_target::target_features::all_rust_features()
|
||||
.map(|(a, b)| (a.to_string(), b))
|
||||
.collect()
|
||||
} else {
|
||||
tcx.sess
|
||||
.target
|
||||
.supported_target_features()
|
||||
.rust_target_features()
|
||||
.iter()
|
||||
.map(|&(a, b, _)| (a.to_string(), b.as_feature_name()))
|
||||
.map(|&(a, b, _)| (a.to_string(), b))
|
||||
.collect()
|
||||
}
|
||||
},
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue