diff --git a/src/librustc/middle/lang_items.rs b/src/librustc/middle/lang_items.rs index 9fbb151ccdf5e..c8e284be6fc09 100644 --- a/src/librustc/middle/lang_items.rs +++ b/src/librustc/middle/lang_items.rs @@ -31,7 +31,7 @@ impl<'tcx> TyCtxt<'tcx> { }) } - pub fn fn_trait_lang_item(&self, id: DefId) -> Option { + pub fn fn_trait_kind_from_lang_item(&self, id: DefId) -> Option { let items = self.lang_items(); match Some(id) { x if x == items.fn_trait() => Some(ty::ClosureKind::Fn), diff --git a/src/librustc/traits/select.rs b/src/librustc/traits/select.rs index 4c4947d134fcc..bf82d743c2b04 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().fn_trait_lang_item(obligation.predicate.def_id()) { + let kind = match self.tcx().fn_trait_kind_from_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().fn_trait_lang_item(obligation.predicate.def_id()).is_none() { + if self.tcx().fn_trait_kind_from_lang_item(obligation.predicate.def_id()).is_none() { return Ok(()); } @@ -2889,7 +2889,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { let kind = self .tcx() - .fn_trait_lang_item(obligation.predicate.def_id()) + .fn_trait_kind_from_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 b175de6e0e88c..b5e17661c5de1 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.fn_trait_lang_item(trait_id).unwrap(); + let trait_closure_kind = tcx.fn_trait_kind_from_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 2a43d0486d06b..274482cba64cc 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().fn_trait_lang_item(principal.def_id); + let fn_trait_kind = self.tcx().fn_trait_kind_from_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_hir/lang_items.rs b/src/librustc_hir/lang_items.rs index c5bad979f5416..cb5ebba463394 100644 --- a/src/librustc_hir/lang_items.rs +++ b/src/librustc_hir/lang_items.rs @@ -28,76 +28,76 @@ 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, )* + 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,)* + } } - } -} - -#[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>, - /// Lang items that were not found during collection. - pub missing: Vec, -} - -impl LanguageItems { - /// Construct an empty collection of lang items and no missing ones. - pub fn new() -> Self { - fn init_none(_: LangItem) -> Option { None } - Self { - items: vec![$(init_none($variant)),*], - missing: Vec::new(), + 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, )* + } + } } - } - - /// Returns the mappings to the possibly found `DefId`s for each lang item. - pub fn items(&self) -> &[Option] { - &*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 = ""]`, - /// returns an error message as a string. - pub fn require(&self, it: LangItem) -> Result { - self.items[it as usize].ok_or_else(|| format!("requires `{}` lang_item", it.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>, + /// Lang items that were not found during collection. + pub missing: Vec, + } - $( - /// Returns the corresponding `DefId` for the lang item - #[doc = $name] - /// if it exists. - #[allow(dead_code)] - pub fn $method(&self) -> Option { - self.items[$variant as usize] + impl LanguageItems { + /// Construct an empty collection of lang items and no missing ones. + pub fn new() -> Self { + fn init_none(_: LangItem) -> Option { 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] { + &*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 = ""]`, + /// returns an error message as a string. + pub fn require(&self, it: LangItem) -> Result { + 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 { + 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 - }; -} + 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 } diff --git a/src/librustc_mir/shim.rs b/src/librustc_mir/shim.rs index cc5e142351d4f..bfd30ff5da553 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.fn_trait_lang_item(trait_) { + let adjustment = match tcx.fn_trait_kind_from_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 f601a0e8253c1..396534b3caeb7 100644 --- a/src/librustc_typeck/check/closure.rs +++ b/src/librustc_typeck/check/closure.rs @@ -174,8 +174,9 @@ 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.fn_trait_lang_item(did)); + let kind = object_type + .principal_def_id() + .and_then(|did| self.tcx.fn_trait_kind_from_lang_item(did)); (sig, kind) } ty::Infer(ty::TyVar(vid)) => self.deduce_expectations_from_obligations(vid), @@ -213,7 +214,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.fn_trait_lang_item(tr.def_id())) + .filter_map(|(tr, _)| self.tcx.fn_trait_kind_from_lang_item(tr.def_id())) .fold(None, |best, cur| Some(best.map_or(cur, |best| cmp::min(best, cur)))); (expected_sig, expected_kind) @@ -236,7 +237,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { let trait_ref = projection.to_poly_trait_ref(tcx); - let is_fn = tcx.fn_trait_lang_item(trait_ref.def_id()).is_some(); + let is_fn = tcx.fn_trait_kind_from_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 c179674432332..ef35705650452 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.fn_trait_lang_item(did).is_some() => { + Some(did) if cx.tcx.fn_trait_kind_from_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(),