Skip to content

Commit 24be7ce

Browse files
committed
Allow to create definitions inside the query system.
1 parent 0a9ebe9 commit 24be7ce

File tree

23 files changed

+261
-147
lines changed

23 files changed

+261
-147
lines changed

compiler/rustc_codegen_llvm/src/debuginfo/metadata/type_map.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,9 @@ impl<'tcx> UniqueTypeId<'tcx> {
9393
/// Right now this takes the form of a hex-encoded opaque hash value.
9494
pub fn generate_unique_id_string(self, tcx: TyCtxt<'tcx>) -> String {
9595
let mut hasher = StableHasher::new();
96-
let mut hcx = tcx.create_stable_hashing_context();
97-
hcx.while_hashing_spans(false, |hcx| self.hash_stable(hcx, &mut hasher));
96+
tcx.with_stable_hashing_context(|mut hcx| {
97+
hcx.while_hashing_spans(false, |hcx| self.hash_stable(hcx, &mut hasher))
98+
});
9899
hasher.finish::<Fingerprint>().to_hex()
99100
}
100101

compiler/rustc_codegen_ssa/src/debuginfo/type_names.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -701,12 +701,14 @@ fn push_const_param<'tcx>(tcx: TyCtxt<'tcx>, ct: ty::Const<'tcx>, output: &mut S
701701
// If we cannot evaluate the constant to a known type, we fall back
702702
// to emitting a stable hash value of the constant. This isn't very pretty
703703
// but we get a deterministic, virtually unique value for the constant.
704-
let hcx = &mut tcx.create_stable_hashing_context();
705-
let mut hasher = StableHasher::new();
706-
hcx.while_hashing_spans(false, |hcx| ct.val().hash_stable(hcx, &mut hasher));
704+
//
707705
// Let's only emit 64 bits of the hash value. That should be plenty for
708706
// avoiding collisions and will make the emitted type names shorter.
709-
let hash: u64 = hasher.finish();
707+
let hash: u64 = tcx.with_stable_hashing_context(|mut hcx| {
708+
let mut hasher = StableHasher::new();
709+
hcx.while_hashing_spans(false, |hcx| ct.val().hash_stable(hcx, &mut hasher));
710+
hasher.finish()
711+
});
710712

711713
if cpp_like_debuginfo(tcx) {
712714
write!(output, "CONST${:x}", hash)

compiler/rustc_data_structures/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#![feature(associated_type_bounds)]
1212
#![feature(auto_traits)]
1313
#![feature(bool_to_option)]
14+
#![feature(cell_leak)]
1415
#![feature(control_flow_enum)]
1516
#![feature(core_intrinsics)]
1617
#![feature(extend_one)]

compiler/rustc_data_structures/src/sync.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,33 @@ impl<T> RwLock<T> {
536536
pub fn borrow_mut(&self) -> WriteGuard<'_, T> {
537537
self.write()
538538
}
539+
540+
#[cfg(not(parallel_compiler))]
541+
#[inline(always)]
542+
pub fn clone_guard<'a>(rg: &ReadGuard<'a, T>) -> ReadGuard<'a, T> {
543+
ReadGuard::clone(rg)
544+
}
545+
546+
#[cfg(parallel_compiler)]
547+
#[inline(always)]
548+
pub fn clone_guard<'a>(rg: &ReadGuard<'a, T>) -> ReadGuard<'a, T> {
549+
ReadGuard::rwlock(&rg).read()
550+
}
551+
552+
#[cfg(not(parallel_compiler))]
553+
#[inline(always)]
554+
pub fn leak(&self) -> &T {
555+
ReadGuard::leak(self.read())
556+
}
557+
558+
#[cfg(parallel_compiler)]
559+
#[inline(always)]
560+
pub fn leak(&self) -> &T {
561+
let guard = self.read();
562+
let ret = unsafe { &*(&*guard as *const T) };
563+
std::mem::forget(guard);
564+
ret
565+
}
539566
}
540567

541568
// FIXME: Probably a bad idea

compiler/rustc_metadata/src/rmeta/encoder.rs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
455455
}
456456

