]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_passes/src/weak_lang_items.rs
3a662a9bab2f6ee2f8fdf9826021ab2c1de86b53
[rust.git] / compiler / rustc_passes / src / weak_lang_items.rs
1 //! Validity checking for weak lang items
2
3 use rustc_ast::Attribute;
4 use rustc_data_structures::fx::FxHashSet;
5 use rustc_errors::struct_span_err;
6 use rustc_hir as hir;
7 use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor};
8 use rustc_hir::lang_items::{self, LangItem};
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(LangItem::EhPersonality);
29     }
30     if tcx.sess.target.is_like_emscripten && items.eh_catch_typeinfo().is_none() {
31         items.missing.push(LangItem::EhCatchTypeinfo);
32     }
33
34     {
35         let mut cx = Context { tcx, items };
36         tcx.hir().krate().visit_all_item_likes(&mut cx.as_deep_visitor());
37     }
38     verify(tcx, items);
39 }
40
41 fn verify<'tcx>(tcx: TyCtxt<'tcx>, items: &lang_items::LanguageItems) {
42     // We only need to check for the presence of weak lang items if we're
43     // emitting something that's not an rlib.
44     let needs_check = tcx.sess.crate_types().iter().any(|kind| match *kind {
45         CrateType::Dylib
46         | CrateType::ProcMacro
47         | CrateType::Cdylib
48         | CrateType::Executable
49         | CrateType::Staticlib => true,
50         CrateType::Rlib => false,
51     });
52     if !needs_check {
53         return;
54     }
55
56     let mut missing = FxHashSet::default();
57     for &cnum in tcx.crates(()).iter() {
58         for &item in tcx.missing_lang_items(cnum).iter() {
59             missing.insert(item);
60         }
61     }
62
63     for (name, item) in WEAK_ITEMS_REFS.clone().into_sorted_vector().into_iter() {
64         if missing.contains(&item) && required(tcx, item) && items.require(item).is_err() {
65             if item == LangItem::PanicImpl {
66                 tcx.sess.err("`#[panic_handler]` function required, but not found");
67             } else if item == LangItem::Oom {
68                 if !tcx.features().default_alloc_error_handler {
69                     tcx.sess.err("`#[alloc_error_handler]` function required, but not found.");
70                     tcx.sess.note_without_error("Use `#![feature(default_alloc_error_handler)]` for a default error handler.");
71                 }
72             } else {
73                 tcx.sess.err(&format!("language item required, but not found: `{}`", name));
74             }
75         }
76     }
77 }
78
79 impl<'a, 'tcx> Context<'a, 'tcx> {
80     fn register(&mut self, name: Symbol, span: Span) {
81         if let Some(&item) = WEAK_ITEMS_REFS.get(&name) {
82             if self.items.require(item).is_err() {
83                 self.items.missing.push(item);
84             }
85         } else {
86             struct_span_err!(self.tcx.sess, span, E0264, "unknown external lang item: `{}`", name)
87                 .emit();
88         }
89     }
90 }
91
92 impl<'a, 'tcx, 'v> Visitor<'v> for Context<'a, 'tcx> {
93     type Map = intravisit::ErasedMap<'v>;
94
95     fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
96         NestedVisitorMap::None
97     }
98
99     fn visit_foreign_item(&mut self, i: &hir::ForeignItem<'_>) {
100         let check_name = |attr: &Attribute, sym| attr.has_name(sym);
101         let attrs = self.tcx.hir().attrs(i.hir_id());
102         if let Some((lang_item, _)) = lang_items::extract(check_name, attrs) {
103             self.register(lang_item, i.span);
104         }
105         intravisit::walk_foreign_item(self, i)
106     }
107 }