Skip to content

Commit 6621279

Browse files
committed
Cleanup weak lang items
1 parent 1e349fb commit 6621279

File tree

6 files changed

+40
-65
lines changed

6 files changed

+40
-65
lines changed

compiler/rustc_codegen_ssa/src/base.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ use rustc_data_structures::sync::ParallelIterator;
2222
use rustc_hir as hir;
2323
use rustc_hir::def_id::{DefId, LOCAL_CRATE};
2424
use rustc_hir::lang_items::LangItem;
25-
use rustc_hir::weak_lang_items::WEAK_ITEMS_SYMBOLS;
2625
use rustc_index::vec::Idx;
2726
use rustc_metadata::EncodedMetadata;
2827
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrs;
@@ -887,14 +886,14 @@ impl CrateInfo {
887886
// by the compiler, but that's ok because all this stuff is unstable anyway.
888887
let target = &tcx.sess.target;
889888
if !are_upstream_rust_objects_already_included(tcx.sess) {
890-
let missing_weak_lang_items: FxHashSet<&Symbol> = info
889+
let missing_weak_lang_items: FxHashSet<Symbol> = info
891890
.used_crates
892891
.iter()
893-
.flat_map(|cnum| {
894-
tcx.missing_lang_items(*cnum)
895-
.iter()
896-
.filter(|l| lang_items::required(tcx, **l))
897-
.filter_map(|item| WEAK_ITEMS_SYMBOLS.get(item))
892+
.flat_map(|&cnum| tcx.missing_lang_items(cnum))
893+
.filter(|l| l.is_weak())
894+
.filter_map(|&l| {
895+
let name = l.link_name()?;
896+
lang_items::required(tcx, l).then_some(name)
898897
})
899898
.collect();
900899
let prefix = if target.is_like_windows && target.arch == "x86" { "_" } else { "" };

compiler/rustc_hir/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#![feature(associated_type_defaults)]
66
#![feature(closure_track_caller)]
77
#![feature(const_btree_len)]
8-
#![feature(once_cell)]
98
#![feature(min_specialization)]
109
#![feature(never_type)]
1110
#![feature(rustc_attrs)]
Lines changed: 19 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,31 @@
11
//! Validity checking for weak lang items
22
3-
use crate::def_id::DefId;
4-
use crate::{lang_items, LangItem, LanguageItems};
3+
use crate::LangItem;
54

6-
use rustc_ast as ast;
7-
use rustc_data_structures::fx::FxIndexMap;
85
use rustc_span::symbol::{sym, Symbol};
96

10-
use std::sync::LazyLock;
11-
127
macro_rules! weak_lang_items {
13-
($($name:ident, $item:ident, $sym:ident;)*) => (
14-
15-
pub static WEAK_ITEMS_REFS: LazyLock<FxIndexMap<Symbol, LangItem>> = LazyLock::new(|| {
16-
let mut map = FxIndexMap::default();
17-
$(map.insert(sym::$name, LangItem::$item);)*
18-
map
19-
});
20-
21-
pub static WEAK_ITEMS_SYMBOLS: LazyLock<FxIndexMap<LangItem, Symbol>> = LazyLock::new(|| {
22-
let mut map = FxIndexMap::default();
23-
$(map.insert(LangItem::$item, sym::$sym);)*
24-
map
25-
});
26-
27-
pub fn link_name(attrs: &[ast::Attribute]) -> Option<Symbol>
28-
{
29-
lang_items::extract(attrs).and_then(|(name, _)| {
30-
$(if name == sym::$name {
31-
Some(sym::$sym)
32-
} else)* {
33-
None
8+
($($item:ident, $sym:ident;)*) => {
9+
pub static WEAK_LANG_ITEMS: &[LangItem] = &[$(LangItem::$item,)*];
10+
11+
impl LangItem {
12+
pub fn is_weak(self) -> bool {
13+
matches!(self, $(LangItem::$item)|*)
14+
}
15+
16+
pub fn link_name(self) -> Option<Symbol> {
17+
match self {
18+
$( LangItem::$item => Some(sym::$sym),)*
19+
_ => None,
20+
}
21+
}
3422
}
35-
})
36-
}
37-
38-
impl LanguageItems {
39-
pub fn is_weak_lang_item(&self, item_def_id: DefId) -> bool {
40-
let did = Some(item_def_id);
41-
42-
$(self.$name() == did)||*
4323
}
4424
}
4525

46-
) }
47-
4826
weak_lang_items! {
49-
panic_impl, PanicImpl, rust_begin_unwind;
50-
eh_personality, EhPersonality, rust_eh_personality;
51-
eh_catch_typeinfo, EhCatchTypeinfo, rust_eh_catch_typeinfo;
52-
oom, Oom, rust_oom;
27+
PanicImpl, rust_begin_unwind;
28+
EhPersonality, rust_eh_personality;
29+
EhCatchTypeinfo, rust_eh_catch_typeinfo;
30+
Oom, rust_oom;
5331
}

compiler/rustc_hir_analysis/src/collect.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ use rustc_hir as hir;
2727
use rustc_hir::def::CtorKind;
2828
use rustc_hir::def_id::{DefId, LocalDefId, LOCAL_CRATE};
2929
use rustc_hir::intravisit::{self, Visitor};
30-
use rustc_hir::weak_lang_items;
31-
use rustc_hir::{GenericParamKind, Node};
30+
use rustc_hir::weak_lang_items::WEAK_LANG_ITEMS;
31+
use rustc_hir::{lang_items, GenericParamKind, LangItem, Node};
3232
use rustc_middle::hir::nested_filter;
3333
use rustc_middle::middle::codegen_fn_attrs::{CodegenFnAttrFlags, CodegenFnAttrs};
3434
use rustc_middle::mir::mono::Linkage;
@@ -2104,12 +2104,15 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: DefId) -> CodegenFnAttrs {
21042104
// strippable by the linker.
21052105
//
21062106
// Additionally weak lang items have predetermined symbol names.
2107-
if tcx.is_weak_lang_item(did.to_def_id()) {
2107+
if WEAK_LANG_ITEMS.iter().any(|&l| tcx.lang_items().get(l) == Some(did.to_def_id())) {
21082108
codegen_fn_attrs.flags |= CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL;
21092109
}
2110-
if let Some(name) = weak_lang_items::link_name(attrs) {
2111-
codegen_fn_attrs.export_name = Some(name);
2112-
codegen_fn_attrs.link_name = Some(name);
2110+
if let Some((name, _)) = lang_items::extract(attrs)
2111+
&& let Some(lang_item) = LangItem::from_name(name)
2112+
&& let Some(link_name) = lang_item.link_name()
2113+
{
2114+
codegen_fn_attrs.export_name = Some(link_name);
2115+
codegen_fn_attrs.link_name = Some(link_name);
21132116
}
21142117
check_link_name_xor_ordinal(tcx, &codegen_fn_attrs, link_ordinal_span);
21152118

compiler/rustc_middle/src/middle/lang_items.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,6 @@ impl<'tcx> TyCtxt<'tcx> {
3636
_ => None,
3737
}
3838
}
39-
40-
pub fn is_weak_lang_item(self, item_def_id: DefId) -> bool {
41-
self.lang_items().is_weak_lang_item(item_def_id)
42-
}
4339
}
4440

4541
/// Returns `true` if the specified `lang_item` must be present for this

compiler/rustc_passes/src/weak_lang_items.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use rustc_data_structures::fx::FxHashSet;
44
use rustc_hir::lang_items::{self, LangItem};
5-
use rustc_hir::weak_lang_items::WEAK_ITEMS_REFS;
5+
use rustc_hir::weak_lang_items::WEAK_LANG_ITEMS;
66
use rustc_middle::middle::lang_items::required;
77
use rustc_middle::ty::TyCtxt;
88
use rustc_session::config::CrateType;
@@ -29,8 +29,8 @@ pub fn check_crate<'tcx>(tcx: TyCtxt<'tcx>, items: &mut lang_items::LanguageItem
2929
for id in crate_items.foreign_items() {
3030
let attrs = tcx.hir().attrs(id.hir_id());
3131
if let Some((lang_item, _)) = lang_items::extract(attrs) {
32-
if let Some(&item) = WEAK_ITEMS_REFS.get(&lang_item) {
33-
if items.require(item).is_err() {
32+
if let Some(item) = LangItem::from_name(lang_item) && item.is_weak() {
33+
if items.get(item).is_none() {
3434
items.missing.push(item);
3535
}
3636
} else {
@@ -65,8 +65,8 @@ fn verify<'tcx>(tcx: TyCtxt<'tcx>, items: &lang_items::LanguageItems) {
6565
}
6666
}
6767

68-
for (name, &item) in WEAK_ITEMS_REFS.iter() {
69-
if missing.contains(&item) && required(tcx, item) && items.require(item).is_err() {
68+
for &item in WEAK_LANG_ITEMS.iter() {
69+
if missing.contains(&item) && required(tcx, item) && items.get(item).is_none() {
7070
if item == LangItem::PanicImpl {
7171
tcx.sess.emit_err(MissingPanicHandler);
7272
} else if item == LangItem::Oom {
@@ -75,7 +75,7 @@ fn verify<'tcx>(tcx: TyCtxt<'tcx>, items: &lang_items::LanguageItems) {
7575
tcx.sess.emit_note(MissingAllocErrorHandler);
7676
}
7777
} else {
78-
tcx.sess.emit_err(MissingLangItem { name: *name });
78+
tcx.sess.emit_err(MissingLangItem { name: item.name() });
7979
}
8080
}
8181
}

0 commit comments

Comments
 (0)