]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_middle/src/dep_graph/mod.rs
Auto merge of #100848 - xfix:use-metadata-for-slice-len, r=thomcc
[rust.git] / compiler / rustc_middle / src / dep_graph / mod.rs
1 use crate::ty::{self, TyCtxt};
2 use rustc_data_structures::profiling::SelfProfilerRef;
3 use rustc_query_system::ich::StableHashingContext;
4 use rustc_session::Session;
5
6 #[macro_use]
7 mod dep_node;
8
9 pub use rustc_query_system::dep_graph::{
10     debug::DepNodeFilter, hash_result, DepContext, DepNodeColor, DepNodeIndex,
11     SerializedDepNodeIndex, WorkProduct, WorkProductId,
12 };
13
14 pub use dep_node::{label_strs, DepKind, DepNode, DepNodeExt};
15 pub(crate) use dep_node::{make_compile_codegen_unit, make_compile_mono_item};
16
17 pub type DepGraph = rustc_query_system::dep_graph::DepGraph<DepKind>;
18
19 pub type TaskDeps = rustc_query_system::dep_graph::TaskDeps<DepKind>;
20 pub type TaskDepsRef<'a> = rustc_query_system::dep_graph::TaskDepsRef<'a, DepKind>;
21 pub type DepGraphQuery = rustc_query_system::dep_graph::DepGraphQuery<DepKind>;
22 pub type SerializedDepGraph = rustc_query_system::dep_graph::SerializedDepGraph<DepKind>;
23 pub type EdgeFilter = rustc_query_system::dep_graph::debug::EdgeFilter<DepKind>;
24 pub type DepKindStruct<'tcx> = rustc_query_system::dep_graph::DepKindStruct<TyCtxt<'tcx>>;
25
26 impl rustc_query_system::dep_graph::DepKind for DepKind {
27     const NULL: Self = DepKind::Null;
28     const RED: Self = DepKind::Red;
29
30     fn debug_node(node: &DepNode, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
31         write!(f, "{:?}(", node.kind)?;
32
33         ty::tls::with_opt(|opt_tcx| {
34             if let Some(tcx) = opt_tcx {
35                 if let Some(def_id) = node.extract_def_id(tcx) {
36                     write!(f, "{}", tcx.def_path_debug_str(def_id))?;
37                 } else if let Some(ref s) = tcx.dep_graph.dep_node_debug_str(*node) {
38                     write!(f, "{}", s)?;
39                 } else {
40                     write!(f, "{}", node.hash)?;
41                 }
42             } else {
43                 write!(f, "{}", node.hash)?;
44             }
45             Ok(())
46         })?;
47
48         write!(f, ")")
49     }
50
51     fn with_deps<OP, R>(task_deps: TaskDepsRef<'_>, op: OP) -> R
52     where
53         OP: FnOnce() -> R,
54     {
55         ty::tls::with_context(|icx| {
56             let icx = ty::tls::ImplicitCtxt { task_deps, ..icx.clone() };
57
58             ty::tls::enter_context(&icx, |_| op())
59         })
60     }
61
62     fn read_deps<OP>(op: OP)
63     where
64         OP: for<'a> FnOnce(TaskDepsRef<'a>),
65     {
66         ty::tls::with_context_opt(|icx| {
67             let Some(icx) = icx else { return };
68             op(icx.task_deps)
69         })
70     }
71 }
72
73 impl<'tcx> DepContext for TyCtxt<'tcx> {
74     type DepKind = DepKind;
75
76     #[inline]
77     fn with_stable_hashing_context<R>(&self, f: impl FnOnce(StableHashingContext<'_>) -> R) -> R {
78         TyCtxt::with_stable_hashing_context(*self, f)
79     }
80
81     #[inline]
82     fn dep_graph(&self) -> &DepGraph {
83         &self.dep_graph
84     }
85
86     #[inline(always)]
87     fn profiler(&self) -> &SelfProfilerRef {
88         &self.prof
89     }
90
91     #[inline(always)]
92     fn sess(&self) -> &Session {
93         self.sess
94     }
95
96     #[inline]
97     fn dep_kind_info(&self, dep_kind: DepKind) -> &DepKindStruct<'tcx> {
98         &self.query_kinds[dep_kind as usize]
99     }
100 }