Skip to content

Commit 05b2081

Browse files
committed
Queryify item_body_nested_bodies
1 parent 526d399 commit 05b2081

File tree

5 files changed

+27
-23
lines changed

5 files changed

+27
-23
lines changed

src/librustc/middle/cstore.rs

-5
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ use session::search_paths::PathKind;
3535
use util::nodemap::{NodeSet, DefIdMap};
3636

3737
use std::any::Any;
38-
use std::collections::BTreeMap;
3938
use std::path::PathBuf;
4039
use std::rc::Rc;
4140
use syntax::ast;
@@ -250,7 +249,6 @@ pub trait CrateStore {
250249
// misc. metadata
251250
fn item_body<'a, 'tcx>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId)
252251
-> &'tcx hir::Body;
253-
fn item_body_nested_bodies(&self, def: DefId) -> BTreeMap<hir::BodyId, hir::Body>;
254252
fn const_is_rvalue_promotable_to_static(&self, def: DefId) -> bool;
255253

256254
fn is_item_mir_available(&self, def: DefId) -> bool;
@@ -401,9 +399,6 @@ impl CrateStore for DummyCrateStore {
401399
-> &'tcx hir::Body {
402400
bug!("item_body")
403401
}
404-
fn item_body_nested_bodies(&self, def: DefId) -> BTreeMap<hir::BodyId, hir::Body> {
405-
bug!("item_body_nested_bodies")
406-
}
407402
fn const_is_rvalue_promotable_to_static(&self, def: DefId) -> bool {
408403
bug!("const_is_rvalue_promotable_to_static")
409404
}

src/librustc/ty/maps.rs

+16-3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use util::nodemap::NodeSet;
2424
use rustc_data_structures::indexed_vec::IndexVec;
2525
use std::cell::{RefCell, RefMut};
2626
use std::mem;
27+
use std::collections::BTreeMap;
2728
use std::ops::Deref;
2829
use std::rc::Rc;
2930
use syntax_pos::{Span, DUMMY_SP};
@@ -291,10 +292,16 @@ impl<'tcx> QueryDescription for queries::def_span<'tcx> {
291292
}
292293
}
293294

