rustc_passes/
weak_lang_items.rs

1//! Validity checking for weak lang items
2
3use rustc_ast as ast;
4use rustc_ast::visit;
5use rustc_data_structures::fx::FxHashSet;
6use rustc_hir::lang_items::{self, LangItem};
7use rustc_hir::weak_lang_items::WEAK_LANG_ITEMS;
8use rustc_middle::middle::lang_items::required;
9use rustc_middle::ty::TyCtxt;
10use rustc_session::config::CrateType;
11
12use crate::errors::{
13    MissingLangItem, MissingPanicHandler, PanicUnwindWithoutStd, UnknownExternLangItem,
14};
15
16/// Checks the crate for usage of weak lang items, returning a vector of all the
17/// lang items required by this crate, but not defined yet.
18pub(crate) fn check_crate(
19    tcx: TyCtxt<'_>,
20    items: &mut lang_items::LanguageItems,
21    krate: &ast::Crate,
22) {
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.os == "emscripten"
30        && items.eh_catch_typeinfo().is_none()
31        && !tcx.sess.opts.unstable_opts.emscripten_wasm_eh
32    {
33        items.missing.push(LangItem::EhCatchTypeinfo);
34    }
35
36    visit::Visitor::visit_crate(&mut WeakLangItemVisitor { tcx, items }, krate);
37
38    verify(tcx, items);
39}
40
41struct WeakLangItemVisitor<'a, 'tcx> {
42    tcx: TyCtxt<'tcx>,
43    items: &'a mut lang_items::LanguageItems,
44}
45
46impl<'ast> visit::Visitor<'ast> for WeakLangItemVisitor<'_, '_> {
47    fn visit_foreign_item(&mut self, i: &'ast ast::ForeignItem) {
48        if let Some((lang_item, _)) = lang_items::extract(&i.attrs) {
49            if let Some(item) = LangItem::from_name(lang_item)
50                && item.is_weak()
51            {
52                if self.items.get(item).is_none() {
53                    self.items.missing.push(item);
54                }
55            } else {
56                self.tcx.dcx().emit_err(UnknownExternLangItem { span: i.span, lang_item });
57            }
58        }
59    }
60}
61
62fn verify(tcx: TyCtxt<'_>, items: &lang_items::LanguageItems) {
63    // We only need to check for the presence of weak lang items if we're
64    // emitting something that's not an rlib.
65    let needs_check = tcx.crate_types().iter().any(|kind| match *kind {
66        CrateType::Dylib
67        | CrateType::ProcMacro
68        | CrateType::Cdylib
69        | CrateType::Executable
70        | CrateType::Staticlib => true,
71        CrateType::Rlib => false,
72    });
73    if !needs_check {
74        return;
75    }
76
77    let mut missing = FxHashSet::default();
78    for &cnum in tcx.crates(()).iter() {
79        for &item in tcx.missing_lang_items(cnum).iter() {
80            missing.insert(item);
81        }
82    }
83
84    for &item in WEAK_LANG_ITEMS.iter() {
85        if missing.contains(&item) && required(tcx, item) && items.get(item).is_none() {
86            if item == LangItem::PanicImpl {
87                tcx.dcx().emit_err(MissingPanicHandler);
88            } else if item == LangItem::EhPersonality {
89                tcx.dcx().emit_err(PanicUnwindWithoutStd);
90            } else {
91                tcx.dcx().emit_err(MissingLangItem { name: item.name() });
92            }
93        }
94    }
95}