rustc_query_system/query/
mod.rs

1mod plumbing;
2pub use self::plumbing::*;
3
4mod job;
5pub use self::job::{
6    QueryInfo, QueryJob, QueryJobId, QueryJobInfo, QueryMap, break_query_cycles, print_query_stack,
7    report_cycle,
8};
9
10mod caches;
11pub use self::caches::{DefIdCache, DefaultCache, QueryCache, SingleCache, VecCache};
12
13mod config;
14use rustc_errors::DiagInner;
15use rustc_hashes::Hash64;
16use rustc_hir::def::DefKind;
17use rustc_macros::{Decodable, Encodable};
18use rustc_span::Span;
19use rustc_span::def_id::DefId;
20
21pub use self::config::{HashResult, QueryConfig};
22use crate::dep_graph::{DepKind, DepNodeIndex, HasDepContext, SerializedDepNodeIndex};
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)]
28pub struct QueryStackFrame {
29    pub description: String,
30    span: Option<Span>,
31    pub def_id: Option<DefId>,
32    pub def_kind: Option<DefKind>,
33    /// A def-id that is extracted from a `Ty` in a query key
34    pub def_id_for_ty_in_cycle: Option<DefId>,
35    pub dep_kind: DepKind,
36    /// This hash is used to deterministically pick
37    /// a query to remove cycles in the parallel compiler.
38    hash: Hash64,
39}
40
41impl QueryStackFrame {
42    #[inline]
43    pub fn new(
44        description: String,
45        span: Option<Span>,
46        def_id: Option<DefId>,
47        def_kind: Option<DefKind>,
48        dep_kind: DepKind,
49        def_id_for_ty_in_cycle: Option<DefId>,
50        hash: impl FnOnce() -> Hash64,
51    ) -> Self {
52        Self { description, span, def_id, def_kind, def_id_for_ty_in_cycle, dep_kind, hash: hash() }
53    }
54
55    // FIXME(eddyb) Get more valid `Span`s on queries.
56    #[inline]
57    pub fn default_span(&self, span: Span) -> Span {
58        if !span.is_dummy() {
59            return span;
60        }
61        self.span.unwrap_or(span)
62    }
63}
64
65/// Track a 'side effects' for a particular query.
66/// This struct is saved to disk along with the query result,
67/// and loaded from disk if we mark the query as green.
68/// This allows us to 'replay' changes to global state
69/// that would otherwise only occur if we actually
70/// executed the query method.
71///
72/// Each side effect gets an unique dep node index which is added
73/// as a dependency of the query which had the effect.
74#[derive(Debug, Encodable, Decodable)]
75pub enum QuerySideEffect {
76    /// Stores a diagnostic emitted during query execution.
77    /// This diagnostic will be re-emitted if we mark
78    /// the query as green, as that query will have the side
79    /// effect dep node as a dependency.
80    Diagnostic(DiagInner),
81}
82
83pub trait QueryContext: HasDepContext {
84    fn next_job_id(self) -> QueryJobId;
85
86    /// Get the query information from the TLS context.
87    fn current_query_job(self) -> Option<QueryJobId>;
88
89    fn collect_active_jobs(self) -> Result<QueryMap, QueryMap>;
90
91    /// Load a side effect associated to the node in the previous session.
92    fn load_side_effect(
93        self,
94        prev_dep_node_index: SerializedDepNodeIndex,
95    ) -> Option<QuerySideEffect>;
96
97    /// Register a side effect for the given node, for use in next session.
98    fn store_side_effect(self, dep_node_index: DepNodeIndex, side_effect: QuerySideEffect);
99
100    /// Executes a job by changing the `ImplicitCtxt` to point to the
101    /// new query job while it executes.
102    fn start_query<R>(self, token: QueryJobId, depth_limit: bool, compute: impl FnOnce() -> R)
103    -> R;
104
105    fn depth_limit_error(self, job: QueryJobId);
106}