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