Skip to content

Commit b5c4244

Browse files
committed
rustc: introduce a query system for type information in ty::maps.
1 parent cc8a3a9 commit b5c4244

File tree

27 files changed

+333
-393
lines changed

27 files changed

+333
-393
lines changed

src/librustc/hir/lowering.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,7 @@ impl<'a> LoweringContext<'a> {
525525
return n;
526526
}
527527
assert!(!def_id.is_local());
528-
let n = self.sess.cstore.item_generics(def_id).regions.len();
528+
let n = self.sess.cstore.item_generics_cloned(def_id).regions.len();
529529
self.type_def_lifetime_params.insert(def_id, n);
530530
n
531531
});

src/librustc/middle/cstore.rs

Lines changed: 24 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ use hir::map as hir_map;
2828
use hir::map::definitions::{Definitions, DefKey, DisambiguatedDefPathData};
2929
use hir::svh::Svh;
3030
use middle::lang_items;
31-
use ty::{self, Ty, TyCtxt};
32-
use mir::Mir;
31+
use ty::{self, TyCtxt};
3332
use session::Session;
3433
use session::search_paths::PathKind;
3534
use util::nodemap::{NodeSet, DefIdMap};
3635

36+
use std::any::Any;
3737
use std::collections::BTreeMap;
3838
use std::path::PathBuf;
3939
use std::rc::Rc;
@@ -163,46 +163,31 @@ pub struct ExternCrate {
163163

164164
/// A store of Rust crates, through with their metadata
165165
/// can be accessed.
166-
pub trait CrateStore<'tcx> {
166+
pub trait CrateStore {
167+
fn crate_data_as_rc_any(&self, krate: CrateNum) -> Rc<Any>;
168+
167169
// item info
168170
fn describe_def(&self, def: DefId) -> Option<Def>;
169171
fn def_span(&self, sess: &Session, def: DefId) -> Span;
170172
fn stability(&self, def: DefId) -> Option<attr::Stability>;
171173
fn deprecation(&self, def: DefId) -> Option<attr::Deprecation>;
172174
fn visibility(&self, def: DefId) -> ty::Visibility;
173-
fn closure_kind(&self, def_id: DefId) -> ty::ClosureKind;
174-
fn closure_ty<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId)
175-
-> ty::ClosureTy<'tcx>;
176-
fn item_variances(&self, def: DefId) -> Vec<ty::Variance>;
177-
fn item_type<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId)
178-
-> Ty<'tcx>;
179175
fn visible_parent_map<'a>(&'a self) -> ::std::cell::RefMut<'a, DefIdMap<DefId>>;
180-
fn item_predicates<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId)
181-
-> ty::GenericPredicates<'tcx>;
182-
fn item_super_predicates<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId)
183-
-> ty::GenericPredicates<'tcx>;
184-
fn item_generics(&self, def: DefId) -> ty::Generics;
176+
fn item_generics_cloned(&self, def: DefId) -> ty::Generics;
185177
fn item_attrs(&self, def_id: DefId) -> Vec<ast::Attribute>;
186-
fn trait_def<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId)-> ty::TraitDef;
187-
fn adt_def<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId) -> &'tcx ty::AdtDef;
188178
fn fn_arg_names(&self, did: DefId) -> Vec<ast::Name>;
189179
fn inherent_implementations_for_type(&self, def_id: DefId) -> Vec<DefId>;
190180

191181
// trait info
192182
fn implementations_of_trait(&self, filter: Option<DefId>) -> Vec<DefId>;
193183

194184
// impl info
195-
fn associated_item_def_ids(&self, def_id: DefId) -> Vec<DefId>;
196-
fn impl_trait_ref<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId)
197-
-> Option<ty::TraitRef<'tcx>>;
198185
fn impl_polarity(&self, def: DefId) -> hir::ImplPolarity;
199-
fn custom_coerce_unsized_kind(&self, def: DefId)
200-
-> Option<ty::adjustment::CustomCoerceUnsized>;
201186
fn impl_parent(&self, impl_def_id: DefId) -> Option<DefId>;
202187

