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;
11use rustc_target::spec::Os;
12
13use crate::errors::{
14    MissingLangItem, MissingPanicHandler, PanicUnwindWithoutStd, UnknownExternLangItem,
15};
16
17/// Checks the crate for usage of weak lang items, returning a vector of all the
18/// lang items required by this crate, but not defined yet.
19pub(crate) fn check_crate(
20    tcx: TyCtxt<'_>,
21    items: &mut lang_items::LanguageItems,
22    krate: &ast::Crate,
23) {
24    // These are never called by user code, they're generated by the compiler.
25    // They will never implicitly be added to the `missing` array unless we do
26    // so here.
27    if items.eh_personality().is_none() {
28        items.missing.push(LangItem::EhPersonality);
29    }
30    if tcx.sess.target.os == Os::Emscripten
31        && items.eh_catch_typeinfo().is_none()
32        && !tcx.sess.opts.unstable_opts.emscripten_wasm_eh
33    {
34        items.missing.push(LangItem::EhCatchTypeinfo);
35    }
36
37    visit::Visitor::visit_crate(&mut WeakLangItemVisitor { tcx, items }, krate);
38
39    verify(tcx, items);
40}
41
42struct WeakLangItemVisitor<'a, 'tcx> {
43    tcx: TyCtxt<'tcx>,
44    items: &'a mut lang_items::LanguageItems,
45}
46
47impl<'ast> visit::Visitor<'ast> for WeakLangItemVisitor<'_, '_> {
48    fn visit_foreign_item(&mut self, i: &'ast ast::ForeignItem) {
49        if let Some((lang_item, _)) = lang_items::extract(&i.attrs) {
50            if let Some(item) = LangItem::from_name(lang_item)
51                && item.is_weak()
52            {
53                if self.items.get(item).is_none() {
54                    self.items.missing.push(item);
55                }
56            } else {
57                self.tcx.dcx().emit_err(UnknownExternLangItem { span: i.span, lang_item });
58            }
59        }
60    }
61}
62
63fn verify(tcx: TyCtxt<'_>, items: &lang_items::LanguageItems) {
64    // We only need to check for the presence of weak lang items if we're
65    // emitting something that's not an rlib.
66    let needs_check = tcx.crate_types().iter().any(|kind| match *kind {
67        CrateType::Dylib
68        | CrateType::ProcMacro
69        | CrateType::Cdylib
70        | CrateType::Executable
71        | CrateType::Staticlib
72        | CrateType::Sdylib => true,
73        CrateType::Rlib => false,
74    });
75    if !needs_check {
76        return;
77    }
78
79    let mut missing = FxHashSet::default();
80    for &cnum in tcx.crates(()).iter() {
81        for &item in tcx.missing_lang_items(cnum).iter() {
82            missing.insert(item);
83        }
84    }
85
86    for &item in WEAK_LANG_ITEMS.iter() {
87        if missing.contains(&item) && required(tcx, item) && items.get(item).is_none() {
88            if item == LangItem::PanicImpl {
89                tcx.dcx().emit_err(MissingPanicHandler);
90            } else if item == LangItem::EhPersonality {
91                tcx.dcx().emit_err(PanicUnwindWithoutStd);
92            } else {
93                tcx.dcx().emit_err(MissingLangItem { name: item.name() });
94            }
95        }
96    }
97}