295+
impl<'tcx> QueryDescription for queries::item_body_nested_bodies<'tcx> {
296+
fn describe(tcx: TyCtxt, def_id: DefId) -> String {
297+
format!("nested item bodies of `{}`", tcx.item_path_str(def_id))
298+
}
299+
}
300+
294301
macro_rules! define_maps {
295302
(<$tcx:tt>
296303
$($(#[$attr:meta])*
297-
[$($pub:tt)*] $name:ident: $node:ident($K:ty) -> $V:ty),*) => {
304+
[$($pub:tt)*] $name:ident: $node:ident($K:ty) -> $V:ty,)*) => {
298305
pub struct Maps<$tcx> {
299306
providers: IndexVec<CrateNum, Providers<$tcx>>,
300307
query_stack: RefCell<Vec<(Span, Query<$tcx>)>>,
@@ -577,7 +584,9 @@ define_maps! { <'tcx>
577584
[] symbol_name: symbol_name_dep_node(ty::Instance<'tcx>) -> ty::SymbolName,
578585

579586
[] describe_def: DescribeDef(DefId) -> Option<Def>,
580-
[] def_span: DefSpan(DefId) -> Span
587+
[] def_span: DefSpan(DefId) -> Span,
588+
589+
[] item_body_nested_bodies: metadata_dep_node(DefId) -> Rc<BTreeMap<hir::BodyId, hir::Body>>,
581590
}
582591

583592
fn coherent_trait_dep_node((_, def_id): (CrateNum, DefId)) -> DepNode<DefId> {
@@ -592,6 +601,10 @@ fn reachability_dep_node(_: CrateNum) -> DepNode<DefId> {
592601
DepNode::Reachability
593602
}
594603

604+
fn metadata_dep_node(def_id: DefId) -> DepNode<DefId> {
605+
DepNode::MetaData(def_id)
606+
}
607+
595608
fn mir_shim_dep_node(instance: ty::InstanceDef) -> DepNode<DefId> {
596609
instance.dep_node()
597610
}
@@ -608,4 +621,4 @@ fn typeck_item_bodies_dep_node(_: CrateNum) -> DepNode<DefId> {
608621

609622
fn const_eval_dep_node((def_id, _): (DefId, &Substs)) -> DepNode<DefId> {
610623
DepNode::ConstEval(def_id)
611-
}
624+
}

src/librustc_metadata/cstore_impl.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,13 @@ provide! { <'tcx> tcx, def_id, cdata
115115
is_foreign_item => { cdata.is_foreign_item(def_id.index) }
116116
describe_def => { cdata.get_def(def_id.index) }
117117
def_span => { cdata.get_span(def_id.index, &tcx.sess) }
118+
item_body_nested_bodies => {
119+
let map: BTreeMap<_, _> = cdata.entry(def_id.index).ast.into_iter().flat_map(|ast| {
120+
ast.decode(cdata).nested_bodies.decode(cdata).map(|body| (body.id(), body))
121+
}).collect();
122+
123+
Rc::new(map)
124+
}
118125
}
119126

120127
impl CrateStore for cstore::CStore {
@@ -432,11 +439,6 @@ impl CrateStore for cstore::CStore {
432439
self.get_crate_data(def_id.krate).item_body(tcx, def_id.index)
433440
}
434441

435-
fn item_body_nested_bodies(&self, def: DefId) -> BTreeMap<hir::BodyId, hir::Body> {
436-
self.dep_graph.read(DepNode::MetaData(def));
437-
self.get_crate_data(def.krate).item_body_nested_bodies(def.index)
438-
}
439-
440442
fn const_is_rvalue_promotable_to_static(&self, def: DefId) -> bool {
441443
self.dep_graph.read(DepNode::MetaData(def));
442444
self.get_crate_data(def.krate).const_is_rvalue_promotable_to_static(def.index)

src/librustc_metadata/decoder.rs

+1-8
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ use rustc::mir::Mir;
2828

2929
use std::borrow::Cow;
3030
use std::cell::Ref;
31-
use std::collections::BTreeMap;
3231
use std::io;
3332
use std::mem;
3433
use std::rc::Rc;
@@ -451,7 +450,7 @@ impl<'a, 'tcx> CrateMetadata {
451450
self.root.index.lookup(self.blob.raw_bytes(), item_id)
452451
}
453452

454-
fn entry(&self, item_id: DefIndex) -> Entry<'tcx> {
453+
pub fn entry(&self, item_id: DefIndex) -> Entry<'tcx> {
455454
match self.maybe_entry(item_id) {
456455
None => {
457456
bug!("entry: id not found: {:?} in crate {:?} with number {}",
@@ -773,12 +772,6 @@ impl<'a, 'tcx> CrateMetadata {
773772
tcx.alloc_tables(ast.tables.decode((self, tcx)))
774773
}
775774

776-
pub fn item_body_nested_bodies(&self, id: DefIndex) -> BTreeMap<hir::BodyId, hir::Body> {
777-
self.entry(id).ast.into_iter().flat_map(|ast| {
778-
ast.decode(self).nested_bodies.decode(self).map(|body| (body.id(), body))
779-
}).collect()
780-
}
781-
782775
pub fn const_is_rvalue_promotable_to_static(&self, id: DefIndex) -> bool {
783776
self.entry(id).ast.expect("const item missing `ast`")
784777
.decode(self).rvalue_promotable_to_static

src/librustdoc/clean/inline.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use std::collections::BTreeMap;
1414
use std::io;
1515
use std::iter::once;
16+
use std::rc::Rc;
1617

1718
use syntax::ast;
1819
use rustc::hir;
@@ -471,7 +472,7 @@ fn build_module(cx: &DocContext, did: DefId) -> clean::Module {
471472
}
472473

473474
struct InlinedConst {
474-
nested_bodies: BTreeMap<hir::BodyId, hir::Body>
475+
nested_bodies: Rc<BTreeMap<hir::BodyId, hir::Body>>
475476
}
476477

477478
impl hir::print::PpAnn for InlinedConst {
@@ -488,7 +489,7 @@ impl hir::print::PpAnn for InlinedConst {
488489
fn print_inlined_const(cx: &DocContext, did: DefId) -> String {
489490
let body = cx.tcx.sess.cstore.item_body(cx.tcx, did);
490491
let inlined = InlinedConst {
491-
nested_bodies: cx.tcx.sess.cstore.item_body_nested_bodies(did)
492+
nested_bodies: cx.tcx.item_body_nested_bodies(did)
492493
};
493494
hir::print::to_string(&inlined, |s| s.print_expr(&body.value))
494495
}

0 commit comments

Comments
 (0)