Skip to content

More Queries for Crate Metadata #41724

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
May 11, 2017
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/librustc/dep_graph/dep_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,12 @@ pub enum DepNode<D: Clone + Debug> {
DefSpan(D),
Stability(D),
Deprecation(D),
ItemBodyNestedBodies(D),
ConstIsRvaluePromotableToStatic(D),
TraitOfItem(D),
IsMirAvailable(D),
ItemAttrs(D),
FnArgNames(D),
}

impl<D: Clone + Debug> DepNode<D> {
Expand Down Expand Up @@ -264,6 +270,12 @@ impl<D: Clone + Debug> DepNode<D> {
DefSpan(ref d) => op(d).map(DefSpan),
Stability(ref d) => op(d).map(Stability),
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),
}
}
}
Expand Down
6 changes: 0 additions & 6 deletions src/librustc/middle/cstore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,6 @@ pub trait CrateStore {
fn visibility(&self, def: DefId) -> ty::Visibility;
fn visible_parent_map<'a>(&'a self) -> ::std::cell::Ref<'a, DefIdMap<DefId>>;
fn item_generics_cloned(&self, def: DefId) -> ty::Generics;
fn item_attrs(&self, def_id: DefId) -> Rc<[ast::Attribute]>;
fn fn_arg_names(&self, did: DefId) -> Vec<ast::Name>;

// trait info
fn implementations_of_trait(&self, filter: Option<DefId>) -> Vec<DefId>;
Expand All @@ -193,7 +191,6 @@ pub trait CrateStore {
fn impl_parent(&self, impl_def_id: DefId) -> Option<DefId>;

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

// flags
Expand Down Expand Up @@ -309,8 +306,6 @@ impl CrateStore for DummyCrateStore {
}
fn item_generics_cloned(&self, def: DefId) -> ty::Generics
{ bug!("item_generics_cloned") }
fn item_attrs(&self, def_id: DefId) -> Rc<[ast::Attribute]> { bug!("item_attrs") }
fn fn_arg_names(&self, did: DefId) -> Vec<ast::Name> { bug!("fn_arg_names") }

// trait info
fn implementations_of_trait(&self, filter: Option<DefId>) -> Vec<DefId> { vec![] }
Expand All @@ -320,7 +315,6 @@ impl CrateStore for DummyCrateStore {
fn impl_parent(&self, def: DefId) -> Option<DefId> { bug!("impl_parent") }

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

Expand Down
32 changes: 25 additions & 7 deletions src/librustc/ty/maps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ use std::ops::Deref;
use std::rc::Rc;
use syntax_pos::{Span, DUMMY_SP};
use syntax::attr;
use syntax::ast;
use syntax::symbol::Symbol;

pub trait Key: Clone + Hash + Eq + Debug {
Expand Down Expand Up @@ -334,6 +335,24 @@ impl<'tcx> QueryDescription for queries::deprecation<'tcx> {
}
}

impl<'tcx> QueryDescription for queries::item_attrs<'tcx> {
fn describe(_: TyCtxt, _: DefId) -> String {
bug!("item_attrs")
}
}

impl<'tcx> QueryDescription for queries::fn_arg_names<'tcx> {
fn describe(_: TyCtxt, _: DefId) -> String {
bug!("fn_arg_names")
}
}

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))
Expand Down Expand Up @@ -783,9 +802,12 @@ define_maps! { <'tcx>
[] def_span: DefSpan(DefId) -> Span,
[] stability: Stability(DefId) -> Option<attr::Stability>,
[] deprecation: Deprecation(DefId) -> Option<attr::Deprecation>,
[] item_body_nested_bodies: metadata_dep_node(DefId) -> Rc<BTreeMap<hir::BodyId, hir::Body>>,
[] const_is_rvalue_promotable_to_static: metadata_dep_node(DefId) -> bool,
[] is_mir_available: metadata_dep_node(DefId) -> bool,
[] item_attrs: ItemAttrs(DefId) -> Rc<[ast::Attribute]>,
[] fn_arg_names: FnArgNames(DefId) -> Vec<ast::Name>,
[] trait_of_item: TraitOfItem(DefId) -> Option<DefId>,
[] item_body_nested_bodies: ItemBodyNestedBodies(DefId) -> Rc<BTreeMap<hir::BodyId, hir::Body>>,
[] const_is_rvalue_promotable_to_static: ConstIsRvaluePromotableToStatic(DefId) -> bool,
[] is_mir_available: IsMirAvailable(DefId) -> bool,
}

