diff options
| -rw-r--r-- | src/librustc/ich/impls_hir.rs | 8 | ||||
| -rw-r--r-- | src/librustc/middle/lang_items.rs | 324 | ||||
| -rw-r--r-- | src/librustc/middle/weak_lang_items.rs | 2 | ||||
| -rw-r--r-- | src/librustc/traits/select.rs | 7 | ||||
| -rw-r--r-- | src/librustc/ty/instance.rs | 2 | ||||
| -rw-r--r-- | src/librustc/ty/print/pretty.rs | 2 | ||||
| -rw-r--r-- | src/librustc_lang_items/lang_items.rs | 261 | ||||
| -rw-r--r-- | src/librustc_lang_items/lib.rs | 41 | ||||
| -rw-r--r-- | src/librustc_mir/shim.rs | 2 | ||||
| -rw-r--r-- | src/librustc_typeck/check/closure.rs | 9 | ||||
| -rw-r--r-- | src/librustdoc/clean/utils.rs | 2 |
11 files changed, 372 insertions, 288 deletions
diff --git a/src/librustc/ich/impls_hir.rs b/src/librustc/ich/impls_hir.rs index 625d8a4670f..fa50632e9db 100644 --- a/src/librustc/ich/impls_hir.rs +++ b/src/librustc/ich/impls_hir.rs @@ -107,6 +107,8 @@ impl<'ctx> rustc_hir::HashStableContext for StableHashingContext<'ctx> { } } +impl<'ctx> rustc_lang_items::HashStableContext for StableHashingContext<'ctx> {} + impl<'a> ToStableHashKey<StableHashingContext<'a>> for DefId { type KeyType = DefPathHash; @@ -251,12 +253,6 @@ impl<'a> ToStableHashKey<StableHashingContext<'a>> for hir::def_id::DefIndex { } } -impl<'a> HashStable<StableHashingContext<'a>> for crate::middle::lang_items::LangItem { - fn hash_stable(&self, _: &mut StableHashingContext<'a>, hasher: &mut StableHasher) { - ::std::hash::Hash::hash(self, hasher); - } -} - impl<'a> HashStable<StableHashingContext<'a>> for hir::TraitCandidate { fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) { hcx.with_node_id_hashing_mode(NodeIdHashingMode::HashDefPath, |hcx| { diff --git a/src/librustc/middle/lang_items.rs b/src/librustc/middle/lang_items.rs index b697ef01552..aa235f545b5 100644 --- a/src/librustc/middle/lang_items.rs +++ b/src/librustc/middle/lang_items.rs @@ -13,139 +13,64 @@ use crate::middle::cstore::ExternCrate; use crate::middle::weak_lang_items; use crate::ty::{self, TyCtxt}; -use rustc_data_structures::fx::FxHashMap; use rustc_errors::struct_span_err; use rustc_hir as hir; use rustc_hir::def_id::DefId; use rustc_hir::itemlikevisit::ItemLikeVisitor; +use rustc_lang_items::lang_items::ITEM_REFS; use rustc_lang_items::Target; -use rustc_macros::HashStable; use rustc_span::symbol::{sym, Symbol}; use rustc_span::Span; use syntax::ast; -// The actual lang items defined come at the end of this file in one handy table. -// So you probably just want to nip down to the end. -macro_rules! language_item_table { - ( - $( $variant:ident, $name:expr, $method:ident, $target:path; )* - ) => { - -enum_from_u32! { - /// A representation of all the valid language items in Rust. - #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)] - pub enum LangItem { - $($variant,)* - } -} - -impl LangItem { - /// Returns the `name` in `#[lang = "$name"]`. - /// For example, `LangItem::EqTraitLangItem`, - /// that is `#[lang = "eq"]` would result in `"eq"`. - fn name(self) -> &'static str { - match self { - $( $variant => $name, )* - } - } -} - -#[derive(HashStable)] -pub struct LanguageItems { - /// Mappings from lang items to their possibly found `DefId`s. - /// The index corresponds to the order in `LangItem`. - pub items: Vec<Option<DefId>>, - /// Lang items that were not found during collection. - pub missing: Vec<LangItem>, -} - -impl LanguageItems { - /// Construct an empty collection of lang items and no missing ones. - pub fn new() -> Self { - fn init_none(_: LangItem) -> Option<DefId> { None } - - Self { - items: vec![$(init_none($variant)),*], - missing: Vec::new(), - } - } - - /// Returns the mappings to the possibly found `DefId`s for each lang item. - pub fn items(&self) -> &[Option<DefId>] { - &*self.items - } - - /// Requires that a given `LangItem` was bound and returns the corresponding `DefId`. - /// If it wasn't bound, e.g. due to a missing `#[lang = "<it.name()>"]`, - /// returns an error message as a string. - pub fn require(&self, it: LangItem) -> Result<DefId, String> { - self.items[it as usize].ok_or_else(|| format!("requires `{}` lang_item", it.name())) - } - - /// Returns the kind of closure that `id`, which is one of the `Fn*` traits, corresponds to. - /// If `id` is not one of the `Fn*` traits, `None` is returned. - pub fn fn_trait_kind(&self, id: DefId) -> Option<ty::ClosureKind> { - match Some(id) { - x if x == self.fn_trait() => Some(ty::ClosureKind::Fn), - x if x == self.fn_mut_trait() => Some(ty::ClosureKind::FnMut), - x if x == self.fn_once_trait() => Some(ty::ClosureKind::FnOnce), - _ => None - } - } - - $( - /// Returns the corresponding `DefId` for the lang item - #[doc = $name] - /// if it exists. - #[allow(dead_code)] - pub fn $method(&self) -> Option<DefId> { - self.items[$variant as usize] - } - )* -} +pub use rustc_lang_items::{LangItem, LanguageItems}; struct LanguageItemCollector<'tcx> { items: LanguageItems, tcx: TyCtxt<'tcx>, - /// A mapping from the name of the lang item to its order and the form it must be of. - item_refs: FxHashMap<&'static str, (usize, Target)>, } impl ItemLikeVisitor<'v> for LanguageItemCollector<'tcx> { fn visit_item(&mut self, item: &hir::Item<'_>) { if let Some((value, span)) = extract(&item.attrs) { let actual_target = Target::from_item(item); - match self.item_refs.get(&*value.as_str()).cloned() { + match ITEM_REFS.get(&*value.as_str()).cloned() { // Known lang item with attribute on correct target. Some((item_index, expected_target)) if actual_target == expected_target => { let def_id = self.tcx.hir().local_def_id(item.hir_id); self.collect_item(item_index, def_id); - }, + } // Known lang item with attribute on incorrect target. Some((_, expected_target)) => { struct_span_err!( - self.tcx.sess, span, E0718, + self.tcx.sess, + span, + E0718, "`{}` language item must be applied to a {}", - value, expected_target, - ).span_label( + value, + expected_target, + ) + .span_label( span, format!( "attribute should be applied to a {}, not a {}", expected_target, actual_target, ), - ).emit(); - }, + ) + .emit(); + } // Unknown lang item. _ => { struct_span_err!( - self.tcx.sess, span, E0522, + self.tcx.sess, + span, + E0522, "definition of an unknown language item: `{}`", value - ).span_label( - span, - format!("definition of unknown language item `{}`", value) - ).emit(); - }, + ) + .span_label(span, format!("definition of unknown language item `{}`", value)) + .emit(); + } } } } @@ -161,15 +86,7 @@ impl ItemLikeVisitor<'v> for LanguageItemCollector<'tcx> { impl LanguageItemCollector<'tcx> { fn new(tcx: TyCtxt<'tcx>) -> LanguageItemCollector<'tcx> { - let mut item_refs = FxHashMap::default(); - - $( item_refs.insert($name, ($variant as usize, $target)); )* - - LanguageItemCollector { - tcx, - items: LanguageItems::new(), - item_refs, - } + LanguageItemCollector { tcx, items: LanguageItems::new() } } fn collect_item(&mut self, item_index: usize, item_def_id: DefId) { @@ -185,37 +102,37 @@ impl LanguageItemCollector<'tcx> { "found duplicate lang item `{}`", name ), - None => { - match self.tcx.extern_crate(item_def_id) { - Some(ExternCrate {dependency_of, ..}) => { - self.tcx.sess.struct_err(&format!( + None => match self.tcx.extern_crate(item_def_id) { + Some(ExternCrate { dependency_of, .. }) => { + self.tcx.sess.struct_err(&format!( "duplicate lang item in crate `{}` (which `{}` depends on): `{}`.", self.tcx.crate_name(item_def_id.krate), self.tcx.crate_name(*dependency_of), - name)) - }, - _ => { - self.tcx.sess.struct_err(&format!( - "duplicate lang item in crate `{}`: `{}`.", - self.tcx.crate_name(item_def_id.krate), - name)) - } + name + )) } + _ => self.tcx.sess.struct_err(&format!( + "duplicate lang item in crate `{}`: `{}`.", + self.tcx.crate_name(item_def_id.krate), + name + )), }, }; if let Some(span) = self.tcx.hir().span_if_local(original_def_id) { err.span_note(span, "the lang item is first defined here"); } else { match self.tcx.extern_crate(original_def_id) { - Some(ExternCrate {dependency_of, ..}) => { + Some(ExternCrate { dependency_of, .. }) => { err.note(&format!( "the lang item is first defined in crate `{}` (which `{}` depends on)", self.tcx.crate_name(original_def_id.krate), self.tcx.crate_name(*dependency_of))); - }, + } _ => { - err.note(&format!("the lang item is first defined in crate `{}`.", - self.tcx.crate_name(original_def_id.krate))); + err.note(&format!( + "the lang item is first defined in crate `{}`.", + self.tcx.crate_name(original_def_id.krate) + )); } } } @@ -232,12 +149,14 @@ impl LanguageItemCollector<'tcx> { /// The attributes `#[panic_handler]` and `#[alloc_error_handler]` /// are also extracted out when found. pub fn extract(attrs: &[ast::Attribute]) -> Option<(Symbol, Span)> { - attrs.iter().find_map(|attr| Some(match attr { - _ if attr.check_name(sym::lang) => (attr.value_str()?, attr.span), - _ if attr.check_name(sym::panic_handler) => (sym::panic_impl, attr.span), - _ if attr.check_name(sym::alloc_error_handler) => (sym::oom, attr.span), - _ => return None, - })) + attrs.iter().find_map(|attr| { + Some(match attr { + _ if attr.check_name(sym::lang) => (attr.value_str()?, attr.span), + _ if attr.check_name(sym::panic_handler) => (sym::panic_impl, attr.span), + _ if attr.check_name(sym::alloc_error_handler) => (sym::oom, attr.span), + _ => return None, + }) + }) } /// Traverses and collects all the lang items in all crates. @@ -264,147 +183,6 @@ pub fn collect<'tcx>(tcx: TyCtxt<'tcx>) -> LanguageItems { items } -// End of the macro - } -} - -language_item_table! { -// Variant name, Name, Method name, Target; - BoolImplItem, "bool", bool_impl, Target::Impl; - CharImplItem, "char", char_impl, Target::Impl; - StrImplItem, "str", str_impl, Target::Impl; - SliceImplItem, "slice", slice_impl, Target::Impl; - SliceU8ImplItem, "slice_u8", slice_u8_impl, Target::Impl; - StrAllocImplItem, "str_alloc", str_alloc_impl, Target::Impl; - SliceAllocImplItem, "slice_alloc", slice_alloc_impl, Target::Impl; - SliceU8AllocImplItem, "slice_u8_alloc", slice_u8_alloc_impl, Target::Impl; - ConstPtrImplItem, "const_ptr", const_ptr_impl, Target::Impl; - MutPtrImplItem, "mut_ptr", mut_ptr_impl, Target::Impl; - I8ImplItem, "i8", i8_impl, Target::Impl; - I16ImplItem, "i16", i16_impl, Target::Impl; - I32ImplItem, "i32", i32_impl, Target::Impl; - I64ImplItem, "i64", i64_impl, Target::Impl; - I128ImplItem, "i128", i128_impl, Target::Impl; - IsizeImplItem, "isize", isize_impl, Target::Impl; - U8ImplItem, "u8", u8_impl, Target::Impl; - U16ImplItem, "u16", u16_impl, Target::Impl; - U32ImplItem, "u32", u32_impl, Target::Impl; - U64ImplItem, "u64", u64_impl, Target::Impl; - U128ImplItem, "u128", u128_impl, Target::Impl; - UsizeImplItem, "usize", usize_impl, Target::Impl; - F32ImplItem, "f32", f32_impl, Target::Impl; - F64ImplItem, "f64", f64_impl, Target::Impl; - F32RuntimeImplItem, "f32_runtime", f32_runtime_impl, Target::Impl; - F64RuntimeImplItem, "f64_runtime", f64_runtime_impl, Target::Impl; - - SizedTraitLangItem, "sized", sized_trait, Target::Trait; - UnsizeTraitLangItem, "unsize", unsize_trait, Target::Trait; - // trait injected by #[derive(PartialEq)], (i.e. "Partial EQ"). - StructuralPeqTraitLangItem, "structural_peq", structural_peq_trait, Target::Trait; - // trait injected by #[derive(Eq)], (i.e. "Total EQ"; no, I will not apologize). - StructuralTeqTraitLangItem, "structural_teq", structural_teq_trait, Target::Trait; - CopyTraitLangItem, "copy", copy_trait, Target::Trait; - CloneTraitLangItem, "clone", clone_trait, Target::Trait; - SyncTraitLangItem, "sync", sync_trait, Target::Trait; - FreezeTraitLangItem, "freeze", freeze_trait, Target::Trait; - - DropTraitLangItem, "drop", drop_trait, Target::Trait; - - CoerceUnsizedTraitLangItem, "coerce_unsized", coerce_unsized_trait, Target::Trait; - DispatchFromDynTraitLangItem,"dispatch_from_dyn", dispatch_from_dyn_trait, Target::Trait; - - AddTraitLangItem, "add", add_trait, Target::Trait; - SubTraitLangItem, "sub", sub_trait, Target::Trait; - MulTraitLangItem, "mul", mul_trait, Target::Trait; - DivTraitLangItem, "div", div_trait, Target::Trait; - RemTraitLangItem, "rem", rem_trait, Target::Trait; - NegTraitLangItem, "neg", neg_trait, Target::Trait; - NotTraitLangItem, "not", not_trait, Target::Trait; - BitXorTraitLangItem, "bitxor", bitxor_trait, Target::Trait; - BitAndTraitLangItem, "bitand", bitand_trait, Target::Trait; - BitOrTraitLangItem, "bitor", bitor_trait, Target::Trait; - ShlTraitLangItem, "shl", shl_trait, Target::Trait; - ShrTraitLangItem, "shr", shr_trait, Target::Trait; - AddAssignTraitLangItem, "add_assign", add_assign_trait, Target::Trait; - SubAssignTraitLangItem, "sub_assign", sub_assign_trait, Target::Trait; - MulAssignTraitLangItem, "mul_assign", mul_assign_trait, Target::Trait; - DivAssignTraitLangItem, "div_assign", div_assign_trait, Target::Trait; - RemAssignTraitLangItem, "rem_assign", rem_assign_trait, Target::Trait; - BitXorAssignTraitLangItem, "bitxor_assign", bitxor_assign_trait, Target::Trait; - BitAndAssignTraitLangItem, "bitand_assign", bitand_assign_trait, Target::Trait; - BitOrAssignTraitLangItem, "bitor_assign", bitor_assign_trait, Target::Trait; - ShlAssignTraitLangItem, "shl_assign", shl_assign_trait, Target::Trait; - ShrAssignTraitLangItem, "shr_assign", shr_assign_trait, Target::Trait; - IndexTraitLangItem, "index", index_trait, Target::Trait; - IndexMutTraitLangItem, "index_mut", index_mut_trait, Target::Trait; - - UnsafeCellTypeLangItem, "unsafe_cell", unsafe_cell_type, Target::Struct; - VaListTypeLangItem, "va_list", va_list, Target::Struct; - - DerefTraitLangItem, "deref", deref_trait, Target::Trait; - DerefMutTraitLangItem, "deref_mut", deref_mut_trait, Target::Trait; - ReceiverTraitLangItem, "receiver", receiver_trait, Target::Trait; - - FnTraitLangItem, "fn", fn_trait, Target::Trait; - FnMutTraitLangItem, "fn_mut", fn_mut_trait, Target::Trait; - FnOnceTraitLangItem, "fn_once", fn_once_trait, Target::Trait; - - FutureTraitLangItem, "future_trait", future_trait, Target::Trait; - GeneratorStateLangItem, "generator_state", gen_state, Target::Enum; - GeneratorTraitLangItem, "generator", gen_trait, Target::Trait; - UnpinTraitLangItem, "unpin", unpin_trait, Target::Trait; - PinTypeLangItem, "pin", pin_type, Target::Struct; - - // Don't be fooled by the naming here: this lang item denotes `PartialEq`, not `Eq`. - EqTraitLangItem, "eq", eq_trait, Target::Trait; - PartialOrdTraitLangItem, "partial_ord", partial_ord_trait, Target::Trait; - - // A number of panic-related lang items. The `panic` item corresponds to - // divide-by-zero and various panic cases with `match`. The - // `panic_bounds_check` item is for indexing arrays. - // - // The `begin_unwind` lang item has a predefined symbol name and is sort of - // a "weak lang item" in the sense that a crate is not required to have it - // defined to use it, but a final product is required to define it - // somewhere. Additionally, there are restrictions on crates that use a weak - // lang item, but do not have it defined. - PanicFnLangItem, "panic", panic_fn, Target::Fn; - PanicBoundsCheckFnLangItem, "panic_bounds_check", panic_bounds_check_fn, Target::Fn; - PanicInfoLangItem, "panic_info", panic_info, Target::Struct; - PanicLocationLangItem, "panic_location", panic_location, Target::Struct; - PanicImplLangItem, "panic_impl", panic_impl, Target::Fn; - // Libstd panic entry point. Necessary for const eval to be able to catch it - BeginPanicFnLangItem, "begin_panic", begin_panic_fn, Target::Fn; - - ExchangeMallocFnLangItem, "exchange_malloc", exchange_malloc_fn, Target::Fn; - BoxFreeFnLangItem, "box_free", box_free_fn, Target::Fn; - DropInPlaceFnLangItem, "drop_in_place", drop_in_place_fn, Target::Fn; - OomLangItem, "oom", oom, Target::Fn; - AllocLayoutLangItem, "alloc_layout", alloc_layout, Target::Struct; - - StartFnLangItem, "start", start_fn, Target::Fn; - - EhPersonalityLangItem, "eh_personality", eh_personality, Target::Fn; - EhUnwindResumeLangItem, "eh_unwind_resume", eh_unwind_resume, Target::Fn; - EhCatchTypeinfoLangItem, "eh_catch_typeinfo", eh_catch_typeinfo, Target::Static; - - OwnedBoxLangItem, "owned_box", owned_box, Target::Struct; - - PhantomDataItem, "phantom_data", phantom_data, Target::Struct; - - ManuallyDropItem, "manually_drop", manually_drop, Target::Struct; - - MaybeUninitLangItem, "maybe_uninit", maybe_uninit, Target::Union; - - // Align offset for stride != 1; must not panic. - AlignOffsetLangItem, "align_offset", align_offset_fn, Target::Fn; - - TerminationTraitLangItem, "termination", termination, Target::Trait; - - Arc, "arc", arc, Target::Struct; - Rc, "rc", rc, Target::Struct; -} - impl<'tcx> TyCtxt<'tcx> { /// Returns the `DefId` for a given `LangItem`. /// If not found, fatally aborts compilation. @@ -417,4 +195,14 @@ impl<'tcx> TyCtxt<'tcx> { } }) } + + pub fn fn_trait_lang_item(&self, id: DefId) -> Option<ty::ClosureKind> { + let items = self.lang_items(); + match Some(id) { + x if x == items.fn_trait() => Some(ty::ClosureKind::Fn), + x if x == items.fn_mut_trait() => Some(ty::ClosureKind::FnMut), + x if x == items.fn_once_trait() => Some(ty::ClosureKind::FnOnce), + _ => None, + } + } } diff --git a/src/librustc/middle/weak_lang_items.rs b/src/librustc/middle/weak_lang_items.rs index 5571f8f2313..82bf0d200d7 100644 --- a/src/librustc/middle/weak_lang_items.rs +++ b/src/librustc/middle/weak_lang_items.rs @@ -46,7 +46,7 @@ pub fn check_crate<'tcx>(tcx: TyCtxt<'tcx>, } pub fn link_name(attrs: &[ast::Attribute]) -> Option<Symbol> { - lang_items::extract(attrs).and_then(|(name, _)| { + rustc_lang_items::lang_items::extract(attrs).and_then(|(name, _)| { $(if name == sym::$name { Some(sym::$sym) } else)* { diff --git a/src/librustc/traits/select.rs b/src/librustc/traits/select.rs index 17337ff0c92..4c4947d134f 100644 --- a/src/librustc/traits/select.rs +++ b/src/librustc/traits/select.rs @@ -1634,7 +1634,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { obligation: &TraitObligation<'tcx>, candidates: &mut SelectionCandidateSet<'tcx>, ) -> Result<(), SelectionError<'tcx>> { - let kind = match self.tcx().lang_items().fn_trait_kind(obligation.predicate.def_id()) { + let kind = match self.tcx().fn_trait_lang_item(obligation.predicate.def_id()) { Some(k) => k, None => { return Ok(()); @@ -1677,7 +1677,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { candidates: &mut SelectionCandidateSet<'tcx>, ) -> Result<(), SelectionError<'tcx>> { // We provide impl of all fn traits for fn pointers. - if self.tcx().lang_items().fn_trait_kind(obligation.predicate.def_id()).is_none() { + if self.tcx().fn_trait_lang_item(obligation.predicate.def_id()).is_none() { return Ok(()); } @@ -2889,8 +2889,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { let kind = self .tcx() - .lang_items() - .fn_trait_kind(obligation.predicate.def_id()) + .fn_trait_lang_item(obligation.predicate.def_id()) .unwrap_or_else(|| bug!("closure candidate for non-fn trait {:?}", obligation)); // Okay to skip binder because the substs on closure types never diff --git a/src/librustc/ty/instance.rs b/src/librustc/ty/instance.rs index c7f19513f66..b175de6e0e8 100644 --- a/src/librustc/ty/instance.rs +++ b/src/librustc/ty/instance.rs @@ -450,7 +450,7 @@ fn resolve_associated_item<'tcx>( substs: generator_data.substs, }), traits::VtableClosure(closure_data) => { - let trait_closure_kind = tcx.lang_items().fn_trait_kind(trait_id).unwrap(); + let trait_closure_kind = tcx.fn_trait_lang_item(trait_id).unwrap(); Some(Instance::resolve_closure( tcx, closure_data.closure_def_id, diff --git a/src/librustc/ty/print/pretty.rs b/src/librustc/ty/print/pretty.rs index 0da680d1f91..2a43d0486d0 100644 --- a/src/librustc/ty/print/pretty.rs +++ b/src/librustc/ty/print/pretty.rs @@ -724,7 +724,7 @@ pub trait PrettyPrinter<'tcx>: let mut resugared = false; // Special-case `Fn(...) -> ...` and resugar it. - let fn_trait_kind = self.tcx().lang_items().fn_trait_kind(principal.def_id); + let fn_trait_kind = self.tcx().fn_trait_lang_item(principal.def_id); if !self.tcx().sess.verbose() && fn_trait_kind.is_some() { if let ty::Tuple(ref args) = principal.substs.type_at(0).kind { let mut projections = predicates.projection_bounds(); diff --git a/src/librustc_lang_items/lang_items.rs b/src/librustc_lang_items/lang_items.rs new file mode 100644 index 00000000000..6cccad73fcb --- /dev/null +++ b/src/librustc_lang_items/lang_items.rs @@ -0,0 +1,261 @@ +//! Detecting language items. +//! +//! Language items are items that represent concepts intrinsic to the language +//! itself. Examples are: +//! +//! * Traits that specify "kinds"; e.g., `Sync`, `Send`. +//! * Traits that represent operators; e.g., `Add`, `Sub`, `Index`. +//! * Functions called by the compiler itself. + +pub use self::LangItem::*; + +use crate::Target; + +use rustc_data_structures::fx::FxHashMap; +use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; +use rustc_hir::def_id::DefId; +use rustc_macros::HashStable_Generic; +use rustc_span::symbol::{sym, Symbol}; +use rustc_span::Span; +use syntax::ast; + +use lazy_static::lazy_static; + +// The actual lang items defined come at the end of this file in one handy table. +// So you probably just want to nip down to the end. +macro_rules! language_item_table { + ( + $( $variant:ident, $name:expr, $method:ident, $target:path; )* + ) => { + +enum_from_u32! { + /// A representation of all the valid language items in Rust. + #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)] + pub enum LangItem { + $($variant,)* + } +} + +impl LangItem { + /// Returns the `name` in `#[lang = "$name"]`. + /// For example, `LangItem::EqTraitLangItem`, + /// that is `#[lang = "eq"]` would result in `"eq"`. + pub fn name(self) -> &'static str { + match self { + $( $variant => $name, )* + } + } +} + +#[derive(HashStable_Generic)] +pub struct LanguageItems { + /// Mappings from lang items to their possibly found `DefId`s. + /// The index corresponds to the order in `LangItem`. + pub items: Vec<Option<DefId>>, + /// Lang items that were not found during collection. + pub missing: Vec<LangItem>, +} + +impl LanguageItems { + /// Construct an empty collection of lang items and no missing ones. + pub fn new() -> Self { + fn init_none(_: LangItem) -> Option<DefId> { None } + + Self { + items: vec![$(init_none($variant)),*], + missing: Vec::new(), + } + } + + /// Returns the mappings to the possibly found `DefId`s for each lang item. + pub fn items(&self) -> &[Option<DefId>] { + &*self.items + } + + /// Requires that a given `LangItem` was bound and returns the corresponding `DefId`. + /// If it wasn't bound, e.g. due to a missing `#[lang = "<it.name()>"]`, + /// returns an error message as a string. + pub fn require(&self, it: LangItem) -> Result<DefId, String> { + self.items[it as usize].ok_or_else(|| format!("requires `{}` lang_item", it.name())) + } + + $( + /// Returns the corresponding `DefId` for the lang item + #[doc = $name] + /// if it exists. + #[allow(dead_code)] + pub fn $method(&self) -> Option<DefId> { + self.items[$variant as usize] + } + )* +} + +lazy_static! { + /// A mapping from the name of the lang item to its order and the form it must be of. + pub static ref ITEM_REFS: FxHashMap<&'static str, (usize, Target)> = { + let mut item_refs = FxHashMap::default(); + $( item_refs.insert($name, ($variant as usize, $target)); )* + item_refs + }; +} + +// End of the macro + } +} + +impl<CTX> HashStable<CTX> for LangItem { + fn hash_stable(&self, _: &mut CTX, hasher: &mut StableHasher) { + ::std::hash::Hash::hash(self, hasher); + } +} + +/// Extracts the first `lang = "$name"` out of a list of attributes. +/// The attributes `#[panic_handler]` and `#[alloc_error_handler]` +/// are also extracted out when found. +pub fn extract(attrs: &[ast::Attribute]) -> Option<(Symbol, Span)> { + attrs.iter().find_map(|attr| { + Some(match attr { + _ if attr.check_name(sym::lang) => (attr.value_str()?, attr.span), + _ if attr.check_name(sym::panic_handler) => (sym::panic_impl, attr.span), + _ if attr.check_name(sym::alloc_error_handler) => (sym::oom, attr.span), + _ => return None, + }) + }) +} + +language_item_table! { +// Variant name, Name, Method name, Target; + BoolImplItem, "bool", bool_impl, Target::Impl; + CharImplItem, "char", char_impl, Target::Impl; + StrImplItem, "str", str_impl, Target::Impl; + SliceImplItem, "slice", slice_impl, Target::Impl; + SliceU8ImplItem, "slice_u8", slice_u8_impl, Target::Impl; + StrAllocImplItem, "str_alloc", str_alloc_impl, Target::Impl; + SliceAllocImplItem, "slice_alloc", slice_alloc_impl, Target::Impl; + SliceU8AllocImplItem, "slice_u8_alloc", slice_u8_alloc_impl, Target::Impl; + ConstPtrImplItem, "const_ptr", const_ptr_impl, Target::Impl; + MutPtrImplItem, "mut_ptr", mut_ptr_impl, Target::Impl; + I8ImplItem, "i8", i8_impl, Target::Impl; + I16ImplItem, "i16", i16_impl, Target::Impl; + I32ImplItem, "i32", i32_impl, Target::Impl; + I64ImplItem, "i64", i64_impl, Target::Impl; + I128ImplItem, "i128", i128_impl, Target::Impl; + IsizeImplItem, "isize", isize_impl, Target::Impl; + U8ImplItem, "u8", u8_impl, Target::Impl; + U16ImplItem, "u16", u16_impl, Target::Impl; + U32ImplItem, "u32", u32_impl, Target::Impl; + U64ImplItem, "u64", u64_impl, Target::Impl; + U128ImplItem, "u128", u128_impl, Target::Impl; + UsizeImplItem, "usize", usize_impl, Target::Impl; + F32ImplItem, "f32", f32_impl, Target::Impl; + F64ImplItem, "f64", f64_impl, Target::Impl; + F32RuntimeImplItem, "f32_runtime", f32_runtime_impl, Target::Impl; + F64RuntimeImplItem, "f64_runtime", f64_runtime_impl, Target::Impl; + + SizedTraitLangItem, "sized", sized_trait, Target::Trait; + UnsizeTraitLangItem, "unsize", unsize_trait, Target::Trait; + // trait injected by #[derive(PartialEq)], (i.e. "Partial EQ"). + StructuralPeqTraitLangItem, "structural_peq", structural_peq_trait, Target::Trait; + // trait injected by #[derive(Eq)], (i.e. "Total EQ"; no, I will not apologize). + StructuralTeqTraitLangItem, "structural_teq", structural_teq_trait, Target::Trait; + CopyTraitLangItem, "copy", copy_trait, Target::Trait; + CloneTraitLangItem, "clone", clone_trait, Target::Trait; + SyncTraitLangItem, "sync", sync_trait, Target::Trait; + FreezeTraitLangItem, "freeze", freeze_trait, Target::Trait; + + DropTraitLangItem, "drop", drop_trait, Target::Trait; + + CoerceUnsizedTraitLangItem, "coerce_unsized", coerce_unsized_trait, Target::Trait; + DispatchFromDynTraitLangItem,"dispatch_from_dyn", dispatch_from_dyn_trait, Target::Trait; + + AddTraitLangItem, "add", add_trait, Target::Trait; + SubTraitLangItem, "sub", sub_trait, Target::Trait; + MulTraitLangItem, "mul", mul_trait, Target::Trait; + DivTraitLangItem, "div", div_trait, Target::Trait; + RemTraitLangItem, "rem", rem_trait, Target::Trait; + NegTraitLangItem, "neg", neg_trait, Target::Trait; + NotTraitLangItem, "not", not_trait, Target::Trait; + BitXorTraitLangItem, "bitxor", bitxor_trait, Target::Trait; + BitAndTraitLangItem, "bitand", bitand_trait, Target::Trait; + BitOrTraitLangItem, "bitor", bitor_trait, Target::Trait; + ShlTraitLangItem, "shl", shl_trait, Target::Trait; + ShrTraitLangItem, "shr", shr_trait, Target::Trait; + AddAssignTraitLangItem, "add_assign", add_assign_trait, Target::Trait; + SubAssignTraitLangItem, "sub_assign", sub_assign_trait, Target::Trait; + MulAssignTraitLangItem, "mul_assign", mul_assign_trait, Target::Trait; + DivAssignTraitLangItem, "div_assign", div_assign_trait, Target::Trait; + RemAssignTraitLangItem, "rem_assign", rem_assign_trait, Target::Trait; + BitXorAssignTraitLangItem, "bitxor_assign", bitxor_assign_trait, Target::Trait; + BitAndAssignTraitLangItem, "bitand_assign", bitand_assign_trait, Target::Trait; + BitOrAssignTraitLangItem, "bitor_assign", bitor_assign_trait, Target::Trait; + ShlAssignTraitLangItem, "shl_assign", shl_assign_trait, Target::Trait; + ShrAssignTraitLangItem, "shr_assign", shr_assign_trait, Target::Trait; + IndexTraitLangItem, "index", index_trait, Target::Trait; + IndexMutTraitLangItem, "index_mut", index_mut_trait, Target::Trait; + + UnsafeCellTypeLangItem, "unsafe_cell", unsafe_cell_type, Target::Struct; + VaListTypeLangItem, "va_list", va_list, Target::Struct; + + DerefTraitLangItem, "deref", deref_trait, Target::Trait; + DerefMutTraitLangItem, "deref_mut", deref_mut_trait, Target::Trait; + ReceiverTraitLangItem, "receiver", receiver_trait, Target::Trait; + + FnTraitLangItem, "fn", fn_trait, Target::Trait; + FnMutTraitLangItem, "fn_mut", fn_mut_trait, Target::Trait; + FnOnceTraitLangItem, "fn_once", fn_once_trait, Target::Trait; + + FutureTraitLangItem, "future_trait", future_trait, Target::Trait; + GeneratorStateLangItem, "generator_state", gen_state, Target::Enum; + GeneratorTraitLangItem, "generator", gen_trait, Target::Trait; + UnpinTraitLangItem, "unpin", unpin_trait, Target::Trait; + PinTypeLangItem, "pin", pin_type, Target::Struct; + + // Don't be fooled by the naming here: this lang item denotes `PartialEq`, not `Eq`. + EqTraitLangItem, "eq", eq_trait, Target::Trait; + PartialOrdTraitLangItem, "partial_ord", partial_ord_trait, Target::Trait; + + // A number of panic-related lang items. The `panic` item corresponds to + // divide-by-zero and various panic cases with `match`. The + // `panic_bounds_check` item is for indexing arrays. + // + // The `begin_unwind` lang item has a predefined symbol name and is sort of + // a "weak lang item" in the sense that a crate is not required to have it + // defined to use it, but a final product is required to define it + // somewhere. Additionally, there are restrictions on crates that use a weak + // lang item, but do not have it defined. + PanicFnLangItem, "panic", panic_fn, Target::Fn; + PanicBoundsCheckFnLangItem, "panic_bounds_check", panic_bounds_check_fn, Target::Fn; + PanicInfoLangItem, "panic_info", panic_info, Target::Struct; + PanicLocationLangItem, "panic_location", panic_location, Target::Struct; + PanicImplLangItem, "panic_impl", panic_impl, Target::Fn; + // Libstd panic entry point. Necessary for const eval to be able to catch it + BeginPanicFnLangItem, "begin_panic", begin_panic_fn, Target::Fn; + + ExchangeMallocFnLangItem, "exchange_malloc", exchange_malloc_fn, Target::Fn; + BoxFreeFnLangItem, "box_free", box_free_fn, Target::Fn; + DropInPlaceFnLangItem, "drop_in_place", drop_in_place_fn, Target::Fn; + OomLangItem, "oom", oom, Target::Fn; + AllocLayoutLangItem, "alloc_layout", alloc_layout, Target::Struct; + + StartFnLangItem, "start", start_fn, Target::Fn; + + EhPersonalityLangItem, "eh_personality", eh_personality, Target::Fn; + EhUnwindResumeLangItem, "eh_unwind_resume", eh_unwind_resume, Target::Fn; + EhCatchTypeinfoLangItem, "eh_catch_typeinfo", eh_catch_typeinfo, Target::Static; + + OwnedBoxLangItem, "owned_box", owned_box, Target::Struct; + + PhantomDataItem, "phantom_data", phantom_data, Target::Struct; + + ManuallyDropItem, "manually_drop", manually_drop, Target::Struct; + + MaybeUninitLangItem, "maybe_uninit", maybe_uninit, Target::Union; + + // Align offset for stride != 1; must not panic. + AlignOffsetLangItem, "align_offset", align_offset_fn, Target::Fn; + + TerminationTraitLangItem, "termination", termination, Target::Trait; + + Arc, "arc", arc, Target::Struct; + Rc, "rc", rc, Target::Struct; +} diff --git a/src/librustc_lang_items/lib.rs b/src/librustc_lang_items/lib.rs index 1d2cbefa64a..90d090e6f61 100644 --- a/src/librustc_lang_items/lib.rs +++ b/src/librustc_lang_items/lib.rs @@ -1,3 +1,44 @@ +macro_rules! enum_from_u32 { + ($(#[$attr:meta])* pub enum $name:ident { + $($variant:ident = $e:expr,)* + }) => { + $(#[$attr])* + pub enum $name { + $($variant = $e),* + } + + impl $name { + pub fn from_u32(u: u32) -> Option<$name> { + $(if u == $name::$variant as u32 { + return Some($name::$variant) + })* + None + } + } + }; + ($(#[$attr:meta])* pub enum $name:ident { + $($variant:ident,)* + }) => { + $(#[$attr])* + pub enum $name { + $($variant,)* + } + + impl $name { + pub fn from_u32(u: u32) -> Option<$name> { + $(if u == $name::$variant as u32 { + return Some($name::$variant) + })* + None + } + } + } +} + +pub mod lang_items; mod target; +pub use lang_items::{LangItem, LanguageItems}; pub use target::{MethodKind, Target}; + +pub trait HashStableContext: rustc_hir::HashStableContext {} diff --git a/src/librustc_mir/shim.rs b/src/librustc_mir/shim.rs index a8c66be359c..cc5e142351d 100644 --- a/src/librustc_mir/shim.rs +++ b/src/librustc_mir/shim.rs @@ -40,7 +40,7 @@ fn make_shim<'tcx>(tcx: TyCtxt<'tcx>, instance: ty::InstanceDef<'tcx>) -> &'tcx ), ty::InstanceDef::FnPtrShim(def_id, ty) => { let trait_ = tcx.trait_of_item(def_id).unwrap(); - let adjustment = match tcx.lang_items().fn_trait_kind(trait_) { + let adjustment = match tcx.fn_trait_lang_item(trait_) { Some(ty::ClosureKind::FnOnce) => Adjustment::Identity, Some(ty::ClosureKind::FnMut) | Some(ty::ClosureKind::Fn) => Adjustment::Deref, None => bug!("fn pointer {:?} is not an fn", ty), diff --git a/src/librustc_typeck/check/closure.rs b/src/librustc_typeck/check/closure.rs index 707125b3fd5..f601a0e8253 100644 --- a/src/librustc_typeck/check/closure.rs +++ b/src/librustc_typeck/check/closure.rs @@ -174,9 +174,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { self.deduce_sig_from_projection(None, &pb) }) .next(); - let kind = object_type - .principal_def_id() - .and_then(|did| self.tcx.lang_items().fn_trait_kind(did)); + let kind = + object_type.principal_def_id().and_then(|did| self.tcx.fn_trait_lang_item(did)); (sig, kind) } ty::Infer(ty::TyVar(vid)) => self.deduce_expectations_from_obligations(vid), @@ -214,7 +213,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { // many viable options, so pick the most restrictive. let expected_kind = self .obligations_for_self_ty(expected_vid) - .filter_map(|(tr, _)| self.tcx.lang_items().fn_trait_kind(tr.def_id())) + .filter_map(|(tr, _)| self.tcx.fn_trait_lang_item(tr.def_id())) .fold(None, |best, cur| Some(best.map_or(cur, |best| cmp::min(best, cur)))); (expected_sig, expected_kind) @@ -237,7 +236,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { let trait_ref = projection.to_poly_trait_ref(tcx); - let is_fn = tcx.lang_items().fn_trait_kind(trait_ref.def_id()).is_some(); + let is_fn = tcx.fn_trait_lang_item(trait_ref.def_id()).is_some(); let gen_trait = tcx.require_lang_item(lang_items::GeneratorTraitLangItem, cause_span); let is_gen = gen_trait == trait_ref.def_id(); if !is_fn && !is_gen { diff --git a/src/librustdoc/clean/utils.rs b/src/librustdoc/clean/utils.rs index 356660763a7..c1796744323 100644 --- a/src/librustdoc/clean/utils.rs +++ b/src/librustdoc/clean/utils.rs @@ -138,7 +138,7 @@ pub fn external_generic_args( match trait_did { // Attempt to sugar an external path like Fn<(A, B,), C> to Fn(A, B) -> C - Some(did) if cx.tcx.lang_items().fn_trait_kind(did).is_some() => { + Some(did) if cx.tcx.fn_trait_lang_item(did).is_some() => { assert!(ty_kind.is_some()); let inputs = match ty_kind { Some(ty::Tuple(ref tys)) => tys.iter().map(|t| t.expect_ty().clean(cx)).collect(), |
