Skip to content

Commit 777ee20

Browse files
committed
Auto merge of #41611 - cramertj:metadata-queries-1, r=nikomatsakis
Queryify crate metadata Part of #41417. r? @nikomatsakis
2 parents 4cb396c + daa0094 commit 777ee20

File tree

8 files changed

+56
-58
lines changed

8 files changed

+56
-58
lines changed

src/librustc/middle/cstore.rs

-15
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,10 +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>;
254-
fn const_is_rvalue_promotable_to_static(&self, def: DefId) -> bool;
255-
256-
fn is_item_mir_available(&self, def: DefId) -> bool;
257252

258253
// This is basically a 1-based range of ints, which is a little
259254
// silly - I may fix that.
@@ -401,16 +396,6 @@ impl CrateStore for DummyCrateStore {
401396
-> &'tcx hir::Body {
402397
bug!("item_body")
403398
}
404-
fn item_body_nested_bodies(&self, def: DefId) -> BTreeMap<hir::BodyId, hir::Body> {
405-
bug!("item_body_nested_bodies")
406-
}
407-
fn const_is_rvalue_promotable_to_static(&self, def: DefId) -> bool {
408-
bug!("const_is_rvalue_promotable_to_static")
409-
}
410-
411-
fn is_item_mir_available(&self, def: DefId) -> bool {
412-
bug!("is_item_mir_available")
413-
}
414399

415400
// This is basically a 1-based range of ints, which is a little
416401
// silly - I may fix that.

src/librustc/ty/maps.rs

