]> git.lizzy.rs Git - rust.git/blob - src/librustc_query_system/dep_graph/dep_node.rs
e3df9d5d04be106727dd783d97e5a3103931a1aa
[rust.git] / src / librustc_query_system / dep_graph / dep_node.rs
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:
12 //!
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
21 //!   reconstruction).
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.
28 //!
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.
33 //!
34 //! Because the macro sees what parameters a given `DepKind` requires, it can
35 //! "infer" some properties for each kind of `DepNode`:
36 //!
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.
44 //!
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.
51
52 use crate::hir::map::DefPathHash;
53 use crate::ich::{Fingerprint, StableHashingContext};
54 use crate::mir;
55 use crate::mir::interpret::{GlobalId, LitToConstInput};
56 use crate::traits;
57 use crate::traits::query::{
58     CanonicalPredicateGoal, CanonicalProjectionGoal, CanonicalTyGoal,
59     CanonicalTypeOpAscribeUserTypeGoal, CanonicalTypeOpEqGoal, CanonicalTypeOpNormalizeGoal,
60     CanonicalTypeOpProvePredicateGoal, CanonicalTypeOpSubtypeGoal,
61 };
62 use crate::ty::subst::SubstsRef;
63 use crate::ty::{self, ParamEnvAnd, Ty, TyCtxt};
64
65 use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
66 use rustc_hir::def_id::{CrateNum, DefId, DefIndex, CRATE_DEF_INDEX};
67 use rustc_hir::HirId;
68 use rustc_span::symbol::Symbol;
69 use std::fmt;
70 use std::hash::Hash;
71
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.
75 macro_rules! erase {
76     ($x:tt) => {{}};
77 }
78
79 macro_rules! is_anon_attr {
80     (anon) => {
81         true
82     };
83     ($attr:ident) => {
84         false
85     };
86 }
87
88 macro_rules! is_eval_always_attr {
89     (eval_always) => {
90         true
91     };
92     ($attr:ident) => {
93         false
94     };
95 }
96
97 macro_rules! contains_anon_attr {
98     ($($attr:ident $(($($attr_args:tt)*))* ),*) => ({$(is_anon_attr!($attr) | )* false});
99 }
100
101 macro_rules! contains_eval_always_attr {
102     ($($attr:ident $(($($attr_args:tt)*))* ),*) => ({$(is_eval_always_attr!($attr) | )* false});
103 }
104
105 macro_rules! define_dep_nodes {
106     (<$tcx:tt>
107     $(
108         [$($attrs:tt)*]
109         $variant:ident $(( $tuple_arg_ty:ty $(,)? ))*
110       ,)*
111     ) => (
112         #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash,
113                  RustcEncodable, RustcDecodable)]
114         #[allow(non_camel_case_types)]
115         pub enum DepKind {
116             $($variant),*
117         }
118
119         impl DepKind {
120             #[allow(unreachable_code)]
121             pub fn can_reconstruct_query_key<$tcx>(&self) -> bool {
122                 match *self {
123                     $(
124                         DepKind :: $variant => {
125                             if contains_anon_attr!($($attrs)*) {
126                                 return false;
127                             }
128
129                             // tuple args
130                             $({
131                                 return <$tuple_arg_ty as DepNodeParams>
132                                     ::CAN_RECONSTRUCT_QUERY_KEY;
133                             })*
134
135                             true
136                         }
137                     )*
138                 }
139             }
140
141             pub fn is_anon(&self) -> bool {
142                 match *self {
143                     $(
144                         DepKind :: $variant => { contains_anon_attr!($($attrs)*) }
145                     )*
146                 }
147             }
148
149             pub fn is_eval_always(&self) -> bool {
150                 match *self {
151                     $(
152                         DepKind :: $variant => { contains_eval_always_attr!($($attrs)*) }
153                     )*
154                 }
155             }
156
157             #[allow(unreachable_code)]
158             pub fn has_params(&self) -> bool {
159                 match *self {
160                     $(
161                         DepKind :: $variant => {
162                             // tuple args
163                             $({
164                                 erase!($tuple_arg_ty);
165                                 return true;
166                             })*
167
168                             false
169                         }
170                     )*
171                 }
172             }
173         }
174
175         pub struct DepConstructor;
176
177         #[allow(non_camel_case_types)]
178         impl DepConstructor {
179             $(
180                 #[inline(always)]
181                 #[allow(unreachable_code, non_snake_case)]
182                 pub fn $variant(_tcx: TyCtxt<'_>, $(arg: $tuple_arg_ty)*) -> DepNode {
183                     // tuple args
184                     $({
185                         erase!($tuple_arg_ty);
186                         let hash = DepNodeParams::to_fingerprint(&arg, _tcx);
187                         let dep_node = DepNode {
188                             kind: DepKind::$variant,
189                             hash
190                         };
191
192                         #[cfg(debug_assertions)]
193                         {
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)
197                             {
198                                 _tcx.dep_graph.register_dep_node_debug_str(dep_node, || {
199                                     arg.to_debug_str(_tcx)
200                                 });
201                             }
202                         }
203
204                         return dep_node;
205                     })*
206
207                     DepNode {
208                         kind: DepKind::$variant,
209                         hash: Fingerprint::ZERO,
210                     }
211                 }
212             )*
213         }
214
215         #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash,
216                  RustcEncodable, RustcDecodable)]
217         pub struct DepNode {
218             pub kind: DepKind,
219             pub hash: Fingerprint,
220         }
221
222         impl 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             pub fn from_def_path_hash(def_path_hash: DefPathHash,
227                                       kind: DepKind)
228                                       -> DepNode {
229                 debug_assert!(kind.can_reconstruct_query_key() && kind.has_params());
230                 DepNode {
231                     kind,
232                     hash: def_path_hash.0,
233                 }
234             }
235
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());
241                 DepNode {
242                     kind,
243                     hash: Fingerprint::ZERO,
244                 }
245             }
246
247             /// Extracts the DefId corresponding to this DepNode. This will work
248             /// if two conditions are met:
249             ///
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.
252             ///
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()
262                 } else {
263                     None
264                 }
265             }
266
267             /// Used in testing
268             pub fn from_label_string(label: &str,
269                                      def_path_hash: DefPathHash)
270                                      -> Result<DepNode, ()> {
271                 let kind = match label {
272                     $(
273                         stringify!($variant) => DepKind::$variant,
274                     )*
275                     _ => return Err(()),
276                 };
277
278                 if !kind.can_reconstruct_query_key() {
279                     return Err(());
280                 }
281
282                 if kind.has_params() {
283                     Ok(DepNode::from_def_path_hash(def_path_hash, kind))
284                 } else {
285                     Ok(DepNode::new_no_params(kind))
286                 }
287             }
288
289             /// Used in testing
290             pub fn has_label_string(label: &str) -> bool {
291                 match label {
292                     $(
293                         stringify!($variant) => true,
294                     )*
295                     _ => false,
296                 }
297             }
298         }
299
300         /// Contains variant => str representations for constructing
301         /// DepNode groups for tests.
302         #[allow(dead_code, non_upper_case_globals)]
303         pub mod label_strs {
304            $(
305                 pub const $variant: &str = stringify!($variant);
306             )*
307         }
308     );
309 }
310
311 impl fmt::Debug for DepNode {
312     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
313         write!(f, "{:?}", self.kind)?;
314
315         if !self.kind.has_params() && !self.kind.is_anon() {
316             return Ok(());
317         }
318
319         write!(f, "(")?;
320
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) {
326                     write!(f, "{}", s)?;
327                 } else {
328                     write!(f, "{}", self.hash)?;
329                 }
330             } else {
331                 write!(f, "{}", self.hash)?;
332             }
333             Ok(())
334         })?;
335
336         write!(f, ")")
337     }
338 }
339
340 rustc_dep_node_append!([define_dep_nodes!][ <'tcx>
341     // We use this for most things when incr. comp. is turned off.
342     [] Null,
343
344     // Represents metadata from an extern crate.
345     [eval_always] CrateMetadata(CrateNum),
346
347     [anon] TraitSelect,
348
349     [] CompileCodegenUnit(Symbol),
350 ]);
351
352 pub(crate) trait DepNodeParams<'tcx>: fmt::Debug + Sized {
353     const CAN_RECONSTRUCT_QUERY_KEY: bool;
354
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?")
361     }
362
363     fn to_debug_str(&self, _: TyCtxt<'tcx>) -> String {
364         format!("{:?}", self)
365     }
366
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>;
374 }
375
376 impl<'tcx, T> DepNodeParams<'tcx> for T
377 where
378     T: HashStable<StableHashingContext<'tcx>> + fmt::Debug,
379 {
380     default const CAN_RECONSTRUCT_QUERY_KEY: bool = false;
381
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();
385
386         self.hash_stable(&mut hcx, &mut hasher);
387
388         hasher.finish()
389     }
390
391     default fn to_debug_str(&self, _: TyCtxt<'tcx>) -> String {
392         format!("{:?}", *self)
393     }
394
395     default fn recover(_: TyCtxt<'tcx>, _: &DepNode) -> Option<Self> {
396         None
397     }
398 }
399
400 impl<'tcx> DepNodeParams<'tcx> for DefId {
401     const CAN_RECONSTRUCT_QUERY_KEY: bool = true;
402
403     fn to_fingerprint(&self, tcx: TyCtxt<'_>) -> Fingerprint {
404         tcx.def_path_hash(*self).0
405     }
406
407     fn to_debug_str(&self, tcx: TyCtxt<'tcx>) -> String {
408         tcx.def_path_str(*self)
409     }
410
411     fn recover(tcx: TyCtxt<'tcx>, dep_node: &DepNode) -> Option<Self> {
412         dep_node.extract_def_id(tcx)
413     }
414 }
415
416 impl<'tcx> DepNodeParams<'tcx> for DefIndex {
417     const CAN_RECONSTRUCT_QUERY_KEY: bool = true;
418
419     fn to_fingerprint(&self, tcx: TyCtxt<'_>) -> Fingerprint {
420         tcx.hir().definitions().def_path_hash(*self).0
421     }
422
423     fn to_debug_str(&self, tcx: TyCtxt<'tcx>) -> String {
424         tcx.def_path_str(DefId::local(*self))
425     }
426
427     fn recover(tcx: TyCtxt<'tcx>, dep_node: &DepNode) -> Option<Self> {
428         dep_node.extract_def_id(tcx).map(|id| id.index)
429     }
430 }
431
432 impl<'tcx> DepNodeParams<'tcx> for CrateNum {
433     const CAN_RECONSTRUCT_QUERY_KEY: bool = true;
434
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
438     }
439
440     fn to_debug_str(&self, tcx: TyCtxt<'tcx>) -> String {
441         tcx.crate_name(*self).to_string()
442     }
443
444     fn recover(tcx: TyCtxt<'tcx>, dep_node: &DepNode) -> Option<Self> {
445         dep_node.extract_def_id(tcx).map(|id| id.krate)
446     }
447 }
448
449 impl<'tcx> DepNodeParams<'tcx> for (DefId, DefId) {
450     const CAN_RECONSTRUCT_QUERY_KEY: bool = false;
451
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;
457
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);
460
461         def_path_hash_0.0.combine(def_path_hash_1.0)
462     }
463
464     fn to_debug_str(&self, tcx: TyCtxt<'tcx>) -> String {
465         let (def_id_0, def_id_1) = *self;
466
467         format!("({}, {})", tcx.def_path_debug_str(def_id_0), tcx.def_path_debug_str(def_id_1))
468     }
469 }
470
471 impl<'tcx> DepNodeParams<'tcx> for HirId {
472     const CAN_RECONSTRUCT_QUERY_KEY: bool = false;
473
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;
479
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());
482
483         def_path_hash.0.combine(local_id)
484     }
485 }
486
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.)
492 #[derive(
493     Clone,
494     Copy,
495     Debug,
496     PartialEq,
497     Eq,
498     PartialOrd,
499     Ord,
500     Hash,
501     RustcEncodable,
502     RustcDecodable,
503     HashStable
504 )]
505 pub struct WorkProductId {
506     hash: Fingerprint,
507 }
508
509 impl 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() }
515     }
516
517     pub fn from_fingerprint(fingerprint: Fingerprint) -> WorkProductId {
518         WorkProductId { hash: fingerprint }
519     }
520 }