Cleanup weak lang items

This commit is contained in:
Cameron Steffen 2022-10-26 16:18:59 -05:00
parent 1e349fb0dd
commit 6621279a75
6 changed files with 38 additions and 63 deletions

View File

@ -22,7 +22,6 @@ use rustc_data_structures::sync::ParallelIterator;
use rustc_hir as hir; use rustc_hir as hir;
use rustc_hir::def_id::{DefId, LOCAL_CRATE}; use rustc_hir::def_id::{DefId, LOCAL_CRATE};
use rustc_hir::lang_items::LangItem; use rustc_hir::lang_items::LangItem;
use rustc_hir::weak_lang_items::WEAK_ITEMS_SYMBOLS;
use rustc_index::vec::Idx; use rustc_index::vec::Idx;
use rustc_metadata::EncodedMetadata; use rustc_metadata::EncodedMetadata;
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrs; use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrs;
@ -887,14 +886,14 @@ impl CrateInfo {
// by the compiler, but that's ok because all this stuff is unstable anyway. // by the compiler, but that's ok because all this stuff is unstable anyway.
let target = &tcx.sess.target; let target = &tcx.sess.target;
if !are_upstream_rust_objects_already_included(tcx.sess) { if !are_upstream_rust_objects_already_included(tcx.sess) {
let missing_weak_lang_items: FxHashSet<&Symbol> = info let missing_weak_lang_items: FxHashSet<Symbol> = info
.used_crates .used_crates
.iter() .iter()
.flat_map(|cnum| { .flat_map(|&cnum| tcx.missing_lang_items(cnum))
tcx.missing_lang_items(*cnum) .filter(|l| l.is_weak())
.iter() .filter_map(|&l| {
.filter(|l| lang_items::required(tcx, **l)) let name = l.link_name()?;
.filter_map(|item| WEAK_ITEMS_SYMBOLS.get(item)) lang_items::required(tcx, l).then_some(name)
}) })
.collect(); .collect();
let prefix = if target.is_like_windows && target.arch == "x86" { "_" } else { "" }; let prefix = if target.is_like_windows && target.arch == "x86" { "_" } else { "" };

View File

@ -5,7 +5,6 @@
#![feature(associated_type_defaults)] #![feature(associated_type_defaults)]
#![feature(closure_track_caller)] #![feature(closure_track_caller)]
#![feature(const_btree_len)] #![feature(const_btree_len)]
#![feature(once_cell)]
#![feature(min_specialization)] #![feature(min_specialization)]
#![feature(never_type)] #![feature(never_type)]
#![feature(rustc_attrs)] #![feature(rustc_attrs)]

View File

@ -1,53 +1,31 @@
//! Validity checking for weak lang items //! Validity checking for weak lang items
use crate::def_id::DefId; use crate::LangItem;
use crate::{lang_items, LangItem, LanguageItems};
use rustc_ast as ast;
use rustc_data_structures::fx::FxIndexMap;
use rustc_span::symbol::{sym, Symbol}; use rustc_span::symbol::{sym, Symbol};
use std::sync::LazyLock;
macro_rules! weak_lang_items { macro_rules! weak_lang_items {
($($name:ident, $item:ident, $sym:ident;)*) => ( ($($item:ident, $sym:ident;)*) => {
pub static WEAK_LANG_ITEMS: &[LangItem] = &[$(LangItem::$item,)*];
pub static WEAK_ITEMS_REFS: LazyLock<FxIndexMap<Symbol, LangItem>> = LazyLock::new(|| { impl LangItem {
let mut map = FxIndexMap::default(); pub fn is_weak(self) -> bool {
$(map.insert(sym::$name, LangItem::$item);)* matches!(self, $(LangItem::$item)|*)
map }
});
pub static WEAK_ITEMS_SYMBOLS: LazyLock<FxIndexMap<LangItem, Symbol>> = LazyLock::new(|| { pub fn link_name(self) -> Option<Symbol> {
let mut map = FxIndexMap::default(); match self {
$(map.insert(LangItem::$item, sym::$sym);)* $( LangItem::$item => Some(sym::$sym),)*
map _ => None,
}); }
}
pub fn link_name(attrs: &[ast::Attribute]) -> Option<Symbol>
{
lang_items::extract(attrs).and_then(|(name, _)| {
$(if name == sym::$name {
Some(sym::$sym)
} else)* {
None
} }
})
}
impl LanguageItems {
pub fn is_weak_lang_item(&self, item_def_id: DefId) -> bool {
let did = Some(item_def_id);
$(self.$name() == did)||*
} }
} }
) }
weak_lang_items! { weak_lang_items! {
panic_impl, PanicImpl, rust_begin_unwind; PanicImpl, rust_begin_unwind;
eh_personality, EhPersonality, rust_eh_personality; EhPersonality, rust_eh_personality;
eh_catch_typeinfo, EhCatchTypeinfo, rust_eh_catch_typeinfo; EhCatchTypeinfo, rust_eh_catch_typeinfo;
oom, Oom, rust_oom; Oom, rust_oom;
} }

View File

@ -27,8 +27,8 @@ use rustc_hir as hir;
use rustc_hir::def::CtorKind; use rustc_hir::def::CtorKind;
use rustc_hir::def_id::{DefId, LocalDefId, LOCAL_CRATE}; use rustc_hir::def_id::{DefId, LocalDefId, LOCAL_CRATE};
use rustc_hir::intravisit::{self, Visitor}; use rustc_hir::intravisit::{self, Visitor};
use rustc_hir::weak_lang_items; use rustc_hir::weak_lang_items::WEAK_LANG_ITEMS;
use rustc_hir::{GenericParamKind, Node}; use rustc_hir::{lang_items, GenericParamKind, LangItem, Node};
use rustc_middle::hir::nested_filter; use rustc_middle::hir::nested_filter;
use rustc_middle::middle::codegen_fn_attrs::{CodegenFnAttrFlags, CodegenFnAttrs}; use rustc_middle::middle::codegen_fn_attrs::{CodegenFnAttrFlags, CodegenFnAttrs};
use rustc_middle::mir::mono::Linkage; use rustc_middle::mir::mono::Linkage;
@ -2104,12 +2104,15 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: 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(did.to_def_id()) { if WEAK_LANG_ITEMS.iter().any(|&l| tcx.lang_items().get(l) == Some(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, _)) = lang_items::extract(attrs)
codegen_fn_attrs.export_name = Some(name); && let Some(lang_item) = LangItem::from_name(name)
codegen_fn_attrs.link_name = Some(name); && let Some(link_name) = lang_item.link_name()
{
codegen_fn_attrs.export_name = Some(link_name);
codegen_fn_attrs.link_name = Some(link_name);
} }
check_link_name_xor_ordinal(tcx, &codegen_fn_attrs, link_ordinal_span); check_link_name_xor_ordinal(tcx, &codegen_fn_attrs, link_ordinal_span);

View File

@ -36,10 +36,6 @@ impl<'tcx> TyCtxt<'tcx> {
_ => None, _ => None,
} }
} }
pub fn is_weak_lang_item(self, item_def_id: DefId) -> bool {
self.lang_items().is_weak_lang_item(item_def_id)
}
} }
/// Returns `true` if the specified `lang_item` must be present for this /// Returns `true` if the specified `lang_item` must be present for this