203188
// trait/impl-item info
204189
fn trait_of_item(&self, def_id: DefId) -> Option<DefId>;
205-
fn associated_item(&self, def: DefId) -> Option<ty::AssociatedItem>;
190+
fn associated_item_cloned(&self, def: DefId) -> ty::AssociatedItem;
206191

207192
// flags
208193
fn is_const_fn(&self, did: DefId) -> bool;
@@ -252,12 +237,11 @@ pub trait CrateStore<'tcx> {
252237
fn load_macro(&self, did: DefId, sess: &Session) -> LoadedMacro;
253238

254239
// misc. metadata
255-
fn maybe_get_item_body<'a>(&'tcx self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId)
256-
-> Option<&'tcx hir::Body>;
240+
fn maybe_get_item_body<'a, 'tcx>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId)
241+
-> Option<&'tcx hir::Body>;
257242
fn item_body_nested_bodies(&self, def: DefId) -> BTreeMap<hir::BodyId, hir::Body>;
258243
fn const_is_rvalue_promotable_to_static(&self, def: DefId) -> bool;
259244

260-
fn get_item_mir<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId) -> Mir<'tcx>;
261245
fn is_item_mir_available(&self, def: DefId) -> bool;
262246

263247
// This is basically a 1-based range of ints, which is a little
@@ -272,10 +256,10 @@ pub trait CrateStore<'tcx> {
272256
fn used_crates(&self, prefer: LinkagePreference) -> Vec<(CrateNum, LibSource)>;
273257
fn used_crate_source(&self, cnum: CrateNum) -> CrateSource;
274258
fn extern_mod_stmt_cnum(&self, emod_id: ast::NodeId) -> Option<CrateNum>;
275-
fn encode_metadata<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>,
276-
reexports: &def::ExportMap,
277-
link_meta: &LinkMeta,
278-
reachable: &NodeSet) -> Vec<u8>;
259+
fn encode_metadata<'a, 'tcx>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>,
260+
reexports: &def::ExportMap,
261+
link_meta: &LinkMeta,
262+
reachable: &NodeSet) -> Vec<u8>;
279263
fn metadata_encoding_version(&self) -> &[u8];
280264
}
281265

@@ -309,53 +293,37 @@ pub fn validate_crate_name(sess: Option<&Session>, s: &str, sp: Option<Span>) {
309293
/// A dummy crate store that does not support any non-local crates,
310294
/// for test purposes.
311295
pub struct DummyCrateStore;
296+
312297
#[allow(unused_variables)]
313-
impl<'tcx> CrateStore<'tcx> for DummyCrateStore {
298+
impl CrateStore for DummyCrateStore {
299+
fn crate_data_as_rc_any(&self, krate: CrateNum) -> Rc<Any>
300+
{ bug!("crate_data_as_rc_any") }
314301
// item info
315302
fn describe_def(&self, def: DefId) -> Option<Def> { bug!("describe_def") }
316303
fn def_span(&self, sess: &Session, def: DefId) -> Span { bug!("def_span") }
317304
fn stability(&self, def: DefId) -> Option<attr::Stability> { bug!("stability") }
318305
fn deprecation(&self, def: DefId) -> Option<attr::Deprecation> { bug!("deprecation") }
319306
fn visibility(&self, def: DefId) -> ty::Visibility { bug!("visibility") }
320-
fn closure_kind(&self, def_id: DefId) -> ty::ClosureKind { bug!("closure_kind") }
321-
fn closure_ty<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId)
322-
-> ty::ClosureTy<'tcx> { bug!("closure_ty") }
323-
fn item_variances(&self, def: DefId) -> Vec<ty::Variance> { bug!("item_variances") }
324-
fn item_type<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId)
325-
-> Ty<'tcx> { bug!("item_type") }
326307
fn visible_parent_map<'a>(&'a self) -> ::std::cell::RefMut<'a, DefIdMap<DefId>> {
327308
bug!("visible_parent_map")
328309
}
329-
fn item_predicates<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId)
330-
-> ty::GenericPredicates<'tcx> { bug!("item_predicates") }
331-
fn item_super_predicates<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId)
332-
-> ty::GenericPredicates<'tcx> { bug!("item_super_predicates") }
333-
fn item_generics(&self, def: DefId) -> ty::Generics { bug!("item_generics") }
310+
fn item_generics_cloned(&self, def: DefId) -> ty::Generics
311+
{ bug!("item_generics_cloned") }
334312
fn item_attrs(&self, def_id: DefId) -> Vec<ast::Attribute> { bug!("item_attrs") }
335-
fn trait_def<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId)-> ty::TraitDef
336-
{ bug!("trait_def") }
337-
fn adt_def<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId) -> &'tcx ty::AdtDef
338-
{ bug!("adt_def") }
339313
fn fn_arg_names(&self, did: DefId) -> Vec<ast::Name> { bug!("fn_arg_names") }
340314
fn inherent_implementations_for_type(&self, def_id: DefId) -> Vec<DefId> { vec![] }
341315

