]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_passes/src/weak_lang_items.rs
Rollup merge of #106796 - vadorovsky:revert-105708-enable-atomic-cas-bpf, r=bjorn3
[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_hir::lang_items::{self, LangItem};
5 use rustc_hir::weak_lang_items::WEAK_LANG_ITEMS;
6 use rustc_middle::middle::lang_items::required;
7 use rustc_middle::ty::TyCtxt;
8 use rustc_session::config::CrateType;
9
10 use crate::errors::{MissingLangItem, MissingPanicHandler, UnknownExternLangItem};
11
12 /// Checks the crate for usage of weak lang items, returning a vector of all the
13 /// language items required by this crate, but not defined yet.
14 pub fn check_crate(tcx: TyCtxt<'_>, items: &mut lang_items::LanguageItems) {
15     // These are never called by user code, they're generated by the compiler.
16     // They will never implicitly be added to the `missing` array unless we do
17     // so here.
18     if items.eh_personality().is_none() {
19         items.missing.push(LangItem::EhPersonality);
20     }
21     if tcx.sess.target.os == "emscripten" && items.eh_catch_typeinfo().is_none() {
22         items.missing.push(LangItem::EhCatchTypeinfo);
23     }
24
25     let crate_items = tcx.hir_crate_items(());
26     for id in crate_items.foreign_items() {
27         let attrs = tcx.hir().attrs(id.hir_id());
28         if let Some((lang_item, _)) = lang_items::extract(attrs) {
29             if let Some(item) = LangItem::from_name(lang_item) && item.is_weak() {
30                 if items.get(item).is_none() {
31                     items.missing.push(item);
32                 }
33             } else {
34                 let span = tcx.def_span(id.owner_id);
35                 tcx.sess.emit_err(UnknownExternLangItem { span, lang_item });
36             }
37         }
38     }
39
40     verify(tcx, items);
41 }
42
43 fn verify(tcx: TyCtxt<'_>, items: &lang_items::LanguageItems) {
44     // We only need to check for the presence of weak lang items if we're
45     // emitting something that's not an rlib.
46     let needs_check = tcx.sess.crate_types().iter().any(|kind| match *kind {
47         CrateType::Dylib
48         | CrateType::ProcMacro
49         | CrateType::Cdylib
50         | CrateType::Executable
51         | CrateType::Staticlib => true,
52         CrateType::Rlib => false,
53     });
54     if !needs_check {
55         return;
56     }
57
58     let mut missing = FxHashSet::default();
59     for &cnum in tcx.crates(()).iter() {
60         for &item in tcx.missing_lang_items(cnum).iter() {
61             missing.insert(item);
62         }
63     }
64
65     for &item in WEAK_LANG_ITEMS.iter() {
66         if missing.contains(&item) && required(tcx, item) && items.get(item).is_none() {
67             if item == LangItem::PanicImpl {
68                 tcx.sess.emit_err(MissingPanicHandler);
69             } else {
70                 tcx.sess.emit_err(MissingLangItem { name: item.name() });
71             }
72         }
73     }
74 }