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 could 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! is_anon_attr {
88 macro_rules! is_eval_always_attr {
97 macro_rules! contains_anon_attr {
98 ($($attr:ident $(($($attr_args:tt)*))* ),*) => ({$(is_anon_attr!($attr) | )* false});
101 macro_rules! contains_eval_always_attr {
102 ($($attr:ident $(($($attr_args:tt)*))* ),*) => ({$(is_eval_always_attr!($attr) | )* false});
105 macro_rules! define_dep_nodes {
109 $variant:ident $(( $tuple_arg_ty:ty $(,)? ))*
112 #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash,
113 RustcEncodable, RustcDecodable)]
114 #[allow(non_camel_case_types)]
120 #[allow(unreachable_code)]
121 pub fn can_reconstruct_query_key<$tcx>(&self) -> bool {
124 DepKind :: $variant => {
125 if contains_anon_attr!($($attrs)*) {
131 return <$tuple_arg_ty as DepNodeParams>
132 ::CAN_RECONSTRUCT_QUERY_KEY;
141 pub fn is_anon(&self) -> bool {
144 DepKind :: $variant => { contains_anon_attr!($($attrs)*) }
149 pub fn is_eval_always(&self) -> bool {
152 DepKind :: $variant => { contains_eval_always_attr!($($attrs)*) }
157 #[allow(unreachable_code)]
158 pub fn has_params(&self) -> bool {
161 DepKind :: $variant => {
164 erase!($tuple_arg_ty);
175 pub struct DepConstructor;
177 #[allow(non_camel_case_types)]
178 impl DepConstructor {
181 #[allow(unreachable_code, non_snake_case)]
182 pub fn $variant(_tcx: TyCtxt<'_>, $(arg: $tuple_arg_ty)*) -> DepNode {
185 erase!($tuple_arg_ty);
186 let hash = DepNodeParams::to_fingerprint(&arg, _tcx);
187 let dep_node = DepNode {
188 kind: DepKind::$variant,
192 #[cfg(debug_assertions)]
194 if !dep_node.kind.can_reconstruct_query_key() &&
195 (_tcx.sess.opts.debugging_opts.incremental_info ||
196 _tcx.sess.opts.debugging_opts.query_dep_graph)
198 _tcx.dep_graph.register_dep_node_debug_str(dep_node, || {
199 arg.to_debug_str(_tcx)
208 kind: DepKind::$variant,
209 hash: Fingerprint::ZERO,
215 #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash,
216 RustcEncodable, RustcDecodable)]
219 pub hash: Fingerprint,
223 /// Construct a DepNode from the given DepKind and DefPathHash. This
224 /// method will assert that the given DepKind actually requires a
225 /// single DefId/DefPathHash parameter.
226 pub fn from_def_path_hash(def_path_hash: DefPathHash,
229 debug_assert!(kind.can_reconstruct_query_key() && kind.has_params());
232 hash: def_path_hash.0,
236 /// Creates a new, parameterless DepNode. This method will assert
237 /// that the DepNode corresponding to the given DepKind actually
238 /// does not require any parameters.
239 pub fn new_no_params(kind: DepKind) -> DepNode {
240 debug_assert!(!kind.has_params());
243 hash: Fingerprint::ZERO,
247 /// Extracts the DefId corresponding to this DepNode. This will work
248 /// if two conditions are met:
250 /// 1. The Fingerprint of the DepNode actually is a DefPathHash, and
251 /// 2. the item that the DefPath refers to exists in the current tcx.
253 /// Condition (1) is determined by the DepKind variant of the
254 /// DepNode. Condition (2) might not be fulfilled if a DepNode
255 /// refers to something from the previous compilation session that
256 /// has been removed.
257 pub fn extract_def_id(&self, tcx: TyCtxt<'_>) -> Option<DefId> {
258 if self.kind.can_reconstruct_query_key() {
259 let def_path_hash = DefPathHash(self.hash);
260 tcx.def_path_hash_to_def_id.as_ref()?
261 .get(&def_path_hash).cloned()
268 pub fn from_label_string(label: &str,
269 def_path_hash: DefPathHash)
270 -> Result<DepNode, ()> {
271 let kind = match label {
273 stringify!($variant) => DepKind::$variant,
278 if !kind.can_reconstruct_query_key() {
282 if kind.has_params() {
283 Ok(DepNode::from_def_path_hash(def_path_hash, kind))
285 Ok(DepNode::new_no_params(kind))
290 pub fn has_label_string(label: &str) -> bool {
293 stringify!($variant) => true,
300 /// Contains variant => str representations for constructing
301 /// DepNode groups for tests.
302 #[allow(dead_code, non_upper_case_globals)]
305 pub const $variant: &str = stringify!($variant);
311 impl fmt::Debug for DepNode {
312 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
313 write!(f, "{:?}", self.kind)?;
315 if !self.kind.has_params() && !self.kind.is_anon() {
321 crate::ty::tls::with_opt(|opt_tcx| {
322 if let Some(tcx) = opt_tcx {
323 if let Some(def_id) = self.extract_def_id(tcx) {
324 write!(f, "{}", tcx.def_path_debug_str(def_id))?;
325 } else if let Some(ref s) = tcx.dep_graph.dep_node_debug_str(*self) {
328 write!(f, "{}", self.hash)?;
331 write!(f, "{}", self.hash)?;
340 rustc_dep_node_append!([define_dep_nodes!][ <'tcx>
341 // We use this for most things when incr. comp. is turned off.
344 // Represents metadata from an extern crate.
345 [eval_always] CrateMetadata(CrateNum),
349 [] CompileCodegenUnit(Symbol),
352 pub(crate) trait DepNodeParams<'tcx>: fmt::Debug + Sized {
353 const CAN_RECONSTRUCT_QUERY_KEY: bool;
355 /// This method turns the parameters of a DepNodeConstructor into an opaque
356 /// Fingerprint to be used in DepNode.
357 /// Not all DepNodeParams support being turned into a Fingerprint (they
358 /// don't need to if the corresponding DepNode is anonymous).
359 fn to_fingerprint(&self, _: TyCtxt<'tcx>) -> Fingerprint {
360 panic!("Not implemented. Accidentally called on anonymous node?")
363 fn to_debug_str(&self, _: TyCtxt<'tcx>) -> String {
364 format!("{:?}", self)
367 /// This method tries to recover the query key from the given `DepNode`,
368 /// something which is needed when forcing `DepNode`s during red-green
369 /// evaluation. The query system will only call this method if
370 /// `CAN_RECONSTRUCT_QUERY_KEY` is `true`.
371 /// It is always valid to return `None` here, in which case incremental
372 /// compilation will treat the query as having changed instead of forcing it.
373 fn recover(tcx: TyCtxt<'tcx>, dep_node: &DepNode) -> Option<Self>;
376 impl<'tcx, T> DepNodeParams<'tcx> for T
378 T: HashStable<StableHashingContext<'tcx>> + fmt::Debug,
380 default const CAN_RECONSTRUCT_QUERY_KEY: bool = false;
382 default fn to_fingerprint(&self, tcx: TyCtxt<'tcx>) -> Fingerprint {
383 let mut hcx = tcx.create_stable_hashing_context();
384 let mut hasher = StableHasher::new();
386 self.hash_stable(&mut hcx, &mut hasher);
391 default fn to_debug_str(&self, _: TyCtxt<'tcx>) -> String {
392 format!("{:?}", *self)
395 default fn recover(_: TyCtxt<'tcx>, _: &DepNode) -> Option<Self> {
400 impl<'tcx> DepNodeParams<'tcx> for DefId {
401 const CAN_RECONSTRUCT_QUERY_KEY: bool = true;
403 fn to_fingerprint(&self, tcx: TyCtxt<'_>) -> Fingerprint {
404 tcx.def_path_hash(*self).0
407 fn to_debug_str(&self, tcx: TyCtxt<'tcx>) -> String {
408 tcx.def_path_str(*self)
411 fn recover(tcx: TyCtxt<'tcx>, dep_node: &DepNode) -> Option<Self> {
412 dep_node.extract_def_id(tcx)
416 impl<'tcx> DepNodeParams<'tcx> for DefIndex {
417 const CAN_RECONSTRUCT_QUERY_KEY: bool = true;
419 fn to_fingerprint(&self, tcx: TyCtxt<'_>) -> Fingerprint {
420 tcx.hir().definitions().def_path_hash(*self).0
423 fn to_debug_str(&self, tcx: TyCtxt<'tcx>) -> String {
424 tcx.def_path_str(DefId::local(*self))
427 fn recover(tcx: TyCtxt<'tcx>, dep_node: &DepNode) -> Option<Self> {
428 dep_node.extract_def_id(tcx).map(|id| id.index)
432 impl<'tcx> DepNodeParams<'tcx> for CrateNum {
433 const CAN_RECONSTRUCT_QUERY_KEY: bool = true;
435 fn to_fingerprint(&self, tcx: TyCtxt<'_>) -> Fingerprint {
436 let def_id = DefId { krate: *self, index: CRATE_DEF_INDEX };
437 tcx.def_path_hash(def_id).0
440 fn to_debug_str(&self, tcx: TyCtxt<'tcx>) -> String {
441 tcx.crate_name(*self).to_string()
444 fn recover(tcx: TyCtxt<'tcx>, dep_node: &DepNode) -> Option<Self> {
445 dep_node.extract_def_id(tcx).map(|id| id.krate)
449 impl<'tcx> DepNodeParams<'tcx> for (DefId, DefId) {
450 const CAN_RECONSTRUCT_QUERY_KEY: bool = false;
452 // We actually would not need to specialize the implementation of this
453 // method but it's faster to combine the hashes than to instantiate a full
454 // hashing context and stable-hashing state.
455 fn to_fingerprint(&self, tcx: TyCtxt<'_>) -> Fingerprint {
456 let (def_id_0, def_id_1) = *self;
458 let def_path_hash_0 = tcx.def_path_hash(def_id_0);
459 let def_path_hash_1 = tcx.def_path_hash(def_id_1);
461 def_path_hash_0.0.combine(def_path_hash_1.0)
464 fn to_debug_str(&self, tcx: TyCtxt<'tcx>) -> String {
465 let (def_id_0, def_id_1) = *self;
467 format!("({}, {})", tcx.def_path_debug_str(def_id_0), tcx.def_path_debug_str(def_id_1))
471 impl<'tcx> DepNodeParams<'tcx> for HirId {
472 const CAN_RECONSTRUCT_QUERY_KEY: bool = false;
474 // We actually would not need to specialize the implementation of this
475 // method but it's faster to combine the hashes than to instantiate a full
476 // hashing context and stable-hashing state.
477 fn to_fingerprint(&self, tcx: TyCtxt<'_>) -> Fingerprint {
478 let HirId { owner, local_id } = *self;
480 let def_path_hash = tcx.def_path_hash(DefId::local(owner));
481 let local_id = Fingerprint::from_smaller_hash(local_id.as_u32().into());
483 def_path_hash.0.combine(local_id)
487 /// A "work product" corresponds to a `.o` (or other) file that we
488 /// save in between runs. These IDs do not have a `DefId` but rather
489 /// some independent path or string that persists between runs without
490 /// the need to be mapped or unmapped. (This ensures we can serialize
491 /// them even in the absence of a tcx.)
505 pub struct WorkProductId {
510 pub fn from_cgu_name(cgu_name: &str) -> WorkProductId {
511 let mut hasher = StableHasher::new();
512 cgu_name.len().hash(&mut hasher);
513 cgu_name.hash(&mut hasher);
514 WorkProductId { hash: hasher.finish() }
517 pub fn from_fingerprint(fingerprint: Fingerprint) -> WorkProductId {
518 WorkProductId { hash: fingerprint }