342316
// trait info
343317
fn implementations_of_trait(&self, filter: Option<DefId>) -> Vec<DefId> { vec![] }
344318

345319
// impl info
346-
fn associated_item_def_ids(&self, def_id: DefId) -> Vec<DefId>
347-
{ bug!("associated_items") }
348-
fn impl_trait_ref<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId)
349-
-> Option<ty::TraitRef<'tcx>> { bug!("impl_trait_ref") }
350320
fn impl_polarity(&self, def: DefId) -> hir::ImplPolarity { bug!("impl_polarity") }
351-
fn custom_coerce_unsized_kind(&self, def: DefId)
352-
-> Option<ty::adjustment::CustomCoerceUnsized>
353-
{ bug!("custom_coerce_unsized_kind") }
354321
fn impl_parent(&self, def: DefId) -> Option<DefId> { bug!("impl_parent") }
355322

356323
// trait/impl-item info
357324
fn trait_of_item(&self, def_id: DefId) -> Option<DefId> { bug!("trait_of_item") }
358-
fn associated_item(&self, def: DefId) -> Option<ty::AssociatedItem> { bug!("associated_item") }
325+
fn associated_item_cloned(&self, def: DefId) -> ty::AssociatedItem
326+
{ bug!("associated_item_cloned") }
359327

360328
// flags
361329
fn is_const_fn(&self, did: DefId) -> bool { bug!("is_const_fn") }
@@ -418,8 +386,8 @@ impl<'tcx> CrateStore<'tcx> for DummyCrateStore {
418386
fn load_macro(&self, did: DefId, sess: &Session) -> LoadedMacro { bug!("load_macro") }
419387

420388
// misc. metadata
421-
fn maybe_get_item_body<'a>(&'tcx self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId)
422-
-> Option<&'tcx hir::Body> {
389+
fn maybe_get_item_body<'a, 'tcx>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId)
390+
-> Option<&'tcx hir::Body> {
423391
bug!("maybe_get_item_body")
424392
}
425393
fn item_body_nested_bodies(&self, def: DefId) -> BTreeMap<hir::BodyId, hir::Body> {
@@ -429,8 +397,6 @@ impl<'tcx> CrateStore<'tcx> for DummyCrateStore {
429397
bug!("const_is_rvalue_promotable_to_static")
430398
}
431399

432-
fn get_item_mir<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId)
433-
-> Mir<'tcx> { bug!("get_item_mir") }
434400
fn is_item_mir_available(&self, def: DefId) -> bool {
435401
bug!("is_item_mir_available")
436402
}
@@ -448,7 +414,7 @@ impl<'tcx> CrateStore<'tcx> for DummyCrateStore {
448414
{ vec![] }
449415
fn used_crate_source(&self, cnum: CrateNum) -> CrateSource { bug!("used_crate_source") }
450416
fn extern_mod_stmt_cnum(&self, emod_id: ast::NodeId) -> Option<CrateNum> { None }
451-
fn encode_metadata<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>,
417+
fn encode_metadata<'a, 'tcx>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>,
452418
reexports: &def::ExportMap,
453419
link_meta: &LinkMeta,
454420
reachable: &NodeSet) -> Vec<u8> { vec![] }