+32-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,30 @@ 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+
301+
impl<'tcx> QueryDescription for queries::const_is_rvalue_promotable_to_static<'tcx> {
302+
fn describe(tcx: TyCtxt, def_id: DefId) -> String {
303+
format!("const checking if rvalue is promotable to static `{}`",
304+
tcx.item_path_str(def_id))
305+
}
306+
}
307+
308+
impl<'tcx> QueryDescription for queries::is_item_mir_available<'tcx> {
309+
fn describe(tcx: TyCtxt, def_id: DefId) -> String {
310+
format!("checking if item is mir available: `{}`",
311+
tcx.item_path_str(def_id))
312+
}
313+
}
314+
294315
macro_rules! define_maps {
295316
(<$tcx:tt>
296317
$($(#[$attr:meta])*
297-
[$($pub:tt)*] $name:ident: $node:ident($K:ty) -> $V:ty),*) => {
318+
[$($pub:tt)*] $name:ident: $node:ident($K:ty) -> $V:ty,)*) => {
298319
pub struct Maps<$tcx> {
299320
providers: IndexVec<CrateNum, Providers<$tcx>>,
300321
query_stack: RefCell<Vec<(Span, Query<$tcx>)>>,
@@ -577,7 +598,11 @@ define_maps! { <'tcx>
577598
[] symbol_name: symbol_name_dep_node(ty::Instance<'tcx>) -> ty::SymbolName,
578599

579600
[] describe_def: DescribeDef(DefId) -> Option<Def>,
580-
[] def_span: DefSpan(DefId) -> Span
601+
[] def_span: DefSpan(DefId) -> Span,
602+
603+
[] item_body_nested_bodies: metadata_dep_node(DefId) -> Rc<BTreeMap<hir::BodyId, hir::Body>>,
604+
[] const_is_rvalue_promotable_to_static: metadata_dep_node(DefId) -> bool,
605+
[] is_item_mir_available: metadata_dep_node(DefId) -> bool,
581606
}
582607

583608
fn coherent_trait_dep_node((_, def_id): (CrateNum, DefId)) -> DepNode<DefId> {
@@ -592,6 +617,10 @@ fn reachability_dep_node(_: CrateNum) -> DepNode<DefId> {
592617
DepNode::Reachability
593618
}
594619

620+
fn metadata_dep_node(def_id: DefId) -> DepNode<DefId> {
621+
DepNode::MetaData(def_id)
622+
}
623+
595624
fn mir_shim_dep_node(instance: ty::InstanceDef) -> DepNode<DefId> {
596625
instance.dep_node()
597626
}
@@ -608,4 +637,4 @@ fn typeck_item_bodies_dep_node(_: CrateNum) -> DepNode<DefId> {
608637

609638
fn const_eval_dep_node((def_id, _): (DefId, &Substs)) -> DepNode<DefId> {
610639
DepNode::ConstEval(def_id)
611-
}
640+
}

src/librustc/ty/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2332,7 +2332,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
23322332
return None;
23332333
}
23342334

2335-
if !did.is_local() && !self.sess.cstore.is_item_mir_available(did) {
2335+
if !did.is_local() && !self.is_item_mir_available(did) {
23362336
return None;
23372337
}
23382338

src/librustc_metadata/cstore_impl.rs

+15-15
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,21 @@ 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+
}
125+
const_is_rvalue_promotable_to_static => {
126+
cdata.entry(def_id.index).ast.expect("const item missing `ast`")
127+
.decode(cdata).rvalue_promotable_to_static
128+
}
129+
is_item_mir_available => {
130+
!cdata.is_proc_macro(def_id.index) &&
131+
cdata.maybe_entry(def_id.index).and_then(|item| item.decode(cdata).mir).is_some()
132+
}
118133
}
119134

120135
impl CrateStore for cstore::CStore {
@@ -432,21 +447,6 @@ impl CrateStore for cstore::CStore {
432447
self.get_crate_data(def_id.krate).item_body(tcx, def_id.index)
433448
}
434449

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-
440-
fn const_is_rvalue_promotable_to_static(&self, def: DefId) -> bool {
441-
self.dep_graph.read(DepNode::MetaData(def));
442-
self.get_crate_data(def.krate).const_is_rvalue_promotable_to_static(def.index)
443-
}
444-
445-
fn is_item_mir_available(&self, def: DefId) -> bool {
446-
self.dep_graph.read(DepNode::MetaData(def));
447-
self.get_crate_data(def.krate).is_item_mir_available(def.index)
448-
}
449-
450450
fn crates(&self) -> Vec<CrateNum>
451451
{
452452
let mut result = vec![];

src/librustc_metadata/decoder.rs

+3-20
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;
@@ -442,16 +441,16 @@ impl<'tcx> EntryKind<'tcx> {
442441
}
443442

444443
impl<'a, 'tcx> CrateMetadata {
445-
fn is_proc_macro(&self, id: DefIndex) -> bool {
444+
pub fn is_proc_macro(&self, id: DefIndex) -> bool {
446445
self.proc_macros.is_some() && id != CRATE_DEF_INDEX
447446
}
448447

449-
fn maybe_entry(&self, item_id: DefIndex) -> Option<Lazy<Entry<'tcx>>> {
448+
pub fn maybe_entry(&self, item_id: DefIndex) -> Option<Lazy<Entry<'tcx>>> {
450449
assert!(!self.is_proc_macro(item_id));
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,22 +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-
782-
pub fn const_is_rvalue_promotable_to_static(&self, id: DefIndex) -> bool {
783-
self.entry(id).ast.expect("const item missing `ast`")
784-
.decode(self).rvalue_promotable_to_static
785-
}
786-
787-
pub fn is_item_mir_available(&self, id: DefIndex) -> bool {
788-
!self.is_proc_macro(id) &&
789-
self.maybe_entry(id).and_then(|item| item.decode(self).mir).is_some()
790-
}
791-
792775
pub fn maybe_get_item_mir(&self,
793776
tcx: TyCtxt<'a, 'tcx, 'tcx>,
794777
id: DefIndex)

src/librustc_passes/consts.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ fn check_expr<'a, 'tcx>(v: &mut CheckCrateVisitor<'a, 'tcx>, e: &hir::Expr, node
336336
_ => false
337337
}
338338
} else {
339-
v.tcx.sess.cstore.const_is_rvalue_promotable_to_static(did)
339+
v.tcx.const_is_rvalue_promotable_to_static(did)
340340
};
341341
}
342342
_ => {

src/librustc_trans/collector.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,7 @@ fn should_trans_locally<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, instance: &Instan
659659
// in this crate
660660
false
661661
} else {
662-
if !tcx.sess.cstore.is_item_mir_available(def_id) {
662+
if !tcx.is_item_mir_available(def_id) {
663663
bug!("Cannot create local trans-item for {:?}", def_id)
664664
}
665665
true

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)