Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 43bb31b

Browse files
committed
Allow to create definitions inside the query system.
1 parent 3dcb616 commit 43bb31b

File tree

23 files changed

+263
-153
lines changed

23 files changed

+263
-153
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: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -701,16 +701,20 @@ 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-
let ct = ct.eval(tcx, ty::ParamEnv::reveal_all());
707-
hcx.while_hashing_spans(false, |hcx| ct.to_valtree().hash_stable(hcx, &mut hasher));
704+
//
708705
// Let's only emit 64 bits of the hash value. That should be plenty for
709706
// avoiding collisions and will make the emitted type names shorter.
710-
// Note: Don't use `StableHashResult` impl of `u64` here directly, since that
711-
// would lead to endianness problems.
712-
let hash: u128 = hasher.finish();
713-
let hash_short = (hash.to_le() as u64).to_le();
707+
let hash_short = tcx.with_stable_hashing_context(|mut hcx| {
708+
let mut hasher = StableHasher::new();
709+
let ct = ct.eval(tcx, ty::ParamEnv::reveal_all());
710+
hcx.while_hashing_spans(false, |hcx| {
711+
ct.to_valtree().hash_stable(hcx, &mut hasher)
712+
});
713+
// Note: Don't use `StableHashResult` impl of `u64` here directly, since that
714+
// would lead to endianness problems.
715+
let hash: u128 = hasher.finish();
716+
(hash.to_le() as u64).to_le()
717+
});
714718

715719
if cpp_like_debuginfo(tcx) {
716720
write!(output, "CONST${:x}", hash_short)

compiler/rustc_data_structures/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#![feature(array_windows)]
1111
#![feature(associated_type_bounds)]
1212
#![feature(auto_traits)]
13+
#![feature(cell_leak)]
1314
#![feature(control_flow_enum)]
1415
#![feature(extend_one)]
1516
#![feature(let_else)]

compiler/rustc_data_structures/src/sync.rs

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

544571
// 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
@@ -423,7 +423,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
423423
}
424424

425425
fn encode_def_path_table(&mut self) {
426-
let table = self.tcx.definitions_untracked().def_path_table();
426+
let table = self.tcx.def_path_table();
427427
if self.is_proc_macro {
428428
for def_index in std::iter::once(CRATE_DEF_INDEX)
429429
.chain(self.tcx.resolutions(()).proc_macros.iter().map(|p| p.local_def_index))
@@ -443,9 +443,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
443443
}
444444

445445
fn encode_def_path_hash_map(&mut self) -> LazyValue<DefPathHashMapRef<'static>> {
446-
self.lazy(DefPathHashMapRef::BorrowedFromTcx(
447-
self.tcx.definitions_untracked().def_path_hash_to_def_index_map(),
448-
))
446+
self.lazy(DefPathHashMapRef::BorrowedFromTcx(self.tcx.def_path_hash_to_def_index_map()))
449447
}
450448

