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