]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_passes/src/weak_lang_items.rs
Auto merge of #2641 - DrMeepster:init_once_acquire, r=RalfJung
[rust.git] / compiler / rustc_passes / src / weak_lang_items.rs
1 //! Validity checking for weak lang items
2
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;
9
10 use crate::errors::{
11     AllocFuncRequired, MissingAllocErrorHandler, MissingLangItem, MissingPanicHandler,
12     UnknownExternLangItem,
13 };
14
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
20     // so here.
21     if items.eh_personality().is_none() {
22         items.missing.push(LangItem::EhPersonality);
23     }
24     if tcx.sess.target.os == "emscripten" && items.eh_catch_typeinfo().is_none() {
25         items.missing.push(LangItem::EhCatchTypeinfo);
26     }
27
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);
35                 }
36             } else {
37                 let span = tcx.def_span(id.owner_id);
38                 tcx.sess.emit_err(UnknownExternLangItem { span, lang_item });
39             }
40         }
41     }
42
43     verify(tcx, items);
44 }
45
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 {
50         CrateType::Dylib
51         | CrateType::ProcMacro
52         | CrateType::Cdylib
53         | CrateType::Executable
54         | CrateType::Staticlib => true,
55         CrateType::Rlib => false,
56     });
57     if !needs_check {
58         return;
59     }
60
61     let mut missing = FxHashSet::default();
62     for &cnum in tcx.crates(()).iter() {
63         for &item in tcx.missing_lang_items(cnum).iter() {
64             missing.insert(item);
65         }
66     }
67
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);
76                 }
77             } else {
78                 tcx.sess.emit_err(MissingLangItem { name: *name });
79             }
80         }
81     }
82 }