]> git.lizzy.rs Git - rust.git/blob - src/librustc/query/mod.rs
Rollup merge of #59432 - phansch:compiletest_docs, r=alexcrichton
[rust.git] / src / librustc / query / mod.rs
1 use crate::ty::query::QueryDescription;
2 use crate::ty::query::queries;
3 use crate::ty::{self, Ty, TyCtxt};
4 use crate::hir::def_id::{DefId, CrateNum};
5 use crate::dep_graph::SerializedDepNodeIndex;
6 use crate::traits;
7 use std::borrow::Cow;
8
9 // Each of these queries corresponds to a function pointer field in the
10 // `Providers` struct for requesting a value of that type, and a method
11 // on `tcx: TyCtxt` (and `tcx.at(span)`) for doing that request in a way
12 // which memoizes and does dep-graph tracking, wrapping around the actual
13 // `Providers` that the driver creates (using several `rustc_*` crates).
14 //
15 // The result type of each query must implement `Clone`, and additionally
16 // `ty::query::values::Value`, which produces an appropriate placeholder
17 // (error) value if the query resulted in a query cycle.
18 // Queries marked with `fatal_cycle` do not need the latter implementation,
19 // as they will raise an fatal error on query cycles instead.
20 rustc_queries! {
21     Other {
22         /// Records the type of every item.
23         query type_of(key: DefId) -> Ty<'tcx> {
24             cache { key.is_local() }
25         }
26
27         /// Maps from the `DefId` of an item (trait/struct/enum/fn) to its
28         /// associated generics.
29         query generics_of(key: DefId) -> &'tcx ty::Generics {
30             cache { key.is_local() }
31             load_cached(tcx, id) {
32                 let generics: Option<ty::Generics> = tcx.queries.on_disk_cache
33                                                         .try_load_query_result(tcx, id);
34                 generics.map(|x| tcx.alloc_generics(x))
35             }
36         }
37
38         /// Maps from the `DefId` of an item (trait/struct/enum/fn) to the
39         /// predicates (where-clauses) that must be proven true in order
40         /// to reference it. This is almost always the "predicates query"
41         /// that you want.
42         ///
43         /// `predicates_of` builds on `predicates_defined_on` -- in fact,
44         /// it is almost always the same as that query, except for the
45         /// case of traits. For traits, `predicates_of` contains
46         /// an additional `Self: Trait<...>` predicate that users don't
47         /// actually write. This reflects the fact that to invoke the
48         /// trait (e.g., via `Default::default`) you must supply types
49         /// that actually implement the trait. (However, this extra
50         /// predicate gets in the way of some checks, which are intended
51         /// to operate over only the actual where-clauses written by the
52         /// user.)
53         query predicates_of(_: DefId) -> Lrc<ty::GenericPredicates<'tcx>> {}
54
55         query native_libraries(_: CrateNum) -> Lrc<Vec<NativeLibrary>> {
56             desc { "looking up the native libraries of a linked crate" }
57         }
58
59         query lint_levels(_: CrateNum) -> Lrc<lint::LintLevelMap> {
60             eval_always
61             desc { "computing the lint levels for items in this crate" }
62         }
63     }
64
65     Codegen {
66         query is_panic_runtime(_: CrateNum) -> bool {
67             fatal_cycle
68             desc { "checking if the crate is_panic_runtime" }
69         }
70     }
71
72     Codegen {
73         /// Set of all the `DefId`s in this crate that have MIR associated with
74         /// them. This includes all the body owners, but also things like struct
75         /// constructors.
76         query mir_keys(_: CrateNum) -> Lrc<DefIdSet> {
77             desc { "getting a list of all mir_keys" }
78         }
79
80         /// Maps DefId's that have an associated Mir to the result
81         /// of the MIR qualify_consts pass. The actual meaning of
82         /// the value isn't known except to the pass itself.
83         query mir_const_qualif(key: DefId) -> (u8, Lrc<BitSet<mir::Local>>) {
84             cache { key.is_local() }
85         }
86
87         /// Fetch the MIR for a given `DefId` right after it's built - this includes
88         /// unreachable code.
89         query mir_built(_: DefId) -> &'tcx Steal<mir::Mir<'tcx>> {}
90
91         /// Fetch the MIR for a given `DefId` up till the point where it is
92         /// ready for const evaluation.
93         ///
94         /// See the README for the `mir` module for details.
95         query mir_const(_: DefId) -> &'tcx Steal<mir::Mir<'tcx>> {
96             no_hash
97         }
98
99         query mir_validated(_: DefId) -> &'tcx Steal<mir::Mir<'tcx>> {
100             no_hash
101         }
102
103         /// MIR after our optimization passes have run. This is MIR that is ready
104         /// for codegen. This is also the only query that can fetch non-local MIR, at present.
105         query optimized_mir(key: DefId) -> &'tcx mir::Mir<'tcx> {
106             cache { key.is_local() }
107             load_cached(tcx, id) {
108                 let mir: Option<crate::mir::Mir<'tcx>> = tcx.queries.on_disk_cache
109                                                             .try_load_query_result(tcx, id);
110                 mir.map(|x| tcx.alloc_mir(x))
111             }
112         }
113     }
114
115     TypeChecking {
116         // Erases regions from `ty` to yield a new type.
117         // Normally you would just use `tcx.erase_regions(&value)`,
118         // however, which uses this query as a kind of cache.
119         query erase_regions_ty(ty: Ty<'tcx>) -> Ty<'tcx> {
120             // This query is not expected to have input -- as a result, it
121             // is not a good candidates for "replay" because it is essentially a
122             // pure function of its input (and hence the expectation is that
123             // no caller would be green **apart** from just these
124             // queries). Making it anonymous avoids hashing the result, which
125             // may save a bit of time.
126             anon
127             no_force
128             desc { "erasing regions from `{:?}`", ty }
129         }
130
131         query program_clauses_for(_: DefId) -> Clauses<'tcx> {
132             desc { "generating chalk-style clauses" }
133         }
134
135         query program_clauses_for_env(_: traits::Environment<'tcx>) -> Clauses<'tcx> {
136             no_force
137             desc { "generating chalk-style clauses for environment" }
138         }
139
140         // Get the chalk-style environment of the given item.
141         query environment(_: DefId) -> traits::Environment<'tcx> {
142             desc { "return a chalk-style environment" }
143         }
144     }
145
146     Linking {
147         query wasm_import_module_map(_: CrateNum) -> Lrc<FxHashMap<DefId, String>> {
148             desc { "wasm import module map" }
149         }
150     }
151 }