]> git.lizzy.rs Git - rust.git/blob - src/librustc_passes/weak_lang_items.rs
Auto merge of #69716 - jonas-schievink:generator-size, r=tmandry
[rust.git] / src / librustc_passes / weak_lang_items.rs
1 //! Validity checking for weak lang items
2
3 use rustc::middle::lang_items;
4 use rustc::middle::lang_items::whitelisted;
5 use rustc::session::config;
6
7 use rustc::hir::map::Map;
8 use rustc::ty::TyCtxt;
9 use rustc_data_structures::fx::FxHashSet;
10 use rustc_errors::struct_span_err;
11 use rustc_hir as hir;
12 use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor};
13 use rustc_hir::weak_lang_items::WEAK_ITEMS_REFS;
14 use rustc_span::symbol::Symbol;
15 use rustc_span::Span;
16
17 struct Context<'a, 'tcx> {
18     tcx: TyCtxt<'tcx>,
19     items: &'a mut lang_items::LanguageItems,
20 }
21
22 /// Checks the crate for usage of weak lang items, returning a vector of all the
23 /// language items required by this crate, but not defined yet.
24 pub fn check_crate<'tcx>(tcx: TyCtxt<'tcx>, items: &mut lang_items::LanguageItems) {
25     // These are never called by user code, they're generated by the compiler.
26     // They will never implicitly be added to the `missing` array unless we do
27     // so here.
28     if items.eh_personality().is_none() {
29         items.missing.push(lang_items::EhPersonalityLangItem);
30     }
31
32     {
33         let mut cx = Context { tcx, items };
34         tcx.hir().krate().visit_all_item_likes(&mut cx.as_deep_visitor());
35     }
36     verify(tcx, items);
37 }
38
39 fn verify<'tcx>(tcx: TyCtxt<'tcx>, items: &lang_items::LanguageItems) {
40     // We only need to check for the presence of weak lang items if we're
41     // emitting something that's not an rlib.
42     let needs_check = tcx.sess.crate_types.borrow().iter().any(|kind| match *kind {
43         config::CrateType::Dylib
44         | config::CrateType::ProcMacro
45         | config::CrateType::Cdylib
46         | config::CrateType::Executable
47         | config::CrateType::Staticlib => true,
48         config::CrateType::Rlib => false,
49     });
50     if !needs_check {
51         return;
52     }
53
54     let mut missing = FxHashSet::default();
55     for &cnum in tcx.crates().iter() {
56         for &item in tcx.missing_lang_items(cnum).iter() {
57             missing.insert(item);
58         }
59     }
60
61     for (name, &item) in WEAK_ITEMS_REFS.iter() {
62         if missing.contains(&item) && !whitelisted(tcx, item) && items.require(item).is_err() {
63             if item == lang_items::PanicImplLangItem {
64                 tcx.sess.err("`#[panic_handler]` function required, but not found");
65             } else if item == lang_items::OomLangItem {
66                 tcx.sess.err("`#[alloc_error_handler]` function required, but not found");
67             } else {
68                 tcx.sess.err(&format!("language item required, but not found: `{}`", name));
69             }
70         }
71     }
72 }
73
74 impl<'a, 'tcx> Context<'a, 'tcx> {
75     fn register(&mut self, name: Symbol, span: Span) {
76         if let Some(&item) = WEAK_ITEMS_REFS.get(&name) {
77             if self.items.require(item).is_err() {
78                 self.items.missing.push(item);
79             }
80         } else {
81             struct_span_err!(self.tcx.sess, span, E0264, "unknown external lang item: `{}`", name)
82                 .emit();
83         }
84     }
85 }
86
87 impl<'a, 'tcx, 'v> Visitor<'v> for Context<'a, 'tcx> {
88     type Map = Map<'v>;
89
90     fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, Map<'v>> {
91         NestedVisitorMap::None
92     }
93
94     fn visit_foreign_item(&mut self, i: &hir::ForeignItem<'_>) {
95         if let Some((lang_item, _)) = hir::lang_items::extract(&i.attrs) {
96             self.register(lang_item, i.span);
97         }
98         intravisit::walk_foreign_item(self, i)
99     }
100 }