]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_query_system/src/query/mod.rs
Auto merge of #105323 - cjgillot:simplify-const-prop, r=davidtwco
[rust.git] / compiler / rustc_query_system / src / query / mod.rs
1 mod plumbing;
2 pub use self::plumbing::*;
3
4 mod job;
5 #[cfg(parallel_compiler)]
6 pub use self::job::deadlock;
7 pub use self::job::{print_query_stack, QueryInfo, QueryJob, QueryJobId, QueryJobInfo, QueryMap};
8
9 mod caches;
10 pub use self::caches::{
11     CacheSelector, DefaultCacheSelector, QueryCache, QueryStorage, VecCacheSelector,
12 };
13
14 mod config;
15 pub use self::config::{HashResult, QueryConfig, TryLoadFromDisk};
16
17 use crate::dep_graph::DepKind;
18 use crate::dep_graph::{DepNodeIndex, HasDepContext, SerializedDepNodeIndex};
19 use rustc_data_structures::sync::Lock;
20 use rustc_errors::Diagnostic;
21 use rustc_hir::def::DefKind;
22 use rustc_span::def_id::DefId;
23 use rustc_span::Span;
24 use thin_vec::ThinVec;
25
26 /// Description of a frame in the query stack.
27 ///
28 /// This is mostly used in case of cycles for error reporting.
29 #[derive(Clone, Debug)]
30 pub struct QueryStackFrame<D: DepKind> {
31     pub description: String,
32     span: Option<Span>,
33     pub def_id: Option<DefId>,
34     pub def_kind: Option<DefKind>,
35     pub ty_adt_id: Option<DefId>,
36     pub dep_kind: D,
37     /// This hash is used to deterministically pick
38     /// a query to remove cycles in the parallel compiler.
39     #[cfg(parallel_compiler)]
40     hash: u64,
41 }
42
43 impl<D: DepKind> QueryStackFrame<D> {
44     #[inline]
45     pub fn new(
46         description: String,
47         span: Option<Span>,
48         def_id: Option<DefId>,
49         def_kind: Option<DefKind>,
50         dep_kind: D,
51         ty_adt_id: Option<DefId>,
52         _hash: impl FnOnce() -> u64,
53     ) -> Self {
54         Self {
55             description,
56             span,
57             def_id,
58             def_kind,
59             ty_adt_id,
60             dep_kind,
61             #[cfg(parallel_compiler)]
62             hash: _hash(),
63         }
64     }
65
66     // FIXME(eddyb) Get more valid `Span`s on queries.
67     #[inline]
68     pub fn default_span(&self, span: Span) -> Span {
69         if !span.is_dummy() {
70             return span;
71         }
72         self.span.unwrap_or(span)
73     }
74 }
75
76 /// Tracks 'side effects' for a particular query.
77 /// This struct is saved to disk along with the query result,
78 /// and loaded from disk if we mark the query as green.
79 /// This allows us to 'replay' changes to global state
80 /// that would otherwise only occur if we actually
81 /// executed the query method.
82 #[derive(Debug, Clone, Default, Encodable, Decodable)]
83 pub struct QuerySideEffects {
84     /// Stores any diagnostics emitted during query execution.
85     /// These diagnostics will be re-emitted if we mark
86     /// the query as green.
87     pub(super) diagnostics: ThinVec<Diagnostic>,
88 }
89
90 impl QuerySideEffects {
91     #[inline]
92     pub fn is_empty(&self) -> bool {
93         let QuerySideEffects { diagnostics } = self;
94         diagnostics.is_empty()
95     }
96     pub fn append(&mut self, other: QuerySideEffects) {
97         let QuerySideEffects { diagnostics } = self;
98         diagnostics.extend(other.diagnostics);
99     }
100 }
101
102 pub trait QueryContext: HasDepContext {
103     fn next_job_id(&self) -> QueryJobId;
104
105     /// Get the query information from the TLS context.
106     fn current_query_job(&self) -> Option<QueryJobId>;
107
108     fn try_collect_active_jobs(&self) -> Option<QueryMap<Self::DepKind>>;
109
110     /// Load side effects associated to the node in the previous session.
111     fn load_side_effects(&self, prev_dep_node_index: SerializedDepNodeIndex) -> QuerySideEffects;
112
113     /// Register diagnostics for the given node, for use in next session.
114     fn store_side_effects(&self, dep_node_index: DepNodeIndex, side_effects: QuerySideEffects);
115
116     /// Register diagnostics for the given node, for use in next session.
117     fn store_side_effects_for_anon_node(
118         &self,
119         dep_node_index: DepNodeIndex,
120         side_effects: QuerySideEffects,
121     );
122
123     /// Executes a job by changing the `ImplicitCtxt` to point to the
124     /// new query job while it executes. It returns the diagnostics
125     /// captured during execution and the actual result.
126     fn start_query<R>(
127         &self,
128         token: QueryJobId,
129         depth_limit: bool,
130         diagnostics: Option<&Lock<ThinVec<Diagnostic>>>,
131         compute: impl FnOnce() -> R,
132     ) -> R;
133
134     fn depth_limit_error(&self, job: QueryJobId);
135 }