1 //! Validity checking for weak lang items
3 use rustc::middle::lang_items;
4 use rustc::middle::lang_items::whitelisted;
5 use rustc::session::config;
7 use rustc::hir::map::Map;
9 use rustc_data_structures::fx::FxHashSet;
10 use rustc_errors::struct_span_err;
12 use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor};
13 use rustc_hir::weak_lang_items::WEAK_ITEMS_REFS;
14 use rustc_span::symbol::Symbol;
17 struct Context<'a, 'tcx> {
19 items: &'a mut lang_items::LanguageItems,
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
28 if items.eh_personality().is_none() {
29 items.missing.push(lang_items::EhPersonalityLangItem);
31 if tcx.sess.target.target.options.custom_unwind_resume & items.eh_unwind_resume().is_none() {
32 items.missing.push(lang_items::EhUnwindResumeLangItem);
36 let mut cx = Context { tcx, items };
37 tcx.hir().krate().visit_all_item_likes(&mut cx.as_deep_visitor());
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,
57 let mut missing = FxHashSet::default();
58 for &cnum in tcx.crates().iter() {
59 for &item in tcx.missing_lang_items(cnum).iter() {
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");
71 tcx.sess.err(&format!("language item required, but not found: `{}`", name));
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);
84 struct_span_err!(self.tcx.sess, span, E0264, "unknown external lang item: `{}`", name)
90 impl<'a, 'tcx, 'v> Visitor<'v> for Context<'a, 'tcx> {
93 fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, Map<'v>> {
94 NestedVisitorMap::None
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);
101 intravisit::walk_foreign_item(self, i)