]> git.lizzy.rs Git - rust.git/blob - src/librustc_passes/weak_lang_items.rs
Fix caching issue when building tools.
[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::intravisit::{self, NestedVisitorMap, Visitor};
7 use rustc_hir::lang_items;
8 use rustc_hir::lang_items::ITEM_REFS;
9 use rustc_hir::weak_lang_items::WEAK_ITEMS_REFS;
10 use rustc_middle::middle::lang_items::whitelisted;
11 use rustc_middle::ty::TyCtxt;
12 use rustc_session::config::CrateType;
13 use rustc_span::symbol::sym;
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().iter().any(|kind| match *kind {
43         CrateType::Dylib
44         | CrateType::ProcMacro
45         | CrateType::Cdylib
46         | CrateType::Executable
47         | CrateType::Staticlib => true,
48         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, hir_id: hir::HirId) {
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 if name == sym::count_code_region {
81             // `core::intrinsics::code_count_region()` is (currently) the only `extern` lang item
82             // that is never actually linked. It is not a `weak_lang_item` that can be registered
83             // when used, and should be registered here instead.
84             if let Some((item_index, _)) = ITEM_REFS.get(&*name.as_str()).cloned() {
85                 if self.items.items[item_index].is_none() {
86                     let item_def_id = self.tcx.hir().local_def_id(hir_id).to_def_id();
87                     self.items.items[item_index] = Some(item_def_id);
88                 }
89             }
90         } else {
91             struct_span_err!(self.tcx.sess, span, E0264, "unknown external lang item: `{}`", name)
92                 .emit();
93         }
94     }
95 }
96
97 impl<'a, 'tcx, 'v> Visitor<'v> for Context<'a, 'tcx> {
98     type Map = intravisit::ErasedMap<'v>;
99
100     fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
101         NestedVisitorMap::None
102     }
103
104     fn visit_foreign_item(&mut self, i: &hir::ForeignItem<'_>) {
105         if let Some((lang_item, _)) = hir::lang_items::extract(&i.attrs) {
106             self.register(lang_item, i.span, i.hir_id);
107         }
108         intravisit::walk_foreign_item(self, i)
109     }
110 }