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