提交 bd1d1866 编写于 作者: L lcnr

store `codegen_fn_attrs` in crate metadata

上级 e013f9e0
...@@ -129,6 +129,7 @@ fn into_args(self) -> (DefId, SimplifiedType) { ...@@ -129,6 +129,7 @@ fn into_args(self) -> (DefId, SimplifiedType) {
type_of => { table } type_of => { table }
variances_of => { table } variances_of => { table }
fn_sig => { table } fn_sig => { table }
codegen_fn_attrs => { table }
impl_trait_ref => { table } impl_trait_ref => { table }
const_param_default => { table } const_param_default => { table }
thir_abstract_const => { table } thir_abstract_const => { table }
......
...@@ -1007,6 +1007,7 @@ fn encode_def_ids(&mut self) { ...@@ -1007,6 +1007,7 @@ fn encode_def_ids(&mut self) {
record!(self.tables.def_span[def_id] <- tcx.def_span(def_id)); record!(self.tables.def_span[def_id] <- tcx.def_span(def_id));
self.encode_attrs(def_id); self.encode_attrs(def_id);
record!(self.tables.expn_that_defined[def_id] <- self.tcx.expn_that_defined(def_id)); record!(self.tables.expn_that_defined[def_id] <- self.tcx.expn_that_defined(def_id));
record!(self.tables.codegen_fn_attrs[def_id] <- self.tcx.codegen_fn_attrs(def_id));
if should_encode_visibility(def_kind) { if should_encode_visibility(def_kind) {
record!(self.tables.visibility[def_id] <- self.tcx.visibility(def_id)); record!(self.tables.visibility[def_id] <- self.tcx.visibility(def_id));
} }
......
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
use rustc_hir::lang_items; use rustc_hir::lang_items;
use rustc_index::{bit_set::FiniteBitSet, vec::IndexVec}; use rustc_index::{bit_set::FiniteBitSet, vec::IndexVec};
use rustc_middle::metadata::ModChild; use rustc_middle::metadata::ModChild;
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrs;
use rustc_middle::middle::exported_symbols::{ExportedSymbol, SymbolExportInfo}; use rustc_middle::middle::exported_symbols::{ExportedSymbol, SymbolExportInfo};
use rustc_middle::mir; use rustc_middle::mir;
use rustc_middle::thir; use rustc_middle::thir;
...@@ -329,6 +330,7 @@ fn encode(&self, buf: &mut Encoder) -> LazyTables<'tcx> { ...@@ -329,6 +330,7 @@ fn encode(&self, buf: &mut Encoder) -> LazyTables<'tcx> {
type_of: Table<DefIndex, Lazy!(Ty<'tcx>)>, type_of: Table<DefIndex, Lazy!(Ty<'tcx>)>,
variances_of: Table<DefIndex, Lazy<[ty::Variance]>>, variances_of: Table<DefIndex, Lazy<[ty::Variance]>>,
fn_sig: Table<DefIndex, Lazy!(ty::PolyFnSig<'tcx>)>, fn_sig: Table<DefIndex, Lazy!(ty::PolyFnSig<'tcx>)>,
codegen_fn_attrs: Table<DefIndex, Lazy!(CodegenFnAttrs)>,
impl_trait_ref: Table<DefIndex, Lazy!(ty::TraitRef<'tcx>)>, impl_trait_ref: Table<DefIndex, Lazy!(ty::TraitRef<'tcx>)>,
const_param_default: Table<DefIndex, Lazy<rustc_middle::ty::Const<'tcx>>>, const_param_default: Table<DefIndex, Lazy<rustc_middle::ty::Const<'tcx>>>,
optimized_mir: Table<DefIndex, Lazy!(mir::Body<'tcx>)>, optimized_mir: Table<DefIndex, Lazy!(mir::Body<'tcx>)>,
......
...@@ -1081,6 +1081,7 @@ ...@@ -1081,6 +1081,7 @@
desc { |tcx| "computing codegen attributes of `{}`", tcx.def_path_str(def_id) } desc { |tcx| "computing codegen attributes of `{}`", tcx.def_path_str(def_id) }
storage(ArenaCacheSelector<'tcx>) storage(ArenaCacheSelector<'tcx>)
cache_on_disk_if { true } cache_on_disk_if { true }
separate_provide_extern
} }
query asm_target_features(def_id: DefId) -> &'tcx FxHashSet<Symbol> { query asm_target_features(def_id: DefId) -> &'tcx FxHashSet<Symbol> {
......
...@@ -2610,7 +2610,6 @@ fn generator_kind(tcx: TyCtxt<'_>, def_id: DefId) -> Option<hir::GeneratorKind> ...@@ -2610,7 +2610,6 @@ fn generator_kind(tcx: TyCtxt<'_>, def_id: DefId) -> Option<hir::GeneratorKind>
fn from_target_feature( fn from_target_feature(
tcx: TyCtxt<'_>, tcx: TyCtxt<'_>,
id: DefId,
attr: &ast::Attribute, attr: &ast::Attribute,
supported_target_features: &FxHashMap<String, Option<Symbol>>, supported_target_features: &FxHashMap<String, Option<Symbol>>,
target_features: &mut Vec<Symbol>, target_features: &mut Vec<Symbol>,
...@@ -2679,7 +2678,7 @@ fn from_target_feature( ...@@ -2679,7 +2678,7 @@ fn from_target_feature(
Some(name) => bug!("unknown target feature gate {}", name), Some(name) => bug!("unknown target feature gate {}", name),
None => true, None => true,
}; };
if !allowed && id.is_local() { if !allowed {
feature_err( feature_err(
&tcx.sess.parse_sess, &tcx.sess.parse_sess,
feature_gate.unwrap(), feature_gate.unwrap(),
...@@ -2693,7 +2692,7 @@ fn from_target_feature( ...@@ -2693,7 +2692,7 @@ fn from_target_feature(
} }
} }
fn linkage_by_name(tcx: TyCtxt<'_>, def_id: DefId, name: &str) -> Linkage { fn linkage_by_name(tcx: TyCtxt<'_>, def_id: LocalDefId, name: &str) -> Linkage {
use rustc_middle::mir::mono::Linkage::*; use rustc_middle::mir::mono::Linkage::*;
// Use the names from src/llvm/docs/LangRef.rst here. Most types are only // Use the names from src/llvm/docs/LangRef.rst here. Most types are only
...@@ -2716,28 +2715,20 @@ fn linkage_by_name(tcx: TyCtxt<'_>, def_id: DefId, name: &str) -> Linkage { ...@@ -2716,28 +2715,20 @@ fn linkage_by_name(tcx: TyCtxt<'_>, def_id: DefId, name: &str) -> Linkage {
"private" => Private, "private" => Private,
"weak" => WeakAny, "weak" => WeakAny,
"weak_odr" => WeakODR, "weak_odr" => WeakODR,
_ => { _ => tcx.sess.span_fatal(tcx.def_span(def_id), "invalid linkage specified"),
let span = tcx.hir().span_if_local(def_id);
if let Some(span) = span {
tcx.sess.span_fatal(span, "invalid linkage specified")
} else {
tcx.sess.fatal(&format!("invalid linkage specified: {}", name))
}
}
} }
} }
fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: DefId) -> CodegenFnAttrs {
fn codegen_fn_attrs(tcx: TyCtxt<'_>, id: DefId) -> CodegenFnAttrs { let did = did.expect_local();
let attrs = tcx.get_attrs(id); let attrs = tcx.hir().attrs(tcx.hir().local_def_id_to_hir_id(did));
let mut codegen_fn_attrs = CodegenFnAttrs::new(); let mut codegen_fn_attrs = CodegenFnAttrs::new();
if tcx.should_inherit_track_caller(id) { if tcx.should_inherit_track_caller(did) {
codegen_fn_attrs.flags |= CodegenFnAttrFlags::TRACK_CALLER; codegen_fn_attrs.flags |= CodegenFnAttrFlags::TRACK_CALLER;
} }
// With -Z panic-in-drop=abort, drop_in_place never unwinds. // With -Z panic-in-drop=abort, drop_in_place never unwinds.
if tcx.sess.opts.debugging_opts.panic_in_drop == PanicStrategy::Abort { if tcx.sess.opts.debugging_opts.panic_in_drop == PanicStrategy::Abort {
if Some(id) == tcx.lang_items().drop_in_place_fn() { if Some(did.to_def_id()) == tcx.lang_items().drop_in_place_fn() {
codegen_fn_attrs.flags |= CodegenFnAttrFlags::NEVER_UNWIND; codegen_fn_attrs.flags |= CodegenFnAttrFlags::NEVER_UNWIND;
} }
} }
...@@ -2745,7 +2736,7 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, id: DefId) -> CodegenFnAttrs { ...@@ -2745,7 +2736,7 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, id: DefId) -> CodegenFnAttrs {
// The panic_no_unwind function called by TerminatorKind::Abort will never // The panic_no_unwind function called by TerminatorKind::Abort will never
// unwind. If the panic handler that it invokes unwind then it will simply // unwind. If the panic handler that it invokes unwind then it will simply
// call the panic handler again. // call the panic handler again.
if Some(id) == tcx.lang_items().panic_no_unwind() { if Some(did.to_def_id()) == tcx.lang_items().panic_no_unwind() {
codegen_fn_attrs.flags |= CodegenFnAttrFlags::NEVER_UNWIND; codegen_fn_attrs.flags |= CodegenFnAttrFlags::NEVER_UNWIND;
} }
...@@ -2760,7 +2751,7 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, id: DefId) -> CodegenFnAttrs { ...@@ -2760,7 +2751,7 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, id: DefId) -> CodegenFnAttrs {
} else if attr.has_name(sym::rustc_allocator) { } else if attr.has_name(sym::rustc_allocator) {
codegen_fn_attrs.flags |= CodegenFnAttrFlags::ALLOCATOR; codegen_fn_attrs.flags |= CodegenFnAttrFlags::ALLOCATOR;
} else if attr.has_name(sym::ffi_returns_twice) { } else if attr.has_name(sym::ffi_returns_twice) {
if tcx.is_foreign_item(id) { if tcx.is_foreign_item(did) {
codegen_fn_attrs.flags |= CodegenFnAttrFlags::FFI_RETURNS_TWICE; codegen_fn_attrs.flags |= CodegenFnAttrFlags::FFI_RETURNS_TWICE;
} else { } else {
// `#[ffi_returns_twice]` is only allowed `extern fn`s. // `#[ffi_returns_twice]` is only allowed `extern fn`s.
...@@ -2773,7 +2764,7 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, id: DefId) -> CodegenFnAttrs { ...@@ -2773,7 +2764,7 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, id: DefId) -> CodegenFnAttrs {
.emit(); .emit();
} }
} else if attr.has_name(sym::ffi_pure) { } else if attr.has_name(sym::ffi_pure) {
if tcx.is_foreign_item(id) { if tcx.is_foreign_item(did) {
if attrs.iter().any(|a| a.has_name(sym::ffi_const)) { if attrs.iter().any(|a| a.has_name(sym::ffi_const)) {
// `#[ffi_const]` functions cannot be `#[ffi_pure]` // `#[ffi_const]` functions cannot be `#[ffi_pure]`
struct_span_err!( struct_span_err!(
...@@ -2797,7 +2788,7 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, id: DefId) -> CodegenFnAttrs { ...@@ -2797,7 +2788,7 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, id: DefId) -> CodegenFnAttrs {
.emit(); .emit();
} }
} else if attr.has_name(sym::ffi_const) { } else if attr.has_name(sym::ffi_const) {
if tcx.is_foreign_item(id) { if tcx.is_foreign_item(did) {
codegen_fn_attrs.flags |= CodegenFnAttrFlags::FFI_CONST; codegen_fn_attrs.flags |= CodegenFnAttrFlags::FFI_CONST;
} else { } else {
// `#[ffi_const]` is only allowed on foreign functions // `#[ffi_const]` is only allowed on foreign functions
...@@ -2857,7 +2848,7 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, id: DefId) -> CodegenFnAttrs { ...@@ -2857,7 +2848,7 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, id: DefId) -> CodegenFnAttrs {
None => codegen_fn_attrs.flags |= CodegenFnAttrFlags::USED, None => codegen_fn_attrs.flags |= CodegenFnAttrFlags::USED,
} }
} else if attr.has_name(sym::cmse_nonsecure_entry) { } else if attr.has_name(sym::cmse_nonsecure_entry) {
if !matches!(tcx.fn_sig(id).abi(), abi::Abi::C { .. }) { if !matches!(tcx.fn_sig(did).abi(), abi::Abi::C { .. }) {
struct_span_err!( struct_span_err!(
tcx.sess, tcx.sess,
attr.span, attr.span,
...@@ -2874,11 +2865,11 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, id: DefId) -> CodegenFnAttrs { ...@@ -2874,11 +2865,11 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, id: DefId) -> CodegenFnAttrs {
} else if attr.has_name(sym::thread_local) { } else if attr.has_name(sym::thread_local) {
codegen_fn_attrs.flags |= CodegenFnAttrFlags::THREAD_LOCAL; codegen_fn_attrs.flags |= CodegenFnAttrFlags::THREAD_LOCAL;
} else if attr.has_name(sym::track_caller) { } else if attr.has_name(sym::track_caller) {
if !tcx.is_closure(id) && tcx.fn_sig(id).abi() != abi::Abi::Rust { if !tcx.is_closure(did.to_def_id()) && tcx.fn_sig(did).abi() != abi::Abi::Rust {
struct_span_err!(tcx.sess, attr.span, E0737, "`#[track_caller]` requires Rust ABI") struct_span_err!(tcx.sess, attr.span, E0737, "`#[track_caller]` requires Rust ABI")
.emit(); .emit();
} }
if tcx.is_closure(id) && !tcx.features().closure_track_caller { if tcx.is_closure(did.to_def_id()) && !tcx.features().closure_track_caller {
feature_err( feature_err(
&tcx.sess.parse_sess, &tcx.sess.parse_sess,
sym::closure_track_caller, sym::closure_track_caller,
...@@ -2904,7 +2895,9 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, id: DefId) -> CodegenFnAttrs { ...@@ -2904,7 +2895,9 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, id: DefId) -> CodegenFnAttrs {
codegen_fn_attrs.export_name = Some(s); codegen_fn_attrs.export_name = Some(s);
} }
} else if attr.has_name(sym::target_feature) { } else if attr.has_name(sym::target_feature) {
if !tcx.is_closure(id) && tcx.fn_sig(id).unsafety() == hir::Unsafety::Normal { if !tcx.is_closure(did.to_def_id())
&& tcx.fn_sig(did).unsafety() == hir::Unsafety::Normal
{
if tcx.sess.target.is_like_wasm || tcx.sess.opts.actually_rustdoc { if tcx.sess.target.is_like_wasm || tcx.sess.opts.actually_rustdoc {
// The `#[target_feature]` attribute is allowed on // The `#[target_feature]` attribute is allowed on
// WebAssembly targets on all functions, including safe // WebAssembly targets on all functions, including safe
...@@ -2930,22 +2923,21 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, id: DefId) -> CodegenFnAttrs { ...@@ -2930,22 +2923,21 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, id: DefId) -> CodegenFnAttrs {
attr.span, attr.span,
"`#[target_feature(..)]` can only be applied to `unsafe` functions", "`#[target_feature(..)]` can only be applied to `unsafe` functions",
); );
err.span_label(tcx.def_span(id), "not an `unsafe` function"); err.span_label(tcx.def_span(did), "not an `unsafe` function");
err.emit(); err.emit();
} else if let Some(local_id) = id.as_local() { } else {
check_target_feature_trait_unsafe(tcx, local_id, attr.span); check_target_feature_trait_unsafe(tcx, did, attr.span);
} }
} }
from_target_feature( from_target_feature(
tcx, tcx,
id,
attr, attr,
supported_target_features, supported_target_features,
&mut codegen_fn_attrs.target_features, &mut codegen_fn_attrs.target_features,
); );
} else if attr.has_name(sym::linkage) { } else if attr.has_name(sym::linkage) {
if let Some(val) = attr.value_str() { if let Some(val) = attr.value_str() {
codegen_fn_attrs.linkage = Some(linkage_by_name(tcx, id, val.as_str())); codegen_fn_attrs.linkage = Some(linkage_by_name(tcx, did, val.as_str()));
} }
} else if attr.has_name(sym::link_section) { } else if attr.has_name(sym::link_section) {
if let Some(val) = attr.value_str() { if let Some(val) = attr.value_str() {
...@@ -3161,8 +3153,8 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, id: DefId) -> CodegenFnAttrs { ...@@ -3161,8 +3153,8 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, id: DefId) -> CodegenFnAttrs {
}); });
// #73631: closures inherit `#[target_feature]` annotations // #73631: closures inherit `#[target_feature]` annotations
if tcx.features().target_feature_11 && tcx.is_closure(id) { if tcx.features().target_feature_11 && tcx.is_closure(did.to_def_id()) {
let owner_id = tcx.parent(id); let owner_id = tcx.parent(did.to_def_id());
codegen_fn_attrs codegen_fn_attrs
.target_features .target_features
.extend(tcx.codegen_fn_attrs(owner_id).target_features.iter().copied()) .extend(tcx.codegen_fn_attrs(owner_id).target_features.iter().copied())
...@@ -3187,7 +3179,7 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, id: DefId) -> CodegenFnAttrs { ...@@ -3187,7 +3179,7 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, id: DefId) -> CodegenFnAttrs {
if !codegen_fn_attrs.no_sanitize.is_empty() { if !codegen_fn_attrs.no_sanitize.is_empty() {
if codegen_fn_attrs.inline == InlineAttr::Always { if codegen_fn_attrs.inline == InlineAttr::Always {
if let (Some(no_sanitize_span), Some(inline_span)) = (no_sanitize_span, inline_span) { if let (Some(no_sanitize_span), Some(inline_span)) = (no_sanitize_span, inline_span) {
let hir_id = tcx.hir().local_def_id_to_hir_id(id.expect_local()); let hir_id = tcx.hir().local_def_id_to_hir_id(did);
tcx.struct_span_lint_hir( tcx.struct_span_lint_hir(
lint::builtin::INLINE_NO_SANITIZE, lint::builtin::INLINE_NO_SANITIZE,
hir_id, hir_id,
...@@ -3207,7 +3199,7 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, id: DefId) -> CodegenFnAttrs { ...@@ -3207,7 +3199,7 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, id: DefId) -> CodegenFnAttrs {
// strippable by the linker. // strippable by the linker.
// //
// Additionally weak lang items have predetermined symbol names. // Additionally weak lang items have predetermined symbol names.
if tcx.is_weak_lang_item(id) { if tcx.is_weak_lang_item(did.to_def_id()) {
codegen_fn_attrs.flags |= CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL; codegen_fn_attrs.flags |= CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL;
} }
if let Some(name) = weak_lang_items::link_name(attrs) { if let Some(name) = weak_lang_items::link_name(attrs) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册