src/librustc/middle/resolve_lifetime.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -995,7 +995,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
995995
} else {
996996
let cstore = &self.sess.cstore;
997997
self.xcrate_object_lifetime_defaults.entry(def_id).or_insert_with(|| {
998-
cstore.item_generics(def_id).types.into_iter().map(|def| {
998+
cstore.item_generics_cloned(def_id).types.into_iter().map(|def| {
999999
def.object_lifetime_default
10001000
}).collect()
10011001
})

src/librustc/middle/stability.rs

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -433,23 +433,27 @@ struct Checker<'a, 'tcx: 'a> {
433433

434434
impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
435435
// (See issue #38412)
436-
fn skip_stability_check_due_to_privacy(self, def_id: DefId) -> bool {
437-
let visibility = {
438-
// Check if `def_id` is a trait method.
439-
match self.sess.cstore.associated_item(def_id) {
440-
Some(ty::AssociatedItem { container: ty::TraitContainer(trait_def_id), .. }) => {
441-
// Trait methods do not declare visibility (even
442-
// for visibility info in cstore). Use containing
443-
// trait instead, so methods of pub traits are
444-
// themselves considered pub.
445-
self.sess.cstore.visibility(trait_def_id)
446-
}
447-
_ => {
448-
// Otherwise, cstore info works directly.
449-
self.sess.cstore.visibility(def_id)
436+
fn skip_stability_check_due_to_privacy(self, mut def_id: DefId) -> bool {
437+
// Check if `def_id` is a trait method.
438+
match self.sess.cstore.describe_def(def_id) {
439+
Some(Def::Method(_)) |
440+
Some(Def::AssociatedTy(_)) |
441+
Some(Def::AssociatedConst(_)) => {
442+
match self.associated_item(def_id).container {
443+
ty::TraitContainer(trait_def_id) => {
444+
// Trait methods do not declare visibility (even
445+
// for visibility info in cstore). Use containing
446+
// trait instead, so methods of pub traits are
447+
// themselves considered pub.
448+
def_id = trait_def_id;
449+
}
450+
_ => {}
450451
}
451452
}
452-
};
453+
_ => {}
454+
}
455+
456+
let visibility = self.sess.cstore.visibility(def_id);
453457

454458
match visibility {
455459
// must check stability for pub items.

src/librustc/session/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ pub struct Session {
6464
pub target: config::Config,
6565
pub host: Target,
6666
pub opts: config::Options,
67-
pub cstore: Rc<for<'a> CrateStore<'a>>,
67+
pub cstore: Rc<CrateStore>,
6868
pub parse_sess: ParseSess,
6969
// For a library crate, this is always none
7070
pub entry_fn: RefCell<Option<(NodeId, Span)>>,
@@ -510,7 +510,7 @@ pub fn build_session(sopts: config::Options,
510510
dep_graph: &DepGraph,
511511
local_crate_source_file: Option<PathBuf>,
512512
registry: errors::registry::Registry,
513-
cstore: Rc<for<'a> CrateStore<'a>>)
513+
cstore: Rc<CrateStore>)
514514
-> Session {
515515
build_session_with_codemap(sopts,
516516
dep_graph,
@@ -525,7 +525,7 @@ pub fn build_session_with_codemap(sopts: config::Options,
525525
dep_graph: &DepGraph,
526526
local_crate_source_file: Option<PathBuf>,
527527
registry: errors::registry::Registry,
528-
cstore: Rc<for<'a> CrateStore<'a>>,
528+
cstore: Rc<CrateStore>,
529529
codemap: Rc<codemap::CodeMap>,
530530
emitter_dest: Option<Box<Write + Send>>)
531531
-> Session {
@@ -575,7 +575,7 @@ pub fn build_session_(sopts: config::Options,
575575
local_crate_source_file: Option<PathBuf>,
576576
span_diagnostic: errors::Handler,
577577
codemap: Rc<codemap::CodeMap>,
578-
cstore: Rc<for<'a> CrateStore<'a>>)
578+
cstore: Rc<CrateStore>)
579579
-> Session {
580580
let host = match Target::search(config::host_triple()) {
581581
Ok(t) => t,

src/librustc/traits/fulfill.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ impl<'a, 'gcx, 'tcx> DeferredObligation<'tcx> {
155155
-> Option<Vec<PredicateObligation<'tcx>>> {
156156
if let ty::TyAnon(def_id, substs) = self.predicate.skip_binder().self_ty().sty {
157157
let ty = if def_id.is_local() {
158-
tcx.maps.types.borrow().get(&def_id).cloned()
158+
tcx.maps.ty.borrow().get(&def_id).cloned()
159159
} else {
160160
Some(tcx.item_type(def_id))
161161
};

src/librustc/ty/context.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ use util::nodemap::{FxHashMap, FxHashSet};
4444
use rustc_data_structures::accumulate_vec::AccumulateVec;
4545

4646
use arena::{TypedArena, DroplessArena};
47+
use rustc_data_structures::indexed_vec::IndexVec;
4748
use std::borrow::Borrow;
4849
use std::cell::{Cell, RefCell};
4950
use std::hash::{Hash, Hasher};
@@ -220,7 +221,7 @@ pub struct TypeckTables<'tcx> {
220221
/// Records the type of each closure.
221222
pub closure_tys: NodeMap<ty::ClosureTy<'tcx>>,
222223

223-
/// Records the type of each closure.
224+
/// Records the kind of each closure.
224225
pub closure_kinds: NodeMap<ty::ClosureKind>,
225226

226227
/// For each fn, records the "liberated" types of its arguments
@@ -389,15 +390,15 @@ pub struct GlobalCtxt<'tcx> {
389390
global_arenas: &'tcx GlobalArenas<'tcx>,
390391
global_interners: CtxtInterners<'tcx>,
391392

393+
pub sess: &'tcx Session,
394+
392395
pub specializes_cache: RefCell<traits::SpecializesCache>,
393396

394397
pub dep_graph: DepGraph,
395398

396399
/// Common types, pre-interned for your convenience.
397400
pub types: CommonTypes<'tcx>,
398401

399-
pub sess: &'tcx Session,
400-
401402
/// Map indicating what traits are in scope for places where this
402403
/// is relevant; generated by resolve.
403404
pub trait_map: TraitMap,
@@ -659,6 +660,8 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
659660
/// value (types, substs, etc.) can only be used while `ty::tls` has a valid
660661
/// reference to the context, to allow formatting values that need it.
661662
pub fn create_and_enter<F, R>(s: &'tcx Session,
663+
local_providers: ty::maps::Providers<'tcx>,
664+
extern_providers: ty::maps::Providers<'tcx>,
662665
arenas: &'tcx GlobalArenas<'tcx>,
663666
arena: &'tcx DroplessArena,
664667
resolutions: ty::Resolutions,
@@ -676,7 +679,11 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
676679
let common_types = CommonTypes::new(&interners);
677680
let dep_graph = hir.dep_graph.clone();
678681
let fulfilled_predicates = traits::GlobalFulfilledPredicates::new(dep_graph.clone());
682+
let max_cnum = s.cstore.crates().iter().map(|c| c.as_usize()).max().unwrap_or(0);
683+
let mut providers = IndexVec::from_elem_n(extern_providers, max_cnum + 1);
684+
providers[LOCAL_CRATE] = local_providers;
679685
tls::enter_global(GlobalCtxt {
686+
sess: s,
680687
specializes_cache: RefCell::new(traits::SpecializesCache::new()),
681688
global_arenas: arenas,
682689
global_interners: interners,
@@ -686,11 +693,10 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
686693
region_maps: region_maps,
687694
free_region_maps: RefCell::new(FxHashMap()),
688695
variance_computed: Cell::new(false),
689-
sess: s,
690696
trait_map: resolutions.trait_map,
691697
fulfilled_predicates: RefCell::new(fulfilled_predicates),
692698
hir: hir,
693-
maps: maps::Maps::new(dep_graph),
699+
maps: maps::Maps::new(dep_graph, providers),
694700
freevars: RefCell::new(resolutions.freevars),
695701
maybe_unused_trait_imports: resolutions.maybe_unused_trait_imports,
696702
rcache: RefCell::new(FxHashMap()),

src/librustc/ty/item_path.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
201201
} else {
202202
// for local crates, check whether type info is
203203
// available; typeck might not have completed yet
204-
self.maps.impl_trait_refs.borrow().contains_key(&impl_def_id)
204+
self.maps.impl_trait_ref.borrow().contains_key(&impl_def_id)
205205
};
206206

207207
if !use_types {

0 commit comments

Comments
 (0)