]> git.lizzy.rs Git - rust.git/blob - src/librustc_passes/weak_lang_items.rs
Rollup merge of #69598 - ollie27:rustdoc_crate-version_escape, r=GuillaumeGomez
[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     if tcx.sess.target.target.options.custom_unwind_resume & items.eh_unwind_resume().is_none() {
32         items.missing.push(lang_items::EhUnwindResumeLangItem);
33     }
34
35     {
36         let mut cx = Context { tcx, items };
37         tcx.hir().krate().visit_all_item_likes(&mut cx.as_deep_visitor());
38     }
39     verify(tcx, items);
40 }
41
42 fn verify<'tcx>(tcx: TyCtxt<'tcx>, items: &lang_items::LanguageItems) {
43     // We only need to check for the presence of weak lang items if we're
44     // emitting something that's not an rlib.
45     let needs_check = tcx.sess.crate_types.borrow().iter().any(|kind| match *kind {
46         config::CrateType::Dylib
47         | config::CrateType::ProcMacro
48         | config::CrateType::Cdylib
49         | config::CrateType::Executable
50         | config::CrateType::Staticlib => true,
51         config::CrateType::Rlib => false,
52     });
53     if !needs_check {
54         return;
55     }
56
57     let mut missing = FxHashSet::default();
58     for &cnum in tcx.crates().iter() {
59         for &item in tcx.missing_lang_items(cnum).iter() {
60             missing.insert(item);
61         }
62     }
63
64     for (name, &item) in WEAK_ITEMS_REFS.iter() {
65         if missing.contains(&item) && !whitelisted(tcx, item) && items.require(item).is_err() {
66             if item == lang_items::PanicImplLangItem {
67                 tcx.sess.err("`#[panic_handler]` function required, but not found");
68             } else if item == lang_items::OomLangItem {
69                 tcx.sess.err("`#[alloc_error_handler]` function required, but not found");
70             } else {
71                 tcx.sess.err(&format!("language item required, but not found: `{}`", name));
72             }
73         }
74     }
75 }
76
77 impl<'a, 'tcx> Context<'a, 'tcx> {
78     fn register(&mut self, name: Symbol, span: Span) {
79         if let Some(&item) = WEAK_ITEMS_REFS.get(&name) {
80             if self.items.require(item).is_err() {
81                 self.items.missing.push(item);
82             }
83         } else {
84             struct_span_err!(self.tcx.sess, span, E0264, "unknown external lang item: `{}`", name)
85                 .emit();
86         }
87     }
88 }
89
90 impl<'a, 'tcx, 'v> Visitor<'v> for Context<'a, 'tcx> {
91     type Map = Map<'v>;
92
93     fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, Map<'v>> {
94         NestedVisitorMap::None
95     }
96
97     fn visit_foreign_item(&mut self, i: &hir::ForeignItem<'_>) {
98         if let Some((lang_item, _)) = hir::lang_items::extract(&i.attrs) {
99             self.register(lang_item, i.span);
100         }
101         intravisit::walk_foreign_item(self, i)
102     }
103 }