451449
fn encode_source_map(&mut self) -> LazyArray<rustc_span::SourceFile> {
@@ -614,7 +612,8 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
614612
let interpret_alloc_index_bytes = self.position() - i;
615613

616614
// Encode the proc macro data. This affects 'tables',
617-
// so we need to do this before we encode the tables
615+
// so we need to do this before we encode the tables.
616+
// This overwrites def_keys, so it must happen after encode_def_path_table.
618617
i = self.position();
619618
let proc_macro_data = self.encode_proc_macros();
620619
let proc_macro_data_bytes = self.position() - i;
@@ -992,8 +991,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
992991
return;
993992
}
994993
let tcx = self.tcx;
995-
let hir = tcx.hir();
996-
for local_id in hir.iter_local_def_id() {
994+
for local_id in tcx.iter_local_def_id() {
997995
let def_id = local_id.to_def_id();
998996
let def_kind = tcx.opt_def_kind(local_id);
999997
let Some(def_kind) = def_kind else { continue };
@@ -1854,12 +1852,13 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
18541852
debug!("EncodeContext::encode_traits_and_impls()");
18551853
empty_proc_macro!(self);
18561854
let tcx = self.tcx;
1857-
let mut ctx = tcx.create_stable_hashing_context();
18581855
let mut all_impls: Vec<_> = tcx.crate_inherent_impls(()).incoherent_impls.iter().collect();
1859-
all_impls.sort_by_cached_key(|&(&simp, _)| {
1860-
let mut hasher = StableHasher::new();
1861-
simp.hash_stable(&mut ctx, &mut hasher);
1862-
hasher.finish::<Fingerprint>();
1856+
tcx.with_stable_hashing_context(|mut ctx| {
1857+
all_impls.sort_by_cached_key(|&(&simp, _)| {
1858+
let mut hasher = StableHasher::new();
1859+
simp.hash_stable(&mut ctx, &mut hasher);
1860+
hasher.finish::<Fingerprint>()
1861+
})
18631862
});
18641863
let all_impls: Vec<_> = all_impls
18651864
.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: 28 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -218,13 +218,6 @@ impl<'hir> Map<'hir> {
218218
self.tcx.local_def_id_to_hir_id(def_id)
219219
}
220220

221-
pub fn iter_local_def_id(self) -> impl Iterator<Item = LocalDefId> + 'hir {
222-
// Create a dependency to the crate to be sure we re-execute this when the amount of
223-
// definitions change.
224-
self.tcx.ensure().hir_crate(());
225-
self.tcx.definitions_untracked().iter_local_def_id()
226-
}
227-
228221
/// Do not call this function directly. The query should be called.
229222
pub(super) fn opt_def_kind(self, local_def_id: LocalDefId) -> Option<DefKind> {
230223
let hir_id = self.local_def_id_to_hir_id(local_def_id);
@@ -1141,34 +1134,35 @@ pub(super) fn crate_hash(tcx: TyCtxt<'_>, crate_num: CrateNum) -> Svh {
11411134

11421135
source_file_names.sort_unstable();
11431136

1144-
let mut hcx = tcx.create_stable_hashing_context();
1145-
let mut stable_hasher = StableHasher::new();
1146-
hir_body_hash.hash_stable(&mut hcx, &mut stable_hasher);
1147-
upstream_crates.hash_stable(&mut hcx, &mut stable_hasher);
1148-
source_file_names.hash_stable(&mut hcx, &mut stable_hasher);
1149-
if tcx.sess.opts.debugging_opts.incremental_relative_spans {
1150-
let definitions = &tcx.definitions_untracked();
1151-
let mut owner_spans: Vec<_> = krate
1152-
.owners
1153-
.iter_enumerated()
1154-
.filter_map(|(def_id, info)| {
1155-
let _ = info.as_owner()?;
1156-
let def_path_hash = definitions.def_path_hash(def_id);
1157-
let span = resolutions.source_span[def_id];
1158-
debug_assert_eq!(span.parent(), None);
1159-
Some((def_path_hash, span))
1160-
})
1161-
.collect();
1162-
owner_spans.sort_unstable_by_key(|bn| bn.0);
1163-
owner_spans.hash_stable(&mut hcx, &mut stable_hasher);
1164-
}
1165-
tcx.sess.opts.dep_tracking_hash(true).hash_stable(&mut hcx, &mut stable_hasher);
1166-
tcx.sess.local_stable_crate_id().hash_stable(&mut hcx, &mut stable_hasher);
1167-
// Hash visibility information since it does not appear in HIR.
1168-
resolutions.visibilities.hash_stable(&mut hcx, &mut stable_hasher);
1169-
resolutions.has_pub_restricted.hash_stable(&mut hcx, &mut stable_hasher);
1137+
let crate_hash: Fingerprint = tcx.with_stable_hashing_context(|mut hcx| {
1138+
let mut stable_hasher = StableHasher::new();
1139+
hir_body_hash.hash_stable(&mut hcx, &mut stable_hasher);
1140+
upstream_crates.hash_stable(&mut hcx, &mut stable_hasher);
1141+
source_file_names.hash_stable(&mut hcx, &mut stable_hasher);
1142+
if tcx.sess.opts.debugging_opts.incremental_relative_spans {
1143+
let definitions = tcx.definitions_untracked();
1144+
let mut owner_spans: Vec<_> = krate
1145+
.owners
1146+
.iter_enumerated()
1147+
.filter_map(|(def_id, info)| {
1148+
let _ = info.as_owner()?;
1149+
let def_path_hash = definitions.def_path_hash(def_id);
1150+
let span = resolutions.source_span[def_id];
1151+
debug_assert_eq!(span.parent(), None);
1152+
Some((def_path_hash, span))
1153+
})
1154+
.collect();
1155+
owner_spans.sort_unstable_by_key(|bn| bn.0);
1156+
owner_spans.hash_stable(&mut hcx, &mut stable_hasher);
1157+
}
1158+
tcx.sess.opts.dep_tracking_hash(true).hash_stable(&mut hcx, &mut stable_hasher);
1159+
tcx.sess.local_stable_crate_id().hash_stable(&mut hcx, &mut stable_hasher);
1160+
// Hash visibility information since it does not appear in HIR.
1161+
resolutions.visibilities.hash_stable(&mut hcx, &mut stable_hasher);
1162+
resolutions.has_pub_restricted.hash_stable(&mut hcx, &mut stable_hasher);
1163+
stable_hasher.finish()
1164+
});
11701165

1171-
let crate_hash: Fingerprint = stable_hasher.finish();
11721166
Svh::new(crate_hash.to_smaller_hash())
11731167
}
11741168

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)