1 //! Validity checking for weak lang items
3 use crate::session::config;
4 use crate::middle::lang_items;
6 use rustc_data_structures::fx::FxHashSet;
7 use rustc_target::spec::PanicStrategy;
9 use syntax::symbol::{Symbol, sym};
11 use crate::hir::def_id::DefId;
12 use crate::hir::intravisit::{Visitor, NestedVisitorMap};
13 use crate::hir::intravisit;
15 use crate::ty::TyCtxt;
17 use rustc_error_codes::*;
19 macro_rules! weak_lang_items {
20 ($($name:ident, $item:ident, $sym:ident;)*) => (
22 struct Context<'a, 'tcx> {
24 items: &'a mut lang_items::LanguageItems,
27 /// Checks the crate for usage of weak lang items, returning a vector of all the
28 /// language items required by this crate, but not defined yet.
29 pub fn check_crate<'tcx>(tcx: TyCtxt<'tcx>,
30 items: &mut lang_items::LanguageItems) {
31 // These are never called by user code, they're generated by the compiler.
32 // They will never implicitly be added to the `missing` array unless we do
34 if items.eh_personality().is_none() {
35 items.missing.push(lang_items::EhPersonalityLangItem);
37 if tcx.sess.target.target.options.custom_unwind_resume &
38 items.eh_unwind_resume().is_none() {
39 items.missing.push(lang_items::EhUnwindResumeLangItem);
43 let mut cx = Context { tcx, items };
44 tcx.hir().krate().visit_all_item_likes(&mut cx.as_deep_visitor());
49 pub fn link_name(attrs: &[ast::Attribute]) -> Option<Symbol> {
50 lang_items::extract(attrs).and_then(|(name, _)| {
51 $(if name == sym::$name {
59 /// Returns `true` if the specified `lang_item` doesn't actually need to be
60 /// present for this compilation.
62 /// Not all lang items are always required for each compilation, particularly in
63 /// the case of panic=abort. In these situations some lang items are injected by
64 /// crates and don't actually need to be defined in libstd.
65 pub fn whitelisted(tcx: TyCtxt<'_>, lang_item: lang_items::LangItem) -> bool {
66 // If we're not compiling with unwinding, we won't actually need these
67 // symbols. Other panic runtimes ensure that the relevant symbols are
68 // available to link things together, but they're never exercised.
69 if tcx.sess.panic_strategy() != PanicStrategy::Unwind {
70 return lang_item == lang_items::EhPersonalityLangItem ||
71 lang_item == lang_items::EhUnwindResumeLangItem
77 fn verify<'tcx>(tcx: TyCtxt<'tcx>,
78 items: &lang_items::LanguageItems) {
79 // We only need to check for the presence of weak lang items if we're
80 // emitting something that's not an rlib.
81 let needs_check = tcx.sess.crate_types.borrow().iter().any(|kind| {
83 config::CrateType::Dylib |
84 config::CrateType::ProcMacro |
85 config::CrateType::Cdylib |
86 config::CrateType::Executable |
87 config::CrateType::Staticlib => true,
88 config::CrateType::Rlib => false,
95 let mut missing = FxHashSet::default();
96 for &cnum in tcx.crates().iter() {
97 for &item in tcx.missing_lang_items(cnum).iter() {
103 if missing.contains(&lang_items::$item) &&
104 !whitelisted(tcx, lang_items::$item) &&
105 items.$name().is_none() {
106 if lang_items::$item == lang_items::PanicImplLangItem {
107 tcx.sess.err(&format!("`#[panic_handler]` function required, \
109 } else if lang_items::$item == lang_items::OomLangItem {
110 tcx.sess.err(&format!("`#[alloc_error_handler]` function required, \
113 tcx.sess.err(&format!("language item required, but not found: `{}`",
120 impl<'a, 'tcx> Context<'a, 'tcx> {
121 fn register(&mut self, name: Symbol, span: Span) {
122 $(if name == sym::$name {
123 if self.items.$name().is_none() {
124 self.items.missing.push(lang_items::$item);
127 span_err!(self.tcx.sess, span, E0264,
128 "unknown external lang item: `{}`",
134 impl<'a, 'tcx, 'v> Visitor<'v> for Context<'a, 'tcx> {
135 fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'v> {
136 NestedVisitorMap::None
139 fn visit_foreign_item(&mut self, i: &hir::ForeignItem) {
140 if let Some((lang_item, _)) = lang_items::extract(&i.attrs) {
141 self.register(lang_item, i.span);
143 intravisit::walk_foreign_item(self, i)
147 impl<'tcx> TyCtxt<'tcx> {
148 pub fn is_weak_lang_item(&self, item_def_id: DefId) -> bool {
149 let lang_items = self.lang_items();
150 let did = Some(item_def_id);
152 $(lang_items.$name() == did)||*
159 panic_impl, PanicImplLangItem, rust_begin_unwind;
160 eh_personality, EhPersonalityLangItem, rust_eh_personality;
161 eh_unwind_resume, EhUnwindResumeLangItem, rust_eh_unwind_resume;
162 oom, OomLangItem, rust_oom;