diff --git a/compiler/rustc_codegen_ssa/src/base.rs b/compiler/rustc_codegen_ssa/src/base.rs index 84b89cd71a6..a92087305b8 100644 --- a/compiler/rustc_codegen_ssa/src/base.rs +++ b/compiler/rustc_codegen_ssa/src/base.rs @@ -22,7 +22,6 @@ use rustc_data_structures::sync::ParallelIterator; use rustc_hir as hir; use rustc_hir::def_id::{DefId, LOCAL_CRATE}; use rustc_hir::lang_items::LangItem; -use rustc_hir::weak_lang_items::WEAK_ITEMS_SYMBOLS; use rustc_index::vec::Idx; use rustc_metadata::EncodedMetadata; 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. let target = &tcx.sess.target; if !are_upstream_rust_objects_already_included(tcx.sess) { - let missing_weak_lang_items: FxHashSet<&Symbol> = info + let missing_weak_lang_items: FxHashSet = info .used_crates .iter() - .flat_map(|cnum| { - tcx.missing_lang_items(*cnum) - .iter() - .filter(|l| lang_items::required(tcx, **l)) - .filter_map(|item| WEAK_ITEMS_SYMBOLS.get(item)) + .flat_map(|&cnum| tcx.missing_lang_items(cnum)) + .filter(|l| l.is_weak()) + .filter_map(|&l| { + let name = l.link_name()?; + lang_items::required(tcx, l).then_some(name) }) .collect(); let prefix = if target.is_like_windows && target.arch == "x86" { "_" } else { "" }; diff --git a/compiler/rustc_hir/src/lib.rs b/compiler/rustc_hir/src/lib.rs index d54d8e84aaf..1c55cd8fee8 100644 --- a/compiler/rustc_hir/src/lib.rs +++ b/compiler/rustc_hir/src/lib.rs @@ -5,7 +5,6 @@ #![feature(associated_type_defaults)] #![feature(closure_track_caller)] #![feature(const_btree_len)] -#![feature(once_cell)] #![feature(min_specialization)] #![feature(never_type)] #![feature(rustc_attrs)] diff --git a/compiler/rustc_hir/src/weak_lang_items.rs b/compiler/rustc_hir/src/weak_lang_items.rs index da9c9c1216e..d8e5dd4ffdc 100644 --- a/compiler/rustc_hir/src/weak_lang_items.rs +++ b/compiler/rustc_hir/src/weak_lang_items.rs @@ -1,53 +1,31 @@ //! Validity checking for weak lang items -use crate::def_id::DefId; -use crate::{lang_items, LangItem, LanguageItems}; +use crate::LangItem; -use rustc_ast as ast; -use rustc_data_structures::fx::FxIndexMap; use rustc_span::symbol::{sym, Symbol}; -use std::sync::LazyLock; - 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> = LazyLock::new(|| { - let mut map = FxIndexMap::default(); - $(map.insert(sym::$name, LangItem::$item);)* - map -}); + impl LangItem { + pub fn is_weak(self) -> bool { + matches!(self, $(LangItem::$item)|*) + } -pub static WEAK_ITEMS_SYMBOLS: LazyLock> = LazyLock::new(|| { - let mut map = FxIndexMap::default(); - $(map.insert(LangItem::$item, sym::$sym);)* - map -}); - -pub fn link_name(attrs: &[ast::Attribute]) -> Option -{ - lang_items::extract(attrs).and_then(|(name, _)| { - $(if name == sym::$name { - Some(sym::$sym) - } else)* { - None + pub fn link_name(self) -> Option { + match self { + $( LangItem::$item => Some(sym::$sym),)* + _ => 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! { - panic_impl, PanicImpl, rust_begin_unwind; - eh_personality, EhPersonality, rust_eh_personality; - eh_catch_typeinfo, EhCatchTypeinfo, rust_eh_catch_typeinfo; - oom, Oom, rust_oom; + PanicImpl, rust_begin_unwind; + EhPersonality, rust_eh_personality; + EhCatchTypeinfo, rust_eh_catch_typeinfo; + Oom, rust_oom; } diff --git a/compiler/rustc_hir_analysis/src/collect.rs b/compiler/rustc_hir_analysis/src/collect.rs index 346d2e2fc4b..65a1d61e87c 100644 --- a/compiler/rustc_hir_analysis/src/collect.rs +++ b/compiler/rustc_hir_analysis/src/collect.rs @@ -27,8 +27,8 @@ use rustc_hir as hir; use rustc_hir::def::CtorKind; use rustc_hir::def_id::{DefId, LocalDefId, LOCAL_CRATE}; use rustc_hir::intravisit::{self, Visitor}; -use rustc_hir::weak_lang_items; -use rustc_hir::{GenericParamKind, Node}; +use rustc_hir::weak_lang_items::WEAK_LANG_ITEMS; +use rustc_hir::{lang_items, GenericParamKind, LangItem, Node}; use rustc_middle::hir::nested_filter; use rustc_middle::middle::codegen_fn_attrs::{CodegenFnAttrFlags, CodegenFnAttrs}; use rustc_middle::mir::mono::Linkage; @@ -2104,12 +2104,15 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: DefId) -> CodegenFnAttrs { // strippable by the linker. // // 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; } - if let Some(name) = weak_lang_items::link_name(attrs) { - codegen_fn_attrs.export_name = Some(name); - codegen_fn_attrs.link_name = Some(name); + if let Some((name, _)) = lang_items::extract(attrs) + && let Some(lang_item) = LangItem::from_name(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); diff --git a/compiler/rustc_middle/src/middle/lang_items.rs b/compiler/rustc_middle/src/middle/lang_items.rs index 31c20fa14aa..dd4332d0db6 100644 --- a/compiler/rustc_middle/src/middle/lang_items.rs +++ b/compiler/rustc_middle/src/middle/lang_items.rs @@ -36,10 +36,6 @@ impl<'tcx> TyCtxt<'tcx> { _ => 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 diff --git a/compiler/rustc_passes/src/weak_lang_items.rs b/compiler/rustc_passes/src/weak_lang_items.rs index 959ee600c07..94d6a405b53 100644 --- a/compiler/rustc_passes/src/weak_lang_items.rs +++ b/compiler/rustc_passes/src/weak_lang_items.rs @@ -2,7 +2,7 @@ use rustc_data_structures::fx::FxHashSet; 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::ty::TyCtxt; 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() { let attrs = tcx.hir().attrs(id.hir_id()); if let Some((lang_item, _)) = lang_items::extract(attrs) { - if let Some(&item) = WEAK_ITEMS_REFS.get(&lang_item) { - if items.require(item).is_err() { + if let Some(item) = LangItem::from_name(lang_item) && item.is_weak() { + if items.get(item).is_none() { items.missing.push(item); } } else { @@ -65,8 +65,8 @@ fn verify<'tcx>(tcx: TyCtxt<'tcx>, items: &lang_items::LanguageItems) { } } - for (name, &item) in WEAK_ITEMS_REFS.iter() { - if missing.contains(&item) && required(tcx, item) && items.require(item).is_err() { + for &item in WEAK_LANG_ITEMS.iter() { + if missing.contains(&item) && required(tcx, item) && items.get(item).is_none() { if item == LangItem::PanicImpl { tcx.sess.emit_err(MissingPanicHandler); } else if item == LangItem::Oom { @@ -75,7 +75,7 @@ fn verify<'tcx>(tcx: TyCtxt<'tcx>, items: &lang_items::LanguageItems) { tcx.sess.emit_note(MissingAllocErrorHandler); } } else { - tcx.sess.emit_err(MissingLangItem { name: *name }); + tcx.sess.emit_err(MissingLangItem { name: item.name() }); } } }