]> git.lizzy.rs Git - rust.git/blob - src/librustc/ty/query/keys.rs
Auto merge of #67768 - wesleywiser:dnm_test_perf_65244, r=Mark-Simulacrum
[rust.git] / src / librustc / ty / query / keys.rs
1 //! Defines the set of legal keys that can be used in queries.
2
3 use crate::hir::def_id::{CrateNum, DefId, DefIndex, LOCAL_CRATE};
4 use crate::infer::canonical::Canonical;
5 use crate::mir;
6 use crate::traits;
7 use crate::ty::fast_reject::SimplifiedType;
8 use crate::ty::subst::SubstsRef;
9 use crate::ty::{self, Ty, TyCtxt};
10
11 use rustc_span::symbol::Symbol;
12 use rustc_span::{Span, DUMMY_SP};
13
14 /// The `Key` trait controls what types can legally be used as the key
15 /// for a query.
16 pub(super) trait Key {
17     /// Given an instance of this key, what crate is it referring to?
18     /// This is used to find the provider.
19     fn query_crate(&self) -> CrateNum;
20
21     /// In the event that a cycle occurs, if no explicit span has been
22     /// given for a query with key `self`, what span should we use?
23     fn default_span(&self, tcx: TyCtxt<'_>) -> Span;
24 }
25
26 impl<'tcx> Key for ty::InstanceDef<'tcx> {
27     fn query_crate(&self) -> CrateNum {
28         LOCAL_CRATE
29     }
30
31     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
32         tcx.def_span(self.def_id())
33     }
34 }
35
36 impl<'tcx> Key for ty::Instance<'tcx> {
37     fn query_crate(&self) -> CrateNum {
38         LOCAL_CRATE
39     }
40
41     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
42         tcx.def_span(self.def_id())
43     }
44 }
45
46 impl<'tcx> Key for mir::interpret::GlobalId<'tcx> {
47     fn query_crate(&self) -> CrateNum {
48         self.instance.query_crate()
49     }
50
51     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
52         self.instance.default_span(tcx)
53     }
54 }
55
56 impl Key for CrateNum {
57     fn query_crate(&self) -> CrateNum {
58         *self
59     }
60     fn default_span(&self, _: TyCtxt<'_>) -> Span {
61         DUMMY_SP
62     }
63 }
64
65 impl Key for DefIndex {
66     fn query_crate(&self) -> CrateNum {
67         LOCAL_CRATE
68     }
69     fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
70         DUMMY_SP
71     }
72 }
73
74 impl Key for DefId {
75     fn query_crate(&self) -> CrateNum {
76         self.krate
77     }
78     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
79         tcx.def_span(*self)
80     }
81 }
82
83 impl Key for (DefId, DefId) {
84     fn query_crate(&self) -> CrateNum {
85         self.0.krate
86     }
87     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
88         self.1.default_span(tcx)
89     }
90 }
91
92 impl Key for (CrateNum, DefId) {
93     fn query_crate(&self) -> CrateNum {
94         self.0
95     }
96     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
97         self.1.default_span(tcx)
98     }
99 }
100
101 impl Key for (DefId, SimplifiedType) {
102     fn query_crate(&self) -> CrateNum {
103         self.0.krate
104     }
105     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
106         self.0.default_span(tcx)
107     }
108 }
109
110 impl<'tcx> Key for (DefId, SubstsRef<'tcx>) {
111     fn query_crate(&self) -> CrateNum {
112         self.0.krate
113     }
114     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
115         self.0.default_span(tcx)
116     }
117 }
118
119 impl<'tcx> Key for (ty::ParamEnv<'tcx>, ty::PolyTraitRef<'tcx>) {
120     fn query_crate(&self) -> CrateNum {
121         self.1.def_id().krate
122     }
123     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
124         tcx.def_span(self.1.def_id())
125     }
126 }
127
128 impl<'tcx> Key for (&'tcx ty::Const<'tcx>, mir::Field) {
129     fn query_crate(&self) -> CrateNum {
130         LOCAL_CRATE
131     }
132     fn default_span(&self, _: TyCtxt<'_>) -> Span {
133         DUMMY_SP
134     }
135 }
136
137 impl<'tcx> Key for ty::PolyTraitRef<'tcx> {
138     fn query_crate(&self) -> CrateNum {
139         self.def_id().krate
140     }
141     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
142         tcx.def_span(self.def_id())
143     }
144 }
145
146 impl<'tcx> Key for ty::Const<'tcx> {
147     fn query_crate(&self) -> CrateNum {
148         LOCAL_CRATE
149     }
150     fn default_span(&self, _: TyCtxt<'_>) -> Span {
151         DUMMY_SP
152     }
153 }
154
155 impl<'tcx> Key for Ty<'tcx> {
156     fn query_crate(&self) -> CrateNum {
157         LOCAL_CRATE
158     }
159     fn default_span(&self, _: TyCtxt<'_>) -> Span {
160         DUMMY_SP
161     }
162 }
163
164 impl<'tcx> Key for ty::ParamEnv<'tcx> {
165     fn query_crate(&self) -> CrateNum {
166         LOCAL_CRATE
167     }
168     fn default_span(&self, _: TyCtxt<'_>) -> Span {
169         DUMMY_SP
170     }
171 }
172
173 impl<'tcx, T: Key> Key for ty::ParamEnvAnd<'tcx, T> {
174     fn query_crate(&self) -> CrateNum {
175         self.value.query_crate()
176     }
177     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
178         self.value.default_span(tcx)
179     }
180 }
181
182 impl<'tcx> Key for traits::Environment<'tcx> {
183     fn query_crate(&self) -> CrateNum {
184         LOCAL_CRATE
185     }
186     fn default_span(&self, _: TyCtxt<'_>) -> Span {
187         DUMMY_SP
188     }
189 }
190
191 impl Key for Symbol {
192     fn query_crate(&self) -> CrateNum {
193         LOCAL_CRATE
194     }
195     fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
196         DUMMY_SP
197     }
198 }
199
200 /// Canonical query goals correspond to abstract trait operations that
201 /// are not tied to any crate in particular.
202 impl<'tcx, T> Key for Canonical<'tcx, T> {
203     fn query_crate(&self) -> CrateNum {
204         LOCAL_CRATE
205     }
206
207     fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
208         DUMMY_SP
209     }
210 }
211
212 impl Key for (Symbol, u32, u32) {
213     fn query_crate(&self) -> CrateNum {
214         LOCAL_CRATE
215     }
216
217     fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
218         DUMMY_SP
219     }
220 }