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