]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_passes/src/weak_lang_items.rs
Rollup merge of #92366 - jhpratt:derive-default-enum, r=Mark-Simulacrum
[rust.git] / compiler / rustc_passes / src / 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, Visitor};
7 use rustc_hir::lang_items::{self, LangItem};
8 use rustc_hir::weak_lang_items::WEAK_ITEMS_REFS;
9 use rustc_middle::middle::lang_items::required;
10 use rustc_middle::ty::TyCtxt;
11 use rustc_session::config::CrateType;
12 use rustc_span::symbol::Symbol;
13 use rustc_span::Span;
14
15 struct Context<'a, 'tcx> {
16     tcx: TyCtxt<'tcx>,
17     items: &'a mut lang_items::LanguageItems,
18 }
19
20 /// Checks the crate for usage of weak lang items, returning a vector of all the
21 /// language items required by this crate, but not defined yet.
22 pub fn check_crate<'tcx>(tcx: TyCtxt<'tcx>, items: &mut lang_items::LanguageItems) {
23     // These are never called by user code, they're generated by the compiler.
24     // They will never implicitly be added to the `missing` array unless we do
25     // so here.
26     if items.eh_personality().is_none() {
27         items.missing.push(LangItem::EhPersonality);
28     }
29     if tcx.sess.target.is_like_emscripten && items.eh_catch_typeinfo().is_none() {
30         items.missing.push(LangItem::EhCatchTypeinfo);
31     }
32
33     {
34         let mut cx = Context { tcx, items };
35         tcx.hir().visit_all_item_likes(&mut cx.as_deep_visitor());
36     }
37     verify(tcx, items);
38 }
39
40 fn verify<'tcx>(tcx: TyCtxt<'tcx>, items: &lang_items::LanguageItems) {
41     // We only need to check for the presence of weak lang items if we're
42     // emitting something that's not an rlib.
43     let needs_check = tcx.sess.crate_types().iter().any(|kind| match *kind {
44         CrateType::Dylib
45         | CrateType::ProcMacro
46         | CrateType::Cdylib
47         | CrateType::Executable
48         | CrateType::Staticlib => true,
49         CrateType::Rlib => false,
50     });
51     if !needs_check {
52         return;
53     }
54
55     let mut missing = FxHashSet::default();
56     for &cnum in tcx.crates(()).iter() {
57         for &item in tcx.missing_lang_items(cnum).iter() {
58             missing.insert(item);
59         }
60     }
61
62     for (name, item) in WEAK_ITEMS_REFS.clone().into_sorted_vector().into_iter() {
63         if missing.contains(&item) && required(tcx, item) && items.require(item).is_err() {
64             if item == LangItem::PanicImpl {
65                 tcx.sess.err("`#[panic_handler]` function required, but not found");
66             } else if item == LangItem::Oom {
67                 if !tcx.features().default_alloc_error_handler {
68                     tcx.sess.err("`#[alloc_error_handler]` function required, but not found");
69                     tcx.sess.note_without_error("use `#![feature(default_alloc_error_handler)]` for a default error handler");
70                 }
71             } else {
72                 tcx
73                     .sess
74                     .diagnostic()
75                     .struct_err(&format!("language item required, but not found: `{}`", name))
76                     .note(&format!("this can occur when a binary crate with `#![no_std]` is compiled for a target where `{}` is defined in the standard library", name))
77                     .help(&format!("you may be able to compile for a target that doesn't need `{}`, specify a target with `--target` or in `.cargo/config`", name))
78                     .emit();
79             }
80         }
81     }
82 }
83
84 impl<'a, 'tcx> Context<'a, 'tcx> {
85     fn register(&mut self, name: Symbol, span: Span) {
86         if let Some(&item) = WEAK_ITEMS_REFS.get(&name) {
87             if self.items.require(item).is_err() {
88                 self.items.missing.push(item);
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     fn visit_foreign_item(&mut self, i: &hir::ForeignItem<'_>) {
99         let attrs = self.tcx.hir().attrs(i.hir_id());
100         if let Some((lang_item, _)) = lang_items::extract(attrs) {
101             self.register(lang_item, i.span);
102         }
103         intravisit::walk_foreign_item(self, i)
104     }
105 }