fn coherent_trait_dep_node((_, def_id): (CrateNum, DefId)) -> DepNode<DefId> {
Expand All @@ -800,10 +822,6 @@ fn reachability_dep_node(_: CrateNum) -> DepNode<DefId> {
DepNode::Reachability
}

fn metadata_dep_node(def_id: DefId) -> DepNode<DefId> {
DepNode::MetaData(def_id)
}

fn mir_shim_dep_node(instance: ty::InstanceDef) -> DepNode<DefId> {
instance.dep_node()
}
Expand Down
36 changes: 19 additions & 17 deletions src/librustc/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2357,7 +2357,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
if let Some(id) = self.hir.as_local_node_id(did) {
Attributes::Borrowed(self.hir.attrs(id))
} else {
Attributes::Owned(self.sess.cstore.item_attrs(did))
Attributes::Owned(self.item_attrs(did))
}
}

Expand Down Expand Up @@ -2430,22 +2430,6 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
}
}

/// 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<DefId> {
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> {
Expand Down Expand Up @@ -2693,13 +2677,31 @@ 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<DefId> {
if def_id.krate != LOCAL_CRATE {
return None
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Er, wait, this seems surprising. What is going on here exactly?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I expected this code to only run when the krate is LOCAL_CRATE, no?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't that what this is doing? This returns None if it's not a LOCAL_CRATE.

}
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,
associated_item_def_ids,
adt_sized_constraint,
adt_dtorck_constraint,
def_span,
trait_of_item,
..*providers
};
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_const_eval/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
28 changes: 7 additions & 21 deletions src/librustc_metadata/cstore_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,13 @@ provide! { <'tcx> tcx, def_id, cdata
def_span => { cdata.get_span(def_id.index, &tcx.sess) }
stability => { cdata.get_stability(def_id.index) }
deprecation => { cdata.get_deprecation(def_id.index) }
item_attrs => { cdata.get_item_attrs(def_id.index) }
// FIXME(#38501) We've skipped a `read` on the `HirBody` of
// a `fn` when encoding, so the dep-tracking wouldn't work.
// 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) }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...and I expected this code to run for other crates.

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))
Expand Down Expand Up @@ -145,22 +152,6 @@ impl CrateStore for cstore::CStore {
self.get_crate_data(def.krate).get_generics(def.index)
}

fn item_attrs(&self, def_id: DefId) -> Rc<[ast::Attribute]>
{
self.dep_graph.read(DepNode::MetaData(def_id));
self.get_crate_data(def_id.krate).get_item_attrs(def_id.index)
}

fn fn_arg_names(&self, did: DefId) -> Vec<ast::Name>
{
// FIXME(#38501) We've skipped a `read` on the `HirBody` of
// a `fn` when encoding, so the dep-tracking wouldn't work.
// This is only used by rustdoc anyway, which shouldn't have
// incremental recompilation ever enabled.
assert!(!self.dep_graph.is_fully_enabled());
self.get_crate_data(did.krate).get_fn_arg_names(did.index)
}

fn implementations_of_trait(&self, filter: Option<DefId>) -> Vec<DefId>
{
if let Some(def_id) = filter {
Expand All @@ -184,11 +175,6 @@ impl CrateStore for cstore::CStore {
self.get_crate_data(impl_def.krate).get_parent_impl(impl_def.index)
}

fn trait_of_item(&self, def_id: DefId) -> Option<DefId> {
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));
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1179,7 +1179,7 @@ impl<'a, 'tcx> Clean<FnDecl> for (DefId, ty::PolyFnSig<'tcx>) {
let mut names = if cx.tcx.hir.as_local_node_id(did).is_some() {
vec![].into_iter()
} else {
cx.tcx.sess.cstore.fn_arg_names(did).into_iter()
cx.tcx.fn_arg_names(did).into_iter()
}.peekable();
FnDecl {
output: Return(sig.skip_binder().output().clean(cx)),
Expand Down