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