Skip to main content

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