Skip to content

Commit

Permalink
Rollup merge of rust-lang#44420 - alexcrichton:private-cstore, r=mich…
Browse files Browse the repository at this point in the history
…aelwoerister

rustc: Make `CrateStore` private to `TyCtxt`

This commit makes the `CrateStore` object private to the `ty/context.rs` module and also absent on the `Session` itself.

cc rust-lang#44390
cc rust-lang#44341 (initial commit pulled and rebased from here)
  • Loading branch information
frewsxcv authored Sep 11, 2017
2 parents dc34c64 + 224d47d commit 5ecaddb
Show file tree
Hide file tree
Showing 24 changed files with 190 additions and 131 deletions.
9 changes: 7 additions & 2 deletions src/librustc/hir/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ use hir::map::definitions::DefPathData;
use hir::def_id::{DefIndex, DefId, CRATE_DEF_INDEX};
use hir::def::{Def, PathResolution};
use lint::builtin::PARENTHESIZED_PARAMS_IN_TYPES_AND_MODULES;
use middle::cstore::CrateStore;
use rustc_data_structures::indexed_vec::IndexVec;
use session::Session;
use util::common::FN_OUTPUT_NAME;
Expand Down Expand Up @@ -75,6 +76,8 @@ pub struct LoweringContext<'a> {
// Use to assign ids to hir nodes that do not directly correspond to an ast node
sess: &'a Session,

cstore: &'a CrateStore,

// As we walk the AST we must keep track of the current 'parent' def id (in
// the form of a DefIndex) so that if we create a new node which introduces
// a definition, then we can properly create the def id.
Expand Down Expand Up @@ -119,6 +122,7 @@ pub trait Resolver {
}

