1use std::fmt;
2use std::hash::Hash;
3use std::marker::PhantomData;
4use std::ops::{ControlFlow, Deref};
5
6use derive_where::derive_where;
7#[cfg(feature = "nightly")]
8use rustc_macros::{Decodable_NoContext, Encodable_NoContext, StableHash, StableHash_NoContext};
9use rustc_type_ir_macros::{
10 GenericTypeVisitable, Lift_Generic, TypeFoldable_Generic, TypeVisitable_Generic,
11};
12use tracing::instrument;
13
14use crate::data_structures::SsoHashSet;
15use crate::fold::{FallibleTypeFolder, TypeFoldable, TypeFolder, TypeSuperFoldable};
16use crate::inherent::*;
17use crate::visit::{Flags, TypeSuperVisitable, TypeVisitable, TypeVisitableExt, TypeVisitor};
18use crate::{self as ty, DebruijnIndex, Interner, Region, UniverseIndex, Unnormalized};
19
20#[automatically_derived]
impl<I: Interner, T> ::core::fmt::Debug for Binder<I, T> where I: Interner,
T: ::core::fmt::Debug {
fn fmt(&self, __f: &mut ::core::fmt::Formatter<'_>)
-> ::core::fmt::Result {
match self {
Binder {
value: ref __field_value, bound_vars: ref __field_bound_vars }
=> {
let mut __builder =
::core::fmt::Formatter::debug_struct(__f, "Binder");
::core::fmt::DebugStruct::field(&mut __builder, "value",
__field_value);
::core::fmt::DebugStruct::field(&mut __builder, "bound_vars",
__field_bound_vars);
::core::fmt::DebugStruct::finish(&mut __builder)
}
}
}
}#[derive_where(Clone, Copy, Hash, PartialEq, Debug; I: Interner, T)]
27#[derive(GenericTypeVisitable, const _: () =
{
impl<I: Interner, T, J> ::rustc_type_ir::lift::Lift<J> for
Binder<I, T> where J: Interner, I: ::rustc_type_ir::LiftInto<J>,
T: ::rustc_type_ir::lift::Lift<J> {
type Lifted =
Binder<J, <T as ::rustc_type_ir::lift::Lift<J>>::Lifted>;
fn lift_to_interner(self, interner: J) -> Self::Lifted {
match self {
Binder { value: __binding_0, bound_vars: __binding_1 } => {
Binder {
value: __binding_0.lift_to_interner(interner),
bound_vars: __binding_1.lift_to_interner(interner),
}
}
}
}
}
};Lift_Generic)]
28#[cfg_attr(feature = "nightly", derive(const _: () =
{
impl<I: Interner, T> ::rustc_data_structures::stable_hash::StableHash
for Binder<I, T> where
T: ::rustc_data_structures::stable_hash::StableHash,
I::BoundVarKinds: ::rustc_data_structures::stable_hash::StableHash
{
#[inline]
fn stable_hash<__Hcx: ::rustc_data_structures::stable_hash::StableHashCtxt>(&self,
__hcx: &mut __Hcx,
__hasher:
&mut ::rustc_data_structures::stable_hash::StableHasher) {
match *self {
Binder { value: ref __binding_0, bound_vars: ref __binding_1
} => {
{ __binding_0.stable_hash(__hcx, __hasher); }
{ __binding_1.stable_hash(__hcx, __hasher); }
}
}
}
}
};StableHash_NoContext))]
29pub struct Binder<I: Interner, T> {
30 value: T,
31 bound_vars: I::BoundVarKinds,
32}
33
34impl<I: Interner, T: Eq> Eq for Binder<I, T> {}
35
36impl<I: Interner, T> Binder<I, T>
37where
38 T: TypeVisitable<I>,
39{
40 #[track_caller]
45 pub fn dummy(value: T) -> Binder<I, T> {
46 if !!value.has_escaping_bound_vars() {
{
::core::panicking::panic_fmt(format_args!("`{0:?}` has escaping bound vars, so it cannot be wrapped in a dummy binder.",
value));
}
};assert!(
47 !value.has_escaping_bound_vars(),
48 "`{value:?}` has escaping bound vars, so it cannot be wrapped in a dummy binder."
49 );
50 Binder { value, bound_vars: Default::default() }
51 }
52
53 pub fn bind_with_vars(value: T, bound_vars: I::BoundVarKinds) -> Binder<I, T> {
54 if truecfg!(debug_assertions) {
55 let mut validator = ValidateBoundVars::new(bound_vars);
56 let _ = value.visit_with(&mut validator);
57 }
58 Binder { value, bound_vars }
59 }
60}
61
62impl<I: Interner, T: TypeFoldable<I>> TypeFoldable<I> for Binder<I, T> {
63 fn try_fold_with<F: FallibleTypeFolder<I>>(self, folder: &mut F) -> Result<Self, F::Error> {
64 folder.try_fold_binder(self)
65 }
66
67 fn fold_with<F: TypeFolder<I>>(self, folder: &mut F) -> Self {
68 folder.fold_binder(self)
69 }
70}
71
72impl<I: Interner, T: TypeVisitable<I>> TypeVisitable<I> for Binder<I, T> {
73 fn visit_with<V: TypeVisitor<I>>(&self, visitor: &mut V) -> V::Result {
74 visitor.visit_binder(self)
75 }
76}
77
78impl<I: Interner, T: TypeFoldable<I>> TypeSuperFoldable<I> for Binder<I, T> {
79 fn try_super_fold_with<F: FallibleTypeFolder<I>>(
80 self,
81 folder: &mut F,
82 ) -> Result<Self, F::Error> {
83 self.try_map_bound(|t| t.try_fold_with(folder))
84 }
85
86 fn super_fold_with<F: TypeFolder<I>>(self, folder: &mut F) -> Self {
87 self.map_bound(|t| t.fold_with(folder))
88 }
89}
90
91impl<I: Interner, T: TypeVisitable<I>> TypeSuperVisitable<I> for Binder<I, T> {
92 fn super_visit_with<V: TypeVisitor<I>>(&self, visitor: &mut V) -> V::Result {
93 self.as_ref().skip_binder().visit_with(visitor)
94 }
95}
96
97impl<I: Interner, T> Binder<I, T> {
98 pub fn skip_binder(self) -> T {
112 self.value
113 }
114
115 pub fn bound_vars(&self) -> I::BoundVarKinds {
116 self.bound_vars
117 }
118
119 pub fn as_ref(&self) -> Binder<I, &T> {
120 Binder { value: &self.value, bound_vars: self.bound_vars }
121 }
122
123 pub fn as_deref(&self) -> Binder<I, &T::Target>
124 where
125 T: Deref,
126 {
127 Binder { value: &self.value, bound_vars: self.bound_vars }
128 }
129
130 pub fn map_bound_ref<F, U: TypeVisitable<I>>(&self, f: F) -> Binder<I, U>
131 where
132 F: FnOnce(&T) -> U,
133 {
134 self.as_ref().map_bound(f)
135 }
136
137 pub fn map_bound<F, U: TypeVisitable<I>>(self, f: F) -> Binder<I, U>
138 where
139 F: FnOnce(T) -> U,
140 {
141 let Binder { value, bound_vars } = self;
142 let value = f(value);
143 if truecfg!(debug_assertions) {
144 let mut validator = ValidateBoundVars::new(bound_vars);
145 let _ = value.visit_with(&mut validator);
146 }
147 Binder { value, bound_vars }
148 }
149
150 pub fn try_map_bound<F, U: TypeVisitable<I>, E>(self, f: F) -> Result<Binder<I, U>, E>
151 where
152 F: FnOnce(T) -> Result<U, E>,
153 {
154 let Binder { value, bound_vars } = self;
155 let value = f(value)?;
156 if truecfg!(debug_assertions) {
157 let mut validator = ValidateBoundVars::new(bound_vars);
158 let _ = value.visit_with(&mut validator);
159 }
160 Ok(Binder { value, bound_vars })
161 }
162
163 pub fn rebind<U>(&self, value: U) -> Binder<I, U>
173 where
174 U: TypeVisitable<I>,
175 {
176 Binder::bind_with_vars(value, self.bound_vars)
177 }
178
179 pub fn no_bound_vars(self) -> Option<T>
190 where
191 T: TypeVisitable<I>,
192 {
193 if self.value.has_escaping_bound_vars() { None } else { Some(self.skip_binder()) }
195 }
196}
197
198impl<I: Interner, T> Binder<I, Option<T>> {
199 pub fn transpose(self) -> Option<Binder<I, T>> {
200 let Binder { value, bound_vars } = self;
201 value.map(|value| Binder { value, bound_vars })
202 }
203}
204
205impl<I: Interner, T: IntoIterator> Binder<I, T> {
206 pub fn iter(self) -> impl Iterator<Item = Binder<I, T::Item>> {
207 let Binder { value, bound_vars } = self;
208 value.into_iter().map(move |value| Binder { value, bound_vars })
209 }
210}
211
212pub struct ValidateBoundVars<I: Interner> {
213 bound_vars: I::BoundVarKinds,
214 binder_index: ty::DebruijnIndex,
215 visited: SsoHashSet<(ty::DebruijnIndex, I::Ty)>,
219}
220
221impl<I: Interner> ValidateBoundVars<I> {
222 pub fn new(bound_vars: I::BoundVarKinds) -> Self {
223 ValidateBoundVars {
224 bound_vars,
225 binder_index: ty::INNERMOST,
226 visited: SsoHashSet::default(),
227 }
228 }
229}
230
231impl<I: Interner> TypeVisitor<I> for ValidateBoundVars<I> {
232 type Result = ControlFlow<()>;
233
234 fn visit_binder<T: TypeVisitable<I>>(&mut self, t: &Binder<I, T>) -> Self::Result {
235 self.binder_index.shift_in(1);
236 let result = t.super_visit_with(self);
237 self.binder_index.shift_out(1);
238 result
239 }
240
241 fn visit_ty(&mut self, t: I::Ty) -> Self::Result {
242 if t.outer_exclusive_binder() < self.binder_index
243 || !self.visited.insert((self.binder_index, t))
244 {
245 return ControlFlow::Break(());
246 }
247 match t.kind() {
248 ty::Bound(ty::BoundVarIndexKind::Bound(debruijn), bound_ty)
249 if debruijn == self.binder_index =>
250 {
251 let idx = bound_ty.var().as_usize();
252 if self.bound_vars.len() <= idx {
253 {
::core::panicking::panic_fmt(format_args!("Not enough bound vars: {0:?} not found in {1:?}",
t, self.bound_vars));
};panic!("Not enough bound vars: {:?} not found in {:?}", t, self.bound_vars);
254 }
255 bound_ty.assert_eq(self.bound_vars.get(idx).unwrap());
256 }
257 _ => {}
258 };
259
260 t.super_visit_with(self)
261 }
262
263 fn visit_const(&mut self, c: I::Const) -> Self::Result {
264 if c.outer_exclusive_binder() < self.binder_index {
265 return ControlFlow::Break(());
266 }
267 match c.kind() {
268 ty::ConstKind::Bound(debruijn, bound_const)
269 if debruijn == ty::BoundVarIndexKind::Bound(self.binder_index) =>
270 {
271 let idx = bound_const.var().as_usize();
272 if self.bound_vars.len() <= idx {
273 {
::core::panicking::panic_fmt(format_args!("Not enough bound vars: {0:?} not found in {1:?}",
c, self.bound_vars));
};panic!("Not enough bound vars: {:?} not found in {:?}", c, self.bound_vars);
274 }
275 bound_const.assert_eq(self.bound_vars.get(idx).unwrap());
276 }
277 _ => {}
278 };
279
280 c.super_visit_with(self)
281 }
282
283 fn visit_region(&mut self, r: Region<I>) -> Self::Result {
284 match r.kind() {
285 ty::ReBound(index, br) if index == ty::BoundVarIndexKind::Bound(self.binder_index) => {
286 let idx = br.var().as_usize();
287 if self.bound_vars.len() <= idx {
288 {
::core::panicking::panic_fmt(format_args!("Not enough bound vars: {0:?} not found in {1:?}",
r, self.bound_vars));
};panic!("Not enough bound vars: {:?} not found in {:?}", r, self.bound_vars);
289 }
290 br.assert_eq(self.bound_vars.get(idx).unwrap());
291 }
292
293 _ => (),
294 };
295
296 ControlFlow::Continue(())
297 }
298}
299
300#[automatically_derived]
impl<I: Interner, T> ::core::fmt::Debug for EarlyBinder<I, T> where
I: Interner, T: ::core::fmt::Debug {
fn fmt(&self, __f: &mut ::core::fmt::Formatter<'_>)
-> ::core::fmt::Result {
match self {
EarlyBinder { value: ref __field_value, _tcx: ref __field__tcx }
=> {
let mut __builder =
::core::fmt::Formatter::debug_struct(__f, "EarlyBinder");
::core::fmt::DebugStruct::field(&mut __builder, "value",
__field_value);
::core::fmt::DebugStruct::finish_non_exhaustive(&mut __builder)
}
}
}
}#[derive_where(Clone, Copy, PartialOrd, Ord, PartialEq, Hash, Debug; I: Interner, T)]
306#[derive(GenericTypeVisitable)]
307#[cfg_attr(
308 feature = "nightly",
309 derive(const _: () =
{
impl<I: Interner, T, __E: ::rustc_serialize::Encoder>
::rustc_serialize::Encodable<__E> for EarlyBinder<I, T> where
T: ::rustc_serialize::Encodable<__E>,
PhantomData<fn() -> I>: ::rustc_serialize::Encodable<__E> {
fn encode(&self, __encoder: &mut __E) {
match *self {
EarlyBinder { value: ref __binding_0, _tcx: ref __binding_1
} => {
::rustc_serialize::Encodable::<__E>::encode(__binding_0,
__encoder);
::rustc_serialize::Encodable::<__E>::encode(__binding_1,
__encoder);
}
}
}
}
};Encodable_NoContext, const _: () =
{
impl<I: Interner, T, __D: ::rustc_serialize::Decoder>
::rustc_serialize::Decodable<__D> for EarlyBinder<I, T> where
T: ::rustc_serialize::Decodable<__D>,
PhantomData<fn() -> I>: ::rustc_serialize::Decodable<__D> {
fn decode(__decoder: &mut __D) -> Self {
EarlyBinder {
value: ::rustc_serialize::Decodable::decode(__decoder),
_tcx: ::rustc_serialize::Decodable::decode(__decoder),
}
}
}
};Decodable_NoContext, const _: () =
{
impl<I: Interner, T> ::rustc_data_structures::stable_hash::StableHash
for EarlyBinder<I, T> where
T: ::rustc_data_structures::stable_hash::StableHash,
PhantomData<fn()
-> I>: ::rustc_data_structures::stable_hash::StableHash {
#[inline]
fn stable_hash<__Hcx: ::rustc_data_structures::stable_hash::StableHashCtxt>(&self,
__hcx: &mut __Hcx,
__hasher:
&mut ::rustc_data_structures::stable_hash::StableHasher) {
match *self {
EarlyBinder { value: ref __binding_0, _tcx: ref __binding_1
} => {
{ __binding_0.stable_hash(__hcx, __hasher); }
{ __binding_1.stable_hash(__hcx, __hasher); }
}
}
}
}
};StableHash_NoContext)
310)]
311pub struct EarlyBinder<I: Interner, T> {
312 value: T,
313 #[derive_where(skip(Debug))]
314 _tcx: PhantomData<fn() -> I>,
315}
316
317impl<I: Interner, T: Eq> Eq for EarlyBinder<I, T> {}
318
319#[cfg(feature = "nightly")]
321macro_rules! generate { ($( $tt:tt )*) => { $( $tt )* } }
322
323#[cfg(feature = "nightly")]
324generate!(
325 impl<I: Interner, T> !TypeFoldable<I> for ty::EarlyBinder<I, T> {}
327 impl<I: Interner, T> !TypeVisitable<I> for ty::EarlyBinder<I, T> {}
329);
330
331impl<I: Interner, T: TypeFoldable<I>> EarlyBinder<I, T> {
332 pub fn bind(cx: I, value: T) -> EarlyBinder<I, T> {
333 let value = ty::set_aliases_to_non_rigid(cx, value).skip_normalization();
335 EarlyBinder { value, _tcx: PhantomData }
336 }
337}
338
339impl<I: Interner, T: IntoIterator<Item: TypeVisitable<I>> + Clone> EarlyBinder<I, T> {
340 pub fn bind_iter(value: T) -> EarlyBinder<I, T> {
341 #[cfg(debug_assertions)]
342 {
343 value.clone().into_iter().for_each(|v| if !!v.has_rigid_aliases() {
::core::panicking::panic("assertion failed: !v.has_rigid_aliases()")
}assert!(!v.has_rigid_aliases()));
344 }
345
346 EarlyBinder { value, _tcx: PhantomData }
347 }
348}
349
350impl<I: Interner, T: TypeVisitable<I>> EarlyBinder<I, T> {
351 pub fn bind_no_rigid_aliases(value: T) -> EarlyBinder<I, T> {
352 if true {
if !!value.has_rigid_aliases() {
::core::panicking::panic("assertion failed: !value.has_rigid_aliases()")
};
};debug_assert!(!value.has_rigid_aliases());
353 EarlyBinder { value, _tcx: PhantomData }
354 }
355}
356
357impl<I: Interner, T> EarlyBinder<I, T> {
358 pub fn as_ref(&self) -> EarlyBinder<I, &T> {
359 EarlyBinder { value: &self.value, _tcx: PhantomData }
360 }
361
362 pub fn map_bound_ref<F, U>(&self, f: F) -> EarlyBinder<I, U>
363 where
364 F: FnOnce(&T) -> U,
365 {
366 self.as_ref().map_bound(f)
367 }
368
369 pub fn map_bound<F, U>(self, f: F) -> EarlyBinder<I, U>
370 where
371 F: FnOnce(T) -> U,
372 {
373 let value = f(self.value);
374 EarlyBinder { value, _tcx: PhantomData }
375 }
376
377 pub fn try_map_bound<F, U, E>(self, f: F) -> Result<EarlyBinder<I, U>, E>
378 where
379 F: FnOnce(T) -> Result<U, E>,
380 {
381 let value = f(self.value)?;
382 Ok(EarlyBinder { value, _tcx: PhantomData })
383 }
384
385 pub fn rebind<U>(&self, value: U) -> EarlyBinder<I, U> {
386 EarlyBinder { value, _tcx: PhantomData }
387 }
388
389 pub fn skip_binder(self) -> T {
406 self.value
407 }
408}
409
410impl<I: Interner> EarlyBinder<I, ty::TraitRef<I>> {
411 pub fn def_id(&self) -> I::TraitId {
412 self.value.def_id
413 }
414}
415
416impl<I: Interner, T> EarlyBinder<I, Option<T>> {
417 pub fn transpose(self) -> Option<EarlyBinder<I, T>> {
418 self.value.map(|value| EarlyBinder { value, _tcx: PhantomData })
419 }
420}
421
422impl<I: Interner, Iter: IntoIterator> EarlyBinder<I, Iter>
423where
424 Iter::Item: TypeFoldable<I>,
425{
426 pub fn iter_instantiated<A>(self, cx: I, args: A) -> IterInstantiated<I, Iter, A>
427 where
428 A: SliceLike<Item = I::GenericArg>,
429 {
430 IterInstantiated { it: self.value.into_iter(), cx, args }
431 }
432
433 pub fn iter_identity(self) -> impl Iterator<Item = Unnormalized<I, Iter::Item>> {
436 self.value.into_iter().map(Unnormalized::new)
437 }
438}
439
440pub struct IterInstantiated<I: Interner, Iter: IntoIterator, A> {
441 it: Iter::IntoIter,
442 cx: I,
443 args: A,
444}
445
446impl<I: Interner, Iter: IntoIterator, A> Iterator for IterInstantiated<I, Iter, A>
447where
448 Iter::Item: TypeFoldable<I>,
449 A: SliceLike<Item = I::GenericArg>,
450{
451 type Item = Unnormalized<I, Iter::Item>;
452
453 fn next(&mut self) -> Option<Self::Item> {
454 Some(
455 EarlyBinder { value: self.it.next()?, _tcx: PhantomData }
456 .instantiate(self.cx, self.args),
457 )
458 }
459
460 fn size_hint(&self) -> (usize, Option<usize>) {
461 self.it.size_hint()
462 }
463}
464
465impl<I: Interner, Iter: IntoIterator, A> DoubleEndedIterator for IterInstantiated<I, Iter, A>
466where
467 Iter::IntoIter: DoubleEndedIterator,
468 Iter::Item: TypeFoldable<I>,
469 A: SliceLike<Item = I::GenericArg>,
470{
471 fn next_back(&mut self) -> Option<Self::Item> {
472 Some(
473 EarlyBinder { value: self.it.next_back()?, _tcx: PhantomData }
474 .instantiate(self.cx, self.args),
475 )
476 }
477}
478
479impl<I: Interner, Iter: IntoIterator, A> ExactSizeIterator for IterInstantiated<I, Iter, A>
480where
481 Iter::IntoIter: ExactSizeIterator,
482 Iter::Item: TypeFoldable<I>,
483 A: SliceLike<Item = I::GenericArg>,
484{
485}
486
487impl<'s, I: Interner, Iter: IntoIterator> EarlyBinder<I, Iter>
488where
489 Iter::Item: Deref,
490 <Iter::Item as Deref>::Target: Copy + TypeFoldable<I>,
491{
492 pub fn iter_instantiated_copied(
493 self,
494 cx: I,
495 args: &'s [I::GenericArg],
496 ) -> IterInstantiatedCopied<'s, I, Iter> {
497 IterInstantiatedCopied { it: self.value.into_iter(), cx, args }
498 }
499
500 pub fn iter_identity_copied(self) -> IterIdentityCopied<I, Iter> {
503 IterIdentityCopied { it: self.value.into_iter(), _tcx: PhantomData }
504 }
505}
506
507pub struct IterInstantiatedCopied<'a, I: Interner, Iter: IntoIterator> {
508 it: Iter::IntoIter,
509 cx: I,
510 args: &'a [I::GenericArg],
511}
512
513impl<'a, I: Interner, Iter: IntoIterator<IntoIter: Clone>> Clone
514 for IterInstantiatedCopied<'a, I, Iter>
515{
516 fn clone(&self) -> IterInstantiatedCopied<'a, I, Iter> {
517 IterInstantiatedCopied { it: self.it.clone(), cx: self.cx, args: self.args }
518 }
519}
520
521impl<I: Interner, Iter: IntoIterator> Iterator for IterInstantiatedCopied<'_, I, Iter>
522where
523 Iter::Item: Deref,
524 <Iter::Item as Deref>::Target: Copy + TypeFoldable<I>,
525{
526 type Item = Unnormalized<I, <Iter::Item as Deref>::Target>;
527
528 fn next(&mut self) -> Option<Self::Item> {
529 self.it.next().map(|value| {
530 EarlyBinder { value: *value, _tcx: PhantomData }.instantiate(self.cx, self.args)
531 })
532 }
533
534 fn size_hint(&self) -> (usize, Option<usize>) {
535 self.it.size_hint()
536 }
537}
538
539impl<I: Interner, Iter: IntoIterator> DoubleEndedIterator for IterInstantiatedCopied<'_, I, Iter>
540where
541 Iter::IntoIter: DoubleEndedIterator,
542 Iter::Item: Deref,
543 <Iter::Item as Deref>::Target: Copy + TypeFoldable<I>,
544{
545 fn next_back(&mut self) -> Option<Self::Item> {
546 self.it.next_back().map(|value| {
547 EarlyBinder { value: *value, _tcx: PhantomData }.instantiate(self.cx, self.args)
548 })
549 }
550}
551
552impl<I: Interner, Iter: IntoIterator> ExactSizeIterator for IterInstantiatedCopied<'_, I, Iter>
553where
554 Iter::IntoIter: ExactSizeIterator,
555 Iter::Item: Deref,
556 <Iter::Item as Deref>::Target: Copy + TypeFoldable<I>,
557{
558}
559
560pub struct IterIdentityCopied<I: Interner, Iter: IntoIterator> {
561 it: Iter::IntoIter,
562 _tcx: PhantomData<fn() -> I>,
563}
564
565impl<I: Interner, Iter: IntoIterator<IntoIter: Clone>> Clone for IterIdentityCopied<I, Iter> {
566 fn clone(&self) -> IterIdentityCopied<I, Iter> {
567 IterIdentityCopied { it: self.it.clone(), _tcx: self._tcx }
568 }
569}
570
571impl<I: Interner, Iter: IntoIterator> Iterator for IterIdentityCopied<I, Iter>
572where
573 Iter::Item: Deref,
574 <Iter::Item as Deref>::Target: Copy,
575{
576 type Item = Unnormalized<I, <Iter::Item as Deref>::Target>;
577
578 fn next(&mut self) -> Option<Self::Item> {
579 self.it.next().map(|i| Unnormalized::new(*i))
580 }
581
582 fn size_hint(&self) -> (usize, Option<usize>) {
583 self.it.size_hint()
584 }
585}
586
587impl<I: Interner, Iter: IntoIterator> DoubleEndedIterator for IterIdentityCopied<I, Iter>
588where
589 Iter::IntoIter: DoubleEndedIterator,
590 Iter::Item: Deref,
591 <Iter::Item as Deref>::Target: Copy,
592{
593 fn next_back(&mut self) -> Option<Self::Item> {
594 self.it.next_back().map(|i| Unnormalized::new(*i))
595 }
596}
597
598impl<I: Interner, Iter: IntoIterator> ExactSizeIterator for IterIdentityCopied<I, Iter>
599where
600 Iter::IntoIter: ExactSizeIterator,
601 Iter::Item: Deref,
602 <Iter::Item as Deref>::Target: Copy,
603{
604}
605pub struct EarlyBinderIter<I, T> {
606 t: T,
607 _tcx: PhantomData<I>,
608}
609
610impl<I: Interner, T: IntoIterator> EarlyBinder<I, T> {
611 pub fn transpose_iter(self) -> EarlyBinderIter<I, T::IntoIter> {
612 EarlyBinderIter { t: self.value.into_iter(), _tcx: PhantomData }
613 }
614}
615
616impl<I: Interner, T: Iterator> Iterator for EarlyBinderIter<I, T> {
617 type Item = EarlyBinder<I, T::Item>;
618
619 fn next(&mut self) -> Option<Self::Item> {
620 self.t.next().map(|value| EarlyBinder { value, _tcx: PhantomData })
621 }
622
623 fn size_hint(&self) -> (usize, Option<usize>) {
624 self.t.size_hint()
625 }
626}
627
628impl<I: Interner, T: TypeFoldable<I>> ty::EarlyBinder<I, T> {
629 pub fn instantiate<A>(self, cx: I, args: A) -> Unnormalized<I, T>
630 where
631 A: SliceLike<Item = I::GenericArg>,
632 {
633 if args.is_empty() {
637 if !!self.value.has_param() {
{
::core::panicking::panic_fmt(format_args!("{0:?} has parameters, but no args were provided in instantiate",
self.value));
}
};assert!(
638 !self.value.has_param(),
639 "{:?} has parameters, but no args were provided in instantiate",
640 self.value,
641 );
642 return Unnormalized::new(self.value);
643 }
644 let mut folder = ArgFolder { cx, args: args.as_slice(), binders_passed: 0 };
645 Unnormalized::new(self.value.fold_with(&mut folder))
646 }
647
648 pub fn instantiate_identity(self) -> Unnormalized<I, T> {
657 Unnormalized::new(self.value)
666 }
667
668 pub fn no_bound_vars(self) -> Option<T> {
670 if !self.value.has_param() { Some(self.value) } else { None }
671 }
672}
673
674struct ArgFolder<'a, I: Interner> {
678 cx: I,
679 args: &'a [I::GenericArg],
680
681 binders_passed: u32,
683}
684
685impl<'a, I: Interner> TypeFolder<I> for ArgFolder<'a, I> {
686 #[inline]
687 fn cx(&self) -> I {
688 self.cx
689 }
690
691 fn fold_binder<T: TypeFoldable<I>>(&mut self, t: ty::Binder<I, T>) -> ty::Binder<I, T> {
692 self.binders_passed += 1;
693 let t = t.super_fold_with(self);
694 self.binders_passed -= 1;
695 t
696 }
697
698 fn fold_region(&mut self, r: Region<I>) -> Region<I> {
699 match r.kind() {
705 ty::ReEarlyParam(data) => {
706 let rk = self.args.get(data.index() as usize).map(|arg| arg.kind());
707 match rk {
708 Some(ty::GenericArgKind::Lifetime(lt)) => self.shift_region_through_binders(lt),
709 Some(other) => self.region_param_expected(data, r, other),
710 None => self.region_param_out_of_range(data, r),
711 }
712 }
713 ty::ReBound(..)
714 | ty::ReLateParam(_)
715 | ty::ReStatic
716 | ty::RePlaceholder(_)
717 | ty::ReErased
718 | ty::ReError(_) => r,
719 ty::ReVar(_) => { ::core::panicking::panic_fmt(format_args!("unexpected region: {0:?}", r)); }panic!("unexpected region: {r:?}"),
720 }
721 }
722
723 fn fold_ty(&mut self, t: I::Ty) -> I::Ty {
724 if !t.has_param() {
725 return t;
726 }
727
728 match t.kind() {
729 ty::Param(p) => self.ty_for_param(p, t),
730 _ => t.super_fold_with(self),
731 }
732 }
733
734 fn fold_const(&mut self, c: I::Const) -> I::Const {
735 if let ty::ConstKind::Param(p) = c.kind() {
736 self.const_for_param(p, c)
737 } else {
738 c.super_fold_with(self)
739 }
740 }
741
742 fn fold_predicate(&mut self, p: I::Predicate) -> I::Predicate {
743 if p.has_param() { p.super_fold_with(self) } else { p }
744 }
745
746 fn fold_clauses(&mut self, c: I::Clauses) -> I::Clauses {
747 if c.has_param() { c.super_fold_with(self) } else { c }
748 }
749}
750
751impl<'a, I: Interner> ArgFolder<'a, I> {
752 fn ty_for_param(&self, p: I::ParamTy, source_ty: I::Ty) -> I::Ty {
753 let opt_ty = self.args.get(p.index() as usize).map(|arg| arg.kind());
755 let ty = match opt_ty {
756 Some(ty::GenericArgKind::Type(ty)) => ty,
757 Some(kind) => self.type_param_expected(p, source_ty, kind),
758 None => self.type_param_out_of_range(p, source_ty),
759 };
760
761 self.shift_vars_through_binders(ty)
762 }
763
764 #[cold]
765 #[inline(never)]
766 fn type_param_expected(&self, p: I::ParamTy, ty: I::Ty, kind: ty::GenericArgKind<I>) -> ! {
767 {
::core::panicking::panic_fmt(format_args!("expected type for `{0:?}` ({1:?}/{2}) but found {3:?} when instantiating, args={4:?}",
p, ty, p.index(), kind, self.args));
}panic!(
768 "expected type for `{:?}` ({:?}/{}) but found {:?} when instantiating, args={:?}",
769 p,
770 ty,
771 p.index(),
772 kind,
773 self.args,
774 )
775 }
776
777 #[cold]
778 #[inline(never)]
779 fn type_param_out_of_range(&self, p: I::ParamTy, ty: I::Ty) -> ! {
780 {
::core::panicking::panic_fmt(format_args!("type parameter `{0:?}` ({1:?}/{2}) out of range when instantiating, args={3:?}",
p, ty, p.index(), self.args));
}panic!(
781 "type parameter `{:?}` ({:?}/{}) out of range when instantiating, args={:?}",
782 p,
783 ty,
784 p.index(),
785 self.args,
786 )
787 }
788
789 fn const_for_param(&self, p: I::ParamConst, source_ct: I::Const) -> I::Const {
790 let opt_ct = self.args.get(p.index() as usize).map(|arg| arg.kind());
792 let ct = match opt_ct {
793 Some(ty::GenericArgKind::Const(ct)) => ct,
794 Some(kind) => self.const_param_expected(p, source_ct, kind),
795 None => self.const_param_out_of_range(p, source_ct),
796 };
797
798 self.shift_vars_through_binders(ct)
799 }
800
801 #[cold]
802 #[inline(never)]
803 fn const_param_expected(
804 &self,
805 p: I::ParamConst,
806 ct: I::Const,
807 kind: ty::GenericArgKind<I>,
808 ) -> ! {
809 {
::core::panicking::panic_fmt(format_args!("expected const for `{0:?}` ({1:?}/{2}) but found {3:?} when instantiating args={4:?}",
p, ct, p.index(), kind, self.args));
}panic!(
810 "expected const for `{:?}` ({:?}/{}) but found {:?} when instantiating args={:?}",
811 p,
812 ct,
813 p.index(),
814 kind,
815 self.args,
816 )
817 }
818
819 #[cold]
820 #[inline(never)]
821 fn const_param_out_of_range(&self, p: I::ParamConst, ct: I::Const) -> ! {
822 {
::core::panicking::panic_fmt(format_args!("const parameter `{0:?}` ({1:?}/{2}) out of range when instantiating args={3:?}",
p, ct, p.index(), self.args));
}panic!(
823 "const parameter `{:?}` ({:?}/{}) out of range when instantiating args={:?}",
824 p,
825 ct,
826 p.index(),
827 self.args,
828 )
829 }
830
831 #[cold]
832 #[inline(never)]
833 fn region_param_expected(
834 &self,
835 ebr: I::EarlyParamRegion,
836 r: Region<I>,
837 kind: ty::GenericArgKind<I>,
838 ) -> ! {
839 {
::core::panicking::panic_fmt(format_args!("expected region for `{0:?}` ({1:?}/{2}) but found {3:?} when instantiating args={4:?}",
ebr, r, ebr.index(), kind, self.args));
}panic!(
840 "expected region for `{:?}` ({:?}/{}) but found {:?} when instantiating args={:?}",
841 ebr,
842 r,
843 ebr.index(),
844 kind,
845 self.args,
846 )
847 }
848
849 #[cold]
850 #[inline(never)]
851 fn region_param_out_of_range(&self, ebr: I::EarlyParamRegion, r: Region<I>) -> ! {
852 {
::core::panicking::panic_fmt(format_args!("region parameter `{0:?}` ({1:?}/{2}) out of range when instantiating args={3:?}",
ebr, r, ebr.index(), self.args));
}panic!(
853 "region parameter `{:?}` ({:?}/{}) out of range when instantiating args={:?}",
854 ebr,
855 r,
856 ebr.index(),
857 self.args,
858 )
859 }
860
861 x;#[instrument(level = "trace", skip(self), fields(binders_passed = self.binders_passed), ret)]
904 fn shift_vars_through_binders<T: TypeFoldable<I>>(&self, val: T) -> T {
905 if self.binders_passed == 0 || !val.has_escaping_bound_vars() {
906 val
907 } else {
908 ty::shift_vars(self.cx, val, self.binders_passed)
909 }
910 }
911
912 fn shift_region_through_binders(&self, region: Region<I>) -> Region<I> {
913 if self.binders_passed == 0 || !region.has_escaping_bound_vars() {
914 region
915 } else {
916 ty::shift_region(self.cx, region, self.binders_passed)
917 }
918 }
919}
920
921#[derive(#[automatically_derived]
impl ::core::clone::Clone for BoundVarIndexKind {
#[inline]
fn clone(&self) -> BoundVarIndexKind {
let _: ::core::clone::AssertParamIsClone<DebruijnIndex>;
*self
}
}Clone, #[automatically_derived]
impl ::core::marker::Copy for BoundVarIndexKind { }Copy, #[automatically_derived]
impl ::core::fmt::Debug for BoundVarIndexKind {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
match self {
BoundVarIndexKind::Bound(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f, "Bound",
&__self_0),
BoundVarIndexKind::Canonical =>
::core::fmt::Formatter::write_str(f, "Canonical"),
}
}
}Debug, #[automatically_derived]
impl ::core::cmp::PartialEq for BoundVarIndexKind {
#[inline]
fn eq(&self, other: &BoundVarIndexKind) -> bool {
let __self_discr = ::core::intrinsics::discriminant_value(self);
let __arg1_discr = ::core::intrinsics::discriminant_value(other);
__self_discr == __arg1_discr &&
match (self, other) {
(BoundVarIndexKind::Bound(__self_0),
BoundVarIndexKind::Bound(__arg1_0)) => __self_0 == __arg1_0,
_ => true,
}
}
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for BoundVarIndexKind {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {
let _: ::core::cmp::AssertParamIsEq<DebruijnIndex>;
}
}Eq, #[automatically_derived]
impl ::core::hash::Hash for BoundVarIndexKind {
#[inline]
fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
let __self_discr = ::core::intrinsics::discriminant_value(self);
::core::hash::Hash::hash(&__self_discr, state);
match self {
BoundVarIndexKind::Bound(__self_0) =>
::core::hash::Hash::hash(__self_0, state),
_ => {}
}
}
}Hash)]
941#[cfg_attr(feature = "nightly", derive(const _: () =
{
impl<__E: ::rustc_serialize::Encoder>
::rustc_serialize::Encodable<__E> for BoundVarIndexKind {
fn encode(&self, __encoder: &mut __E) {
let disc =
match *self {
BoundVarIndexKind::Bound(ref __binding_0) => { 0usize }
BoundVarIndexKind::Canonical => { 1usize }
};
::rustc_serialize::Encoder::emit_u8(__encoder, disc as u8);
match *self {
BoundVarIndexKind::Bound(ref __binding_0) => {
::rustc_serialize::Encodable::<__E>::encode(__binding_0,
__encoder);
}
BoundVarIndexKind::Canonical => {}
}
}
}
};Encodable_NoContext, const _: () =
{
impl<__D: ::rustc_serialize::Decoder>
::rustc_serialize::Decodable<__D> for BoundVarIndexKind {
fn decode(__decoder: &mut __D) -> Self {
match ::rustc_serialize::Decoder::read_u8(__decoder) as usize
{
0usize => {
BoundVarIndexKind::Bound(::rustc_serialize::Decodable::decode(__decoder))
}
1usize => { BoundVarIndexKind::Canonical }
n => {
::core::panicking::panic_fmt(format_args!("invalid enum variant tag while decoding `BoundVarIndexKind`, expected 0..2, actual {0}",
n));
}
}
}
}
};Decodable_NoContext, const _: () =
{
impl ::rustc_data_structures::stable_hash::StableHash for
BoundVarIndexKind {
#[inline]
fn stable_hash<__Hcx: ::rustc_data_structures::stable_hash::StableHashCtxt>(&self,
__hcx: &mut __Hcx,
__hasher:
&mut ::rustc_data_structures::stable_hash::StableHasher) {
::std::mem::discriminant(self).stable_hash(__hcx, __hasher);
match *self {
BoundVarIndexKind::Bound(ref __binding_0) => {
{ __binding_0.stable_hash(__hcx, __hasher); }
}
BoundVarIndexKind::Canonical => {}
}
}
}
};StableHash))]
942#[derive(const _: () =
{
impl<I> ::rustc_type_ir::TypeVisitable<I> for BoundVarIndexKind where
I: Interner {
fn visit_with<__V: ::rustc_type_ir::TypeVisitor<I>>(&self,
__visitor: &mut __V) -> __V::Result {
match *self {
BoundVarIndexKind::Bound(ref __binding_0) => {
{
match ::rustc_type_ir::VisitorResult::branch(::rustc_type_ir::TypeVisitable::visit_with(__binding_0,
__visitor)) {
::core::ops::ControlFlow::Continue(()) => {}
::core::ops::ControlFlow::Break(r) => {
return ::rustc_type_ir::VisitorResult::from_residual(r);
}
}
}
}
BoundVarIndexKind::Canonical => {}
}
<__V::Result as ::rustc_type_ir::VisitorResult>::output()
}
}
};TypeVisitable_Generic, GenericTypeVisitable, const _: () =
{
impl<I> ::rustc_type_ir::TypeFoldable<I> for BoundVarIndexKind where
I: Interner {
fn try_fold_with<__F: ::rustc_type_ir::FallibleTypeFolder<I>>(self,
__folder: &mut __F) -> Result<Self, __F::Error> {
Ok(match self {
BoundVarIndexKind::Bound(__binding_0) => {
BoundVarIndexKind::Bound(::rustc_type_ir::TypeFoldable::try_fold_with(__binding_0,
__folder)?)
}
BoundVarIndexKind::Canonical => {
BoundVarIndexKind::Canonical
}
})
}
fn fold_with<__F: ::rustc_type_ir::TypeFolder<I>>(self,
__folder: &mut __F) -> Self {
match self {
BoundVarIndexKind::Bound(__binding_0) => {
BoundVarIndexKind::Bound(::rustc_type_ir::TypeFoldable::fold_with(__binding_0,
__folder))
}
BoundVarIndexKind::Canonical => {
BoundVarIndexKind::Canonical
}
}
}
}
};TypeFoldable_Generic)]
943pub enum BoundVarIndexKind {
944 Bound(DebruijnIndex),
945 Canonical,
946}
947
948#[automatically_derived]
impl<I: Interner, T> ::core::hash::Hash for Placeholder<I, T> where
I: Interner, T: ::core::hash::Hash {
fn hash<__H: ::core::hash::Hasher>(&self, __state: &mut __H) {
match self {
Placeholder {
universe: ref __field_universe,
bound: ref __field_bound,
_tcx: ref __field__tcx } => {
::core::hash::Hash::hash(__field_universe, __state);
::core::hash::Hash::hash(__field_bound, __state);
::core::hash::Hash::hash(__field__tcx, __state);
}
}
}
}#[derive_where(Clone, Copy, PartialOrd, Ord, PartialEq, Eq, Hash; I: Interner, T)]
952#[derive(const _: () =
{
impl<I: Interner, T> ::rustc_type_ir::TypeVisitable<I> for
Placeholder<I, T> where I: Interner,
T: ::rustc_type_ir::TypeVisitable<I> {
fn visit_with<__V: ::rustc_type_ir::TypeVisitor<I>>(&self,
__visitor: &mut __V) -> __V::Result {
match *self {
Placeholder {
universe: ref __binding_0, bound: ref __binding_1, .. } => {
{
match ::rustc_type_ir::VisitorResult::branch(::rustc_type_ir::TypeVisitable::visit_with(__binding_0,
__visitor)) {
::core::ops::ControlFlow::Continue(()) => {}
::core::ops::ControlFlow::Break(r) => {
return ::rustc_type_ir::VisitorResult::from_residual(r);
}
}
}
{
match ::rustc_type_ir::VisitorResult::branch(::rustc_type_ir::TypeVisitable::visit_with(__binding_1,
__visitor)) {
::core::ops::ControlFlow::Continue(()) => {}
::core::ops::ControlFlow::Break(r) => {
return ::rustc_type_ir::VisitorResult::from_residual(r);
}
}
}
}
}
<__V::Result as ::rustc_type_ir::VisitorResult>::output()
}
}
};TypeVisitable_Generic, const _: () =
{
impl<I: Interner, T> ::rustc_type_ir::TypeFoldable<I> for
Placeholder<I, T> where I: Interner,
T: ::rustc_type_ir::TypeFoldable<I>,
T: ::rustc_type_ir::TypeFoldable<I> {
fn try_fold_with<__F: ::rustc_type_ir::FallibleTypeFolder<I>>(self,
__folder: &mut __F) -> Result<Self, __F::Error> {
Ok(match self {
Placeholder {
universe: __binding_0, bound: __binding_1, _tcx: __binding_2
} => {
Placeholder {
universe: ::rustc_type_ir::TypeFoldable::try_fold_with(__binding_0,
__folder)?,
bound: ::rustc_type_ir::TypeFoldable::try_fold_with(__binding_1,
__folder)?,
_tcx: __binding_2,
}
}
})
}
fn fold_with<__F: ::rustc_type_ir::TypeFolder<I>>(self,
__folder: &mut __F) -> Self {
match self {
Placeholder {
universe: __binding_0, bound: __binding_1, _tcx: __binding_2
} => {
Placeholder {
universe: ::rustc_type_ir::TypeFoldable::fold_with(__binding_0,
__folder),
bound: ::rustc_type_ir::TypeFoldable::fold_with(__binding_1,
__folder),
_tcx: __binding_2,
}
}
}
}
}
};TypeFoldable_Generic, GenericTypeVisitable, const _: () =
{
impl<I: Interner, T, J> ::rustc_type_ir::lift::Lift<J> for
Placeholder<I, T> where J: Interner,
I: ::rustc_type_ir::LiftInto<J>, T: ::rustc_type_ir::lift::Lift<J>
{
type Lifted =
Placeholder<J, <T as ::rustc_type_ir::lift::Lift<J>>::Lifted>;
fn lift_to_interner(self, interner: J) -> Self::Lifted {
match self {
Placeholder {
universe: __binding_0, bound: __binding_1, _tcx: __binding_2
} => {
Placeholder {
universe: __binding_0,
bound: __binding_1.lift_to_interner(interner),
_tcx: PhantomData,
}
}
}
}
}
};Lift_Generic)]
953#[cfg_attr(
954 feature = "nightly",
955 derive(const _: () =
{
impl<I: Interner, T, __E: ::rustc_serialize::Encoder>
::rustc_serialize::Encodable<__E> for Placeholder<I, T> where
T: ::rustc_serialize::Encodable<__E>,
PhantomData<fn() -> I>: ::rustc_serialize::Encodable<__E> {
fn encode(&self, __encoder: &mut __E) {
match *self {
Placeholder {
universe: ref __binding_0,
bound: ref __binding_1,
_tcx: ref __binding_2 } => {
::rustc_serialize::Encodable::<__E>::encode(__binding_0,
__encoder);
::rustc_serialize::Encodable::<__E>::encode(__binding_1,
__encoder);
::rustc_serialize::Encodable::<__E>::encode(__binding_2,
__encoder);
}
}
}
}
};Encodable_NoContext, const _: () =
{
impl<I: Interner, T, __D: ::rustc_serialize::Decoder>
::rustc_serialize::Decodable<__D> for Placeholder<I, T> where
T: ::rustc_serialize::Decodable<__D>,
PhantomData<fn() -> I>: ::rustc_serialize::Decodable<__D> {
fn decode(__decoder: &mut __D) -> Self {
Placeholder {
universe: ::rustc_serialize::Decodable::decode(__decoder),
bound: ::rustc_serialize::Decodable::decode(__decoder),
_tcx: ::rustc_serialize::Decodable::decode(__decoder),
}
}
}
};Decodable_NoContext, const _: () =
{
impl<I: Interner, T> ::rustc_data_structures::stable_hash::StableHash
for Placeholder<I, T> where
T: ::rustc_data_structures::stable_hash::StableHash,
PhantomData<fn()
-> I>: ::rustc_data_structures::stable_hash::StableHash {
#[inline]
fn stable_hash<__Hcx: ::rustc_data_structures::stable_hash::StableHashCtxt>(&self,
__hcx: &mut __Hcx,
__hasher:
&mut ::rustc_data_structures::stable_hash::StableHasher) {
match *self {
Placeholder {
universe: ref __binding_0,
bound: ref __binding_1,
_tcx: ref __binding_2 } => {
{ __binding_0.stable_hash(__hcx, __hasher); }
{ __binding_1.stable_hash(__hcx, __hasher); }
{ __binding_2.stable_hash(__hcx, __hasher); }
}
}
}
}
};StableHash_NoContext)
956)]
957pub struct Placeholder<I: Interner, T> {
958 #[lift(identity)]
959 pub universe: UniverseIndex,
960 pub bound: T,
961 #[type_foldable(identity)]
962 #[type_visitable(ignore)]
963 _tcx: PhantomData<fn() -> I>,
964}
965
966impl<I: Interner, T: fmt::Debug> fmt::Debug for ty::Placeholder<I, T> {
967 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
968 if self.universe == ty::UniverseIndex::ROOT {
969 f.write_fmt(format_args!("!{0:?}", self.bound))write!(f, "!{:?}", self.bound)
970 } else {
971 f.write_fmt(format_args!("!{0}_{1:?}", self.universe.index(), self.bound))write!(f, "!{}_{:?}", self.universe.index(), self.bound)
972 }
973 }
974}
975
976#[automatically_derived]
impl<I: Interner> ::core::hash::Hash for BoundRegionKind<I> where I: Interner
{
fn hash<__H: ::core::hash::Hasher>(&self, __state: &mut __H) {
match self {
BoundRegionKind::Anon => {
::core::hash::Hash::hash(&::core::mem::discriminant(self),
__state);
}
BoundRegionKind::NamedForPrinting(ref __field_0) => {
::core::hash::Hash::hash(&::core::mem::discriminant(self),
__state);
::core::hash::Hash::hash(__field_0, __state);
}
BoundRegionKind::Named(ref __field_0) => {
::core::hash::Hash::hash(&::core::mem::discriminant(self),
__state);
::core::hash::Hash::hash(__field_0, __state);
}
BoundRegionKind::ClosureEnv => {
::core::hash::Hash::hash(&::core::mem::discriminant(self),
__state);
}
}
}
}#[derive_where(Clone, Copy, PartialEq, Eq, Hash; I: Interner)]
977#[derive(const _: () =
{
impl<I: Interner, J> ::rustc_type_ir::lift::Lift<J> for
BoundRegionKind<I> where J: Interner,
I: ::rustc_type_ir::LiftInto<J> {
type Lifted = BoundRegionKind<J>;
fn lift_to_interner(self, interner: J) -> Self::Lifted {
match self {
BoundRegionKind::Anon => { BoundRegionKind::Anon }
BoundRegionKind::NamedForPrinting(__binding_0) => {
BoundRegionKind::NamedForPrinting(__binding_0.lift_to_interner(interner))
}
BoundRegionKind::Named(__binding_0) => {
BoundRegionKind::Named(__binding_0.lift_to_interner(interner))
}
BoundRegionKind::ClosureEnv => {
BoundRegionKind::ClosureEnv
}
}
}
}
};Lift_Generic, GenericTypeVisitable)]
978#[cfg_attr(
979 feature = "nightly",
980 derive(const _: () =
{
impl<I: Interner, __E: ::rustc_serialize::Encoder>
::rustc_serialize::Encodable<__E> for BoundRegionKind<I> where
I::Symbol: ::rustc_serialize::Encodable<__E>,
I::DefId: ::rustc_serialize::Encodable<__E> {
fn encode(&self, __encoder: &mut __E) {
let disc =
match *self {
BoundRegionKind::Anon => { 0usize }
BoundRegionKind::NamedForPrinting(ref __binding_0) => {
1usize
}
BoundRegionKind::Named(ref __binding_0) => { 2usize }
BoundRegionKind::ClosureEnv => { 3usize }
};
::rustc_serialize::Encoder::emit_u8(__encoder, disc as u8);
match *self {
BoundRegionKind::Anon => {}
BoundRegionKind::NamedForPrinting(ref __binding_0) => {
::rustc_serialize::Encodable::<__E>::encode(__binding_0,
__encoder);
}
BoundRegionKind::Named(ref __binding_0) => {
::rustc_serialize::Encodable::<__E>::encode(__binding_0,
__encoder);
}
BoundRegionKind::ClosureEnv => {}
}
}
}
};Encodable_NoContext, const _: () =
{
impl<I: Interner, __D: ::rustc_serialize::Decoder>
::rustc_serialize::Decodable<__D> for BoundRegionKind<I> where
I::Symbol: ::rustc_serialize::Decodable<__D>,
I::DefId: ::rustc_serialize::Decodable<__D> {
fn decode(__decoder: &mut __D) -> Self {
match ::rustc_serialize::Decoder::read_u8(__decoder) as usize
{
0usize => { BoundRegionKind::Anon }
1usize => {
BoundRegionKind::NamedForPrinting(::rustc_serialize::Decodable::decode(__decoder))
}
2usize => {
BoundRegionKind::Named(::rustc_serialize::Decodable::decode(__decoder))
}
3usize => { BoundRegionKind::ClosureEnv }
n => {
::core::panicking::panic_fmt(format_args!("invalid enum variant tag while decoding `BoundRegionKind`, expected 0..4, actual {0}",
n));
}
}
}
}
};Decodable_NoContext, const _: () =
{
impl<I: Interner> ::rustc_data_structures::stable_hash::StableHash for
BoundRegionKind<I> where
I::Symbol: ::rustc_data_structures::stable_hash::StableHash,
I::DefId: ::rustc_data_structures::stable_hash::StableHash {
#[inline]
fn stable_hash<__Hcx: ::rustc_data_structures::stable_hash::StableHashCtxt>(&self,
__hcx: &mut __Hcx,
__hasher:
&mut ::rustc_data_structures::stable_hash::StableHasher) {
::std::mem::discriminant(self).stable_hash(__hcx, __hasher);
match *self {
BoundRegionKind::Anon => {}
BoundRegionKind::NamedForPrinting(ref __binding_0) => {
{ __binding_0.stable_hash(__hcx, __hasher); }
}
BoundRegionKind::Named(ref __binding_0) => {
{ __binding_0.stable_hash(__hcx, __hasher); }
}
BoundRegionKind::ClosureEnv => {}
}
}
}
};StableHash_NoContext)
981)]
982pub enum BoundRegionKind<I: Interner> {
983 Anon,
985
986 NamedForPrinting(I::Symbol),
990
991 Named(I::DefId),
993
994 ClosureEnv,
997}
998
999impl<I: Interner> fmt::Debug for ty::BoundRegionKind<I> {
1000 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1001 match *self {
1002 ty::BoundRegionKind::Anon => f.write_fmt(format_args!("BrAnon"))write!(f, "BrAnon"),
1003 ty::BoundRegionKind::NamedForPrinting(name) => {
1004 f.write_fmt(format_args!("BrNamedForPrinting({0:?})", name))write!(f, "BrNamedForPrinting({:?})", name)
1005 }
1006 ty::BoundRegionKind::Named(did) => {
1007 f.write_fmt(format_args!("BrNamed({0:?})", did))write!(f, "BrNamed({did:?})")
1008 }
1009 ty::BoundRegionKind::ClosureEnv => f.write_fmt(format_args!("BrEnv"))write!(f, "BrEnv"),
1010 }
1011 }
1012}
1013
1014impl<I: Interner> BoundRegionKind<I> {
1015 pub fn is_named(&self, tcx: I) -> bool {
1016 self.get_name(tcx).is_some()
1017 }
1018
1019 pub fn get_name(&self, tcx: I) -> Option<I::Symbol> {
1020 match *self {
1021 ty::BoundRegionKind::Named(def_id) => {
1022 let name = tcx.item_name(def_id);
1023 if name.is_kw_underscore_lifetime() { None } else { Some(name) }
1024 }
1025 ty::BoundRegionKind::NamedForPrinting(name) => Some(name),
1026 _ => None,
1027 }
1028 }
1029
1030 pub fn get_id(&self) -> Option<I::DefId> {
1031 match *self {
1032 ty::BoundRegionKind::Named(id) => Some(id),
1033 _ => None,
1034 }
1035 }
1036}
1037
1038#[automatically_derived]
impl<I: Interner> ::core::hash::Hash for BoundTyKind<I> where I: Interner {
fn hash<__H: ::core::hash::Hasher>(&self, __state: &mut __H) {
match self {
BoundTyKind::Anon => {
::core::hash::Hash::hash(&::core::mem::discriminant(self),
__state);
}
BoundTyKind::Param(ref __field_0) => {
::core::hash::Hash::hash(&::core::mem::discriminant(self),
__state);
::core::hash::Hash::hash(__field_0, __state);
}
}
}
}#[derive_where(Clone, Copy, PartialEq, Eq, Debug, Hash; I: Interner)]
1039#[derive(const _: () =
{
impl<I: Interner, J> ::rustc_type_ir::lift::Lift<J> for BoundTyKind<I>
where J: Interner, I: ::rustc_type_ir::LiftInto<J> {
type Lifted = BoundTyKind<J>;
fn lift_to_interner(self, interner: J) -> Self::Lifted {
match self {
BoundTyKind::Anon => { BoundTyKind::Anon }
BoundTyKind::Param(__binding_0) => {
BoundTyKind::Param(__binding_0.lift_to_interner(interner))
}
}
}
}
};Lift_Generic, GenericTypeVisitable)]
1040#[cfg_attr(
1041 feature = "nightly",
1042 derive(const _: () =
{
impl<I: Interner, __E: ::rustc_serialize::Encoder>
::rustc_serialize::Encodable<__E> for BoundTyKind<I> where
I::DefId: ::rustc_serialize::Encodable<__E> {
fn encode(&self, __encoder: &mut __E) {
let disc =
match *self {
BoundTyKind::Anon => { 0usize }
BoundTyKind::Param(ref __binding_0) => { 1usize }
};
::rustc_serialize::Encoder::emit_u8(__encoder, disc as u8);
match *self {
BoundTyKind::Anon => {}
BoundTyKind::Param(ref __binding_0) => {
::rustc_serialize::Encodable::<__E>::encode(__binding_0,
__encoder);
}
}
}
}
};Encodable_NoContext, const _: () =
{
impl<I: Interner, __D: ::rustc_serialize::Decoder>
::rustc_serialize::Decodable<__D> for BoundTyKind<I> where
I::DefId: ::rustc_serialize::Decodable<__D> {
fn decode(__decoder: &mut __D) -> Self {
match ::rustc_serialize::Decoder::read_u8(__decoder) as usize
{
0usize => { BoundTyKind::Anon }
1usize => {
BoundTyKind::Param(::rustc_serialize::Decodable::decode(__decoder))
}
n => {
::core::panicking::panic_fmt(format_args!("invalid enum variant tag while decoding `BoundTyKind`, expected 0..2, actual {0}",
n));
}
}
}
}
};Decodable_NoContext, const _: () =
{
impl<I: Interner> ::rustc_data_structures::stable_hash::StableHash for
BoundTyKind<I> where
I::DefId: ::rustc_data_structures::stable_hash::StableHash {
#[inline]
fn stable_hash<__Hcx: ::rustc_data_structures::stable_hash::StableHashCtxt>(&self,
__hcx: &mut __Hcx,
__hasher:
&mut ::rustc_data_structures::stable_hash::StableHasher) {
::std::mem::discriminant(self).stable_hash(__hcx, __hasher);
match *self {
BoundTyKind::Anon => {}
BoundTyKind::Param(ref __binding_0) => {
{ __binding_0.stable_hash(__hcx, __hasher); }
}
}
}
}
};StableHash_NoContext)
1043)]
1044pub enum BoundTyKind<I: Interner> {
1045 Anon,
1046 Param(I::DefId),
1047}
1048
1049#[automatically_derived]
impl<I: Interner> ::core::hash::Hash for BoundVariableKind<I> where
I: Interner {
fn hash<__H: ::core::hash::Hasher>(&self, __state: &mut __H) {
match self {
BoundVariableKind::Ty(ref __field_0) => {
::core::hash::Hash::hash(&::core::mem::discriminant(self),
__state);
::core::hash::Hash::hash(__field_0, __state);
}
BoundVariableKind::Region(ref __field_0) => {
::core::hash::Hash::hash(&::core::mem::discriminant(self),
__state);
::core::hash::Hash::hash(__field_0, __state);
}
BoundVariableKind::Const => {
::core::hash::Hash::hash(&::core::mem::discriminant(self),
__state);
}
}
}
}#[derive_where(Clone, Copy, PartialEq, Eq, Debug, Hash; I: Interner)]
1050#[derive(const _: () =
{
impl<I: Interner, J> ::rustc_type_ir::lift::Lift<J> for
BoundVariableKind<I> where J: Interner,
I: ::rustc_type_ir::LiftInto<J> {
type Lifted = BoundVariableKind<J>;
fn lift_to_interner(self, interner: J) -> Self::Lifted {
match self {
BoundVariableKind::Ty(__binding_0) => {
BoundVariableKind::Ty(__binding_0.lift_to_interner(interner))
}
BoundVariableKind::Region(__binding_0) => {
BoundVariableKind::Region(__binding_0.lift_to_interner(interner))
}
BoundVariableKind::Const => { BoundVariableKind::Const }
}
}
}
};Lift_Generic, GenericTypeVisitable)]
1051#[cfg_attr(
1052 feature = "nightly",
1053 derive(const _: () =
{
impl<I: Interner, __E: ::rustc_serialize::Encoder>
::rustc_serialize::Encodable<__E> for BoundVariableKind<I> where
BoundTyKind<I>: ::rustc_serialize::Encodable<__E>,
BoundRegionKind<I>: ::rustc_serialize::Encodable<__E> {
fn encode(&self, __encoder: &mut __E) {
let disc =
match *self {
BoundVariableKind::Ty(ref __binding_0) => { 0usize }
BoundVariableKind::Region(ref __binding_0) => { 1usize }
BoundVariableKind::Const => { 2usize }
};
::rustc_serialize::Encoder::emit_u8(__encoder, disc as u8);
match *self {
BoundVariableKind::Ty(ref __binding_0) => {
::rustc_serialize::Encodable::<__E>::encode(__binding_0,
__encoder);
}
BoundVariableKind::Region(ref __binding_0) => {
::rustc_serialize::Encodable::<__E>::encode(__binding_0,
__encoder);
}
BoundVariableKind::Const => {}
}
}
}
};Encodable_NoContext, const _: () =
{
impl<I: Interner, __D: ::rustc_serialize::Decoder>
::rustc_serialize::Decodable<__D> for BoundVariableKind<I> where
BoundTyKind<I>: ::rustc_serialize::Decodable<__D>,
BoundRegionKind<I>: ::rustc_serialize::Decodable<__D> {
fn decode(__decoder: &mut __D) -> Self {
match ::rustc_serialize::Decoder::read_u8(__decoder) as usize
{
0usize => {
BoundVariableKind::Ty(::rustc_serialize::Decodable::decode(__decoder))
}
1usize => {
BoundVariableKind::Region(::rustc_serialize::Decodable::decode(__decoder))
}
2usize => { BoundVariableKind::Const }
n => {
::core::panicking::panic_fmt(format_args!("invalid enum variant tag while decoding `BoundVariableKind`, expected 0..3, actual {0}",
n));
}
}
}
}
};Decodable_NoContext, const _: () =
{
impl<I: Interner> ::rustc_data_structures::stable_hash::StableHash for
BoundVariableKind<I> where
BoundTyKind<I>: ::rustc_data_structures::stable_hash::StableHash,
BoundRegionKind<I>: ::rustc_data_structures::stable_hash::StableHash
{
#[inline]
fn stable_hash<__Hcx: ::rustc_data_structures::stable_hash::StableHashCtxt>(&self,
__hcx: &mut __Hcx,
__hasher:
&mut ::rustc_data_structures::stable_hash::StableHasher) {
::std::mem::discriminant(self).stable_hash(__hcx, __hasher);
match *self {
BoundVariableKind::Ty(ref __binding_0) => {
{ __binding_0.stable_hash(__hcx, __hasher); }
}
BoundVariableKind::Region(ref __binding_0) => {
{ __binding_0.stable_hash(__hcx, __hasher); }
}
BoundVariableKind::Const => {}
}
}
}
};StableHash_NoContext)
1054)]
1055pub enum BoundVariableKind<I: Interner> {
1056 Ty(BoundTyKind<I>),
1057 Region(BoundRegionKind<I>),
1058 Const,
1059}
1060
1061impl<I: Interner> BoundVariableKind<I> {
1062 pub fn expect_region(self) -> BoundRegionKind<I> {
1063 match self {
1064 BoundVariableKind::Region(lt) => lt,
1065 _ => {
::core::panicking::panic_fmt(format_args!("expected a region, but found another kind"));
}panic!("expected a region, but found another kind"),
1066 }
1067 }
1068
1069 pub fn expect_ty(self) -> BoundTyKind<I> {
1070 match self {
1071 BoundVariableKind::Ty(ty) => ty,
1072 _ => {
::core::panicking::panic_fmt(format_args!("expected a type, but found another kind"));
}panic!("expected a type, but found another kind"),
1073 }
1074 }
1075
1076 pub fn expect_const(self) {
1077 match self {
1078 BoundVariableKind::Const => (),
1079 _ => {
::core::panicking::panic_fmt(format_args!("expected a const, but found another kind"));
}panic!("expected a const, but found another kind"),
1080 }
1081 }
1082}
1083
1084#[automatically_derived]
impl<I: Interner> ::core::hash::Hash for BoundRegion<I> where I: Interner {
fn hash<__H: ::core::hash::Hasher>(&self, __state: &mut __H) {
match self {
BoundRegion { var: ref __field_var, kind: ref __field_kind } => {
::core::hash::Hash::hash(__field_var, __state);
::core::hash::Hash::hash(__field_kind, __state);
}
}
}
}#[derive_where(Clone, Copy, PartialEq, Eq, Hash; I: Interner)]
1085#[derive(GenericTypeVisitable, const _: () =
{
impl<I: Interner, J> ::rustc_type_ir::lift::Lift<J> for BoundRegion<I>
where J: Interner, I: ::rustc_type_ir::LiftInto<J> {
type Lifted = BoundRegion<J>;
fn lift_to_interner(self, interner: J) -> Self::Lifted {
match self {
BoundRegion { var: __binding_0, kind: __binding_1 } => {
BoundRegion {
var: __binding_0,
kind: __binding_1.lift_to_interner(interner),
}
}
}
}
}
};Lift_Generic)]
1086#[cfg_attr(
1087 feature = "nightly",
1088 derive(const _: () =
{
impl<I: Interner, __E: ::rustc_serialize::Encoder>
::rustc_serialize::Encodable<__E> for BoundRegion<I> where
BoundRegionKind<I>: ::rustc_serialize::Encodable<__E> {
fn encode(&self, __encoder: &mut __E) {
match *self {
BoundRegion { var: ref __binding_0, kind: ref __binding_1 }
=> {
::rustc_serialize::Encodable::<__E>::encode(__binding_0,
__encoder);
::rustc_serialize::Encodable::<__E>::encode(__binding_1,
__encoder);
}
}
}
}
};Encodable_NoContext, const _: () =
{
impl<I: Interner> ::rustc_data_structures::stable_hash::StableHash for
BoundRegion<I> where
BoundRegionKind<I>: ::rustc_data_structures::stable_hash::StableHash
{
#[inline]
fn stable_hash<__Hcx: ::rustc_data_structures::stable_hash::StableHashCtxt>(&self,
__hcx: &mut __Hcx,
__hasher:
&mut ::rustc_data_structures::stable_hash::StableHasher) {
match *self {
BoundRegion { var: ref __binding_0, kind: ref __binding_1 }
=> {
{ __binding_0.stable_hash(__hcx, __hasher); }
{ __binding_1.stable_hash(__hcx, __hasher); }
}
}
}
}
};StableHash_NoContext, const _: () =
{
impl<I: Interner, __D: ::rustc_serialize::Decoder>
::rustc_serialize::Decodable<__D> for BoundRegion<I> where
BoundRegionKind<I>: ::rustc_serialize::Decodable<__D> {
fn decode(__decoder: &mut __D) -> Self {
BoundRegion {
var: ::rustc_serialize::Decodable::decode(__decoder),
kind: ::rustc_serialize::Decodable::decode(__decoder),
}
}
}
};Decodable_NoContext)
1089)]
1090pub struct BoundRegion<I: Interner> {
1091 #[lift(identity)]
1092 pub var: ty::BoundVar,
1093 pub kind: BoundRegionKind<I>,
1094}
1095
1096impl<I: Interner> core::fmt::Debug for BoundRegion<I> {
1097 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1098 match self.kind {
1099 BoundRegionKind::Anon => f.write_fmt(format_args!("{0:?}", self.var))write!(f, "{:?}", self.var),
1100 BoundRegionKind::ClosureEnv => f.write_fmt(format_args!("{0:?}.Env", self.var))write!(f, "{:?}.Env", self.var),
1101 BoundRegionKind::Named(def) => {
1102 f.write_fmt(format_args!("{0:?}.Named({1:?})", self.var, def))write!(f, "{:?}.Named({:?})", self.var, def)
1103 }
1104 BoundRegionKind::NamedForPrinting(symbol) => {
1105 f.write_fmt(format_args!("{0:?}.NamedAnon({1:?})", self.var, symbol))write!(f, "{:?}.NamedAnon({:?})", self.var, symbol)
1106 }
1107 }
1108 }
1109}
1110
1111impl<I: Interner> BoundRegion<I> {
1112 pub fn var(self) -> ty::BoundVar {
1113 self.var
1114 }
1115
1116 pub fn assert_eq(self, var: BoundVariableKind<I>) {
1117 {
match (&self.kind, &var.expect_region()) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val,
&*right_val, ::core::option::Option::None);
}
}
}
}assert_eq!(self.kind, var.expect_region())
1118 }
1119}
1120
1121pub type PlaceholderRegion<I> = ty::Placeholder<I, BoundRegion<I>>;
1122
1123impl<I: Interner> PlaceholderRegion<I> {
1124 pub fn universe(self) -> UniverseIndex {
1125 self.universe
1126 }
1127
1128 pub fn var(self) -> ty::BoundVar {
1129 self.bound.var()
1130 }
1131
1132 pub fn with_updated_universe(self, ui: UniverseIndex) -> Self {
1133 Self { universe: ui, bound: self.bound, _tcx: PhantomData }
1134 }
1135
1136 pub fn new(ui: UniverseIndex, bound: BoundRegion<I>) -> Self {
1137 Self { universe: ui, bound, _tcx: PhantomData }
1138 }
1139
1140 pub fn new_anon(ui: UniverseIndex, var: ty::BoundVar) -> Self {
1141 let bound = BoundRegion { var, kind: BoundRegionKind::Anon };
1142 Self { universe: ui, bound, _tcx: PhantomData }
1143 }
1144}
1145
1146#[automatically_derived]
impl<I: Interner> ::core::hash::Hash for BoundTy<I> where I: Interner {
fn hash<__H: ::core::hash::Hasher>(&self, __state: &mut __H) {
match self {
BoundTy { var: ref __field_var, kind: ref __field_kind } => {
::core::hash::Hash::hash(__field_var, __state);
::core::hash::Hash::hash(__field_kind, __state);
}
}
}
}#[derive_where(Clone, Copy, PartialEq, Eq, Hash; I: Interner)]
1147#[derive(GenericTypeVisitable, const _: () =
{
impl<I: Interner, J> ::rustc_type_ir::lift::Lift<J> for BoundTy<I>
where J: Interner, I: ::rustc_type_ir::LiftInto<J> {
type Lifted = BoundTy<J>;
fn lift_to_interner(self, interner: J) -> Self::Lifted {
match self {
BoundTy { var: __binding_0, kind: __binding_1 } => {
BoundTy {
var: __binding_0,
kind: __binding_1.lift_to_interner(interner),
}
}
}
}
}
};Lift_Generic)]
1148#[cfg_attr(
1149 feature = "nightly",
1150 derive(const _: () =
{
impl<I: Interner, __E: ::rustc_serialize::Encoder>
::rustc_serialize::Encodable<__E> for BoundTy<I> where
BoundTyKind<I>: ::rustc_serialize::Encodable<__E> {
fn encode(&self, __encoder: &mut __E) {
match *self {
BoundTy { var: ref __binding_0, kind: ref __binding_1 } => {
::rustc_serialize::Encodable::<__E>::encode(__binding_0,
__encoder);
::rustc_serialize::Encodable::<__E>::encode(__binding_1,
__encoder);
}
}
}
}
};Encodable_NoContext, const _: () =
{
impl<I: Interner, __D: ::rustc_serialize::Decoder>
::rustc_serialize::Decodable<__D> for BoundTy<I> where
BoundTyKind<I>: ::rustc_serialize::Decodable<__D> {
fn decode(__decoder: &mut __D) -> Self {
BoundTy {
var: ::rustc_serialize::Decodable::decode(__decoder),
kind: ::rustc_serialize::Decodable::decode(__decoder),
}
}
}
};Decodable_NoContext, const _: () =
{
impl<I: Interner> ::rustc_data_structures::stable_hash::StableHash for
BoundTy<I> where
BoundTyKind<I>: ::rustc_data_structures::stable_hash::StableHash {
#[inline]
fn stable_hash<__Hcx: ::rustc_data_structures::stable_hash::StableHashCtxt>(&self,
__hcx: &mut __Hcx,
__hasher:
&mut ::rustc_data_structures::stable_hash::StableHasher) {
match *self {
BoundTy { var: ref __binding_0, kind: ref __binding_1 } => {
{ __binding_0.stable_hash(__hcx, __hasher); }
{ __binding_1.stable_hash(__hcx, __hasher); }
}
}
}
}
};StableHash_NoContext)
1151)]
1152pub struct BoundTy<I: Interner> {
1153 #[lift(identity)]
1154 pub var: ty::BoundVar,
1155 pub kind: BoundTyKind<I>,
1156}
1157
1158impl<I: Interner> fmt::Debug for ty::BoundTy<I> {
1159 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1160 match self.kind {
1161 ty::BoundTyKind::Anon => f.write_fmt(format_args!("{0:?}", self.var))write!(f, "{:?}", self.var),
1162 ty::BoundTyKind::Param(def_id) => f.write_fmt(format_args!("{0:?}", def_id))write!(f, "{def_id:?}"),
1163 }
1164 }
1165}
1166
1167impl<I: Interner> BoundTy<I> {
1168 pub fn var(self) -> ty::BoundVar {
1169 self.var
1170 }
1171
1172 pub fn assert_eq(self, var: BoundVariableKind<I>) {
1173 {
match (&self.kind, &var.expect_ty()) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val,
&*right_val, ::core::option::Option::None);
}
}
}
}assert_eq!(self.kind, var.expect_ty())
1174 }
1175}
1176
1177pub type PlaceholderType<I> = ty::Placeholder<I, BoundTy<I>>;
1178
1179impl<I: Interner> PlaceholderType<I> {
1180 pub fn universe(self) -> UniverseIndex {
1181 self.universe
1182 }
1183
1184 pub fn var(self) -> ty::BoundVar {
1185 self.bound.var
1186 }
1187
1188 pub fn with_updated_universe(self, ui: UniverseIndex) -> Self {
1189 Self { universe: ui, bound: self.bound, _tcx: PhantomData }
1190 }
1191
1192 pub fn new(ui: UniverseIndex, bound: BoundTy<I>) -> Self {
1193 Self { universe: ui, bound, _tcx: PhantomData }
1194 }
1195
1196 pub fn new_anon(ui: UniverseIndex, var: ty::BoundVar) -> Self {
1197 let bound = BoundTy { var, kind: BoundTyKind::Anon };
1198 Self { universe: ui, bound, _tcx: PhantomData }
1199 }
1200}
1201
1202#[automatically_derived]
impl<I: Interner> ::core::hash::Hash for BoundConst<I> where I: Interner {
fn hash<__H: ::core::hash::Hasher>(&self, __state: &mut __H) {
match self {
BoundConst { var: ref __field_var, _tcx: ref __field__tcx } => {
::core::hash::Hash::hash(__field_var, __state);
::core::hash::Hash::hash(__field__tcx, __state);
}
}
}
}#[derive_where(Clone, Copy, PartialEq, Debug, Eq, Hash; I: Interner)]
1203#[derive(GenericTypeVisitable)]
1204#[cfg_attr(
1205 feature = "nightly",
1206 derive(const _: () =
{
impl<I: Interner, __E: ::rustc_serialize::Encoder>
::rustc_serialize::Encodable<__E> for BoundConst<I> where
PhantomData<fn() -> I>: ::rustc_serialize::Encodable<__E> {
fn encode(&self, __encoder: &mut __E) {
match *self {
BoundConst { var: ref __binding_0, _tcx: ref __binding_1 }
=> {
::rustc_serialize::Encodable::<__E>::encode(__binding_0,
__encoder);
::rustc_serialize::Encodable::<__E>::encode(__binding_1,
__encoder);
}
}
}
}
};Encodable_NoContext, const _: () =
{
impl<I: Interner, __D: ::rustc_serialize::Decoder>
::rustc_serialize::Decodable<__D> for BoundConst<I> where
PhantomData<fn() -> I>: ::rustc_serialize::Decodable<__D> {
fn decode(__decoder: &mut __D) -> Self {
BoundConst {
var: ::rustc_serialize::Decodable::decode(__decoder),
_tcx: ::rustc_serialize::Decodable::decode(__decoder),
}
}
}
};Decodable_NoContext, const _: () =
{
impl<I: Interner> ::rustc_data_structures::stable_hash::StableHash for
BoundConst<I> where
PhantomData<fn()
-> I>: ::rustc_data_structures::stable_hash::StableHash {
#[inline]
fn stable_hash<__Hcx: ::rustc_data_structures::stable_hash::StableHashCtxt>(&self,
__hcx: &mut __Hcx,
__hasher:
&mut ::rustc_data_structures::stable_hash::StableHasher) {
match *self {
BoundConst { var: ref __binding_0, _tcx: ref __binding_1 }
=> {
{ __binding_0.stable_hash(__hcx, __hasher); }
{ __binding_1.stable_hash(__hcx, __hasher); }
}
}
}
}
};StableHash_NoContext)
1207)]
1208pub struct BoundConst<I: Interner> {
1209 pub var: ty::BoundVar,
1210 #[derive_where(skip(Debug))]
1211 pub _tcx: PhantomData<fn() -> I>,
1212}
1213
1214impl<I: Interner> BoundConst<I> {
1215 pub fn var(self) -> ty::BoundVar {
1216 self.var
1217 }
1218
1219 pub fn assert_eq(self, var: BoundVariableKind<I>) {
1220 var.expect_const()
1221 }
1222
1223 pub fn new(var: ty::BoundVar) -> Self {
1224 Self { var, _tcx: PhantomData }
1225 }
1226}
1227
1228pub type PlaceholderConst<I> = ty::Placeholder<I, BoundConst<I>>;
1229
1230impl<I: Interner> PlaceholderConst<I> {
1231 pub fn universe(self) -> UniverseIndex {
1232 self.universe
1233 }
1234
1235 pub fn var(self) -> ty::BoundVar {
1236 self.bound.var
1237 }
1238
1239 pub fn with_updated_universe(self, ui: UniverseIndex) -> Self {
1240 Self { universe: ui, bound: self.bound, _tcx: PhantomData }
1241 }
1242
1243 pub fn new(ui: UniverseIndex, bound: BoundConst<I>) -> Self {
1244 Self { universe: ui, bound, _tcx: PhantomData }
1245 }
1246
1247 pub fn new_anon(ui: UniverseIndex, var: ty::BoundVar) -> Self {
1248 let bound = BoundConst::new(var);
1249 Self { universe: ui, bound, _tcx: PhantomData }
1250 }
1251
1252 pub fn find_const_ty_from_env(self, env: I::ParamEnv) -> I::Ty {
1253 let mut candidates = env.caller_bounds().iter().filter_map(|clause| {
1254 match clause.kind().skip_binder() {
1256 ty::ClauseKind::ConstArgHasType(placeholder_ct, ty) => {
1257 if !!(placeholder_ct, ty).has_escaping_bound_vars() {
::core::panicking::panic("assertion failed: !(placeholder_ct, ty).has_escaping_bound_vars()")
};assert!(!(placeholder_ct, ty).has_escaping_bound_vars());
1258
1259 match placeholder_ct.kind() {
1260 ty::ConstKind::Placeholder(placeholder_ct) if placeholder_ct == self => {
1261 Some(ty)
1262 }
1263 _ => None,
1264 }
1265 }
1266 _ => None,
1267 }
1268 });
1269
1270 let ty = candidates.next().unwrap_or_else(|| {
1277 {
::core::panicking::panic_fmt(format_args!("cannot find `{0:?}` in param-env: {1:#?}",
self, env));
};panic!("cannot find `{self:?}` in param-env: {env:#?}");
1278 });
1279 if !candidates.next().is_none() {
{
::core::panicking::panic_fmt(format_args!("did not expect duplicate `ConstParamHasTy` for `{0:?}` in param-env: {1:#?}",
self, env));
}
};assert!(
1280 candidates.next().is_none(),
1281 "did not expect duplicate `ConstParamHasTy` for `{self:?}` in param-env: {env:#?}"
1282 );
1283 ty
1284 }
1285}