1 //! This module defines the `DepNode` type which the compiler uses to represent
2 //! nodes in the dependency graph. A `DepNode` consists of a `DepKind` (which
3 //! specifies the kind of thing it represents, like a piece of HIR, MIR, etc)
4 //! and a `Fingerprint`, a 128 bit hash value the exact meaning of which
5 //! depends on the node's `DepKind`. Together, the kind and the fingerprint
6 //! fully identify a dependency node, even across multiple compilation sessions.
7 //! In other words, the value of the fingerprint does not depend on anything
8 //! that is specific to a given compilation session, like an unpredictable
9 //! interning key (e.g., NodeId, DefId, Symbol) or the numeric value of a
10 //! pointer. The concept behind this could be compared to how git commit hashes
11 //! uniquely identify a given commit and has a few advantages:
13 //! * A `DepNode` can simply be serialized to disk and loaded in another session
14 //! without the need to do any "rebasing (like we have to do for Spans and
15 //! NodeIds) or "retracing" like we had to do for `DefId` in earlier
16 //! implementations of the dependency graph.
17 //! * A `Fingerprint` is just a bunch of bits, which allows `DepNode` to
18 //! implement `Copy`, `Sync`, `Send`, `Freeze`, etc.
19 //! * Since we just have a bit pattern, `DepNode` can be mapped from disk into
20 //! memory without any post-processing (e.g., "abomination-style" pointer
22 //! * Because a `DepNode` is self-contained, we can instantiate `DepNodes` that
23 //! refer to things that do not exist anymore. In previous implementations
24 //! `DepNode` contained a `DefId`. A `DepNode` referring to something that
25 //! had been removed between the previous and the current compilation session
26 //! could not be instantiated because the current compilation session
27 //! contained no `DefId` for thing that had been removed.
29 //! `DepNode` definition happens in the `define_dep_nodes!()` macro. This macro
30 //! defines the `DepKind` enum and a corresponding `DepConstructor` enum. The
31 //! `DepConstructor` enum links a `DepKind` to the parameters that are needed at
32 //! runtime in order to construct a valid `DepNode` fingerprint.
34 //! Because the macro sees what parameters a given `DepKind` requires, it can
35 //! "infer" some properties for each kind of `DepNode`:
37 //! * Whether a `DepNode` of a given kind has any parameters at all. Some
38 //! `DepNode`s, like `Krate`, represent global concepts with only one value.
39 //! * Whether it is possible, in principle, to reconstruct a query key from a
40 //! given `DepNode`. Many `DepKind`s only require a single `DefId` parameter,
41 //! in which case it is possible to map the node's fingerprint back to the
42 //! `DefId` it was computed from. In other cases, too much information gets
43 //! lost during fingerprint computation.
45 //! The `DepConstructor` enum, together with `DepNode::new()` ensures that only
46 //! valid `DepNode` instances can be constructed. For example, the API does not
47 //! allow for constructing parameterless `DepNode`s with anything other
48 //! than a zeroed out fingerprint. More generally speaking, it relieves the
49 //! user of the `DepNode` API of having to know how to compute the expected
50 //! fingerprint for a given set of node parameters.
52 use crate::hir::map::DefPathHash;
53 use crate::ich::{Fingerprint, StableHashingContext};
55 use crate::mir::interpret::{GlobalId, LitToConstInput};
57 use crate::traits::query::{
58 CanonicalPredicateGoal, CanonicalProjectionGoal, CanonicalTyGoal,
59 CanonicalTypeOpAscribeUserTypeGoal, CanonicalTypeOpEqGoal, CanonicalTypeOpNormalizeGoal,
60 CanonicalTypeOpProvePredicateGoal, CanonicalTypeOpSubtypeGoal,
62 use crate::ty::subst::SubstsRef;
63 use crate::ty::{self, ParamEnvAnd, Ty, TyCtxt};
65 use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
66 use rustc_hir::def_id::{CrateNum, DefId, DefIndex, CRATE_DEF_INDEX};
68 use rustc_span::symbol::Symbol;
72 // erase!() just makes tokens go away. It's used to specify which macro argument
73 // is repeated (i.e., which sub-expression of the macro we are in) but don't need
74 // to actually use any of the arguments.
79 macro_rules! replace {
80 ($x:tt with $($y:tt)*) => ($($y)*)
83 macro_rules! is_anon_attr {
92 macro_rules! is_eval_always_attr {
101 macro_rules! contains_anon_attr {
102 ($($attr:ident),*) => ({$(is_anon_attr!($attr) | )* false});
105 macro_rules! contains_eval_always_attr {
106 ($($attr:ident),*) => ({$(is_eval_always_attr!($attr) | )* false});
109 macro_rules! define_dep_nodes {
113 $variant:ident $(( $tuple_arg_ty:ty $(,)? ))*
114 $({ $($struct_arg_name:ident : $struct_arg_ty:ty),* })*
117 #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash,
118 RustcEncodable, RustcDecodable)]
124 #[allow(unreachable_code)]
125 pub fn can_reconstruct_query_key<$tcx>(&self) -> bool {
128 DepKind :: $variant => {
129 if contains_anon_attr!($($attr),*) {
135 return <$tuple_arg_ty as DepNodeParams>
136 ::CAN_RECONSTRUCT_QUERY_KEY;
142 return <( $($struct_arg_ty,)* ) as DepNodeParams>
143 ::CAN_RECONSTRUCT_QUERY_KEY;
152 pub fn is_anon(&self) -> bool {
155 DepKind :: $variant => { contains_anon_attr!($($attr),*) }
160 pub fn is_eval_always(&self) -> bool {
163 DepKind :: $variant => { contains_eval_always_attr!($($attr), *) }
168 #[allow(unreachable_code)]
169 pub fn has_params(&self) -> bool {
172 DepKind :: $variant => {
175 erase!($tuple_arg_ty);
181 $(erase!($struct_arg_name);)*
192 pub enum DepConstructor<$tcx> {
194 $variant $(( $tuple_arg_ty ))*
195 $({ $($struct_arg_name : $struct_arg_ty),* })*
199 #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash,
200 RustcEncodable, RustcDecodable)]
203 pub hash: Fingerprint,
207 #[allow(unreachable_code, non_snake_case)]
208 pub fn new<'tcx>(tcx: TyCtxt<'tcx>,
209 dep: DepConstructor<'tcx>)
214 DepConstructor :: $variant $(( replace!(($tuple_arg_ty) with arg) ))*
215 $({ $($struct_arg_name),* })*
220 erase!($tuple_arg_ty);
221 let hash = DepNodeParams::to_fingerprint(&arg, tcx);
222 let dep_node = DepNode {
223 kind: DepKind::$variant,
227 #[cfg(debug_assertions)]
229 if !dep_node.kind.can_reconstruct_query_key() &&
230 (tcx.sess.opts.debugging_opts.incremental_info ||
231 tcx.sess.opts.debugging_opts.query_dep_graph)
233 tcx.dep_graph.register_dep_node_debug_str(dep_node, || {
234 arg.to_debug_str(tcx)
244 let tupled_args = ( $($struct_arg_name,)* );
245 let hash = DepNodeParams::to_fingerprint(&tupled_args,
247 let dep_node = DepNode {
248 kind: DepKind::$variant,
252 #[cfg(debug_assertions)]
254 if !dep_node.kind.can_reconstruct_query_key() &&
255 (tcx.sess.opts.debugging_opts.incremental_info ||
256 tcx.sess.opts.debugging_opts.query_dep_graph)
258 tcx.dep_graph.register_dep_node_debug_str(dep_node, || {
259 tupled_args.to_debug_str(tcx)
268 kind: DepKind::$variant,
269 hash: Fingerprint::ZERO,
276 /// Construct a DepNode from the given DepKind and DefPathHash. This
277 /// method will assert that the given DepKind actually requires a
278 /// single DefId/DefPathHash parameter.
279 pub fn from_def_path_hash(kind: DepKind,
280 def_path_hash: DefPathHash)
282 debug_assert!(kind.can_reconstruct_query_key() && kind.has_params());
285 hash: def_path_hash.0,
289 /// Creates a new, parameterless DepNode. This method will assert
290 /// that the DepNode corresponding to the given DepKind actually
291 /// does not require any parameters.
292 pub fn new_no_params(kind: DepKind) -> DepNode {
293 debug_assert!(!kind.has_params());
296 hash: Fingerprint::ZERO,
300 /// Extracts the DefId corresponding to this DepNode. This will work
301 /// if two conditions are met:
303 /// 1. The Fingerprint of the DepNode actually is a DefPathHash, and
304 /// 2. the item that the DefPath refers to exists in the current tcx.
306 /// Condition (1) is determined by the DepKind variant of the
307 /// DepNode. Condition (2) might not be fulfilled if a DepNode
308 /// refers to something from the previous compilation session that
309 /// has been removed.
310 pub fn extract_def_id(&self, tcx: TyCtxt<'_>) -> Option<DefId> {
311 if self.kind.can_reconstruct_query_key() {
312 let def_path_hash = DefPathHash(self.hash);
313 tcx.def_path_hash_to_def_id.as_ref()?
314 .get(&def_path_hash).cloned()
321 pub fn from_label_string(label: &str,
322 def_path_hash: DefPathHash)
323 -> Result<DepNode, ()> {
324 let kind = match label {
326 stringify!($variant) => DepKind::$variant,
331 if !kind.can_reconstruct_query_key() {
335 if kind.has_params() {
336 Ok(def_path_hash.to_dep_node(kind))
338 Ok(DepNode::new_no_params(kind))
343 pub fn has_label_string(label: &str) -> bool {
346 stringify!($variant) => true,
353 /// Contains variant => str representations for constructing
354 /// DepNode groups for tests.
355 #[allow(dead_code, non_upper_case_globals)]
358 pub const $variant: &str = stringify!($variant);
364 impl fmt::Debug for DepNode {
365 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
366 write!(f, "{:?}", self.kind)?;
368 if !self.kind.has_params() && !self.kind.is_anon() {
374 crate::ty::tls::with_opt(|opt_tcx| {
375 if let Some(tcx) = opt_tcx {
376 if let Some(def_id) = self.extract_def_id(tcx) {
377 write!(f, "{}", tcx.def_path_debug_str(def_id))?;
378 } else if let Some(ref s) = tcx.dep_graph.dep_node_debug_str(*self) {
381 write!(f, "{}", self.hash)?;
384 write!(f, "{}", self.hash)?;
394 pub fn to_dep_node(self, kind: DepKind) -> DepNode {
395 DepNode::from_def_path_hash(kind, self)
399 rustc_dep_node_append!([define_dep_nodes!][ <'tcx>
400 // We use this for most things when incr. comp. is turned off.
403 // Represents the `Krate` as a whole (the `hir::Krate` value) (as
404 // distinct from the krate module). This is basically a hash of
405 // the entire krate, so if you read from `Krate` (e.g., by calling
406 // `tcx.hir().krate()`), we will have to assume that any change
407 // means that you need to be recompiled. This is because the
408 // `Krate` value gives you access to all other items. To avoid
409 // this fate, do not call `tcx.hir().krate()`; instead, prefer
410 // wrappers like `tcx.visit_all_items_in_krate()`. If there is no
411 // suitable wrapper, you can use `tcx.dep_graph.ignore()` to gain
412 // access to the krate, but you must remember to add suitable
413 // edges yourself for the individual items that you read.
416 // Represents the body of a function or method. The def-id is that of the
418 [eval_always] HirBody(DefId),
420 // Represents the HIR node with the given node-id
421 [eval_always] Hir(DefId),
423 // Represents metadata from an extern crate.
424 [eval_always] CrateMetadata(CrateNum),
426 [eval_always] AllLocalTraitImpls,
430 [] CompileCodegenUnit(Symbol),
432 [eval_always] Analysis(CrateNum),
435 pub trait RecoverKey<'tcx>: Sized {
436 fn recover(tcx: TyCtxt<'tcx>, dep_node: &DepNode) -> Option<Self>;
439 impl RecoverKey<'tcx> for CrateNum {
440 fn recover(tcx: TyCtxt<'tcx>, dep_node: &DepNode) -> Option<Self> {
441 dep_node.extract_def_id(tcx).map(|id| id.krate)
445 impl RecoverKey<'tcx> for DefId {
446 fn recover(tcx: TyCtxt<'tcx>, dep_node: &DepNode) -> Option<Self> {
447 dep_node.extract_def_id(tcx)
451 impl RecoverKey<'tcx> for DefIndex {
452 fn recover(tcx: TyCtxt<'tcx>, dep_node: &DepNode) -> Option<Self> {
453 dep_node.extract_def_id(tcx).map(|id| id.index)
457 trait DepNodeParams<'tcx>: fmt::Debug {
458 const CAN_RECONSTRUCT_QUERY_KEY: bool;
460 /// This method turns the parameters of a DepNodeConstructor into an opaque
461 /// Fingerprint to be used in DepNode.
462 /// Not all DepNodeParams support being turned into a Fingerprint (they
463 /// don't need to if the corresponding DepNode is anonymous).
464 fn to_fingerprint(&self, _: TyCtxt<'tcx>) -> Fingerprint {
465 panic!("Not implemented. Accidentally called on anonymous node?")
468 fn to_debug_str(&self, _: TyCtxt<'tcx>) -> String {
469 format!("{:?}", self)
473 impl<'tcx, T> DepNodeParams<'tcx> for T
475 T: HashStable<StableHashingContext<'tcx>> + fmt::Debug,
477 default const CAN_RECONSTRUCT_QUERY_KEY: bool = false;
479 default fn to_fingerprint(&self, tcx: TyCtxt<'tcx>) -> Fingerprint {
480 let mut hcx = tcx.create_stable_hashing_context();
481 let mut hasher = StableHasher::new();
483 self.hash_stable(&mut hcx, &mut hasher);
488 default fn to_debug_str(&self, _: TyCtxt<'tcx>) -> String {
489 format!("{:?}", *self)
493 impl<'tcx> DepNodeParams<'tcx> for DefId {
494 const CAN_RECONSTRUCT_QUERY_KEY: bool = true;
496 fn to_fingerprint(&self, tcx: TyCtxt<'_>) -> Fingerprint {
497 tcx.def_path_hash(*self).0
500 fn to_debug_str(&self, tcx: TyCtxt<'tcx>) -> String {
501 tcx.def_path_str(*self)
505 impl<'tcx> DepNodeParams<'tcx> for DefIndex {
506 const CAN_RECONSTRUCT_QUERY_KEY: bool = true;
508 fn to_fingerprint(&self, tcx: TyCtxt<'_>) -> Fingerprint {
509 tcx.hir().definitions().def_path_hash(*self).0
512 fn to_debug_str(&self, tcx: TyCtxt<'tcx>) -> String {
513 tcx.def_path_str(DefId::local(*self))
517 impl<'tcx> DepNodeParams<'tcx> for CrateNum {
518 const CAN_RECONSTRUCT_QUERY_KEY: bool = true;
520 fn to_fingerprint(&self, tcx: TyCtxt<'_>) -> Fingerprint {
521 let def_id = DefId { krate: *self, index: CRATE_DEF_INDEX };
522 tcx.def_path_hash(def_id).0
525 fn to_debug_str(&self, tcx: TyCtxt<'tcx>) -> String {
526 tcx.crate_name(*self).to_string()
530 impl<'tcx> DepNodeParams<'tcx> for (DefId, DefId) {
531 const CAN_RECONSTRUCT_QUERY_KEY: bool = false;
533 // We actually would not need to specialize the implementation of this
534 // method but it's faster to combine the hashes than to instantiate a full
535 // hashing context and stable-hashing state.
536 fn to_fingerprint(&self, tcx: TyCtxt<'_>) -> Fingerprint {
537 let (def_id_0, def_id_1) = *self;
539 let def_path_hash_0 = tcx.def_path_hash(def_id_0);
540 let def_path_hash_1 = tcx.def_path_hash(def_id_1);
542 def_path_hash_0.0.combine(def_path_hash_1.0)
545 fn to_debug_str(&self, tcx: TyCtxt<'tcx>) -> String {
546 let (def_id_0, def_id_1) = *self;
548 format!("({}, {})", tcx.def_path_debug_str(def_id_0), tcx.def_path_debug_str(def_id_1))
552 impl<'tcx> DepNodeParams<'tcx> for HirId {
553 const CAN_RECONSTRUCT_QUERY_KEY: bool = false;
555 // We actually would not need to specialize the implementation of this
556 // method but it's faster to combine the hashes than to instantiate a full
557 // hashing context and stable-hashing state.
558 fn to_fingerprint(&self, tcx: TyCtxt<'_>) -> Fingerprint {
559 let HirId { owner, local_id } = *self;
561 let def_path_hash = tcx.def_path_hash(DefId::local(owner));
562 let local_id = Fingerprint::from_smaller_hash(local_id.as_u32().into());
564 def_path_hash.0.combine(local_id)
568 /// A "work product" corresponds to a `.o` (or other) file that we
569 /// save in between runs. These IDs do not have a `DefId` but rather
570 /// some independent path or string that persists between runs without
571 /// the need to be mapped or unmapped. (This ensures we can serialize
572 /// them even in the absence of a tcx.)
586 pub struct WorkProductId {
591 pub fn from_cgu_name(cgu_name: &str) -> WorkProductId {
592 let mut hasher = StableHasher::new();
593 cgu_name.len().hash(&mut hasher);
594 cgu_name.hash(&mut hasher);
595 WorkProductId { hash: hasher.finish() }
598 pub fn from_fingerprint(fingerprint: Fingerprint) -> WorkProductId {
599 WorkProductId { hash: fingerprint }