457457
fn encode_def_path_table(&mut self) {
458-
let table = self.tcx.definitions_untracked().def_path_table();
458+
let table = self.tcx.def_path_table();
459459
if self.is_proc_macro {
460460
for def_index in std::iter::once(CRATE_DEF_INDEX)
461461
.chain(self.tcx.resolutions(()).proc_macros.iter().map(|p| p.local_def_index))
@@ -475,9 +475,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
475475
}
476476

477477
fn encode_def_path_hash_map(&mut self) -> Lazy<DefPathHashMapRef<'tcx>> {
478-
self.lazy(DefPathHashMapRef::BorrowedFromTcx(
479-
self.tcx.definitions_untracked().def_path_hash_to_def_index_map(),
480-
))
478+
self.lazy(DefPathHashMapRef::BorrowedFromTcx(self.tcx.def_path_hash_to_def_index_map()))
481479
}
482480

483481
fn encode_source_map(&mut self) -> Lazy<[rustc_span::SourceFile]> {
@@ -663,7 +661,8 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
663661
};
664662

665663
// Encode the proc macro data. This affects 'tables',
666-
// so we need to do this before we encode the tables
664+
// so we need to do this before we encode the tables.
665+
// This overwrites def_keys, so it must happen after encode_def_path_table.
667666
i = self.position();
668667
let proc_macro_data = self.encode_proc_macros();
669668
let proc_macro_data_bytes = self.position() - i;
@@ -990,8 +989,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
990989
return;
991990
}
992991
let tcx = self.tcx;
993-
let hir = tcx.hir();
994-
for local_id in hir.iter_local_def_id() {
992+
for local_id in tcx.iter_local_def_id() {
995993
let def_id = local_id.to_def_id();
996994
let def_kind = tcx.opt_def_kind(local_id);
997995
let Some(def_kind) = def_kind else { continue };
@@ -1843,12 +1841,13 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
18431841
debug!("EncodeContext::encode_traits_and_impls()");
18441842
empty_proc_macro!(self);
18451843
let tcx = self.tcx;
1846-
let mut ctx = tcx.create_stable_hashing_context();
18471844
let mut all_impls: Vec<_> = tcx.crate_inherent_impls(()).incoherent_impls.iter().collect();
1848-
all_impls.sort_by_cached_key(|&(&simp, _)| {
1849-
let mut hasher = StableHasher::new();
1850-
simp.hash_stable(&mut ctx, &mut hasher);
1851-
hasher.finish::<Fingerprint>();
1845+
tcx.with_stable_hashing_context(|mut ctx| {
1846+
all_impls.sort_by_cached_key(|&(&simp, _)| {
1847+
let mut hasher = StableHasher::new();
1848+
simp.hash_stable(&mut ctx, &mut hasher);
1849+
hasher.finish::<Fingerprint>()
1850+
})
18521851
});
18531852
let all_impls: Vec<_> = all_impls
18541853
.into_iter()

compiler/rustc_middle/src/dep_graph/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ impl<'tcx> DepContext for TyCtxt<'tcx> {
7171
type DepKind = DepKind;
7272

7373
#[inline]
74-
fn create_stable_hashing_context(&self) -> StableHashingContext<'_> {
75-
TyCtxt::create_stable_hashing_context(*self)
74+
fn with_stable_hashing_context<R>(&self, f: impl FnOnce(StableHashingContext<'_>) -> R) -> R {
75+
TyCtxt::with_stable_hashing_context(*self, f)
7676
}
7777

7878
#[inline]

compiler/rustc_middle/src/hir/map/mod.rs

Lines changed: 30 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -215,13 +215,6 @@ impl<'hir> Map<'hir> {
215215
self.tcx.local_def_id_to_hir_id(def_id)
216216
}
217217

218-
pub fn iter_local_def_id(self) -> impl Iterator<Item = LocalDefId> + 'hir {
219-
// Create a dependency to the crate to be sure we re-execute this when the amount of
220-
// definitions change.
221-
self.tcx.ensure().hir_crate(());
222-
self.tcx.definitions_untracked().iter_local_def_id()
223-
}
224-
225218
pub fn opt_def_kind(self, local_def_id: LocalDefId) -> Option<DefKind> {
226219
let hir_id = self.local_def_id_to_hir_id(local_def_id);
227220
let def_kind = match self.find(hir_id)? {
@@ -1098,35 +1091,36 @@ pub(super) fn crate_hash(tcx: TyCtxt<'_>, crate_num: CrateNum) -> Svh {
10981091

10991092
source_file_names.sort_unstable();
11001093

1101-
let mut hcx = tcx.create_stable_hashing_context();
1102-
let mut stable_hasher = StableHasher::new();
1103-
hir_body_hash.hash_stable(&mut hcx, &mut stable_hasher);
1104-
upstream_crates.hash_stable(&mut hcx, &mut stable_hasher);
1105-
source_file_names.hash_stable(&mut hcx, &mut stable_hasher);
1106-
if tcx.sess.opts.debugging_opts.incremental_relative_spans {
1107-
let definitions = &tcx.definitions_untracked();
1108-
let mut owner_spans: Vec<_> = krate
1109-
.owners
1110-
.iter_enumerated()
1111-
.filter_map(|(def_id, info)| {
1112-
let _ = info.as_owner()?;
1113-
let def_path_hash = definitions.def_path_hash(def_id);
1114-
let span = definitions.def_span(def_id);
1115-
debug_assert_eq!(span.parent(), None);
1116-
Some((def_path_hash, span))
1117-
})
1118-
.collect();
1119-
owner_spans.sort_unstable_by_key(|bn| bn.0);
1120-
owner_spans.hash_stable(&mut hcx, &mut stable_hasher);
1121-
}
1122-
tcx.sess.opts.dep_tracking_hash(true).hash_stable(&mut hcx, &mut stable_hasher);
1123-
tcx.sess.local_stable_crate_id().hash_stable(&mut hcx, &mut stable_hasher);
1124-
// Hash visibility information since it does not appear in HIR.
1125-
let resolutions = tcx.resolutions(());
1126-
resolutions.visibilities.hash_stable(&mut hcx, &mut stable_hasher);
1127-
resolutions.has_pub_restricted.hash_stable(&mut hcx, &mut stable_hasher);
1128-
1129-
let crate_hash: Fingerprint = stable_hasher.finish();
1094+
let crate_hash: Fingerprint = tcx.with_stable_hashing_context(|mut hcx| {
1095+
let mut stable_hasher = StableHasher::new();
1096+
hir_body_hash.hash_stable(&mut hcx, &mut stable_hasher);
1097+
upstream_crates.hash_stable(&mut hcx, &mut stable_hasher);
1098+
source_file_names.hash_stable(&mut hcx, &mut stable_hasher);
1099+
if tcx.sess.opts.debugging_opts.incremental_relative_spans {
1100+
let definitions = tcx.definitions_untracked();
1101+
let mut owner_spans: Vec<_> = krate
1102+
.owners
1103+
.iter_enumerated()
1104+
.filter_map(|(def_id, info)| {
1105+
let _ = info.as_owner()?;
1106+
let def_path_hash = definitions.def_path_hash(def_id);
1107+
let span = definitions.def_span(def_id);
1108+
debug_assert_eq!(span.parent(), None);
1109+
Some((def_path_hash, span))
1110+
})
1111+
.collect();
1112+
owner_spans.sort_unstable_by_key(|bn| bn.0);
1113+
owner_spans.hash_stable(&mut hcx, &mut stable_hasher);
1114+
}
1115+
tcx.sess.opts.dep_tracking_hash(true).hash_stable(&mut hcx, &mut stable_hasher);
1116+
tcx.sess.local_stable_crate_id().hash_stable(&mut hcx, &mut stable_hasher);
1117+
// Hash visibility information since it does not appear in HIR.
1118+
let resolutions = tcx.resolutions(());
1119+
resolutions.visibilities.hash_stable(&mut hcx, &mut stable_hasher);
1120+
resolutions.has_pub_restricted.hash_stable(&mut hcx, &mut stable_hasher);
1121+
stable_hasher.finish()
1122+
});
1123+
11301124
Svh::new(crate_hash.to_smaller_hash())
11311125
}
11321126

compiler/rustc_middle/src/query/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ rustc_queries! {
2020
desc { "trigger a delay span bug" }
2121
}
2222

23+
/// Create a new definition within the incr. comp. engine.
24+
query register_def(_: ty::RawLocalDefId) -> LocalDefId {
25+
eval_always
26+
desc { "register a DefId with the incr. comp. engine" }
27+
}
28+
2329
query resolutions(_: ()) -> &'tcx ty::ResolverOutputs {
2430
eval_always
2531
no_hash

0 commit comments

Comments
 (0)