-
-
Notifications
You must be signed in to change notification settings - Fork 15.3k
Expand file tree
/
Copy pathkeys.rs
More file actions
425 lines (346 loc) · 10.6 KB
/
Copy pathkeys.rs
File metadata and controls
425 lines (346 loc) · 10.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
//! Defines the set of legal keys that can be used in queries.
use std::ffi::OsStr;
use std::fmt::Debug;
use std::hash::Hash;
use rustc_ast::tokenstream::TokenStream;
use rustc_data_structures::sso::SsoHashSet;
use rustc_data_structures::stable_hash::StableHash;
use rustc_hir as hir;
use rustc_hir::def_id::{CrateNum, DefId, LOCAL_CRATE, LocalDefId, LocalModDefId};
use rustc_hir::hir_id::OwnerId;
use rustc_span::{DUMMY_SP, Ident, LocalExpnId, Span, Symbol};
use crate::dep_graph::DepNodeIndex;
use crate::infer::canonical::CanonicalQueryInput;
use crate::mono::CollectionMode;
use crate::query::{DefIdCache, DefaultCache, SingleCache, VecCache};
use crate::ty::fast_reject::SimplifiedType;
use crate::ty::layout::ValidityRequirement;
use crate::ty::{self, GenericArg, GenericArgsRef, Ty, TyCtxt};
use crate::{mir, traits};
/// Placeholder for `CrateNum`'s "local" counterpart
#[derive(Copy, Clone, Debug)]
pub struct LocalCrate;
pub trait QueryKeyBounds = Copy + Debug + Eq + Hash + StableHash;
/// Controls what types can legally be used as the key for a query.
pub trait QueryKey: Sized + QueryKeyBounds {
/// The type of in-memory cache to use for queries with this key type.
///
/// In practice the cache type must implement [`QueryCache`], though that
/// constraint is not enforced here.
///
/// [`QueryCache`]: rustc_middle::query::QueryCache
type Cache<V> = DefaultCache<Self, V>;
/// In the event that a cycle occurs, if no explicit span has been
/// given for a query with key `self`, what span should we use?
fn default_span(&self, tcx: TyCtxt<'_>) -> Span;
/// If the key is a [`DefId`] or `DefId`--equivalent, return that `DefId`.
/// Otherwise, return `None`.
fn key_as_def_id(&self) -> Option<DefId> {
None
}
}
pub trait AsLocalQueryKey: QueryKey {
type LocalQueryKey;
/// Given an instance of this key, what crate is it referring to?
/// This is used to find the provider.
fn as_local_key(&self) -> Option<Self::LocalQueryKey>;
}
impl QueryKey for () {
type Cache<V> = SingleCache<V>;
fn default_span(&self, _: TyCtxt<'_>) -> Span {
DUMMY_SP
}
}
impl<'tcx> QueryKey for ty::InstanceKind<'tcx> {
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
tcx.def_span(self.def_id())
}
}
impl<'tcx> QueryKey for ty::Instance<'tcx> {
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
tcx.def_span(self.def_id())
}
}
impl<'tcx> QueryKey for mir::interpret::GlobalId<'tcx> {
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
self.instance.default_span(tcx)
}
}
impl<'tcx> QueryKey for (Ty<'tcx>, Option<ty::ExistentialTraitRef<'tcx>>) {
fn default_span(&self, _: TyCtxt<'_>) -> Span {
DUMMY_SP
}
}
impl<'tcx> QueryKey for ty::LitToConstInput<'tcx> {
fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
DUMMY_SP
}
}
impl QueryKey for CrateNum {
type Cache<V> = VecCache<Self, V, DepNodeIndex>;
fn default_span(&self, _: TyCtxt<'_>) -> Span {
DUMMY_SP
}
}
impl AsLocalQueryKey for CrateNum {
type LocalQueryKey = LocalCrate;
#[inline(always)]
fn as_local_key(&self) -> Option<Self::LocalQueryKey> {
(*self == LOCAL_CRATE).then_some(LocalCrate)
}
}
impl QueryKey for OwnerId {
type Cache<V> = VecCache<Self, V, DepNodeIndex>;
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
self.to_def_id().default_span(tcx)
}
fn key_as_def_id(&self) -> Option<DefId> {
Some(self.to_def_id())
}
}
impl QueryKey for LocalDefId {
type Cache<V> = VecCache<Self, V, DepNodeIndex>;
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
self.to_def_id().default_span(tcx)
}
fn key_as_def_id(&self) -> Option<DefId> {
Some(self.to_def_id())
}
}
impl QueryKey for DefId {
type Cache<V> = DefIdCache<V>;
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
tcx.def_span(*self)
}
#[inline(always)]
fn key_as_def_id(&self) -> Option<DefId> {
Some(*self)
}
}
impl AsLocalQueryKey for DefId {
type LocalQueryKey = LocalDefId;
#[inline(always)]
fn as_local_key(&self) -> Option<Self::LocalQueryKey> {
self.as_local()
}
}
impl QueryKey for LocalModDefId {
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
tcx.def_span(*self)
}
#[inline(always)]
fn key_as_def_id(&self) -> Option<DefId> {
Some(self.to_def_id())
}
}
impl QueryKey for SimplifiedType {
fn default_span(&self, _: TyCtxt<'_>) -> Span {
DUMMY_SP
}
}
impl QueryKey for (DefId, DefId) {
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
self.1.default_span(tcx)
}
}
impl QueryKey for (DefId, Ident) {
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
tcx.def_span(self.0)
}
#[inline(always)]
fn key_as_def_id(&self) -> Option<DefId> {
Some(self.0)
}
}
impl QueryKey for (LocalDefId, LocalDefId, Ident) {
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
self.1.default_span(tcx)
}
}
impl QueryKey for (CrateNum, DefId) {
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
self.1.default_span(tcx)
}
}
impl AsLocalQueryKey for (CrateNum, DefId) {
type LocalQueryKey = DefId;
#[inline(always)]
fn as_local_key(&self) -> Option<Self::LocalQueryKey> {
(self.0 == LOCAL_CRATE).then(|| self.1)
}
}
impl QueryKey for (CrateNum, SimplifiedType) {
fn default_span(&self, _: TyCtxt<'_>) -> Span {
DUMMY_SP
}
}
impl AsLocalQueryKey for (CrateNum, SimplifiedType) {
type LocalQueryKey = SimplifiedType;
#[inline(always)]
fn as_local_key(&self) -> Option<Self::LocalQueryKey> {
(self.0 == LOCAL_CRATE).then(|| self.1)
}
}
impl QueryKey for (DefId, ty::SizedTraitKind) {
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
self.0.default_span(tcx)
}
}
impl<'tcx> QueryKey for GenericArgsRef<'tcx> {
fn default_span(&self, _: TyCtxt<'_>) -> Span {
DUMMY_SP
}
}
impl<'tcx> QueryKey for (DefId, GenericArgsRef<'tcx>) {
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
self.0.default_span(tcx)
}
}
impl<'tcx> QueryKey for (DefId, GenericArgsRef<'tcx>, hir::Constness) {
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
self.0.default_span(tcx)
}
}
impl<'tcx> QueryKey for ty::TraitRef<'tcx> {
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
tcx.def_span(self.def_id)
}
}
impl<'tcx> QueryKey for GenericArg<'tcx> {
fn default_span(&self, _: TyCtxt<'_>) -> Span {
DUMMY_SP
}
}
impl<'tcx> QueryKey for Ty<'tcx> {
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
def_id_of_type(*self).map(|def_id| tcx.def_span(def_id)).unwrap_or(DUMMY_SP)
}
}
impl<'tcx> QueryKey for (Ty<'tcx>, Ty<'tcx>) {
fn default_span(&self, _: TyCtxt<'_>) -> Span {
DUMMY_SP
}
}
impl<'tcx> QueryKey for ty::Clauses<'tcx> {
fn default_span(&self, _: TyCtxt<'_>) -> Span {
DUMMY_SP
}
}
impl<'tcx, T: QueryKey> QueryKey for ty::PseudoCanonicalInput<'tcx, T> {
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
self.value.default_span(tcx)
}
}
impl QueryKey for Symbol {
fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
DUMMY_SP
}
}
impl QueryKey for Option<Symbol> {
fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
DUMMY_SP
}
}
impl<'tcx> QueryKey for &'tcx OsStr {
fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
DUMMY_SP
}
}
/// Canonical query goals correspond to abstract trait operations that
/// are not tied to any crate in particular.
impl<'tcx, T: QueryKeyBounds> QueryKey for CanonicalQueryInput<'tcx, T> {
fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
DUMMY_SP
}
}
impl<'tcx, T: QueryKeyBounds> QueryKey for (CanonicalQueryInput<'tcx, T>, bool) {
fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
DUMMY_SP
}
}
impl<'tcx> QueryKey for (Ty<'tcx>, rustc_abi::VariantIdx) {
fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
DUMMY_SP
}
}
impl<'tcx> QueryKey for (ty::Predicate<'tcx>, traits::WellFormedLoc) {
fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
DUMMY_SP
}
}
impl<'tcx> QueryKey for (ty::PolyFnSig<'tcx>, &'tcx ty::List<Ty<'tcx>>) {
fn default_span(&self, _: TyCtxt<'_>) -> Span {
DUMMY_SP
}
}
impl<'tcx> QueryKey for (ty::Instance<'tcx>, &'tcx ty::List<Ty<'tcx>>) {
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
self.0.default_span(tcx)
}
}
impl<'tcx> QueryKey for ty::Value<'tcx> {
fn default_span(&self, _: TyCtxt<'_>) -> Span {
DUMMY_SP
}
}
impl<'tcx> QueryKey for (LocalExpnId, &'tcx TokenStream) {
fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
self.0.expn_data().call_site
}
}
impl<'tcx> QueryKey for (ValidityRequirement, ty::PseudoCanonicalInput<'tcx, Ty<'tcx>>) {
// Just forward to `Ty<'tcx>`
fn default_span(&self, _: TyCtxt<'_>) -> Span {
DUMMY_SP
}
}
impl<'tcx> QueryKey for (ty::Instance<'tcx>, CollectionMode) {
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
self.0.default_span(tcx)
}
}
/// Gets a `DefId` associated with a type
///
/// Visited set is needed to avoid full iteration over
/// deeply nested tuples that have no DefId.
fn def_id_of_type_cached<'a>(ty: Ty<'a>, visited: &mut SsoHashSet<Ty<'a>>) -> Option<DefId> {
match *ty.kind() {
ty::Adt(adt_def, _) => Some(adt_def.did()),
ty::Dynamic(data, ..) => data.principal_def_id(),
ty::Pat(subty, _) | ty::Array(subty, _) | ty::Slice(subty) => {
def_id_of_type_cached(subty, visited)
}
ty::RawPtr(ty, _) => def_id_of_type_cached(ty, visited),
ty::Ref(_, ty, _) => def_id_of_type_cached(ty, visited),
ty::Tuple(tys) => tys.iter().find_map(|ty| {
if visited.insert(ty) {
return def_id_of_type_cached(ty, visited);
}
return None;
}),
ty::FnDef(def_id, _)
| ty::Closure(def_id, _)
| ty::CoroutineClosure(def_id, _)
| ty::Coroutine(def_id, _)
| ty::CoroutineWitness(def_id, _)
| ty::Foreign(def_id) => Some(def_id),
ty::Alias(alias) => Some(alias.kind.def_id()),
ty::Bool
| ty::Char
| ty::Int(_)
| ty::Uint(_)
| ty::Str
| ty::FnPtr(..)
| ty::UnsafeBinder(_)
| ty::Placeholder(..)
| ty::Param(_)
| ty::Infer(_)
| ty::Bound(..)
| ty::Error(_)
| ty::Never
| ty::Float(_) => None,
}
}
fn def_id_of_type(ty: Ty<'_>) -> Option<DefId> {
def_id_of_type_cached(ty, &mut SsoHashSet::new())
}