]> git.lizzy.rs Git - rust.git/blob - src/librustc/middle/lang_items.rs
Auto merge of #61203 - memoryruins:bare_trait_objects, r=Centril
[rust.git] / src / librustc / middle / lang_items.rs
1 //! Detecting language items.
2 //!
3 //! Language items are items that represent concepts intrinsic to the language
4 //! itself. Examples are:
5 //!
6 //! * Traits that specify "kinds"; e.g., "Sync", "Send".
7 //!
8 //! * Traits that represent operators; e.g., "Add", "Sub", "Index".
9 //!
10 //! * Functions called by the compiler itself.
11
12 pub use self::LangItem::*;
13
14 use crate::hir::def_id::DefId;
15 use crate::hir::check_attr::Target;
16 use crate::ty::{self, TyCtxt};
17 use crate::middle::weak_lang_items;
18 use crate::util::nodemap::FxHashMap;
19
20 use syntax::ast;
21 use syntax::symbol::{Symbol, sym};
22 use syntax_pos::Span;
23 use rustc_macros::HashStable;
24 use crate::hir::itemlikevisit::ItemLikeVisitor;
25 use crate::hir;
26
27 // The actual lang items defined come at the end of this file in one handy table.
28 // So you probably just want to nip down to the end.
29 macro_rules! language_item_table {
30     (
31         $( $variant:ident, $name:expr, $method:ident, $target:path; )*
32     ) => {
33
34 enum_from_u32! {
35     /// A representation of all the valid language items in Rust.
36     #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
37     pub enum LangItem {
38         $($variant,)*
39     }
40 }
41
42 impl LangItem {
43     /// Returns the `name` in `#[lang = "$name"]`.
44     /// For example, `LangItem::EqTraitLangItem`,
45     /// that is `#[lang = "eq"]` would result in `"eq"`.
46     fn name(self) -> &'static str {
47         match self {
48             $( $variant => $name, )*
49         }
50     }
51 }
52
53 #[derive(HashStable)]
54 pub struct LanguageItems {
55     /// Mappings from lang items to their possibly found `DefId`s.
56     /// The index corresponds to the order in `LangItem`.
57     pub items: Vec<Option<DefId>>,
58     /// Lang items that were not found during collection.
59     pub missing: Vec<LangItem>,
60 }
61
62 impl LanguageItems {
63     /// Construct an empty collection of lang items and no missing ones.
64     pub fn new() -> Self {
65         fn init_none(_: LangItem) -> Option<DefId> { None }
66
67         Self {
68             items: vec![$(init_none($variant)),*],
69             missing: Vec::new(),
70         }
71     }
72
73     /// Returns the mappings to the possibly found `DefId`s for each lang item.
74     pub fn items(&self) -> &[Option<DefId>] {
75         &*self.items
76     }
77
78     /// Requires that a given `LangItem` was bound and returns the corresponding `DefId`.
79     /// If it wasn't bound, e.g. due to a missing `#[lang = "<it.name()>"]`,
80     /// returns an error message as a string.
81     pub fn require(&self, it: LangItem) -> Result<DefId, String> {
82         self.items[it as usize].ok_or_else(|| format!("requires `{}` lang_item", it.name()))
83     }
84
85     /// Returns the kind of closure that `id`, which is one of the `Fn*` traits, corresponds to.
86     /// If `id` is not one of the `Fn*` traits, `None` is returned.
87     pub fn fn_trait_kind(&self, id: DefId) -> Option<ty::ClosureKind> {
88         match Some(id) {
89             x if x == self.fn_trait() => Some(ty::ClosureKind::Fn),
90             x if x == self.fn_mut_trait() => Some(ty::ClosureKind::FnMut),
91             x if x == self.fn_once_trait() => Some(ty::ClosureKind::FnOnce),
92             _ => None
93         }
94     }
95
96     $(
97         /// Returns the corresponding `DefId` for the lang item
98         #[doc = $name]
99         /// if it exists.
100         #[allow(dead_code)]
101         pub fn $method(&self) -> Option<DefId> {
102             self.items[$variant as usize]
103         }
104     )*
105 }
106
107 struct LanguageItemCollector<'a, 'tcx: 'a> {
108     items: LanguageItems,
109     tcx: TyCtxt<'a, 'tcx, 'tcx>,
110     /// A mapping from the name of the lang item to its order and the form it must be of.
111     item_refs: FxHashMap<&'static str, (usize, Target)>,
112 }
113
114 impl<'a, 'v, 'tcx> ItemLikeVisitor<'v> for LanguageItemCollector<'a, 'tcx> {
115     fn visit_item(&mut self, item: &hir::Item) {
116         if let Some((value, span)) = extract(&item.attrs) {
117             let actual_target = Target::from_item(item);
118             match self.item_refs.get(&*value.as_str()).cloned() {
119                 // Known lang item with attribute on correct target.
120                 Some((item_index, expected_target)) if actual_target == expected_target => {
121                     let def_id = self.tcx.hir().local_def_id_from_hir_id(item.hir_id);
122                     self.collect_item(item_index, def_id);
123                 },
124                 // Known lang item with attribute on incorrect target.
125                 Some((_, expected_target)) => {
126                     struct_span_err!(
127                         self.tcx.sess, span, E0718,
128                         "`{}` language item must be applied to a {}",
129                         value, expected_target,
130                     ).span_label(
131                         span,
132                         format!(
133                             "attribute should be applied to a {}, not a {}",
134                             expected_target, actual_target,
135                         ),
136                     ).emit();
137                 },
138                 // Unknown lang item.
139                 _ => {
140                     struct_span_err!(
141                         self.tcx.sess, span, E0522,
142                         "definition of an unknown language item: `{}`",
143                         value
144                     ).span_label(
145                         span,
146                         format!("definition of unknown language item `{}`", value)
147                     ).emit();
148                 },
149             }
150         }
151     }
152
153     fn visit_trait_item(&mut self, _trait_item: &hir::TraitItem) {
154         // at present, lang items are always items, not trait items
155     }
156
157     fn visit_impl_item(&mut self, _impl_item: &hir::ImplItem) {
158         // at present, lang items are always items, not impl items
159     }
160 }
161
162 impl<'a, 'tcx> LanguageItemCollector<'a, 'tcx> {
163     fn new(tcx: TyCtxt<'a, 'tcx, 'tcx>) -> LanguageItemCollector<'a, 'tcx> {
164         let mut item_refs = FxHashMap::default();
165
166         $( item_refs.insert($name, ($variant as usize, $target)); )*
167
168         LanguageItemCollector {
169             tcx,
170             items: LanguageItems::new(),
171             item_refs,
172         }
173     }
174
175     fn collect_item(&mut self, item_index: usize, item_def_id: DefId) {
176         // Check for duplicates.
177         if let Some(original_def_id) = self.items.items[item_index] {
178             if original_def_id != item_def_id {
179                 let name = LangItem::from_u32(item_index as u32).unwrap().name();
180                 let mut err = match self.tcx.hir().span_if_local(item_def_id) {
181                     Some(span) => struct_span_err!(
182                         self.tcx.sess,
183                         span,
184                         E0152,
185                         "duplicate lang item found: `{}`.",
186                         name),
187                     None => self.tcx.sess.struct_err(&format!(
188                             "duplicate lang item in crate `{}`: `{}`.",
189                             self.tcx.crate_name(item_def_id.krate),
190                             name)),
191                 };
192                 if let Some(span) = self.tcx.hir().span_if_local(original_def_id) {
193                     span_note!(&mut err, span, "first defined here.");
194                 } else {
195                     err.note(&format!("first defined in crate `{}`.",
196                                       self.tcx.crate_name(original_def_id.krate)));
197                 }
198                 err.emit();
199             }
200         }
201
202         // Matched.
203         self.items.items[item_index] = Some(item_def_id);
204     }
205 }
206
207 /// Extract the first `lang = "$name"` out of a list of attributes.
208 /// The attributes `#[panic_handler]` and `#[alloc_error_handler]`
209 /// are also extracted out when found.
210 pub fn extract(attrs: &[ast::Attribute]) -> Option<(Symbol, Span)> {
211     attrs.iter().find_map(|attr| Some(match attr {
212         _ if attr.check_name(sym::lang) => (attr.value_str()?, attr.span),
213         _ if attr.check_name(sym::panic_handler) => (sym::panic_impl, attr.span),
214         _ if attr.check_name(sym::alloc_error_handler) => (sym::oom, attr.span),
215         _ => return None,
216     }))
217 }
218
219 /// Traverse and collect all the lang items in all crates.
220 pub fn collect<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) -> LanguageItems {
221     // Initialize the collector.
222     let mut collector = LanguageItemCollector::new(tcx);
223
224     // Collect lang items in other crates.
225     for &cnum in tcx.crates().iter() {
226         for &(def_id, item_index) in tcx.defined_lang_items(cnum).iter() {
227             collector.collect_item(item_index, def_id);
228         }
229     }
230
231     // Collect lang items in this crate.
232     tcx.hir().krate().visit_all_item_likes(&mut collector);
233
234     // Extract out the found lang items.
235     let LanguageItemCollector { mut items, .. } = collector;
236
237     // Find all required but not-yet-defined lang items.
238     weak_lang_items::check_crate(tcx, &mut items);
239
240     items
241 }
242
243 // End of the macro
244     }
245 }
246
247 language_item_table! {
248 //  Variant name,                Name,                 Method name,             Target;
249     CharImplItem,                "char",               char_impl,               Target::Impl;
250     StrImplItem,                 "str",                str_impl,                Target::Impl;
251     SliceImplItem,               "slice",              slice_impl,              Target::Impl;
252     SliceU8ImplItem,             "slice_u8",           slice_u8_impl,           Target::Impl;
253     StrAllocImplItem,            "str_alloc",          str_alloc_impl,          Target::Impl;
254     SliceAllocImplItem,          "slice_alloc",        slice_alloc_impl,        Target::Impl;
255     SliceU8AllocImplItem,        "slice_u8_alloc",     slice_u8_alloc_impl,     Target::Impl;
256     ConstPtrImplItem,            "const_ptr",          const_ptr_impl,          Target::Impl;
257     MutPtrImplItem,              "mut_ptr",            mut_ptr_impl,            Target::Impl;
258     I8ImplItem,                  "i8",                 i8_impl,                 Target::Impl;
259     I16ImplItem,                 "i16",                i16_impl,                Target::Impl;
260     I32ImplItem,                 "i32",                i32_impl,                Target::Impl;
261     I64ImplItem,                 "i64",                i64_impl,                Target::Impl;
262     I128ImplItem,                "i128",               i128_impl,               Target::Impl;
263     IsizeImplItem,               "isize",              isize_impl,              Target::Impl;
264     U8ImplItem,                  "u8",                 u8_impl,                 Target::Impl;
265     U16ImplItem,                 "u16",                u16_impl,                Target::Impl;
266     U32ImplItem,                 "u32",                u32_impl,                Target::Impl;
267     U64ImplItem,                 "u64",                u64_impl,                Target::Impl;
268     U128ImplItem,                "u128",               u128_impl,               Target::Impl;
269     UsizeImplItem,               "usize",              usize_impl,              Target::Impl;
270     F32ImplItem,                 "f32",                f32_impl,                Target::Impl;
271     F64ImplItem,                 "f64",                f64_impl,                Target::Impl;
272     F32RuntimeImplItem,          "f32_runtime",        f32_runtime_impl,        Target::Impl;
273     F64RuntimeImplItem,          "f64_runtime",        f64_runtime_impl,        Target::Impl;
274
275     SizedTraitLangItem,          "sized",              sized_trait,             Target::Trait;
276     UnsizeTraitLangItem,         "unsize",             unsize_trait,            Target::Trait;
277     CopyTraitLangItem,           "copy",               copy_trait,              Target::Trait;
278     CloneTraitLangItem,          "clone",              clone_trait,             Target::Trait;
279     SyncTraitLangItem,           "sync",               sync_trait,              Target::Trait;
280     FreezeTraitLangItem,         "freeze",             freeze_trait,            Target::Trait;
281
282     DropTraitLangItem,           "drop",               drop_trait,              Target::Trait;
283
284     CoerceUnsizedTraitLangItem,  "coerce_unsized",     coerce_unsized_trait,    Target::Trait;
285     DispatchFromDynTraitLangItem,"dispatch_from_dyn",  dispatch_from_dyn_trait, Target::Trait;
286
287     AddTraitLangItem,            "add",                add_trait,               Target::Trait;
288     SubTraitLangItem,            "sub",                sub_trait,               Target::Trait;
289     MulTraitLangItem,            "mul",                mul_trait,               Target::Trait;
290     DivTraitLangItem,            "div",                div_trait,               Target::Trait;
291     RemTraitLangItem,            "rem",                rem_trait,               Target::Trait;
292     NegTraitLangItem,            "neg",                neg_trait,               Target::Trait;
293     NotTraitLangItem,            "not",                not_trait,               Target::Trait;
294     BitXorTraitLangItem,         "bitxor",             bitxor_trait,            Target::Trait;
295     BitAndTraitLangItem,         "bitand",             bitand_trait,            Target::Trait;
296     BitOrTraitLangItem,          "bitor",              bitor_trait,             Target::Trait;
297     ShlTraitLangItem,            "shl",                shl_trait,               Target::Trait;
298     ShrTraitLangItem,            "shr",                shr_trait,               Target::Trait;
299     AddAssignTraitLangItem,      "add_assign",         add_assign_trait,        Target::Trait;
300     SubAssignTraitLangItem,      "sub_assign",         sub_assign_trait,        Target::Trait;
301     MulAssignTraitLangItem,      "mul_assign",         mul_assign_trait,        Target::Trait;
302     DivAssignTraitLangItem,      "div_assign",         div_assign_trait,        Target::Trait;
303     RemAssignTraitLangItem,      "rem_assign",         rem_assign_trait,        Target::Trait;
304     BitXorAssignTraitLangItem,   "bitxor_assign",      bitxor_assign_trait,     Target::Trait;
305     BitAndAssignTraitLangItem,   "bitand_assign",      bitand_assign_trait,     Target::Trait;
306     BitOrAssignTraitLangItem,    "bitor_assign",       bitor_assign_trait,      Target::Trait;
307     ShlAssignTraitLangItem,      "shl_assign",         shl_assign_trait,        Target::Trait;
308     ShrAssignTraitLangItem,      "shr_assign",         shr_assign_trait,        Target::Trait;
309     IndexTraitLangItem,          "index",              index_trait,             Target::Trait;
310     IndexMutTraitLangItem,       "index_mut",          index_mut_trait,         Target::Trait;
311
312     UnsafeCellTypeLangItem,      "unsafe_cell",        unsafe_cell_type,        Target::Struct;
313     VaListTypeLangItem,          "va_list",            va_list,                 Target::Struct;
314
315     DerefTraitLangItem,          "deref",              deref_trait,             Target::Trait;
316     DerefMutTraitLangItem,       "deref_mut",          deref_mut_trait,         Target::Trait;
317     ReceiverTraitLangItem,       "receiver",           receiver_trait,          Target::Trait;
318
319     FnTraitLangItem,             "fn",                 fn_trait,                Target::Trait;
320     FnMutTraitLangItem,          "fn_mut",             fn_mut_trait,            Target::Trait;
321     FnOnceTraitLangItem,         "fn_once",            fn_once_trait,           Target::Trait;
322
323     GeneratorStateLangItem,      "generator_state",    gen_state,               Target::Enum;
324     GeneratorTraitLangItem,      "generator",          gen_trait,               Target::Trait;
325     UnpinTraitLangItem,          "unpin",              unpin_trait,             Target::Trait;
326     PinTypeLangItem,             "pin",                pin_type,                Target::Struct;
327
328     EqTraitLangItem,             "eq",                 eq_trait,                Target::Trait;
329     PartialOrdTraitLangItem,     "partial_ord",        partial_ord_trait,       Target::Trait;
330     OrdTraitLangItem,            "ord",                ord_trait,               Target::Trait;
331
332     // A number of panic-related lang items. The `panic` item corresponds to
333     // divide-by-zero and various panic cases with `match`. The
334     // `panic_bounds_check` item is for indexing arrays.
335     //
336     // The `begin_unwind` lang item has a predefined symbol name and is sort of
337     // a "weak lang item" in the sense that a crate is not required to have it
338     // defined to use it, but a final product is required to define it
339     // somewhere. Additionally, there are restrictions on crates that use a weak
340     // lang item, but do not have it defined.
341     PanicFnLangItem,             "panic",              panic_fn,                Target::Fn;
342     PanicBoundsCheckFnLangItem,  "panic_bounds_check", panic_bounds_check_fn,   Target::Fn;
343     PanicInfoLangItem,           "panic_info",         panic_info,              Target::Struct;
344     PanicImplLangItem,           "panic_impl",         panic_impl,              Target::Fn;
345     // Libstd panic entry point. Necessary for const eval to be able to catch it
346     BeginPanicFnLangItem,        "begin_panic",        begin_panic_fn,          Target::Fn;
347
348     ExchangeMallocFnLangItem,    "exchange_malloc",    exchange_malloc_fn,      Target::Fn;
349     BoxFreeFnLangItem,           "box_free",           box_free_fn,             Target::Fn;
350     DropInPlaceFnLangItem,       "drop_in_place",      drop_in_place_fn,        Target::Fn;
351     OomLangItem,                 "oom",                oom,                     Target::Fn;
352     AllocLayoutLangItem,         "alloc_layout",       alloc_layout,            Target::Struct;
353
354     StartFnLangItem,             "start",              start_fn,                Target::Fn;
355
356     EhPersonalityLangItem,       "eh_personality",     eh_personality,          Target::Fn;
357     EhUnwindResumeLangItem,      "eh_unwind_resume",   eh_unwind_resume,        Target::Fn;
358     MSVCTryFilterLangItem,       "msvc_try_filter",    msvc_try_filter,         Target::Static;
359
360     OwnedBoxLangItem,            "owned_box",          owned_box,               Target::Struct;
361
362     PhantomDataItem,             "phantom_data",       phantom_data,            Target::Struct;
363
364     ManuallyDropItem,            "manually_drop",      manually_drop,           Target::Struct;
365
366     DebugTraitLangItem,          "debug_trait",        debug_trait,             Target::Trait;
367
368     // A lang item for each of the 128-bit operators we can optionally lower.
369     I128AddFnLangItem,           "i128_add",           i128_add_fn,             Target::Fn;
370     U128AddFnLangItem,           "u128_add",           u128_add_fn,             Target::Fn;
371     I128SubFnLangItem,           "i128_sub",           i128_sub_fn,             Target::Fn;
372     U128SubFnLangItem,           "u128_sub",           u128_sub_fn,             Target::Fn;
373     I128MulFnLangItem,           "i128_mul",           i128_mul_fn,             Target::Fn;
374     U128MulFnLangItem,           "u128_mul",           u128_mul_fn,             Target::Fn;
375     I128DivFnLangItem,           "i128_div",           i128_div_fn,             Target::Fn;
376     U128DivFnLangItem,           "u128_div",           u128_div_fn,             Target::Fn;
377     I128RemFnLangItem,           "i128_rem",           i128_rem_fn,             Target::Fn;
378     U128RemFnLangItem,           "u128_rem",           u128_rem_fn,             Target::Fn;
379     I128ShlFnLangItem,           "i128_shl",           i128_shl_fn,             Target::Fn;
380     U128ShlFnLangItem,           "u128_shl",           u128_shl_fn,             Target::Fn;
381     I128ShrFnLangItem,           "i128_shr",           i128_shr_fn,             Target::Fn;
382     U128ShrFnLangItem,           "u128_shr",           u128_shr_fn,             Target::Fn;
383     // And overflow versions for the operators that are checkable.
384     // While MIR calls these Checked*, they return (T,bool), not Option<T>.
385     I128AddoFnLangItem,          "i128_addo",          i128_addo_fn,            Target::Fn;
386     U128AddoFnLangItem,          "u128_addo",          u128_addo_fn,            Target::Fn;
387     I128SuboFnLangItem,          "i128_subo",          i128_subo_fn,            Target::Fn;
388     U128SuboFnLangItem,          "u128_subo",          u128_subo_fn,            Target::Fn;
389     I128MuloFnLangItem,          "i128_mulo",          i128_mulo_fn,            Target::Fn;
390     U128MuloFnLangItem,          "u128_mulo",          u128_mulo_fn,            Target::Fn;
391     I128ShloFnLangItem,          "i128_shlo",          i128_shlo_fn,            Target::Fn;
392     U128ShloFnLangItem,          "u128_shlo",          u128_shlo_fn,            Target::Fn;
393     I128ShroFnLangItem,          "i128_shro",          i128_shro_fn,            Target::Fn;
394     U128ShroFnLangItem,          "u128_shro",          u128_shro_fn,            Target::Fn;
395
396     // Align offset for stride != 1, must not panic.
397     AlignOffsetLangItem,         "align_offset",       align_offset_fn,         Target::Fn;
398
399     TerminationTraitLangItem,    "termination",        termination,             Target::Trait;
400
401     Arc,                         "arc",                arc,                     Target::Struct;
402     Rc,                          "rc",                 rc,                      Target::Struct;
403 }
404
405 impl<'a, 'tcx, 'gcx> TyCtxt<'a, 'tcx, 'gcx> {
406     /// Returns the `DefId` for a given `LangItem`.
407     /// If not found, fatally abort compilation.
408     pub fn require_lang_item(&self, lang_item: LangItem) -> DefId {
409         self.lang_items().require(lang_item).unwrap_or_else(|msg| {
410             self.sess.fatal(&msg)
411         })
412     }
413 }