Skip to content

Commit aa5a532

Browse files
committed
trait_of_item
1 parent c72a16b commit aa5a532

File tree

6 files changed

+29
-24
lines changed

6 files changed

+29
-24
lines changed

src/librustc/dep_graph/dep_node.rs

+2
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ pub enum DepNode<D: Clone + Debug> {
156156
Deprecation(D),
157157
ItemBodyNestedBodies(D),
158158
ConstIsRvaluePromotableToStatic(D),
159+
TraitOfItem(D),
159160
IsMirAvailable(D),
160161
ItemAttrs(D),
161162
FnArgNames(D),
@@ -271,6 +272,7 @@ impl<D: Clone + Debug> DepNode<D> {
271272
Deprecation(ref d) => op(d).map(Deprecation),
272273
ItemAttrs(ref d) => op(d).map(ItemAttrs),
273274
FnArgNames(ref d) => op(d).map(FnArgNames),
275+
TraitOfItem(ref d) => op(d).map(TraitOfItem),
274276
ItemBodyNestedBodies(ref d) => op(d).map(ItemBodyNestedBodies),
275277
ConstIsRvaluePromotableToStatic(ref d) => op(d).map(ConstIsRvaluePromotableToStatic),
276278
IsMirAvailable(ref d) => op(d).map(IsMirAvailable),

src/librustc/middle/cstore.rs

-2
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,6 @@ pub trait CrateStore {
191191
fn impl_parent(&self, impl_def_id: DefId) -> Option<DefId>;
192192

193193
// trait/impl-item info
194-
fn trait_of_item(&self, def_id: DefId) -> Option<DefId>;
195194
fn associated_item_cloned(&self, def: DefId) -> ty::AssociatedItem;
196195

197196
// flags
@@ -316,7 +315,6 @@ impl CrateStore for DummyCrateStore {
316315
fn impl_parent(&self, def: DefId) -> Option<DefId> { bug!("impl_parent") }
317316

318317
// trait/impl-item info
319-
fn trait_of_item(&self, def_id: DefId) -> Option<DefId> { bug!("trait_of_item") }
320318
fn associated_item_cloned(&self, def: DefId) -> ty::AssociatedItem
321319
{ bug!("associated_item_cloned") }
322320

src/librustc/ty/maps.rs

+7
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,12 @@ impl<'tcx> QueryDescription for queries::fn_arg_names<'tcx> {
347347
}
348348
}
349349

350+
impl<'tcx> QueryDescription for queries::trait_of_item<'tcx> {
351+
fn describe(_: TyCtxt, _: DefId) -> String {
352+
bug!("trait_of_item")
353+
}
354+
}
355+
350356
impl<'tcx> QueryDescription for queries::item_body_nested_bodies<'tcx> {
351357
fn describe(tcx: TyCtxt, def_id: DefId) -> String {
352358
format!("nested item bodies of `{}`", tcx.item_path_str(def_id))
@@ -798,6 +804,7 @@ define_maps! { <'tcx>
798804
[] deprecation: Deprecation(DefId) -> Option<attr::Deprecation>,
799805
[] item_attrs: ItemAttrs(DefId) -> Rc<[ast::Attribute]>,
800806
[] fn_arg_names: FnArgNames(DefId) -> Vec<ast::Name>,
807+
[] trait_of_item: TraitOfItem(DefId) -> Option<DefId>,
801808
[] item_body_nested_bodies: ItemBodyNestedBodies(DefId) -> Rc<BTreeMap<hir::BodyId, hir::Body>>,
802809
[] const_is_rvalue_promotable_to_static: ConstIsRvaluePromotableToStatic(DefId) -> bool,
803810
[] is_mir_available: IsMirAvailable(DefId) -> bool,

src/librustc/ty/mod.rs

+18-16
Original file line numberDiff line numberDiff line change
@@ -2430,22 +2430,6 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
24302430
}
24312431
}
24322432

2433-
/// If the given def ID describes an item belonging to a trait,
2434-
/// return the ID of the trait that the trait item belongs to.
2435-
/// Otherwise, return `None`.
2436-
pub fn trait_of_item(self, def_id: DefId) -> Option<DefId> {
2437-
if def_id.krate != LOCAL_CRATE {
2438-
return self.sess.cstore.trait_of_item(def_id);
2439-
}
2440-
self.opt_associated_item(def_id)
2441-
.and_then(|associated_item| {
2442-
match associated_item.container {
2443-
TraitContainer(def_id) => Some(def_id),
2444-
ImplContainer(_) => None
2445-
}
2446-
})
2447-
}
2448-
24492433
/// Construct a parameter environment suitable for static contexts or other contexts where there
24502434
/// are no free type/lifetime parameters in scope.
24512435
pub fn empty_parameter_environment(self) -> ParameterEnvironment<'tcx> {
@@ -2693,13 +2677,31 @@ fn def_span<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> Span {
26932677
tcx.hir.span_if_local(def_id).unwrap()
26942678
}
26952679

2680+
/// If the given def ID describes an item belonging to a trait,
2681+
/// return the ID of the trait that the trait item belongs to.
2682+
/// Otherwise, return `None`.
2683+
fn trait_of_item<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> Option<DefId> {
2684+
if def_id.krate != LOCAL_CRATE {
2685+
return None
2686+
}
2687+
tcx.opt_associated_item(def_id)
2688+
.and_then(|associated_item| {
2689+
match associated_item.container {
2690+
TraitContainer(def_id) => Some(def_id),
2691+
ImplContainer(_) => None
2692+
}
2693+
})
2694+
}
2695+
2696+
26962697
pub fn provide(providers: &mut ty::maps::Providers) {
26972698
*providers = ty::maps::Providers {
26982699
associated_item,
26992700
associated_item_def_ids,
27002701
adt_sized_constraint,
27012702
adt_dtorck_constraint,
27022703
def_span,
2704+
trait_of_item,
27032705
..*providers
27042706
};
27052707
}

src/librustc_const_eval/eval.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ pub fn lookup_const_by_id<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
7474
// constants, we only try to find the expression for a
7575
// trait-associated const if the caller gives us the
7676
// substitutions for the reference to it.
77-
if tcx.sess.cstore.trait_of_item(def_id).is_some() {
77+
if tcx.trait_of_item(def_id).is_some() {
7878
resolve_trait_associated_const(tcx, def_id, substs)
7979
} else {
8080
Some((def_id, substs))

src/librustc_metadata/cstore_impl.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ provide! { <'tcx> tcx, def_id, cdata
119119
// This is only used by rustdoc anyway, which shouldn't have
120120
// incremental recompilation ever enabled.
121121
fn_arg_names => { cdata.get_fn_arg_names(def_id.index) }
122+
trait_of_item => { cdata.get_trait_of_item(def_id.index) }
122123
item_body_nested_bodies => {
123124
let map: BTreeMap<_, _> = cdata.entry(def_id.index).ast.into_iter().flat_map(|ast| {
124125
ast.decode(cdata).nested_bodies.decode(cdata).map(|body| (body.id(), body))
@@ -174,11 +175,6 @@ impl CrateStore for cstore::CStore {
174175
self.get_crate_data(impl_def.krate).get_parent_impl(impl_def.index)
175176
}
176177

177-
fn trait_of_item(&self, def_id: DefId) -> Option<DefId> {
178-
self.dep_graph.read(DepNode::MetaData(def_id));
179-
self.get_crate_data(def_id.krate).get_trait_of_item(def_id.index)
180-
}
181-
182178
fn associated_item_cloned(&self, def: DefId) -> ty::AssociatedItem
183179
{
184180
self.dep_graph.read(DepNode::MetaData(def));

0 commit comments

Comments
 (0)