Skip to content

Commit

Permalink
Move the untracked cstore and source_span into a struct
Browse files Browse the repository at this point in the history
  • Loading branch information
oli-obk committed Dec 9, 2022
1 parent 2cd36f2 commit 1c1d357
Show file tree
Hide file tree
Showing 16 changed files with 78 additions and 91 deletions.
1 change: 1 addition & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4342,6 +4342,7 @@ dependencies = [
"rustc_feature",
"rustc_fs_util",
"rustc_hir",
"rustc_index",
"rustc_lint_defs",
"rustc_macros",
"rustc_serialize",
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_interface/src/passes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -808,6 +808,7 @@ pub fn create_global_ctxt<'tcx>(
definitions,
global_ctxt: untracked_resolutions,
ast_lowering: untracked_resolver_for_lowering,
untracked,
} = resolver_outputs;

let gcx = sess.time("setup_global_ctxt", || {
Expand All @@ -819,6 +820,7 @@ pub fn create_global_ctxt<'tcx>(
hir_arena,
definitions,
untracked_resolutions,
untracked,
krate,
dep_graph,
queries.on_disk_cache.as_ref().map(OnDiskCache::as_dyn),
Expand Down
3 changes: 3 additions & 0 deletions compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,9 @@ impl CrateStore for CStore {
fn as_any(&self) -> &dyn Any {
self
}
fn untracked_as_any(&mut self) -> &mut dyn Any {
self
}

fn crate_name(&self, cnum: CrateNum) -> Symbol {
self.get_crate_data(cnum).root.name
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_middle/src/hir/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use rustc_index::vec::Idx;
use rustc_middle::hir::nested_filter;
use rustc_span::def_id::StableCrateId;
use rustc_span::symbol::{kw, sym, Ident, Symbol};
use rustc_span::{Span, DUMMY_SP};
use rustc_span::Span;
use rustc_target::spec::abi::Abi;

#[inline]
Expand Down Expand Up @@ -1162,7 +1162,7 @@ pub(super) fn crate_hash(tcx: TyCtxt<'_>, crate_num: CrateNum) -> Svh {
.filter_map(|(def_id, info)| {
let _ = info.as_owner()?;
let def_path_hash = definitions.def_path_hash(def_id);
let span = resolutions.source_span.get(def_id).unwrap_or(&DUMMY_SP);
let span = tcx.source_span(def_id);
debug_assert_eq!(span.parent(), None);
Some((def_path_hash, span))
})
Expand Down
2 changes: 0 additions & 2 deletions compiler/rustc_middle/src/hir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,6 @@ pub fn provide(providers: &mut Providers) {
providers.hir_attrs = |tcx, id| {
tcx.hir_crate(()).owners[id.def_id].as_owner().map_or(AttributeMap::EMPTY, |o| &o.attrs)
};
providers.source_span =
|tcx, def_id| tcx.resolutions(()).source_span.get(def_id).copied().unwrap_or(DUMMY_SP);
providers.def_span = |tcx, def_id| {
let def_id = def_id.expect_local();
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_middle/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ rustc_queries! {
/// This span is meant for dep-tracking rather than diagnostics. It should not be used outside
/// of rustc_middle::hir::source_map.
query source_span(key: LocalDefId) -> Span {
// Accesses untracked data
eval_always
desc { "getting the source span" }
}

Expand Down
69 changes: 27 additions & 42 deletions compiler/rustc_middle/src/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ use rustc_query_system::dep_graph::DepNodeIndex;
use rustc_query_system::ich::StableHashingContext;
use rustc_serialize::opaque::{FileEncodeResult, FileEncoder};
use rustc_session::config::{CrateType, OutputFilenames};
use rustc_session::cstore::CrateStoreDyn;
use rustc_session::cstore::{CrateStoreDyn, Untracked};
use rustc_session::lint::Lint;
use rustc_session::Limit;
use rustc_session::Session;
Expand Down Expand Up @@ -187,15 +187,13 @@ impl<'tcx> CtxtInterners<'tcx> {
kind: TyKind<'tcx>,
sess: &Session,
definitions: &rustc_hir::definitions::Definitions,
cstore: &CrateStoreDyn,
source_span: &IndexVec<LocalDefId, Span>,
untracked: &Untracked,
) -> Ty<'tcx> {
Ty(Interned::new_unchecked(
self.type_
.intern(kind, |kind| {
let flags = super::flags::FlagComputation::for_kind(&kind);
let stable_hash =
self.stable_hash(&flags, sess, definitions, cstore, source_span, &kind);
let stable_hash = self.stable_hash(&flags, sess, definitions, untracked, &kind);

InternedInSet(self.arena.alloc(WithCachedTypeInfo {
internee: kind,
Expand All @@ -213,8 +211,7 @@ impl<'tcx> CtxtInterners<'tcx> {
flags: &ty::flags::FlagComputation,
sess: &'a Session,
definitions: &'a rustc_hir::definitions::Definitions,
cstore: &'a CrateStoreDyn,
source_span: &'a IndexVec<LocalDefId, Span>,
untracked: &'a Untracked,
val: &T,
) -> Fingerprint {
// It's impossible to hash inference variables (and will ICE), so we don't need to try to cache them.
Expand All @@ -223,7 +220,7 @@ impl<'tcx> CtxtInterners<'tcx> {
Fingerprint::ZERO
} else {
let mut hasher = StableHasher::new();
let mut hcx = StableHashingContext::new(sess, definitions, cstore, source_span);
let mut hcx = StableHashingContext::new(sess, definitions, untracked);
val.hash_stable(&mut hcx, &mut hasher);
hasher.finish()
}
Expand All @@ -235,16 +232,14 @@ impl<'tcx> CtxtInterners<'tcx> {
kind: Binder<'tcx, PredicateKind<'tcx>>,
sess: &Session,
definitions: &rustc_hir::definitions::Definitions,
cstore: &CrateStoreDyn,
source_span: &IndexVec<LocalDefId, Span>,
untracked: &Untracked,
) -> Predicate<'tcx> {
Predicate(Interned::new_unchecked(
self.predicate
.intern(kind, |kind| {
let flags = super::flags::FlagComputation::for_predicate(kind);

let stable_hash =
self.stable_hash(&flags, sess, definitions, cstore, source_span, &kind);
let stable_hash = self.stable_hash(&flags, sess, definitions, untracked, &kind);

InternedInSet(self.arena.alloc(WithCachedTypeInfo {
internee: kind,
Expand Down Expand Up @@ -963,10 +958,9 @@ impl<'tcx> CommonTypes<'tcx> {
interners: &CtxtInterners<'tcx>,
sess: &Session,
definitions: &rustc_hir::definitions::Definitions,
cstore: &CrateStoreDyn,
source_span: &IndexVec<LocalDefId, Span>,
untracked: &Untracked,
) -> CommonTypes<'tcx> {
let mk = |ty| interners.intern_ty(ty, sess, definitions, cstore, source_span);
let mk = |ty| interners.intern_ty(ty, sess, definitions, untracked);

CommonTypes {
unit: mk(Tuple(List::empty())),
Expand Down Expand Up @@ -1114,6 +1108,7 @@ pub struct GlobalCtxt<'tcx> {

definitions: RwLock<Definitions>,

untracked: Untracked,
/// Output of the resolver.
pub(crate) untracked_resolutions: ty::ResolverGlobalCtxt,
/// The entire crate as AST. This field serves as the input for the hir_crate query,
Expand Down Expand Up @@ -1280,6 +1275,7 @@ impl<'tcx> TyCtxt<'tcx> {
hir_arena: &'tcx WorkerLocal<hir::Arena<'tcx>>,
definitions: Definitions,
untracked_resolutions: ty::ResolverGlobalCtxt,
untracked: Untracked,
krate: Lrc<ast::Crate>,
dep_graph: DepGraph,
on_disk_cache: Option<&'tcx dyn OnDiskCache<'tcx>>,
Expand All @@ -1292,14 +1288,7 @@ impl<'tcx> TyCtxt<'tcx> {
s.emit_fatal(err);
});
let interners = CtxtInterners::new(arena);
let common_types = CommonTypes::new(
&interners,
s,
&definitions,
&*untracked_resolutions.cstore,
// This is only used to create a stable hashing context.
&untracked_resolutions.source_span,
);
let common_types = CommonTypes::new(&interners, s, &definitions, &untracked);
let common_lifetimes = CommonLifetimes::new(&interners);
let common_consts = CommonConsts::new(&interners, &common_types);

Expand All @@ -1315,6 +1304,7 @@ impl<'tcx> TyCtxt<'tcx> {
types: common_types,
lifetimes: common_lifetimes,
consts: common_consts,
untracked,
untracked_resolutions,
untracked_crate: Steal::new(krate),
on_disk_cache,
Expand Down Expand Up @@ -1428,7 +1418,7 @@ impl<'tcx> TyCtxt<'tcx> {
if let Some(id) = id.as_local() {
self.definitions_untracked().def_key(id)
} else {
self.untracked_resolutions.cstore.def_key(id)
self.untracked.cstore.def_key(id)
}
}

Expand All @@ -1442,7 +1432,7 @@ impl<'tcx> TyCtxt<'tcx> {
if let Some(id) = id.as_local() {
self.definitions_untracked().def_path(id)
} else {
self.untracked_resolutions.cstore.def_path(id)
self.untracked.cstore.def_path(id)
}
}

Expand All @@ -1452,7 +1442,7 @@ impl<'tcx> TyCtxt<'tcx> {
if let Some(def_id) = def_id.as_local() {
self.definitions_untracked().def_path_hash(def_id)
} else {
self.untracked_resolutions.cstore.def_path_hash(def_id)
self.untracked.cstore.def_path_hash(def_id)
}
}

Expand All @@ -1461,7 +1451,7 @@ impl<'tcx> TyCtxt<'tcx> {
if crate_num == LOCAL_CRATE {
self.sess.local_stable_crate_id()
} else {
self.untracked_resolutions.cstore.stable_crate_id(crate_num)
self.untracked.cstore.stable_crate_id(crate_num)
}
}

Expand All @@ -1472,7 +1462,7 @@ impl<'tcx> TyCtxt<'tcx> {
if stable_crate_id == self.sess.local_stable_crate_id() {
LOCAL_CRATE
} else {
self.untracked_resolutions.cstore.stable_crate_id_to_crate_num(stable_crate_id)
self.untracked.cstore.stable_crate_id_to_crate_num(stable_crate_id)
}
}

Expand All @@ -1491,7 +1481,7 @@ impl<'tcx> TyCtxt<'tcx> {
} else {
// If this is a DefPathHash from an upstream crate, let the CrateStore map
// it to a DefId.
let cstore = &*self.untracked_resolutions.cstore;
let cstore = &*self.untracked.cstore;
let cnum = cstore.stable_crate_id_to_crate_num(stable_crate_id);
cstore.def_path_hash_to_def_id(cnum, hash)
}
Expand All @@ -1505,7 +1495,7 @@ impl<'tcx> TyCtxt<'tcx> {
let (crate_name, stable_crate_id) = if def_id.is_local() {
(self.crate_name, self.sess.local_stable_crate_id())
} else {
let cstore = &*self.untracked_resolutions.cstore;
let cstore = &*self.untracked.cstore;
(cstore.crate_name(def_id.krate), cstore.stable_crate_id(def_id.krate))
};

Expand Down Expand Up @@ -1604,7 +1594,7 @@ impl<'tcx> TyCtxt<'tcx> {
/// Note that this is *untracked* and should only be used within the query
/// system if the result is otherwise tracked through queries
pub fn cstore_untracked(self) -> &'tcx CrateStoreDyn {
&*self.untracked_resolutions.cstore
&*self.untracked.cstore
}

/// Note that this is *untracked* and should only be used within the query
Expand All @@ -1618,7 +1608,7 @@ impl<'tcx> TyCtxt<'tcx> {
/// system if the result is otherwise tracked through queries
#[inline]
pub fn source_span_untracked(self, def_id: LocalDefId) -> Span {
self.untracked_resolutions.source_span.get(def_id).copied().unwrap_or(DUMMY_SP)
self.untracked.source_span.get(def_id).copied().unwrap_or(DUMMY_SP)
}

#[inline(always)]
Expand All @@ -1627,12 +1617,7 @@ impl<'tcx> TyCtxt<'tcx> {
f: impl FnOnce(StableHashingContext<'_>) -> R,
) -> R {
let definitions = self.definitions_untracked();
let hcx = StableHashingContext::new(
self.sess,
&*definitions,
&*self.untracked_resolutions.cstore,
&self.untracked_resolutions.source_span,
);
let hcx = StableHashingContext::new(self.sess, &*definitions, &self.untracked);
f(hcx)
}

Expand Down Expand Up @@ -2428,9 +2413,8 @@ impl<'tcx> TyCtxt<'tcx> {
st,
self.sess,
&self.definitions.read(),
&*self.untracked_resolutions.cstore,
// This is only used to create a stable hashing context.
&self.untracked_resolutions.source_span,
&self.untracked,
)
}

Expand All @@ -2440,9 +2424,8 @@ impl<'tcx> TyCtxt<'tcx> {
binder,
self.sess,
&self.definitions.read(),
&*self.untracked_resolutions.cstore,
// This is only used to create a stable hashing context.
&self.untracked_resolutions.source_span,
&self.untracked,
)
}

Expand Down Expand Up @@ -3124,4 +3107,6 @@ pub fn provide(providers: &mut ty::query::Providers) {
// We want to check if the panic handler was defined in this crate
tcx.lang_items().panic_impl().map_or(false, |did| did.is_local())
};
providers.source_span =
|tcx, def_id| tcx.untracked.source_span.get(def_id).copied().unwrap_or(DUMMY_SP);
}
6 changes: 2 additions & 4 deletions compiler/rustc_middle/src/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ use rustc_index::vec::IndexVec;
use rustc_macros::HashStable;
use rustc_query_system::ich::StableHashingContext;
use rustc_serialize::{Decodable, Encodable};
use rustc_session::cstore::CrateStoreDyn;
use rustc_session::cstore::Untracked;
use rustc_span::hygiene::MacroKind;
use rustc_span::symbol::{kw, sym, Ident, Symbol};
use rustc_span::{ExpnId, Span};
Expand Down Expand Up @@ -153,18 +153,16 @@ pub struct ResolverOutputs {
pub definitions: Definitions,
pub global_ctxt: ResolverGlobalCtxt,
pub ast_lowering: ResolverAstLowering,
pub untracked: Untracked,
}

#[derive(Debug)]
pub struct ResolverGlobalCtxt {
pub cstore: Box<CrateStoreDyn>,
pub visibilities: FxHashMap<LocalDefId, Visibility>,
/// This field is used to decide whether we should make `PRIVATE_IN_PUBLIC` a hard error.
pub has_pub_restricted: bool,
/// Item with a given `LocalDefId` was defined during macro expansion with ID `ExpnId`.
pub expn_that_defined: FxHashMap<LocalDefId, ExpnId>,
/// Reference span for definitions.
pub source_span: IndexVec<LocalDefId, Span>,
pub effective_visibilities: EffectiveVisibilities,
pub extern_crate_map: FxHashMap<LocalDefId, CrateNum>,
pub maybe_unused_trait_imports: FxIndexSet<LocalDefId>,
Expand Down
Loading

0 comments on commit 1c1d357

Please sign in to comment.