Skip to content

Commit

Permalink
Cleanup weak lang items
Browse files Browse the repository at this point in the history
  • Loading branch information
camsteffen committed Oct 29, 2022
1 parent 1e349fb commit 6621279
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 65 deletions.
13 changes: 6 additions & 7 deletions compiler/rustc_codegen_ssa/src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Symbol> = 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 { "" };
Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_hir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
60 changes: 19 additions & 41 deletions compiler/rustc_hir/src/weak_lang_items.rs
Original file line number Diff line number Diff line change
@@ -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;)*) => (

pub static WEAK_ITEMS_REFS: LazyLock<FxIndexMap<Symbol, LangItem>> = LazyLock::new(|| {
let mut map = FxIndexMap::default();
$(map.insert(sym::$name, LangItem::$item);)*
map
});

pub static WEAK_ITEMS_SYMBOLS: LazyLock<FxIndexMap<LangItem, Symbol>> = LazyLock::new(|| {
let mut map = FxIndexMap::default();
$(map.insert(LangItem::$item, sym::$sym);)*
map
});

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
($($item:ident, $sym:ident;)*) => {
pub static WEAK_LANG_ITEMS: &[LangItem] = &[$(LangItem::$item,)*];

impl LangItem {
pub fn is_weak(self) -> bool {
matches!(self, $(LangItem::$item)|*)
}

pub fn link_name(self) -> Option<Symbol> {
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;
}
15 changes: 9 additions & 6 deletions compiler/rustc_hir_analysis/src/collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down
4 changes: 0 additions & 4 deletions compiler/rustc_middle/src/middle/lang_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions compiler/rustc_passes/src/weak_lang_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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() });
}
}
}
Expand Down

0 comments on commit 6621279

Please sign in to comment.