2023-04-10 16:04:14 +01:00
|
|
|
use crate::errors;
|
2022-12-08 03:53:35 +00:00
|
|
|
use rustc_ast::ast;
|
|
|
|
use rustc_attr::InstructionSetAttr;
|
|
|
|
use rustc_data_structures::fx::FxHashMap;
|
2023-02-21 15:15:16 +01:00
|
|
|
use rustc_data_structures::fx::FxIndexSet;
|
2022-12-08 03:53:35 +00:00
|
|
|
use rustc_errors::Applicability;
|
2023-02-12 20:20:45 +00:00
|
|
|
use rustc_hir::def::DefKind;
|
2022-12-08 03:53:35 +00:00
|
|
|
use rustc_hir::def_id::DefId;
|
|
|
|
use rustc_hir::def_id::LocalDefId;
|
2020-10-09 19:35:17 +02:00
|
|
|
use rustc_hir::def_id::LOCAL_CRATE;
|
2023-05-15 06:24:45 +02:00
|
|
|
use rustc_middle::query::Providers;
|
2023-02-22 19:51:17 +04:00
|
|
|
use rustc_middle::ty::TyCtxt;
|
2022-12-08 03:53:35 +00:00
|
|
|
use rustc_session::parse::feature_err;
|
2020-10-04 11:12:56 +02:00
|
|
|
use rustc_span::symbol::sym;
|
|
|
|
use rustc_span::symbol::Symbol;
|
2022-12-08 03:53:35 +00:00
|
|
|
use rustc_span::Span;
|
2020-10-04 11:12:56 +02:00
|
|
|
|
2022-12-08 03:53:35 +00:00
|
|
|
pub fn from_target_feature(
|
|
|
|
tcx: TyCtxt<'_>,
|
|
|
|
attr: &ast::Attribute,
|
|
|
|
supported_target_features: &FxHashMap<String, Option<Symbol>>,
|
|
|
|
target_features: &mut Vec<Symbol>,
|
|
|
|
) {
|
|
|
|
let Some(list) = attr.meta_item_list() else { return };
|
|
|
|
let bad_item = |span| {
|
|
|
|
let msg = "malformed `target_feature` attribute input";
|
|
|
|
let code = "enable = \"..\"";
|
|
|
|
tcx.sess
|
|
|
|
.struct_span_err(span, msg)
|
|
|
|
.span_suggestion(span, "must be of the form", code, Applicability::HasPlaceholders)
|
|
|
|
.emit();
|
2020-10-09 19:35:17 +02:00
|
|
|
};
|
2022-12-08 03:53:35 +00:00
|
|
|
let rust_features = tcx.features();
|
|
|
|
for item in list {
|
|
|
|
// Only `enable = ...` is accepted in the meta-item list.
|
|
|
|
if !item.has_name(sym::enable) {
|
|
|
|
bad_item(item.span());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Must be of the form `enable = "..."` (a string).
|
|
|
|
let Some(value) = item.value_str() else {
|
|
|
|
bad_item(item.span());
|
|
|
|
continue;
|
|
|
|
};
|
|
|
|
|
|
|
|
// We allow comma separation to enable multiple features.
|
|
|
|
target_features.extend(value.as_str().split(',').filter_map(|feature| {
|
|
|
|
let Some(feature_gate) = supported_target_features.get(feature) else {
|
2023-07-25 23:04:01 +02:00
|
|
|
let msg = format!("the feature named `{feature}` is not valid for this target");
|
Restrict `From<S>` for `{D,Subd}iagnosticMessage`.
Currently a `{D,Subd}iagnosticMessage` can be created from any type that
impls `Into<String>`. That includes `&str`, `String`, and `Cow<'static,
str>`, which are reasonable. It also includes `&String`, which is pretty
weird, and results in many places making unnecessary allocations for
patterns like this:
```
self.fatal(&format!(...))
```
This creates a string with `format!`, takes a reference, passes the
reference to `fatal`, which does an `into()`, which clones the
reference, doing a second allocation. Two allocations for a single
string, bleh.
This commit changes the `From` impls so that you can only create a
`{D,Subd}iagnosticMessage` from `&str`, `String`, or `Cow<'static,
str>`. This requires changing all the places that currently create one
from a `&String`. Most of these are of the `&format!(...)` form
described above; each one removes an unnecessary static `&`, plus an
allocation when executed. There are also a few places where the existing
use of `&String` was more reasonable; these now just use `clone()` at
the call site.
As well as making the code nicer and more efficient, this is a step
towards possibly using `Cow<'static, str>` in
`{D,Subd}iagnosticMessage::{Str,Eager}`. That would require changing
the `From<&'a str>` impls to `From<&'static str>`, which is doable, but
I'm not yet sure if it's worthwhile.
2023-04-20 13:26:58 +10:00
|
|
|
let mut err = tcx.sess.struct_span_err(item.span(), msg);
|
2023-07-25 23:04:01 +02:00
|
|
|
err.span_label(item.span(), format!("`{feature}` is not valid for this target"));
|
2022-12-08 03:53:35 +00:00
|
|
|
if let Some(stripped) = feature.strip_prefix('+') {
|
|
|
|
let valid = supported_target_features.contains_key(stripped);
|
|
|
|
if valid {
|
|
|
|
err.help("consider removing the leading `+` in the feature name");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
err.emit();
|
|
|
|
return None;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Only allow features whose feature gates have been enabled.
|
|
|
|
let allowed = match feature_gate.as_ref().copied() {
|
|
|
|
Some(sym::arm_target_feature) => rust_features.arm_target_feature,
|
|
|
|
Some(sym::hexagon_target_feature) => rust_features.hexagon_target_feature,
|
|
|
|
Some(sym::powerpc_target_feature) => rust_features.powerpc_target_feature,
|
|
|
|
Some(sym::mips_target_feature) => rust_features.mips_target_feature,
|
|
|
|
Some(sym::riscv_target_feature) => rust_features.riscv_target_feature,
|
|
|
|
Some(sym::avx512_target_feature) => rust_features.avx512_target_feature,
|
|
|
|
Some(sym::sse4a_target_feature) => rust_features.sse4a_target_feature,
|
|
|
|
Some(sym::tbm_target_feature) => rust_features.tbm_target_feature,
|
|
|
|
Some(sym::wasm_target_feature) => rust_features.wasm_target_feature,
|
|
|
|
Some(sym::rtm_target_feature) => rust_features.rtm_target_feature,
|
|
|
|
Some(sym::ermsb_target_feature) => rust_features.ermsb_target_feature,
|
|
|
|
Some(sym::bpf_target_feature) => rust_features.bpf_target_feature,
|
|
|
|
Some(sym::aarch64_ver_target_feature) => rust_features.aarch64_ver_target_feature,
|
2023-07-14 19:16:38 +08:00
|
|
|
Some(sym::csky_target_feature) => rust_features.csky_target_feature,
|
2023-10-19 21:12:19 +08:00
|
|
|
Some(sym::loongarch_target_feature) => rust_features.loongarch_target_feature,
|
2022-12-08 03:53:35 +00:00
|
|
|
Some(name) => bug!("unknown target feature gate {}", name),
|
|
|
|
None => true,
|
|
|
|
};
|
|
|
|
if !allowed {
|
|
|
|
feature_err(
|
|
|
|
&tcx.sess.parse_sess,
|
|
|
|
feature_gate.unwrap(),
|
|
|
|
item.span(),
|
2023-07-25 23:04:01 +02:00
|
|
|
format!("the target feature `{feature}` is currently unstable"),
|
2022-12-08 03:53:35 +00:00
|
|
|
)
|
|
|
|
.emit();
|
|
|
|
}
|
|
|
|
Some(Symbol::intern(feature))
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Computes the set of target features used in a function for the purposes of
|
|
|
|
/// inline assembly.
|
2023-02-21 15:15:16 +01:00
|
|
|
fn asm_target_features(tcx: TyCtxt<'_>, did: DefId) -> &FxIndexSet<Symbol> {
|
2022-12-08 03:53:35 +00:00
|
|
|
let mut target_features = tcx.sess.unstable_target_features.clone();
|
|
|
|
if tcx.def_kind(did).has_codegen_attrs() {
|
|
|
|
let attrs = tcx.codegen_fn_attrs(did);
|
|
|
|
target_features.extend(&attrs.target_features);
|
|
|
|
match attrs.instruction_set {
|
|
|
|
None => {}
|
|
|
|
Some(InstructionSetAttr::ArmA32) => {
|
|
|
|
target_features.remove(&sym::thumb_mode);
|
|
|
|
}
|
|
|
|
Some(InstructionSetAttr::ArmT32) => {
|
|
|
|
target_features.insert(sym::thumb_mode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tcx.arena.alloc(target_features)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Checks the function annotated with `#[target_feature]` is not a safe
|
|
|
|
/// trait method implementation, reporting an error if it is.
|
|
|
|
pub fn check_target_feature_trait_unsafe(tcx: TyCtxt<'_>, id: LocalDefId, attr_span: Span) {
|
2023-02-12 20:20:45 +00:00
|
|
|
if let DefKind::AssocFn = tcx.def_kind(id) {
|
|
|
|
let parent_id = tcx.local_parent(id);
|
2023-03-10 13:50:51 +01:00
|
|
|
if let DefKind::Trait | DefKind::Impl { of_trait: true } = tcx.def_kind(parent_id) {
|
2023-04-10 16:04:14 +01:00
|
|
|
tcx.sess.emit_err(errors::TargetFeatureSafeTrait {
|
|
|
|
span: attr_span,
|
|
|
|
def: tcx.def_span(id),
|
|
|
|
});
|
2022-12-08 03:53:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn provide(providers: &mut Providers) {
|
|
|
|
*providers = Providers {
|
|
|
|
supported_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
|
2023-12-12 21:47:20 +01:00
|
|
|
rustc_target::target_features::all_known_features()
|
|
|
|
.map(|(a, b)| (a.to_string(), b.as_feature_name()))
|
|
|
|
.collect()
|
2022-12-08 03:53:35 +00:00
|
|
|
} else {
|
2023-12-12 21:47:20 +01:00
|
|
|
tcx.sess
|
|
|
|
.target
|
|
|
|
.supported_target_features()
|
2022-12-08 03:53:35 +00:00
|
|
|
.iter()
|
2023-11-12 12:41:22 +01:00
|
|
|
.map(|&(a, b)| (a.to_string(), b.as_feature_name()))
|
2022-12-08 03:53:35 +00:00
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
},
|
|
|
|
asm_target_features,
|
|
|
|
..*providers
|
|
|
|
}
|
2020-10-09 19:35:17 +02:00
|
|
|
}
|