1 //! Validity checking for weak lang items
3 use rustc_data_structures::fx::FxHashSet;
4 use rustc_hir::lang_items::{self, LangItem};
5 use rustc_hir::weak_lang_items::WEAK_LANG_ITEMS;
6 use rustc_middle::middle::lang_items::required;
7 use rustc_middle::ty::TyCtxt;
8 use rustc_session::config::CrateType;
10 use crate::errors::{MissingLangItem, MissingPanicHandler, UnknownExternLangItem};
12 /// Checks the crate for usage of weak lang items, returning a vector of all the
13 /// language items required by this crate, but not defined yet.
14 pub fn check_crate<'tcx>(tcx: TyCtxt<'tcx>, items: &mut lang_items::LanguageItems) {
15 // These are never called by user code, they're generated by the compiler.
16 // They will never implicitly be added to the `missing` array unless we do
18 if items.eh_personality().is_none() {
19 items.missing.push(LangItem::EhPersonality);
21 if tcx.sess.target.os == "emscripten" && items.eh_catch_typeinfo().is_none() {
22 items.missing.push(LangItem::EhCatchTypeinfo);
25 let crate_items = tcx.hir_crate_items(());
26 for id in crate_items.foreign_items() {
27 let attrs = tcx.hir().attrs(id.hir_id());
28 if let Some((lang_item, _)) = lang_items::extract(attrs) {
29 if let Some(item) = LangItem::from_name(lang_item) && item.is_weak() {
30 if items.get(item).is_none() {
31 items.missing.push(item);
34 let span = tcx.def_span(id.owner_id);
35 tcx.sess.emit_err(UnknownExternLangItem { span, lang_item });
43 fn verify<'tcx>(tcx: TyCtxt<'tcx>, items: &lang_items::LanguageItems) {
44 // We only need to check for the presence of weak lang items if we're
45 // emitting something that's not an rlib.
46 let needs_check = tcx.sess.crate_types().iter().any(|kind| match *kind {
48 | CrateType::ProcMacro
50 | CrateType::Executable
51 | CrateType::Staticlib => true,
52 CrateType::Rlib => false,
58 let mut missing = FxHashSet::default();
59 for &cnum in tcx.crates(()).iter() {
60 for &item in tcx.missing_lang_items(cnum).iter() {
65 for &item in WEAK_LANG_ITEMS.iter() {
66 if missing.contains(&item) && required(tcx, item) && items.get(item).is_none() {
67 if item == LangItem::PanicImpl {
68 tcx.sess.emit_err(MissingPanicHandler);
70 tcx.sess.emit_err(MissingLangItem { name: item.name() });