pub fn lower_crate(sess: &Session,
cstore: &CrateStore,
krate: &Crate,
resolver: &mut Resolver)
-> hir::Crate {
Expand All @@ -130,6 +134,7 @@ pub fn lower_crate(sess: &Session,
LoweringContext {
crate_root: std_inject::injected_crate_name(krate),
sess,
cstore,
parent_def: None,
resolver,
name_map: FxHashMap(),
Expand Down Expand Up @@ -535,7 +540,7 @@ impl<'a> LoweringContext<'a> {
if id.is_local() {
self.resolver.definitions().def_key(id.index)
} else {
self.sess.cstore.def_key(id)
self.cstore.def_key(id)
}
}

Expand Down Expand Up @@ -787,7 +792,7 @@ impl<'a> LoweringContext<'a> {
return n;
}
assert!(!def_id.is_local());
let n = self.sess.cstore.item_generics_cloned_untracked(def_id).regions.len();
let n = self.cstore.item_generics_cloned_untracked(def_id).regions.len();
self.type_def_lifetime_params.insert(def_id, n);
n
});
Expand Down
13 changes: 9 additions & 4 deletions src/librustc/middle/resolve_lifetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@
//! way. Therefore we break lifetime name resolution into a separate pass.
use hir::map::Map;
use session::Session;
use hir::def::Def;
use hir::def_id::DefId;
use middle::cstore::CrateStore;
use session::Session;
use ty;

use std::cell::Cell;
Expand Down Expand Up @@ -160,6 +161,7 @@ pub struct NamedRegionMap {

struct LifetimeContext<'a, 'tcx: 'a> {
sess: &'a Session,
cstore: &'a CrateStore,
hir_map: &'a Map<'tcx>,
map: &'a mut NamedRegionMap,
scope: ScopeRef<'a>,
Expand Down Expand Up @@ -251,6 +253,7 @@ type ScopeRef<'a> = &'a Scope<'a>;
const ROOT_SCOPE: ScopeRef<'static> = &Scope::Root;

pub fn krate(sess: &Session,
cstore: &CrateStore,
hir_map: &Map)
-> Result<NamedRegionMap, ErrorReported> {
let krate = hir_map.krate();
Expand All @@ -262,6 +265,7 @@ pub fn krate(sess: &Session,
sess.track_errors(|| {
let mut visitor = LifetimeContext {
sess,
cstore,
hir_map,
map: &mut map,
scope: ROOT_SCOPE,
Expand Down Expand Up @@ -765,12 +769,13 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
fn with<F>(&mut self, wrap_scope: Scope, f: F) where
F: for<'b> FnOnce(ScopeRef, &mut LifetimeContext<'b, 'tcx>),
{
let LifetimeContext {sess, hir_map, ref mut map, ..} = *self;
let LifetimeContext {sess, cstore, hir_map, ref mut map, ..} = *self;
let labels_in_fn = replace(&mut self.labels_in_fn, vec![]);
let xcrate_object_lifetime_defaults =
replace(&mut self.xcrate_object_lifetime_defaults, DefIdMap());
let mut this = LifetimeContext {
sess,
cstore,
hir_map,
map: *map,
scope: &wrap_scope,
Expand Down Expand Up @@ -932,7 +937,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
let def_key = if def_id.is_local() {
this.hir_map.def_key(def_id)
} else {
this.sess.cstore.def_key(def_id)
this.cstore.def_key(def_id)
};
DefId {
krate: def_id.krate,
Expand Down Expand Up @@ -976,7 +981,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
let unsubst = if let Some(id) = self.hir_map.as_local_node_id(def_id) {
&map.object_lifetime_defaults[&id]
} else {
let cstore = &self.sess.cstore;
let cstore = self.cstore;
self.xcrate_object_lifetime_defaults.entry(def_id).or_insert_with(|| {
cstore.item_generics_cloned_untracked(def_id).types.into_iter().map(|def| {
def.object_lifetime_default
Expand Down
17 changes: 6 additions & 11 deletions src/librustc/session/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1953,13 +1953,12 @@ mod tests {
use errors;
use getopts;
use lint;
use middle::cstore::{self, DummyCrateStore};
use middle::cstore;
use session::config::{build_configuration, build_session_options_and_crate_config};
use session::build_session;
use std::collections::{BTreeMap, BTreeSet};
use std::iter::FromIterator;
use std::path::PathBuf;
use std::rc::Rc;
use super::{OutputType, OutputTypes, Externs};
use rustc_back::{PanicStrategy, RelroLevel};
use syntax::symbol::Symbol;
Expand Down Expand Up @@ -1991,7 +1990,7 @@ mod tests {
};
let registry = errors::registry::Registry::new(&[]);
let (sessopts, cfg) = build_session_options_and_crate_config(matches);
let sess = build_session(sessopts, &dep_graph, None, registry, Rc::new(DummyCrateStore));
let sess = build_session(sessopts, &dep_graph, None, registry);
let cfg = build_configuration(&sess, cfg);
assert!(cfg.contains(&(Symbol::intern("test"), None)));
}
Expand All @@ -2010,8 +2009,7 @@ mod tests {
};
let registry = errors::registry::Registry::new(&[]);
let (sessopts, cfg) = build_session_options_and_crate_config(matches);
let sess = build_session(sessopts, &dep_graph, None, registry,
Rc::new(DummyCrateStore));
let sess = build_session(sessopts, &dep_graph, None, registry);
let cfg = build_configuration(&sess, cfg);
let mut test_items = cfg.iter().filter(|&&(name, _)| name == "test");
assert!(test_items.next().is_some());
Expand All @@ -2027,8 +2025,7 @@ mod tests {
]).unwrap();
let registry = errors::registry::Registry::new(&[]);
let (sessopts, _) = build_session_options_and_crate_config(&matches);
let sess = build_session(sessopts, &dep_graph, None, registry,
Rc::new(DummyCrateStore));
let sess = build_session(sessopts, &dep_graph, None, registry);
assert!(!sess.diagnostic().can_emit_warnings);
}

Expand All @@ -2039,8 +2036,7 @@ mod tests {
]).unwrap();
let registry = errors::registry::Registry::new(&[]);
let (sessopts, _) = build_session_options_and_crate_config(&matches);
let sess = build_session(sessopts, &dep_graph, None, registry,
Rc::new(DummyCrateStore));
let sess = build_session(sessopts, &dep_graph, None, registry);
assert!(sess.diagnostic().can_emit_warnings);
}

Expand All @@ -2050,8 +2046,7 @@ mod tests {
]).unwrap();
let registry = errors::registry::Registry::new(&[]);
let (sessopts, _) = build_session_options_and_crate_config(&matches);
let sess = build_session(sessopts, &dep_graph, None, registry,
Rc::new(DummyCrateStore));
let sess = build_session(sessopts, &dep_graph, None, registry);
assert!(sess.diagnostic().can_emit_warnings);
}
}
Expand Down
14 changes: 3 additions & 11 deletions src/librustc/session/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ use dep_graph::DepGraph;
use hir::def_id::{CrateNum, DefIndex};

use lint;
use middle::cstore::CrateStore;
use middle::allocator::AllocatorKind;
use middle::dependency_format;
use session::search_paths::PathKind;
Expand Down Expand Up @@ -63,7 +62,6 @@ pub struct Session {
pub target: config::Config,
pub host: Target,
pub opts: config::Options,
pub cstore: Rc<CrateStore>,
pub parse_sess: ParseSess,
// For a library crate, this is always none
pub entry_fn: RefCell<Option<(NodeId, Span)>>,
Expand Down Expand Up @@ -621,16 +619,14 @@ impl Session {
pub fn build_session(sopts: config::Options,
dep_graph: &DepGraph,
local_crate_source_file: Option<PathBuf>,
registry: errors::registry::Registry,
cstore: Rc<CrateStore>)
registry: errors::registry::Registry)
-> Session {
let file_path_mapping = sopts.file_path_mapping();

build_session_with_codemap(sopts,
dep_graph,
local_crate_source_file,
registry,
cstore,
Rc::new(codemap::CodeMap::new(file_path_mapping)),
None)
}
Expand All @@ -639,7 +635,6 @@ pub fn build_session_with_codemap(sopts: config::Options,
dep_graph: &DepGraph,
local_crate_source_file: Option<PathBuf>,
registry: errors::registry::Registry,
cstore: Rc<CrateStore>,
codemap: Rc<codemap::CodeMap>,
emitter_dest: Option<Box<Write + Send>>)
-> Session {
Expand Down Expand Up @@ -680,16 +675,14 @@ pub fn build_session_with_codemap(sopts: config::Options,
dep_graph,
local_crate_source_file,
diagnostic_handler,
codemap,
cstore)
codemap)
}

pub fn build_session_(sopts: config::Options,
dep_graph: &DepGraph,
local_crate_source_file: Option<PathBuf>,
span_diagnostic: errors::Handler,
codemap: Rc<codemap::CodeMap>,
cstore: Rc<CrateStore>)
codemap: Rc<codemap::CodeMap>)
-> Session {
let host = match Target::search(config::host_triple()) {
Ok(t) => t,
Expand Down Expand Up @@ -726,7 +719,6 @@ pub fn build_session_(sopts: config::Options,
target: target_cfg,
host,
opts: sopts,
cstore,
parse_sess: p_s,
// For a library crate, this is always none
entry_fn: RefCell::new(None),
Expand Down
75 changes: 71 additions & 4 deletions src/librustc/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ use hir::map as hir_map;
use hir::map::DefPathHash;
use lint::{self, Lint};
use ich::{self, StableHashingContext, NodeIdHashingMode};
use middle::cstore::{CrateStore, LinkMeta, EncodedMetadataHashes};
use middle::cstore::EncodedMetadata;
use middle::free_region::FreeRegionMap;
use middle::lang_items;
use middle::resolve_lifetime::{self, ObjectLifetimeDefault};
Expand Down Expand Up @@ -50,6 +52,7 @@ use rustc_data_structures::stable_hasher::{HashStable, StableHasher,

use arena::{TypedArena, DroplessArena};
use rustc_data_structures::indexed_vec::IndexVec;
use std::any::Any;
use std::borrow::Borrow;
use std::cell::{Cell, RefCell};
use std::cmp::Ordering;
Expand Down Expand Up @@ -805,8 +808,11 @@ pub struct GlobalCtxt<'tcx> {
global_arenas: &'tcx GlobalArenas<'tcx>,
global_interners: CtxtInterners<'tcx>,

cstore: &'tcx CrateStore,

pub sess: &'tcx Session,


pub trans_trait_caches: traits::trans::TransTraitCaches<'tcx>,

pub dep_graph: DepGraph,
Expand Down Expand Up @@ -978,6 +984,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
/// value (types, substs, etc.) can only be used while `ty::tls` has a valid
/// reference to the context, to allow formatting values that need it.
pub fn create_and_enter<F, R>(s: &'tcx Session,
cstore: &'tcx CrateStore,
local_providers: ty::maps::Providers<'tcx>,
extern_providers: ty::maps::Providers<'tcx>,
mir_passes: Rc<Passes>,
Expand All @@ -994,16 +1001,15 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
let interners = CtxtInterners::new(arena);
let common_types = CommonTypes::new(&interners);
let dep_graph = hir.dep_graph.clone();
let max_cnum = s.cstore.crates_untracked().iter().map(|c| c.as_usize()).max().unwrap_or(0);
let max_cnum = cstore.crates_untracked().iter().map(|c| c.as_usize()).max().unwrap_or(0);
let mut providers = IndexVec::from_elem_n(extern_providers, max_cnum + 1);
providers[LOCAL_CRATE] = local_providers;

let def_path_hash_to_def_id = if s.opts.build_dep_graph() {
let upstream_def_path_tables: Vec<(CrateNum, Rc<_>)> = s
.cstore
let upstream_def_path_tables: Vec<(CrateNum, Rc<_>)> = cstore
.crates_untracked()
.iter()
.map(|&cnum| (cnum, s.cstore.def_path_table(cnum)))
.map(|&cnum| (cnum, cstore.def_path_table(cnum)))
.collect();

let def_path_tables = || {
Expand Down Expand Up @@ -1033,6 +1039,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {

tls::enter_global(GlobalCtxt {
sess: s,
cstore,
trans_trait_caches: traits::trans::TransTraitCaches::new(dep_graph.clone()),
global_arenas: arenas,
global_interners: interners,
Expand Down Expand Up @@ -1125,6 +1132,54 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
pub fn crates(self) -> Rc<Vec<CrateNum>> {
self.all_crate_nums(LOCAL_CRATE)
}

pub fn def_key(self, id: DefId) -> hir_map::DefKey {
if id.is_local() {
self.hir.def_key(id)
} else {
self.cstore.def_key(id)
}
}

/// Convert a `DefId` into its fully expanded `DefPath` (every
/// `DefId` is really just an interned def-path).
///
/// Note that if `id` is not local to this crate, the result will
/// be a non-local `DefPath`.
pub fn def_path(self, id: DefId) -> hir_map::DefPath {
if id.is_local() {
self.hir.def_path(id)
} else {
self.cstore.def_path(id)
}
}

#[inline]
pub fn def_path_hash(self, def_id: DefId) -> hir_map::DefPathHash {
if def_id.is_local() {
self.hir.definitions().def_path_hash(def_id.index)
} else {
self.cstore.def_path_hash(def_id)
}
}

pub fn metadata_encoding_version(self) -> Vec<u8> {
self.cstore.metadata_encoding_version().to_vec()
}

// Note that this is *untracked* and should only be used within the query
// system if the result is otherwise tracked through queries
pub fn crate_data_as_rc_any(self, cnum: CrateNum) -> Rc<Any> {
self.cstore.crate_data_as_rc_any(cnum)
}
}

impl<'a, 'tcx> TyCtxt<'a, 'tcx, 'tcx> {
pub fn encode_metadata(self, link_meta: &LinkMeta, reachable: &NodeSet)
-> (EncodedMetadata, EncodedMetadataHashes)
{
self.cstore.encode_metadata(self, link_meta, reachable)
}
}

impl<'gcx: 'tcx, 'tcx> GlobalCtxt<'gcx> {
Expand Down Expand Up @@ -2060,4 +2115,16 @@ pub fn provide(providers: &mut ty::maps::Providers) {
let id = tcx.hir.definitions().def_index_to_hir_id(id.index);
tcx.stability().local_deprecation_entry(id)
};
providers.extern_mod_stmt_cnum = |tcx, id| {
let id = tcx.hir.definitions().find_node_for_hir_id(id);
tcx.cstore.extern_mod_stmt_cnum_untracked(id)
};
providers.all_crate_nums = |tcx, cnum| {
assert_eq!(cnum, LOCAL_CRATE);
Rc::new(tcx.cstore.crates_untracked())
};
providers.postorder_cnums = |tcx, cnum| {
assert_eq!(cnum, LOCAL_CRATE);
Rc::new(tcx.cstore.postorder_cnums_untracked())
};
}
2 changes: 1 addition & 1 deletion src/librustc/ty/item_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
}
}

cur_path.push(self.sess.cstore.def_key(cur_def)
cur_path.push(self.def_key(cur_def)
.disambiguated_data.data.get_opt_name().unwrap_or_else(||
Symbol::intern("<unnamed>").as_str()));
match visible_parent_map.get(&cur_def) {
Expand Down
Loading

0 comments on commit 5ecaddb

Please sign in to comment.