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