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