1 //! Validity checking for weak lang items
3 use rustc_data_structures::fx::FxHashSet;
4 use rustc_errors::struct_span_err;
6 use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor};
7 use rustc_hir::lang_items;
8 use rustc_hir::lang_items::ITEM_REFS;
9 use rustc_hir::weak_lang_items::WEAK_ITEMS_REFS;
10 use rustc_middle::middle::lang_items::whitelisted;
11 use rustc_middle::ty::TyCtxt;
12 use rustc_session::config::CrateType;
13 use rustc_span::symbol::sym;
14 use rustc_span::symbol::Symbol;
17 struct Context<'a, 'tcx> {
19 items: &'a mut lang_items::LanguageItems,
22 /// Checks the crate for usage of weak lang items, returning a vector of all the
23 /// language items required by this crate, but not defined yet.
24 pub fn check_crate<'tcx>(tcx: TyCtxt<'tcx>, items: &mut lang_items::LanguageItems) {
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
28 if items.eh_personality().is_none() {
29 items.missing.push(lang_items::EhPersonalityLangItem);
33 let mut cx = Context { tcx, items };
34 tcx.hir().krate().visit_all_item_likes(&mut cx.as_deep_visitor());
39 fn verify<'tcx>(tcx: TyCtxt<'tcx>, items: &lang_items::LanguageItems) {
40 // We only need to check for the presence of weak lang items if we're
41 // emitting something that's not an rlib.
42 let needs_check = tcx.sess.crate_types().iter().any(|kind| match *kind {
44 | CrateType::ProcMacro
46 | CrateType::Executable
47 | CrateType::Staticlib => true,
48 CrateType::Rlib => false,
54 let mut missing = FxHashSet::default();
55 for &cnum in tcx.crates().iter() {
56 for &item in tcx.missing_lang_items(cnum).iter() {
61 for (name, &item) in WEAK_ITEMS_REFS.iter() {
62 if missing.contains(&item) && !whitelisted(tcx, item) && items.require(item).is_err() {
63 if item == lang_items::PanicImplLangItem {
64 tcx.sess.err("`#[panic_handler]` function required, but not found");
65 } else if item == lang_items::OomLangItem {
66 tcx.sess.err("`#[alloc_error_handler]` function required, but not found");
68 tcx.sess.err(&format!("language item required, but not found: `{}`", name));
74 impl<'a, 'tcx> Context<'a, 'tcx> {
75 fn register(&mut self, name: Symbol, span: Span, hir_id: hir::HirId) {
76 if let Some(&item) = WEAK_ITEMS_REFS.get(&name) {
77 if self.items.require(item).is_err() {
78 self.items.missing.push(item);
80 } else if name == sym::count_code_region {
81 // `core::intrinsics::code_count_region()` is (currently) the only `extern` lang item
82 // that is never actually linked. It is not a `weak_lang_item` that can be registered
83 // when used, and should be registered here instead.
84 if let Some((item_index, _)) = ITEM_REFS.get(&*name.as_str()).cloned() {
85 if self.items.items[item_index].is_none() {
86 let item_def_id = self.tcx.hir().local_def_id(hir_id).to_def_id();
87 self.items.items[item_index] = Some(item_def_id);
91 struct_span_err!(self.tcx.sess, span, E0264, "unknown external lang item: `{}`", name)
97 impl<'a, 'tcx, 'v> Visitor<'v> for Context<'a, 'tcx> {
98 type Map = intravisit::ErasedMap<'v>;
100 fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
101 NestedVisitorMap::None
104 fn visit_foreign_item(&mut self, i: &hir::ForeignItem<'_>) {
105 if let Some((lang_item, _)) = hir::lang_items::extract(&i.attrs) {
106 self.register(lang_item, i.span, i.hir_id);
108 intravisit::walk_foreign_item(self, i)