1 //! This module defines the `DepNode` type which the compiler uses to represent
2 //! nodes in the dependency graph.
4 //! A `DepNode` consists of a `DepKind` (which
5 //! specifies the kind of thing it represents, like a piece of HIR, MIR, etc)
6 //! and a `Fingerprint`, a 128-bit hash value the exact meaning of which
7 //! depends on the node's `DepKind`. Together, the kind and the fingerprint
8 //! fully identify a dependency node, even across multiple compilation sessions.
9 //! In other words, the value of the fingerprint does not depend on anything
10 //! that is specific to a given compilation session, like an unpredictable
11 //! interning key (e.g., NodeId, DefId, Symbol) or the numeric value of a
12 //! pointer. The concept behind this could be compared to how git commit hashes
13 //! uniquely identify a given commit and has a few advantages:
15 //! * A `DepNode` can simply be serialized to disk and loaded in another session
16 //! without the need to do any "rebasing" (like we have to do for Spans and
17 //! NodeIds) or "retracing" (like we had to do for `DefId` in earlier
18 //! implementations of the dependency graph).
19 //! * A `Fingerprint` is just a bunch of bits, which allows `DepNode` to
20 //! implement `Copy`, `Sync`, `Send`, `Freeze`, etc.
21 //! * Since we just have a bit pattern, `DepNode` can be mapped from disk into
22 //! memory without any post-processing (e.g., "abomination-style" pointer
24 //! * Because a `DepNode` is self-contained, we can instantiate `DepNodes` that
25 //! refer to things that do not exist anymore. In previous implementations
26 //! `DepNode` contained a `DefId`. A `DepNode` referring to something that
27 //! had been removed between the previous and the current compilation session
28 //! could not be instantiated because the current compilation session
29 //! contained no `DefId` for thing that had been removed.
31 //! `DepNode` definition happens in the `define_dep_nodes!()` macro. This macro
32 //! defines the `DepKind` enum and a corresponding `DepConstructor` enum. The
33 //! `DepConstructor` enum links a `DepKind` to the parameters that are needed at
34 //! runtime in order to construct a valid `DepNode` fingerprint.
36 //! Because the macro sees what parameters a given `DepKind` requires, it can
37 //! "infer" some properties for each kind of `DepNode`:
39 //! * Whether a `DepNode` of a given kind has any parameters at all. Some
40 //! `DepNode`s could represent global concepts with only one value.
41 //! * Whether it is possible, in principle, to reconstruct a query key from a
42 //! given `DepNode`. Many `DepKind`s only require a single `DefId` parameter,
43 //! in which case it is possible to map the node's fingerprint back to the
44 //! `DefId` it was computed from. In other cases, too much information gets
45 //! lost during fingerprint computation.
47 //! The `DepConstructor` enum, together with `DepNode::new()`, ensures that only
48 //! valid `DepNode` instances can be constructed. For example, the API does not
49 //! allow for constructing parameterless `DepNode`s with anything other
50 //! than a zeroed out fingerprint. More generally speaking, it relieves the
51 //! user of the `DepNode` API of having to know how to compute the expected
52 //! fingerprint for a given set of node parameters.
54 use crate::mir::interpret::{GlobalId, LitToConstInput};
56 use crate::traits::query::{
57 CanonicalPredicateGoal, CanonicalProjectionGoal, CanonicalTyGoal,
58 CanonicalTypeOpAscribeUserTypeGoal, CanonicalTypeOpEqGoal, CanonicalTypeOpNormalizeGoal,
59 CanonicalTypeOpProvePredicateGoal, CanonicalTypeOpSubtypeGoal,
61 use crate::ty::subst::{GenericArg, SubstsRef};
62 use crate::ty::{self, ParamEnvAnd, Ty, TyCtxt};
64 use rustc_data_structures::fingerprint::Fingerprint;
65 use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, CRATE_DEF_INDEX};
66 use rustc_hir::definitions::DefPathHash;
68 use rustc_span::symbol::Symbol;
71 pub use rustc_query_system::dep_graph::{DepContext, DepNodeParams};
73 // erase!() just makes tokens go away. It's used to specify which macro argument
74 // is repeated (i.e., which sub-expression of the macro we are in) but don't need
75 // to actually use any of the arguments.
80 macro_rules! is_anon_attr {
89 macro_rules! is_eval_always_attr {
98 macro_rules! contains_anon_attr {
99 ($($attr:ident $(($($attr_args:tt)*))* ),*) => ({$(is_anon_attr!($attr) | )* false});
102 macro_rules! contains_eval_always_attr {
103 ($($attr:ident $(($($attr_args:tt)*))* ),*) => ({$(is_eval_always_attr!($attr) | )* false});
106 macro_rules! define_dep_nodes {
110 $variant:ident $(( $tuple_arg_ty:ty $(,)? ))*
113 #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Encodable, Decodable)]
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<TyCtxt<'_>>>
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 return DepNode::construct(_tcx, DepKind::$variant, &arg)
189 return DepNode::construct(_tcx, DepKind::$variant, &())
194 pub type DepNode = rustc_query_system::dep_graph::DepNode<DepKind>;
196 pub trait DepNodeExt: Sized {
197 /// Construct a DepNode from the given DepKind and DefPathHash. This
198 /// method will assert that the given DepKind actually requires a
199 /// single DefId/DefPathHash parameter.
200 fn from_def_path_hash(def_path_hash: DefPathHash, kind: DepKind) -> Self;
202 /// Extracts the DefId corresponding to this DepNode. This will work
203 /// if two conditions are met:
205 /// 1. The Fingerprint of the DepNode actually is a DefPathHash, and
206 /// 2. the item that the DefPath refers to exists in the current tcx.
208 /// Condition (1) is determined by the DepKind variant of the
209 /// DepNode. Condition (2) might not be fulfilled if a DepNode
210 /// refers to something from the previous compilation session that
211 /// has been removed.
212 fn extract_def_id(&self, tcx: TyCtxt<'_>) -> Option<DefId>;
215 fn from_label_string(label: &str, def_path_hash: DefPathHash)
219 fn has_label_string(label: &str) -> bool;
222 impl DepNodeExt for DepNode {
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 fn from_def_path_hash(def_path_hash: DefPathHash, kind: DepKind) -> DepNode {
227 debug_assert!(kind.can_reconstruct_query_key() && kind.has_params());
230 hash: def_path_hash.0,
234 /// Extracts the DefId corresponding to this DepNode. This will work
235 /// if two conditions are met:
237 /// 1. The Fingerprint of the DepNode actually is a DefPathHash, and
238 /// 2. the item that the DefPath refers to exists in the current tcx.
240 /// Condition (1) is determined by the DepKind variant of the
241 /// DepNode. Condition (2) might not be fulfilled if a DepNode
242 /// refers to something from the previous compilation session that
243 /// has been removed.
244 fn extract_def_id(&self, tcx: TyCtxt<'tcx>) -> Option<DefId> {
245 if self.kind.can_reconstruct_query_key() {
246 let def_path_hash = DefPathHash(self.hash);
247 tcx.def_path_hash_to_def_id.as_ref()?.get(&def_path_hash).cloned()
254 fn from_label_string(label: &str, def_path_hash: DefPathHash) -> Result<DepNode, ()> {
255 let kind = match label {
257 stringify!($variant) => DepKind::$variant,
262 if !kind.can_reconstruct_query_key() {
266 if kind.has_params() {
267 Ok(DepNode::from_def_path_hash(def_path_hash, kind))
269 Ok(DepNode::new_no_params(kind))
274 fn has_label_string(label: &str) -> bool {
277 stringify!($variant) => true,
284 /// Contains variant => str representations for constructing
285 /// DepNode groups for tests.
286 #[allow(dead_code, non_upper_case_globals)]
289 pub const $variant: &str = stringify!($variant);
295 rustc_dep_node_append!([define_dep_nodes!][ <'tcx>
296 // We use this for most things when incr. comp. is turned off.
299 // Represents metadata from an extern crate.
300 [eval_always] CrateMetadata(CrateNum),
304 [] CompileCodegenUnit(Symbol),
307 impl<'tcx> DepNodeParams<TyCtxt<'tcx>> for DefId {
309 fn can_reconstruct_query_key() -> bool {
313 fn to_fingerprint(&self, tcx: TyCtxt<'tcx>) -> Fingerprint {
314 tcx.def_path_hash(*self).0
317 fn to_debug_str(&self, tcx: TyCtxt<'tcx>) -> String {
318 tcx.def_path_str(*self)
321 fn recover(tcx: TyCtxt<'tcx>, dep_node: &DepNode) -> Option<Self> {
322 dep_node.extract_def_id(tcx)
326 impl<'tcx> DepNodeParams<TyCtxt<'tcx>> for LocalDefId {
328 fn can_reconstruct_query_key() -> bool {
332 fn to_fingerprint(&self, tcx: TyCtxt<'tcx>) -> Fingerprint {
333 self.to_def_id().to_fingerprint(tcx)
336 fn to_debug_str(&self, tcx: TyCtxt<'tcx>) -> String {
337 self.to_def_id().to_debug_str(tcx)
340 fn recover(tcx: TyCtxt<'tcx>, dep_node: &DepNode) -> Option<Self> {
341 dep_node.extract_def_id(tcx).map(|id| id.expect_local())
345 impl<'tcx> DepNodeParams<TyCtxt<'tcx>> for CrateNum {
347 fn can_reconstruct_query_key() -> bool {
351 fn to_fingerprint(&self, tcx: TyCtxt<'tcx>) -> Fingerprint {
352 let def_id = DefId { krate: *self, index: CRATE_DEF_INDEX };
353 tcx.def_path_hash(def_id).0
356 fn to_debug_str(&self, tcx: TyCtxt<'tcx>) -> String {
357 tcx.crate_name(*self).to_string()
360 fn recover(tcx: TyCtxt<'tcx>, dep_node: &DepNode) -> Option<Self> {
361 dep_node.extract_def_id(tcx).map(|id| id.krate)
365 impl<'tcx> DepNodeParams<TyCtxt<'tcx>> for (DefId, DefId) {
367 fn can_reconstruct_query_key() -> bool {
371 // We actually would not need to specialize the implementation of this
372 // method but it's faster to combine the hashes than to instantiate a full
373 // hashing context and stable-hashing state.
374 fn to_fingerprint(&self, tcx: TyCtxt<'tcx>) -> Fingerprint {
375 let (def_id_0, def_id_1) = *self;
377 let def_path_hash_0 = tcx.def_path_hash(def_id_0);
378 let def_path_hash_1 = tcx.def_path_hash(def_id_1);
380 def_path_hash_0.0.combine(def_path_hash_1.0)
383 fn to_debug_str(&self, tcx: TyCtxt<'tcx>) -> String {
384 let (def_id_0, def_id_1) = *self;
386 format!("({}, {})", tcx.def_path_debug_str(def_id_0), tcx.def_path_debug_str(def_id_1))
390 impl<'tcx> DepNodeParams<TyCtxt<'tcx>> for HirId {
392 fn can_reconstruct_query_key() -> bool {
396 // We actually would not need to specialize the implementation of this
397 // method but it's faster to combine the hashes than to instantiate a full
398 // hashing context and stable-hashing state.
399 fn to_fingerprint(&self, tcx: TyCtxt<'tcx>) -> Fingerprint {
400 let HirId { owner, local_id } = *self;
402 let def_path_hash = tcx.def_path_hash(owner.to_def_id());
403 let local_id = Fingerprint::from_smaller_hash(local_id.as_u32().into());
405 def_path_hash.0.combine(local_id)