From aa5a5320466d4a6ac7125cb01956307e63d9918b Mon Sep 17 00:00:00 2001 From: achernyak Date: Thu, 4 May 2017 08:27:48 -0500 Subject: [PATCH] trait_of_item --- src/librustc/dep_graph/dep_node.rs | 2 ++ src/librustc/middle/cstore.rs | 2 -- src/librustc/ty/maps.rs | 7 ++++++ src/librustc/ty/mod.rs | 34 +++++++++++++++------------- src/librustc_const_eval/eval.rs | 2 +- src/librustc_metadata/cstore_impl.rs | 6 +---- 6 files changed, 29 insertions(+), 24 deletions(-) diff --git a/src/librustc/dep_graph/dep_node.rs b/src/librustc/dep_graph/dep_node.rs index 591c128a165..0e462b95d82 100644 --- a/src/librustc/dep_graph/dep_node.rs +++ b/src/librustc/dep_graph/dep_node.rs @@ -156,6 +156,7 @@ pub enum DepNode { Deprecation(D), ItemBodyNestedBodies(D), ConstIsRvaluePromotableToStatic(D), + TraitOfItem(D), IsMirAvailable(D), ItemAttrs(D), FnArgNames(D), @@ -271,6 +272,7 @@ pub fn map_def(&self, mut op: OP) -> Option> Deprecation(ref d) => op(d).map(Deprecation), ItemAttrs(ref d) => op(d).map(ItemAttrs), FnArgNames(ref d) => op(d).map(FnArgNames), + TraitOfItem(ref d) => op(d).map(TraitOfItem), ItemBodyNestedBodies(ref d) => op(d).map(ItemBodyNestedBodies), ConstIsRvaluePromotableToStatic(ref d) => op(d).map(ConstIsRvaluePromotableToStatic), IsMirAvailable(ref d) => op(d).map(IsMirAvailable), diff --git a/src/librustc/middle/cstore.rs b/src/librustc/middle/cstore.rs index 00904f54061..11a2dfc6685 100644 --- a/src/librustc/middle/cstore.rs +++ b/src/librustc/middle/cstore.rs @@ -191,7 +191,6 @@ pub trait CrateStore { fn impl_parent(&self, impl_def_id: DefId) -> Option; // trait/impl-item info - fn trait_of_item(&self, def_id: DefId) -> Option; fn associated_item_cloned(&self, def: DefId) -> ty::AssociatedItem; // flags @@ -316,7 +315,6 @@ fn impl_defaultness(&self, def: DefId) -> hir::Defaultness { bug!("impl_defaultn fn impl_parent(&self, def: DefId) -> Option { bug!("impl_parent") } // trait/impl-item info - fn trait_of_item(&self, def_id: DefId) -> Option { bug!("trait_of_item") } fn associated_item_cloned(&self, def: DefId) -> ty::AssociatedItem { bug!("associated_item_cloned") } diff --git a/src/librustc/ty/maps.rs b/src/librustc/ty/maps.rs index 43f6c94b8b0..5d1b16c3d2e 100644 --- a/src/librustc/ty/maps.rs +++ b/src/librustc/ty/maps.rs @@ -347,6 +347,12 @@ fn describe(_: TyCtxt, _: DefId) -> String { } } +impl<'tcx> QueryDescription for queries::trait_of_item<'tcx> { + fn describe(_: TyCtxt, _: DefId) -> String { + bug!("trait_of_item") + } +} + impl<'tcx> QueryDescription for queries::item_body_nested_bodies<'tcx> { fn describe(tcx: TyCtxt, def_id: DefId) -> String { format!("nested item bodies of `{}`", tcx.item_path_str(def_id)) @@ -798,6 +804,7 @@ fn default() -> Self { [] deprecation: Deprecation(DefId) -> Option, [] item_attrs: ItemAttrs(DefId) -> Rc<[ast::Attribute]>, [] fn_arg_names: FnArgNames(DefId) -> Vec, + [] trait_of_item: TraitOfItem(DefId) -> Option, [] item_body_nested_bodies: ItemBodyNestedBodies(DefId) -> Rc>, [] const_is_rvalue_promotable_to_static: ConstIsRvaluePromotableToStatic(DefId) -> bool, [] is_mir_available: IsMirAvailable(DefId) -> bool, diff --git a/src/librustc/ty/mod.rs b/src/librustc/ty/mod.rs index 3cc250f0814..00f81c9dcf7 100644 --- a/src/librustc/ty/mod.rs +++ b/src/librustc/ty/mod.rs @@ -2430,22 +2430,6 @@ pub fn impl_of_method(self, def_id: DefId) -> Option { } } - /// If the given def ID describes an item belonging to a trait, - /// return the ID of the trait that the trait item belongs to. - /// Otherwise, return `None`. - pub fn trait_of_item(self, def_id: DefId) -> Option { - if def_id.krate != LOCAL_CRATE { - return self.sess.cstore.trait_of_item(def_id); - } - self.opt_associated_item(def_id) - .and_then(|associated_item| { - match associated_item.container { - TraitContainer(def_id) => Some(def_id), - ImplContainer(_) => None - } - }) - } - /// Construct a parameter environment suitable for static contexts or other contexts where there /// are no free type/lifetime parameters in scope. pub fn empty_parameter_environment(self) -> ParameterEnvironment<'tcx> { @@ -2693,6 +2677,23 @@ fn def_span<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> Span { tcx.hir.span_if_local(def_id).unwrap() } +/// If the given def ID describes an item belonging to a trait, +/// return the ID of the trait that the trait item belongs to. +/// Otherwise, return `None`. +fn trait_of_item<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> Option { + if def_id.krate != LOCAL_CRATE { + return None + } + tcx.opt_associated_item(def_id) + .and_then(|associated_item| { + match associated_item.container { + TraitContainer(def_id) => Some(def_id), + ImplContainer(_) => None + } + }) +} + + pub fn provide(providers: &mut ty::maps::Providers) { *providers = ty::maps::Providers { associated_item, @@ -2700,6 +2701,7 @@ pub fn provide(providers: &mut ty::maps::Providers) { adt_sized_constraint, adt_dtorck_constraint, def_span, + trait_of_item, ..*providers }; } diff --git a/src/librustc_const_eval/eval.rs b/src/librustc_const_eval/eval.rs index 8b1aa070880..e79f23aee11 100644 --- a/src/librustc_const_eval/eval.rs +++ b/src/librustc_const_eval/eval.rs @@ -74,7 +74,7 @@ pub fn lookup_const_by_id<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, // constants, we only try to find the expression for a // trait-associated const if the caller gives us the // substitutions for the reference to it. - if tcx.sess.cstore.trait_of_item(def_id).is_some() { + if tcx.trait_of_item(def_id).is_some() { resolve_trait_associated_const(tcx, def_id, substs) } else { Some((def_id, substs)) diff --git a/src/librustc_metadata/cstore_impl.rs b/src/librustc_metadata/cstore_impl.rs index 325ba14da9c..b07dd1ec7b0 100644 --- a/src/librustc_metadata/cstore_impl.rs +++ b/src/librustc_metadata/cstore_impl.rs @@ -119,6 +119,7 @@ pub fn provide<$lt>(providers: &mut Providers<$lt>) { // This is only used by rustdoc anyway, which shouldn't have // incremental recompilation ever enabled. fn_arg_names => { cdata.get_fn_arg_names(def_id.index) } + trait_of_item => { cdata.get_trait_of_item(def_id.index) } item_body_nested_bodies => { let map: BTreeMap<_, _> = cdata.entry(def_id.index).ast.into_iter().flat_map(|ast| { ast.decode(cdata).nested_bodies.decode(cdata).map(|body| (body.id(), body)) @@ -174,11 +175,6 @@ fn impl_parent(&self, impl_def: DefId) -> Option { self.get_crate_data(impl_def.krate).get_parent_impl(impl_def.index) } - fn trait_of_item(&self, def_id: DefId) -> Option { - self.dep_graph.read(DepNode::MetaData(def_id)); - self.get_crate_data(def_id.krate).get_trait_of_item(def_id.index) - } - fn associated_item_cloned(&self, def: DefId) -> ty::AssociatedItem { self.dep_graph.read(DepNode::MetaData(def));