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_ITEMS_REFS;
6 use rustc_middle::middle::lang_items::required;
7 use rustc_middle::ty::TyCtxt;
8 use rustc_session::config::CrateType;
11 AllocFuncRequired, MissingAllocErrorHandler, MissingLangItem, MissingPanicHandler,
12 UnknownExternLangItem,
15 /// Checks the crate for usage of weak lang items, returning a vector of all the
16 /// language items required by this crate, but not defined yet.
17 pub fn check_crate<'tcx>(tcx: TyCtxt<'tcx>, items: &mut lang_items::LanguageItems) {
18 // These are never called by user code, they're generated by the compiler.
19 // They will never implicitly be added to the `missing` array unless we do
21 if items.eh_personality().is_none() {
22 items.missing.push(LangItem::EhPersonality);
24 if tcx.sess.target.os == "emscripten" && items.eh_catch_typeinfo().is_none() {
25 items.missing.push(LangItem::EhCatchTypeinfo);
28 let crate_items = tcx.hir_crate_items(());
29 for id in crate_items.foreign_items() {
30 let attrs = tcx.hir().attrs(id.hir_id());
31 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() {
34 items.missing.push(item);
37 let span = tcx.def_span(id.def_id);
38 tcx.sess.emit_err(UnknownExternLangItem { span, lang_item });
46 fn verify<'tcx>(tcx: TyCtxt<'tcx>, items: &lang_items::LanguageItems) {
47 // We only need to check for the presence of weak lang items if we're
48 // emitting something that's not an rlib.
49 let needs_check = tcx.sess.crate_types().iter().any(|kind| match *kind {
51 | CrateType::ProcMacro
53 | CrateType::Executable
54 | CrateType::Staticlib => true,
55 CrateType::Rlib => false,
61 let mut missing = FxHashSet::default();
62 for &cnum in tcx.crates(()).iter() {
63 for &item in tcx.missing_lang_items(cnum).iter() {
68 for (name, &item) in WEAK_ITEMS_REFS.iter() {
69 if missing.contains(&item) && required(tcx, item) && items.require(item).is_err() {
70 if item == LangItem::PanicImpl {
71 tcx.sess.emit_err(MissingPanicHandler);
72 } else if item == LangItem::Oom {
73 if !tcx.features().default_alloc_error_handler {
74 tcx.sess.emit_err(AllocFuncRequired);
75 tcx.sess.emit_note(MissingAllocErrorHandler);
78 tcx.sess.emit_err(MissingLangItem { name: *name });