Skip to content

Commit a4b1663

Browse files
committed
avoid hash in client code
1 parent 4e5256e commit a4b1663

File tree

2 files changed

+25
-36
lines changed
  • compiler
    • rustc_query_system/src/dep_graph
    • rustc_trait_selection/src/traits/select

2 files changed

+25
-36
lines changed

Diff for: compiler/rustc_query_system/src/dep_graph/graph.rs

+21-18
Original file line numberDiff line numberDiff line change
@@ -378,24 +378,16 @@ impl<K: DepKind> DepGraph<K> {
378378
/// Executes something within an "anonymous" task, that is, a task the
379379
/// `DepNode` of which is determined by the list of inputs it read from.
380380
#[inline]
381-
pub fn with_anon_task<Tcx: DepContext<DepKind = K>, OP, R>( &self,
381+
pub fn with_anon_task<Tcx: DepContext<DepKind = K>, OP, R>(
382+
&self,
382383
cx: Tcx,
383384
dep_kind: K,
384385
op: OP,
385386
) -> (R, DepNodeIndex)
386387
where
387388
OP: FnOnce() -> R,
388389
{
389-
self.with_hash_task(cx, dep_kind, op, |task_deps| {
390-
// The dep node indices are hashed here instead of hashing the dep nodes of the
391-
// dependencies. These indices may refer to different nodes per session, but this isn't
392-
// a problem here because we that ensure the final dep node hash is per session only by
393-
// combining it with the per session random number `anon_id_seed`. This hash only need
394-
// to map the dependencies to a single value on a per session basis.
395-
let mut hasher = StableHasher::new();
396-
task_deps.reads.hash(&mut hasher);
397-
hasher.finish()
398-
})
390+
self.with_hash_task(cx, dep_kind, op, None::<()>)
399391
}
400392

401393
/// Executes something within an "anonymous" task. The `hash` is used for
@@ -405,20 +397,21 @@ impl<K: DepKind> DepGraph<K> {
405397
cx: Ctxt,
406398
dep_kind: K,
407399
op: OP,
408-
hash: H,
400+
hash: Option<H>,
409401
) -> (R, DepNodeIndex)
410402
where
411403
OP: FnOnce() -> R,
412-
H: FnOnce(&TaskDeps<K>) -> Fingerprint,
404+
H: Hash,
413405
{
414406
debug_assert!(!cx.is_eval_always(dep_kind));
415407

416408
if let Some(ref data) = self.data {
417409
let task_deps = Lock::new(TaskDeps::default());
418410
let result = K::with_deps(TaskDepsRef::Allow(&task_deps), op);
419411
let task_deps = task_deps.into_inner();
412+
let task_deps = task_deps.reads;
420413

421-
let dep_node_index = match task_deps.reads.len() {
414+
let dep_node_index = match task_deps.len() {
422415
0 => {
423416
// Because the dep-node id of anon nodes is computed from the sets of its
424417
// dependencies we already know what the ID of this dependency-less node is
@@ -429,23 +422,33 @@ impl<K: DepKind> DepGraph<K> {
429422
}
430423
1 => {
431424
// When there is only one dependency, don't bother creating a node.
432-
task_deps.reads[0]
425+
task_deps[0]
433426
}
434427
_ => {
435-
let hash_result = hash(&task_deps);
428+
let mut hasher = StableHasher::new();
429+
if let Some(hash) = hash {
430+
hash.hash(&mut hasher);
431+
} else {
432+
// The dep node indices are hashed here instead of hashing the dep nodes of the
433+
// dependencies. These indices may refer to different nodes per session, but this isn't
434+
// a problem here because we that ensure the final dep node hash is per session only by
435+
// combining it with the per session random number `anon_id_seed`. This hash only need
436+
// to map the dependencies to a single value on a per session basis.
437+
task_deps.hash(&mut hasher);
438+
}
436439

437440
let target_dep_node = DepNode {
438441
kind: dep_kind,
439442
// Fingerprint::combine() is faster than sending Fingerprint
440443
// through the StableHasher (at least as long as StableHasher
441444
// is so slow).
442-
hash: data.current.anon_id_seed.combine(hash_result).into(),
445+
hash: data.current.anon_id_seed.combine(hasher.finish()).into(),
443446
};
444447

445448
data.current.intern_new_node(
446449
cx.profiler(),
447450
target_dep_node,
448-
task_deps.reads,
451+
task_deps,
449452
Fingerprint::ZERO,
450453
)
451454
}

Diff for: compiler/rustc_trait_selection/src/traits/select/mod.rs

+4-18
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,6 @@ use rustc_middle::ty::{self, EarlyBinder, PolyProjectionPredicate, ToPolyTraitRe
4949
use rustc_middle::ty::{Ty, TyCtxt, TypeFoldable, TypeVisitable};
5050
use rustc_span::symbol::sym;
5151

52-
use rustc_data_structures::fingerprint::Fingerprint;
53-
use rustc_data_structures::stable_hasher::StableHasher;
5452
use std::cell::{Cell, RefCell};
5553
use std::cmp;
5654
use std::fmt::{self, Display};
@@ -59,8 +57,6 @@ use std::iter;
5957

6058
pub use rustc_middle::traits::select::*;
6159
use rustc_middle::ty::print::with_no_trimmed_paths;
62-
use rustc_query_system::dep_graph::TaskDeps;
63-
6460
mod candidate_assembly;
6561
mod confirmation;
6662

@@ -1021,18 +1017,8 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
10211017
return Ok(cycle_result);
10221018
}
10231019

1024-
let (result, dep_node) = if cfg!(parallel_compiler) {
1025-
self.in_task_with_hash(
1026-
|this| this.evaluate_stack(&stack),
1027-
|_| {
1028-
let mut hasher = StableHasher::new();
1029-
(param_env, fresh_trait_pred).hash(&mut hasher);
1030-
hasher.finish()
1031-
},
1032-
)
1033-
} else {
1034-
self.in_task(|this| this.evaluate_stack(&stack))
1035-
};
1020+
let (result, dep_node) = self
1021+
.in_task_with_hash(|this| this.evaluate_stack(&stack), &(param_env, fresh_trait_pred));
10361022

10371023
let result = result?;
10381024

@@ -1359,13 +1345,13 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
13591345
fn in_task_with_hash<OP, R, H>(&mut self, op: OP, hash: H) -> (R, DepNodeIndex)
13601346
where
13611347
OP: FnOnce(&mut Self) -> R,
1362-
H: FnOnce(&TaskDeps<DepKind>) -> Fingerprint,
1348+
H: Hash,
13631349
{
13641350
let (result, dep_node) = self.tcx().dep_graph.with_hash_task(
13651351
self.tcx(),
13661352
DepKind::TraitSelect,
13671353
|| op(self),
1368-
hash,
1354+
Some(hash),
13691355
);
13701356
self.tcx().dep_graph.read_index(dep_node);
13711357
(result, dep_node)

0 commit comments

Comments
 (0)