]> git.lizzy.rs Git - rust.git/blob - src/librustc/middle/weak_lang_items.rs
Update to use new librustc_error_codes library
[rust.git] / src / librustc / middle / weak_lang_items.rs
1 //! Validity checking for weak lang items
2
3 use crate::session::config;
4 use crate::middle::lang_items;
5
6 use rustc_data_structures::fx::FxHashSet;
7 use rustc_target::spec::PanicStrategy;
8 use syntax::ast;
9 use syntax::symbol::{Symbol, sym};
10 use syntax_pos::Span;
11 use crate::hir::def_id::DefId;
12 use crate::hir::intravisit::{Visitor, NestedVisitorMap};
13 use crate::hir::intravisit;
14 use crate::hir;
15 use crate::ty::TyCtxt;
16
17 use rustc_error_codes::*;
18
19 macro_rules! weak_lang_items {
20     ($($name:ident, $item:ident, $sym:ident;)*) => (
21
22 struct Context<'a, 'tcx> {
23     tcx: TyCtxt<'tcx>,
24     items: &'a mut lang_items::LanguageItems,
25 }
26
27 /// Checks the crate for usage of weak lang items, returning a vector of all the
28 /// language items required by this crate, but not defined yet.
29 pub fn check_crate<'tcx>(tcx: TyCtxt<'tcx>,
30                              items: &mut lang_items::LanguageItems) {
31     // These are never called by user code, they're generated by the compiler.
32     // They will never implicitly be added to the `missing` array unless we do
33     // so here.
34     if items.eh_personality().is_none() {
35         items.missing.push(lang_items::EhPersonalityLangItem);
36     }
37     if tcx.sess.target.target.options.custom_unwind_resume &
38        items.eh_unwind_resume().is_none() {
39         items.missing.push(lang_items::EhUnwindResumeLangItem);
40     }
41
42     {
43         let mut cx = Context { tcx, items };
44         tcx.hir().krate().visit_all_item_likes(&mut cx.as_deep_visitor());
45     }
46     verify(tcx, items);
47 }
48
49 pub fn link_name(attrs: &[ast::Attribute]) -> Option<Symbol> {
50     lang_items::extract(attrs).and_then(|(name, _)| {
51         $(if name == sym::$name {
52             Some(sym::$sym)
53         } else)* {
54             None
55         }
56     })
57 }
58
59 /// Returns `true` if the specified `lang_item` doesn't actually need to be
60 /// present for this compilation.
61 ///
62 /// Not all lang items are always required for each compilation, particularly in
63 /// the case of panic=abort. In these situations some lang items are injected by
64 /// crates and don't actually need to be defined in libstd.
65 pub fn whitelisted(tcx: TyCtxt<'_>, lang_item: lang_items::LangItem) -> bool {
66     // If we're not compiling with unwinding, we won't actually need these
67     // symbols. Other panic runtimes ensure that the relevant symbols are
68     // available to link things together, but they're never exercised.
69     if tcx.sess.panic_strategy() != PanicStrategy::Unwind {
70         return lang_item == lang_items::EhPersonalityLangItem ||
71             lang_item == lang_items::EhUnwindResumeLangItem
72     }
73
74     false
75 }
76
77 fn verify<'tcx>(tcx: TyCtxt<'tcx>,
78                     items: &lang_items::LanguageItems) {
79     // We only need to check for the presence of weak lang items if we're
80     // emitting something that's not an rlib.
81     let needs_check = tcx.sess.crate_types.borrow().iter().any(|kind| {
82         match *kind {
83             config::CrateType::Dylib |
84             config::CrateType::ProcMacro |
85             config::CrateType::Cdylib |
86             config::CrateType::Executable |
87             config::CrateType::Staticlib => true,
88             config::CrateType::Rlib => false,
89         }
90     });
91     if !needs_check {
92         return
93     }
94
95     let mut missing = FxHashSet::default();
96     for &cnum in tcx.crates().iter() {
97         for &item in tcx.missing_lang_items(cnum).iter() {
98             missing.insert(item);
99         }
100     }
101
102     $(
103         if missing.contains(&lang_items::$item) &&
104            !whitelisted(tcx, lang_items::$item) &&
105            items.$name().is_none() {
106             if lang_items::$item == lang_items::PanicImplLangItem {
107                 tcx.sess.err(&format!("`#[panic_handler]` function required, \
108                                        but not found"));
109             } else if lang_items::$item == lang_items::OomLangItem {
110                 tcx.sess.err(&format!("`#[alloc_error_handler]` function required, \
111                                        but not found"));
112             } else {
113                 tcx.sess.err(&format!("language item required, but not found: `{}`",
114                                       stringify!($name)));
115             }
116         }
117     )*
118 }
119
120 impl<'a, 'tcx> Context<'a, 'tcx> {
121     fn register(&mut self, name: Symbol, span: Span) {
122         $(if name == sym::$name {
123             if self.items.$name().is_none() {
124                 self.items.missing.push(lang_items::$item);
125             }
126         } else)* {
127             span_err!(self.tcx.sess, span, E0264,
128                       "unknown external lang item: `{}`",
129                       name);
130         }
131     }
132 }
133
134 impl<'a, 'tcx, 'v> Visitor<'v> for Context<'a, 'tcx> {
135     fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'v> {
136         NestedVisitorMap::None
137     }
138
139     fn visit_foreign_item(&mut self, i: &hir::ForeignItem) {
140         if let Some((lang_item, _)) = lang_items::extract(&i.attrs) {
141             self.register(lang_item, i.span);
142         }
143         intravisit::walk_foreign_item(self, i)
144     }
145 }
146
147 impl<'tcx> TyCtxt<'tcx> {
148     pub fn is_weak_lang_item(&self, item_def_id: DefId) -> bool {
149         let lang_items = self.lang_items();
150         let did = Some(item_def_id);
151
152         $(lang_items.$name() == did)||*
153     }
154 }
155
156 ) }
157
158 weak_lang_items! {
159     panic_impl,         PanicImplLangItem,          rust_begin_unwind;
160     eh_personality,     EhPersonalityLangItem,      rust_eh_personality;
161     eh_unwind_resume,   EhUnwindResumeLangItem,     rust_eh_unwind_resume;
162     oom,                OomLangItem,                rust_oom;
163 }