View File

@ -2,7 +2,7 @@
use rustc_data_structures::fx::FxHashSet; use rustc_data_structures::fx::FxHashSet;
use rustc_hir::lang_items::{self, LangItem}; use rustc_hir::lang_items::{self, LangItem};
use rustc_hir::weak_lang_items::WEAK_ITEMS_REFS; use rustc_hir::weak_lang_items::WEAK_LANG_ITEMS;
use rustc_middle::middle::lang_items::required; use rustc_middle::middle::lang_items::required;
use rustc_middle::ty::TyCtxt; use rustc_middle::ty::TyCtxt;
use rustc_session::config::CrateType; use rustc_session::config::CrateType;
@ -29,8 +29,8 @@ pub fn check_crate<'tcx>(tcx: TyCtxt<'tcx>, items: &mut lang_items::LanguageItem
for id in crate_items.foreign_items() { for id in crate_items.foreign_items() {
let attrs = tcx.hir().attrs(id.hir_id()); let attrs = tcx.hir().attrs(id.hir_id());
if let Some((lang_item, _)) = lang_items::extract(attrs) { if let Some((lang_item, _)) = lang_items::extract(attrs) {
if let Some(&item) = WEAK_ITEMS_REFS.get(&lang_item) { if let Some(item) = LangItem::from_name(lang_item) && item.is_weak() {
if items.require(item).is_err() { if items.get(item).is_none() {
items.missing.push(item); items.missing.push(item);
} }
} else { } else {
@ -65,8 +65,8 @@ fn verify<'tcx>(tcx: TyCtxt<'tcx>, items: &lang_items::LanguageItems) {
} }
} }
for (name, &item) in WEAK_ITEMS_REFS.iter() { for &item in WEAK_LANG_ITEMS.iter() {
if missing.contains(&item) && required(tcx, item) && items.require(item).is_err() { if missing.contains(&item) && required(tcx, item) && items.get(item).is_none() {
if item == LangItem::PanicImpl { if item == LangItem::PanicImpl {
tcx.sess.emit_err(MissingPanicHandler); tcx.sess.emit_err(MissingPanicHandler);
} else if item == LangItem::Oom { } else if item == LangItem::Oom {
@ -75,7 +75,7 @@ fn verify<'tcx>(tcx: TyCtxt<'tcx>, items: &lang_items::LanguageItems) {
tcx.sess.emit_note(MissingAllocErrorHandler); tcx.sess.emit_note(MissingAllocErrorHandler);
} }
} else { } else {
tcx.sess.emit_err(MissingLangItem { name: *name }); tcx.sess.emit_err(MissingLangItem { name: item.name() });
} }
} }
} }