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, HashStable_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::lift::Lift;
18use crate::visit::{Flags, TypeSuperVisitable, TypeVisitable, TypeVisitableExt, TypeVisitor};
19use crate::{self as ty, DebruijnIndex, Interner, UniverseIndex};
20
21#[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(Copy; I: Interner, T: Copy)]
33#[derive_where(Clone, Hash, PartialEq, Debug; I: Interner, T)]
34#[derive(GenericTypeVisitable)]
35#[cfg_attr(feature = "nightly", derive(const _: () =
{
impl<I: Interner, T, __CTX>
::rustc_data_structures::stable_hasher::HashStable<__CTX> for
Binder<I, T> where
T: ::rustc_data_structures::stable_hasher::HashStable<__CTX>,
I::BoundVarKinds: ::rustc_data_structures::stable_hasher::HashStable<__CTX>
{
#[inline]
fn hash_stable(&self, __hcx: &mut __CTX,
__hasher:
&mut ::rustc_data_structures::stable_hasher::StableHasher) {
match *self {
Binder { value: ref __binding_0, bound_vars: ref __binding_1
} => {
{ __binding_0.hash_stable(__hcx, __hasher); }
{ __binding_1.hash_stable(__hcx, __hasher); }
}
}
}
}
};HashStable_NoContext))]
36pub struct Binder<I: Interner, T> {
37 value: T,
38 bound_vars: I::BoundVarKinds,
39}
40
41impl<I: Interner, T: Eq> Eq for Binder<I, T> {}
42
43impl<I: Interner, U: Interner, T> Lift<U> for Binder<I, T>
46where
47 T: Lift<U>,
48 I::BoundVarKinds: Lift<U, Lifted = U::BoundVarKinds>,
49{
50 type Lifted = Binder<U, T::Lifted>;
51
52 fn lift_to_interner(self, cx: U) -> Option<Self::Lifted> {
53 Some(Binder {
54 value: self.value.lift_to_interner(cx)?,
55 bound_vars: self.bound_vars.lift_to_interner(cx)?,
56 })
57 }
58}
59
60#[cfg(feature = "nightly")]
61macro_rules! impl_binder_encode_decode {
62 ($($t:ty),+ $(,)?) => {
63 $(
64 impl<I: Interner, E: rustc_serialize::Encoder> rustc_serialize::Encodable<E> for ty::Binder<I, $t>
65 where
66 $t: rustc_serialize::Encodable<E>,
67 I::BoundVarKinds: rustc_serialize::Encodable<E>,
68 {
69 fn encode(&self, e: &mut E) {
70 self.bound_vars().encode(e);
71 self.as_ref().skip_binder().encode(e);
72 }
73 }
74 impl<I: Interner, D: rustc_serialize::Decoder> rustc_serialize::Decodable<D> for ty::Binder<I, $t>
75 where
76 $t: TypeVisitable<I> + rustc_serialize::Decodable<D>,
77 I::BoundVarKinds: rustc_serialize::Decodable<D>,
78 {
79 fn decode(decoder: &mut D) -> Self {
80 let bound_vars = rustc_serialize::Decodable::decode(decoder);
81 ty::Binder::bind_with_vars(rustc_serialize::Decodable::decode(decoder), bound_vars)
82 }
83 }
84 )*
85 }
86}
87
88#[cfg(feature = "nightly")]
89impl<I: Interner, E: rustc_serialize::Encoder> rustc_serialize::Encodable<E>
for ty::Binder<I, ty::HostEffectPredicate<I>> where
ty::HostEffectPredicate<I>: rustc_serialize::Encodable<E>,
I::BoundVarKinds: rustc_serialize::Encodable<E> {
fn encode(&self, e: &mut E) {
self.bound_vars().encode(e);
self.as_ref().skip_binder().encode(e);
}
}
impl<I: Interner, D: rustc_serialize::Decoder> rustc_serialize::Decodable<D>
for ty::Binder<I, ty::HostEffectPredicate<I>> where
ty::HostEffectPredicate<I>: TypeVisitable<I> +
rustc_serialize::Decodable<D>,
I::BoundVarKinds: rustc_serialize::Decodable<D> {
fn decode(decoder: &mut D) -> Self {
let bound_vars = rustc_serialize::Decodable::decode(decoder);
ty::Binder::bind_with_vars(rustc_serialize::Decodable::decode(decoder),
bound_vars)
}
}impl_binder_encode_decode! {
90 ty::FnSig<I>,
91 ty::FnSigTys<I>,
92 ty::TraitPredicate<I>,
93 ty::ExistentialPredicate<I>,
94 ty::TraitRef<I>,
95 ty::ExistentialTraitRef<I>,
96 ty::HostEffectPredicate<I>,
97}
98
99impl<I: Interner, T> Binder<I, T>
100where
101 T: TypeVisitable<I>,
102{
103 #[track_caller]
108 pub fn dummy(value: T) -> Binder<I, T> {
109 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!(
110 !value.has_escaping_bound_vars(),
111 "`{value:?}` has escaping bound vars, so it cannot be wrapped in a dummy binder."
112 );
113 Binder { value, bound_vars: Default::default() }
114 }
115
116 pub fn bind_with_vars(value: T, bound_vars: I::BoundVarKinds) -> Binder<I, T> {
117 if truecfg!(debug_assertions) {
118 let mut validator = ValidateBoundVars::new(bound_vars);
119 let _ = value.visit_with(&mut validator);
120 }
121 Binder { value, bound_vars }
122 }
123}
124
125impl<I: Interner, T: TypeFoldable<I>> TypeFoldable<I> for Binder<I, T> {
126 fn try_fold_with<F: FallibleTypeFolder<I>>(self, folder: &mut F) -> Result<Self, F::Error> {
127 folder.try_fold_binder(self)
128 }
129
130 fn fold_with<F: TypeFolder<I>>(self, folder: &mut F) -> Self {
131 folder.fold_binder(self)
132 }
133}
134
135impl<I: Interner, T: TypeVisitable<I>> TypeVisitable<I> for Binder<I, T> {
136 fn visit_with<V: TypeVisitor<I>>(&self, visitor: &mut V) -> V::Result {
137 visitor.visit_binder(self)
138 }
139}
140
141impl<I: Interner, T: TypeFoldable<I>> TypeSuperFoldable<I> for Binder<I, T> {
142 fn try_super_fold_with<F: FallibleTypeFolder<I>>(
143 self,
144 folder: &mut F,
145 ) -> Result<Self, F::Error> {
146 self.try_map_bound(|t| t.try_fold_with(folder))
147 }
148
149 fn super_fold_with<F: TypeFolder<I>>(self, folder: &mut F) -> Self {
150 self.map_bound(|t| t.fold_with(folder))
151 }
152}
153
154impl<I: Interner, T: TypeVisitable<I>> TypeSuperVisitable<I> for Binder<I, T> {
155 fn super_visit_with<V: TypeVisitor<I>>(&self, visitor: &mut V) -> V::Result {
156 self.as_ref().skip_binder().visit_with(visitor)
157 }
158}
159
160impl<I: Interner, T> Binder<I, T> {
161 pub fn skip_binder(self) -> T {
175 self.value
176 }
177
178 pub fn bound_vars(&self) -> I::BoundVarKinds {
179 self.bound_vars
180 }
181
182 pub fn as_ref(&self) -> Binder<I, &T> {
183 Binder { value: &self.value, bound_vars: self.bound_vars }
184 }
185
186 pub fn as_deref(&self) -> Binder<I, &T::Target>
187 where
188 T: Deref,
189 {
190 Binder { value: &self.value, bound_vars: self.bound_vars }
191 }
192
193 pub fn map_bound_ref<F, U: TypeVisitable<I>>(&self, f: F) -> Binder<I, U>
194 where
195 F: FnOnce(&T) -> U,
196 {
197 self.as_ref().map_bound(f)
198 }
199
200 pub fn map_bound<F, U: TypeVisitable<I>>(self, f: F) -> Binder<I, U>
201 where
202 F: FnOnce(T) -> U,
203 {
204 let Binder { value, bound_vars } = self;
205 let value = f(value);
206 if truecfg!(debug_assertions) {
207 let mut validator = ValidateBoundVars::new(bound_vars);
208 let _ = value.visit_with(&mut validator);
209 }
210 Binder { value, bound_vars }
211 }
212
213 pub fn try_map_bound<F, U: TypeVisitable<I>, E>(self, f: F) -> Result<Binder<I, U>, E>
214 where
215 F: FnOnce(T) -> Result<U, E>,
216 {
217 let Binder { value, bound_vars } = self;
218 let value = f(value)?;
219 if truecfg!(debug_assertions) {
220 let mut validator = ValidateBoundVars::new(bound_vars);
221 let _ = value.visit_with(&mut validator);
222 }
223 Ok(Binder { value, bound_vars })
224 }
225
226 pub fn rebind<U>(&self, value: U) -> Binder<I, U>
236 where
237 U: TypeVisitable<I>,
238 {
239 Binder::bind_with_vars(value, self.bound_vars)
240 }
241
242 pub fn no_bound_vars(self) -> Option<T>
253 where
254 T: TypeVisitable<I>,
255 {
256 if self.value.has_escaping_bound_vars() { None } else { Some(self.skip_binder()) }
258 }
259}
260
261impl<I: Interner, T> Binder<I, Option<T>> {
262 pub fn transpose(self) -> Option<Binder<I, T>> {
263 let Binder { value, bound_vars } = self;
264 value.map(|value| Binder { value, bound_vars })
265 }
266}
267
268impl<I: Interner, T: IntoIterator> Binder<I, T> {
269 pub fn iter(self) -> impl Iterator<Item = Binder<I, T::Item>> {
270 let Binder { value, bound_vars } = self;
271 value.into_iter().map(move |value| Binder { value, bound_vars })
272 }
273}
274
275pub struct ValidateBoundVars<I: Interner> {
276 bound_vars: I::BoundVarKinds,
277 binder_index: ty::DebruijnIndex,
278 visited: SsoHashSet<(ty::DebruijnIndex, I::Ty)>,
282}
283
284impl<I: Interner> ValidateBoundVars<I> {
285 pub fn new(bound_vars: I::BoundVarKinds) -> Self {
286 ValidateBoundVars {
287 bound_vars,
288 binder_index: ty::INNERMOST,
289 visited: SsoHashSet::default(),
290 }
291 }
292}
293
294impl<I: Interner> TypeVisitor<I> for ValidateBoundVars<I> {
295 type Result = ControlFlow<()>;
296
297 fn visit_binder<T: TypeVisitable<I>>(&mut self, t: &Binder<I, T>) -> Self::Result {
298 self.binder_index.shift_in(1);
299 let result = t.super_visit_with(self);
300 self.binder_index.shift_out(1);
301 result
302 }
303
304 fn visit_ty(&mut self, t: I::Ty) -> Self::Result {
305 if t.outer_exclusive_binder() < self.binder_index
306 || !self.visited.insert((self.binder_index, t))
307 {
308 return ControlFlow::Break(());
309 }
310 match t.kind() {
311 ty::Bound(ty::BoundVarIndexKind::Bound(debruijn), bound_ty)
312 if debruijn == self.binder_index =>
313 {
314 let idx = bound_ty.var().as_usize();
315 if self.bound_vars.len() <= idx {
316 {
::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);
317 }
318 bound_ty.assert_eq(self.bound_vars.get(idx).unwrap());
319 }
320 _ => {}
321 };
322
323 t.super_visit_with(self)
324 }
325
326 fn visit_const(&mut self, c: I::Const) -> Self::Result {
327 if c.outer_exclusive_binder() < self.binder_index {
328 return ControlFlow::Break(());
329 }
330 match c.kind() {
331 ty::ConstKind::Bound(debruijn, bound_const)
332 if debruijn == ty::BoundVarIndexKind::Bound(self.binder_index) =>
333 {
334 let idx = bound_const.var().as_usize();
335 if self.bound_vars.len() <= idx {
336 {
::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);
337 }
338 bound_const.assert_eq(self.bound_vars.get(idx).unwrap());
339 }
340 _ => {}
341 };
342
343 c.super_visit_with(self)
344 }
345
346 fn visit_region(&mut self, r: I::Region) -> Self::Result {
347 match r.kind() {
348 ty::ReBound(index, br) if index == ty::BoundVarIndexKind::Bound(self.binder_index) => {
349 let idx = br.var().as_usize();
350 if self.bound_vars.len() <= idx {
351 {
::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);
352 }
353 br.assert_eq(self.bound_vars.get(idx).unwrap());
354 }
355
356 _ => (),
357 };
358
359 ControlFlow::Continue(())
360 }
361}
362
363#[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(Ord; I: Interner, T: Ord)]
372#[derive_where(Copy; I: Interner, T: Copy)]
373#[derive_where(Clone, PartialOrd, PartialEq, Hash, Debug; I: Interner, T)]
374#[derive(GenericTypeVisitable)]
375#[cfg_attr(
376 feature = "nightly",
377 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, __CTX>
::rustc_data_structures::stable_hasher::HashStable<__CTX> for
EarlyBinder<I, T> where
T: ::rustc_data_structures::stable_hasher::HashStable<__CTX>,
PhantomData<fn()
->
I>: ::rustc_data_structures::stable_hasher::HashStable<__CTX>
{
#[inline]
fn hash_stable(&self, __hcx: &mut __CTX,
__hasher:
&mut ::rustc_data_structures::stable_hasher::StableHasher) {
match *self {
EarlyBinder { value: ref __binding_0, _tcx: ref __binding_1
} => {
{ __binding_0.hash_stable(__hcx, __hasher); }
{ __binding_1.hash_stable(__hcx, __hasher); }
}
}
}
}
};HashStable_NoContext)
378)]
379pub struct EarlyBinder<I: Interner, T> {
380 value: T,
381 #[derive_where(skip(Debug))]
382 _tcx: PhantomData<fn() -> I>,
383}
384
385impl<I: Interner, T: Eq> Eq for EarlyBinder<I, T> {}
386
387#[cfg(feature = "nightly")]
389impl<I: Interner, T> !TypeFoldable<I> for ty::EarlyBinder<I, T> {}
390
391#[cfg(feature = "nightly")]
393impl<I: Interner, T> !TypeVisitable<I> for ty::EarlyBinder<I, T> {}
394
395impl<I: Interner, T> EarlyBinder<I, T> {
396 pub fn bind(value: T) -> EarlyBinder<I, T> {
397 EarlyBinder { value, _tcx: PhantomData }
398 }
399
400 pub fn as_ref(&self) -> EarlyBinder<I, &T> {
401 EarlyBinder { value: &self.value, _tcx: PhantomData }
402 }
403
404 pub fn map_bound_ref<F, U>(&self, f: F) -> EarlyBinder<I, U>
405 where
406 F: FnOnce(&T) -> U,
407 {
408 self.as_ref().map_bound(f)
409 }
410
411 pub fn map_bound<F, U>(self, f: F) -> EarlyBinder<I, U>
412 where
413 F: FnOnce(T) -> U,
414 {
415 let value = f(self.value);
416 EarlyBinder { value, _tcx: PhantomData }
417 }
418
419 pub fn try_map_bound<F, U, E>(self, f: F) -> Result<EarlyBinder<I, U>, E>
420 where
421 F: FnOnce(T) -> Result<U, E>,
422 {
423 let value = f(self.value)?;
424 Ok(EarlyBinder { value, _tcx: PhantomData })
425 }
426
427 pub fn rebind<U>(&self, value: U) -> EarlyBinder<I, U> {
428 EarlyBinder { value, _tcx: PhantomData }
429 }
430
431 pub fn skip_binder(self) -> T {
448 self.value
449 }
450}
451
452impl<I: Interner, T> EarlyBinder<I, Option<T>> {
453 pub fn transpose(self) -> Option<EarlyBinder<I, T>> {
454 self.value.map(|value| EarlyBinder { value, _tcx: PhantomData })
455 }
456}
457
458impl<I: Interner, Iter: IntoIterator> EarlyBinder<I, Iter>
459where
460 Iter::Item: TypeFoldable<I>,
461{
462 pub fn iter_instantiated<A>(self, cx: I, args: A) -> IterInstantiated<I, Iter, A>
463 where
464 A: SliceLike<Item = I::GenericArg>,
465 {
466 IterInstantiated { it: self.value.into_iter(), cx, args }
467 }
468
469 pub fn iter_identity(self) -> Iter::IntoIter {
472 self.value.into_iter()
473 }
474}
475
476pub struct IterInstantiated<I: Interner, Iter: IntoIterator, A> {
477 it: Iter::IntoIter,
478 cx: I,
479 args: A,
480}
481
482impl<I: Interner, Iter: IntoIterator, A> Iterator for IterInstantiated<I, Iter, A>
483where
484 Iter::Item: TypeFoldable<I>,
485 A: SliceLike<Item = I::GenericArg>,
486{
487 type Item = Iter::Item;
488
489 fn next(&mut self) -> Option<Self::Item> {
490 Some(
491 EarlyBinder { value: self.it.next()?, _tcx: PhantomData }
492 .instantiate(self.cx, self.args),
493 )
494 }
495
496 fn size_hint(&self) -> (usize, Option<usize>) {
497 self.it.size_hint()
498 }
499}
500
501impl<I: Interner, Iter: IntoIterator, A> DoubleEndedIterator for IterInstantiated<I, Iter, A>
502where
503 Iter::IntoIter: DoubleEndedIterator,
504 Iter::Item: TypeFoldable<I>,
505 A: SliceLike<Item = I::GenericArg>,
506{
507 fn next_back(&mut self) -> Option<Self::Item> {
508 Some(
509 EarlyBinder { value: self.it.next_back()?, _tcx: PhantomData }
510 .instantiate(self.cx, self.args),
511 )
512 }
513}
514
515impl<I: Interner, Iter: IntoIterator, A> ExactSizeIterator for IterInstantiated<I, Iter, A>
516where
517 Iter::IntoIter: ExactSizeIterator,
518 Iter::Item: TypeFoldable<I>,
519 A: SliceLike<Item = I::GenericArg>,
520{
521}
522
523impl<'s, I: Interner, Iter: IntoIterator> EarlyBinder<I, Iter>
524where
525 Iter::Item: Deref,
526 <Iter::Item as Deref>::Target: Copy + TypeFoldable<I>,
527{
528 pub fn iter_instantiated_copied(
529 self,
530 cx: I,
531 args: &'s [I::GenericArg],
532 ) -> IterInstantiatedCopied<'s, I, Iter> {
533 IterInstantiatedCopied { it: self.value.into_iter(), cx, args }
534 }
535
536 pub fn iter_identity_copied(self) -> IterIdentityCopied<Iter> {
539 IterIdentityCopied { it: self.value.into_iter() }
540 }
541}
542
543pub struct IterInstantiatedCopied<'a, I: Interner, Iter: IntoIterator> {
544 it: Iter::IntoIter,
545 cx: I,
546 args: &'a [I::GenericArg],
547}
548
549impl<I: Interner, Iter: IntoIterator> Iterator for IterInstantiatedCopied<'_, I, Iter>
550where
551 Iter::Item: Deref,
552 <Iter::Item as Deref>::Target: Copy + TypeFoldable<I>,
553{
554 type Item = <Iter::Item as Deref>::Target;
555
556 fn next(&mut self) -> Option<Self::Item> {
557 self.it.next().map(|value| {
558 EarlyBinder { value: *value, _tcx: PhantomData }.instantiate(self.cx, self.args)
559 })
560 }
561
562 fn size_hint(&self) -> (usize, Option<usize>) {
563 self.it.size_hint()
564 }
565}
566
567impl<I: Interner, Iter: IntoIterator> DoubleEndedIterator for IterInstantiatedCopied<'_, I, Iter>
568where
569 Iter::IntoIter: DoubleEndedIterator,
570 Iter::Item: Deref,
571 <Iter::Item as Deref>::Target: Copy + TypeFoldable<I>,
572{
573 fn next_back(&mut self) -> Option<Self::Item> {
574 self.it.next_back().map(|value| {
575 EarlyBinder { value: *value, _tcx: PhantomData }.instantiate(self.cx, self.args)
576 })
577 }
578}
579
580impl<I: Interner, Iter: IntoIterator> ExactSizeIterator for IterInstantiatedCopied<'_, I, Iter>
581where
582 Iter::IntoIter: ExactSizeIterator,
583 Iter::Item: Deref,
584 <Iter::Item as Deref>::Target: Copy + TypeFoldable<I>,
585{
586}
587
588pub struct IterIdentityCopied<Iter: IntoIterator> {
589 it: Iter::IntoIter,
590}
591
592impl<Iter: IntoIterator> Iterator for IterIdentityCopied<Iter>
593where
594 Iter::Item: Deref,
595 <Iter::Item as Deref>::Target: Copy,
596{
597 type Item = <Iter::Item as Deref>::Target;
598
599 fn next(&mut self) -> Option<Self::Item> {
600 self.it.next().map(|i| *i)
601 }
602
603 fn size_hint(&self) -> (usize, Option<usize>) {
604 self.it.size_hint()
605 }
606}
607
608impl<Iter: IntoIterator> DoubleEndedIterator for IterIdentityCopied<Iter>
609where
610 Iter::IntoIter: DoubleEndedIterator,
611 Iter::Item: Deref,
612 <Iter::Item as Deref>::Target: Copy,
613{
614 fn next_back(&mut self) -> Option<Self::Item> {
615 self.it.next_back().map(|i| *i)
616 }
617}
618
619impl<Iter: IntoIterator> ExactSizeIterator for IterIdentityCopied<Iter>
620where
621 Iter::IntoIter: ExactSizeIterator,
622 Iter::Item: Deref,
623 <Iter::Item as Deref>::Target: Copy,
624{
625}
626pub struct EarlyBinderIter<I, T> {
627 t: T,
628 _tcx: PhantomData<I>,
629}
630
631impl<I: Interner, T: IntoIterator> EarlyBinder<I, T> {
632 pub fn transpose_iter(self) -> EarlyBinderIter<I, T::IntoIter> {
633 EarlyBinderIter { t: self.value.into_iter(), _tcx: PhantomData }
634 }
635}
636
637impl<I: Interner, T: Iterator> Iterator for EarlyBinderIter<I, T> {
638 type Item = EarlyBinder<I, T::Item>;
639
640 fn next(&mut self) -> Option<Self::Item> {
641 self.t.next().map(|value| EarlyBinder { value, _tcx: PhantomData })
642 }
643
644 fn size_hint(&self) -> (usize, Option<usize>) {
645 self.t.size_hint()
646 }
647}
648
649impl<I: Interner, T: TypeFoldable<I>> ty::EarlyBinder<I, T> {
650 pub fn instantiate<A>(self, cx: I, args: A) -> T
651 where
652 A: SliceLike<Item = I::GenericArg>,
653 {
654 if args.is_empty() {
658 if !!self.value.has_param() {
{
::core::panicking::panic_fmt(format_args!("{0:?} has parameters, but no args were provided in instantiate",
self.value));
}
};assert!(
659 !self.value.has_param(),
660 "{:?} has parameters, but no args were provided in instantiate",
661 self.value,
662 );
663 return self.value;
664 }
665 let mut folder = ArgFolder { cx, args: args.as_slice(), binders_passed: 0 };
666 self.value.fold_with(&mut folder)
667 }
668
669 pub fn instantiate_identity(self) -> T {
678 self.value
679 }
680
681 pub fn no_bound_vars(self) -> Option<T> {
683 if !self.value.has_param() { Some(self.value) } else { None }
684 }
685}
686
687struct ArgFolder<'a, I: Interner> {
691 cx: I,
692 args: &'a [I::GenericArg],
693
694 binders_passed: u32,
696}
697
698impl<'a, I: Interner> TypeFolder<I> for ArgFolder<'a, I> {
699 #[inline]
700 fn cx(&self) -> I {
701 self.cx
702 }
703
704 fn fold_binder<T: TypeFoldable<I>>(&mut self, t: ty::Binder<I, T>) -> ty::Binder<I, T> {
705 self.binders_passed += 1;
706 let t = t.super_fold_with(self);
707 self.binders_passed -= 1;
708 t
709 }
710
711 fn fold_region(&mut self, r: I::Region) -> I::Region {
712 match r.kind() {
718 ty::ReEarlyParam(data) => {
719 let rk = self.args.get(data.index() as usize).map(|arg| arg.kind());
720 match rk {
721 Some(ty::GenericArgKind::Lifetime(lt)) => self.shift_region_through_binders(lt),
722 Some(other) => self.region_param_expected(data, r, other),
723 None => self.region_param_out_of_range(data, r),
724 }
725 }
726 ty::ReBound(..)
727 | ty::ReLateParam(_)
728 | ty::ReStatic
729 | ty::RePlaceholder(_)
730 | ty::ReErased
731 | ty::ReError(_) => r,
732 ty::ReVar(_) => { ::core::panicking::panic_fmt(format_args!("unexpected region: {0:?}", r)); }panic!("unexpected region: {r:?}"),
733 }
734 }
735
736 fn fold_ty(&mut self, t: I::Ty) -> I::Ty {
737 if !t.has_param() {
738 return t;
739 }
740
741 match t.kind() {
742 ty::Param(p) => self.ty_for_param(p, t),
743 _ => t.super_fold_with(self),
744 }
745 }
746
747 fn fold_const(&mut self, c: I::Const) -> I::Const {
748 if let ty::ConstKind::Param(p) = c.kind() {
749 self.const_for_param(p, c)
750 } else {
751 c.super_fold_with(self)
752 }
753 }
754
755 fn fold_predicate(&mut self, p: I::Predicate) -> I::Predicate {
756 if p.has_param() { p.super_fold_with(self) } else { p }
757 }
758
759 fn fold_clauses(&mut self, c: I::Clauses) -> I::Clauses {
760 if c.has_param() { c.super_fold_with(self) } else { c }
761 }
762}
763
764impl<'a, I: Interner> ArgFolder<'a, I> {
765 fn ty_for_param(&self, p: I::ParamTy, source_ty: I::Ty) -> I::Ty {
766 let opt_ty = self.args.get(p.index() as usize).map(|arg| arg.kind());
768 let ty = match opt_ty {
769 Some(ty::GenericArgKind::Type(ty)) => ty,
770 Some(kind) => self.type_param_expected(p, source_ty, kind),
771 None => self.type_param_out_of_range(p, source_ty),
772 };
773
774 self.shift_vars_through_binders(ty)
775 }
776
777 #[cold]
778 #[inline(never)]
779 fn type_param_expected(&self, p: I::ParamTy, ty: I::Ty, kind: ty::GenericArgKind<I>) -> ! {
780 {
::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!(
781 "expected type for `{:?}` ({:?}/{}) but found {:?} when instantiating, args={:?}",
782 p,
783 ty,
784 p.index(),
785 kind,
786 self.args,
787 )
788 }
789
790 #[cold]
791 #[inline(never)]
792 fn type_param_out_of_range(&self, p: I::ParamTy, ty: I::Ty) -> ! {
793 {
::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!(
794 "type parameter `{:?}` ({:?}/{}) out of range when instantiating, args={:?}",
795 p,
796 ty,
797 p.index(),
798 self.args,
799 )
800 }
801
802 fn const_for_param(&self, p: I::ParamConst, source_ct: I::Const) -> I::Const {
803 let opt_ct = self.args.get(p.index() as usize).map(|arg| arg.kind());
805 let ct = match opt_ct {
806 Some(ty::GenericArgKind::Const(ct)) => ct,
807 Some(kind) => self.const_param_expected(p, source_ct, kind),
808 None => self.const_param_out_of_range(p, source_ct),
809 };
810
811 self.shift_vars_through_binders(ct)
812 }
813
814 #[cold]
815 #[inline(never)]
816 fn const_param_expected(
817 &self,
818 p: I::ParamConst,
819 ct: I::Const,
820 kind: ty::GenericArgKind<I>,
821 ) -> ! {
822 {
::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!(
823 "expected const for `{:?}` ({:?}/{}) but found {:?} when instantiating args={:?}",
824 p,
825 ct,
826 p.index(),
827 kind,
828 self.args,
829 )
830 }
831
832 #[cold]
833 #[inline(never)]
834 fn const_param_out_of_range(&self, p: I::ParamConst, ct: I::Const) -> ! {
835 {
::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!(
836 "const parameter `{:?}` ({:?}/{}) out of range when instantiating args={:?}",
837 p,
838 ct,
839 p.index(),
840 self.args,
841 )
842 }
843
844 #[cold]
845 #[inline(never)]
846 fn region_param_expected(
847 &self,
848 ebr: I::EarlyParamRegion,
849 r: I::Region,
850 kind: ty::GenericArgKind<I>,
851 ) -> ! {
852 {
::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!(
853 "expected region for `{:?}` ({:?}/{}) but found {:?} when instantiating args={:?}",
854 ebr,
855 r,
856 ebr.index(),
857 kind,
858 self.args,
859 )
860 }
861
862 #[cold]
863 #[inline(never)]
864 fn region_param_out_of_range(&self, ebr: I::EarlyParamRegion, r: I::Region) -> ! {
865 {
::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!(
866 "region parameter `{:?}` ({:?}/{}) out of range when instantiating args={:?}",
867 ebr,
868 r,
869 ebr.index(),
870 self.args,
871 )
872 }
873
874 x;#[instrument(level = "trace", skip(self), fields(binders_passed = self.binders_passed), ret)]
917 fn shift_vars_through_binders<T: TypeFoldable<I>>(&self, val: T) -> T {
918 if self.binders_passed == 0 || !val.has_escaping_bound_vars() {
919 val
920 } else {
921 ty::shift_vars(self.cx, val, self.binders_passed)
922 }
923 }
924
925 fn shift_region_through_binders(&self, region: I::Region) -> I::Region {
926 if self.binders_passed == 0 || !region.has_escaping_bound_vars() {
927 region
928 } else {
929 ty::shift_region(self.cx, region, self.binders_passed)
930 }
931 }
932}
933
934#[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_receiver_is_total_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)]
954#[cfg_attr(
955 feature = "nightly",
956 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<__CTX> ::rustc_data_structures::stable_hasher::HashStable<__CTX>
for BoundVarIndexKind {
#[inline]
fn hash_stable(&self, __hcx: &mut __CTX,
__hasher:
&mut ::rustc_data_structures::stable_hasher::StableHasher) {
::std::mem::discriminant(self).hash_stable(__hcx, __hasher);
match *self {
BoundVarIndexKind::Bound(ref __binding_0) => {
{ __binding_0.hash_stable(__hcx, __hasher); }
}
BoundVarIndexKind::Canonical => {}
}
}
}
};HashStable_NoContext)
957)]
958#[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)]
959pub enum BoundVarIndexKind {
960 Bound(DebruijnIndex),
961 Canonical,
962}
963
964#[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(Ord; I: Interner, T: Ord)]
971#[derive_where(Copy; I: Interner, T: Copy)]
972#[derive_where(Clone, PartialOrd, PartialEq, Eq, Hash; I: Interner, T)]
973#[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> {
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)]
974#[cfg_attr(
975 feature = "nightly",
976 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, __CTX>
::rustc_data_structures::stable_hasher::HashStable<__CTX> for
Placeholder<I, T> where
T: ::rustc_data_structures::stable_hasher::HashStable<__CTX>,
PhantomData<fn()
->
I>: ::rustc_data_structures::stable_hasher::HashStable<__CTX>
{
#[inline]
fn hash_stable(&self, __hcx: &mut __CTX,
__hasher:
&mut ::rustc_data_structures::stable_hasher::StableHasher) {
match *self {
Placeholder {
universe: ref __binding_0,
bound: ref __binding_1,
_tcx: ref __binding_2 } => {
{ __binding_0.hash_stable(__hcx, __hasher); }
{ __binding_1.hash_stable(__hcx, __hasher); }
{ __binding_2.hash_stable(__hcx, __hasher); }
}
}
}
}
};HashStable_NoContext)
977)]
978pub struct Placeholder<I: Interner, T> {
979 pub universe: UniverseIndex,
980 pub bound: T,
981 #[type_foldable(identity)]
982 #[type_visitable(ignore)]
983 _tcx: PhantomData<fn() -> I>,
984}
985
986impl<I: Interner, T: fmt::Debug> fmt::Debug for ty::Placeholder<I, T> {
987 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
988 if self.universe == ty::UniverseIndex::ROOT {
989 f.write_fmt(format_args!("!{0:?}", self.bound))write!(f, "!{:?}", self.bound)
990 } else {
991 f.write_fmt(format_args!("!{0}_{1:?}", self.universe.index(), self.bound))write!(f, "!{}_{:?}", self.universe.index(), self.bound)
992 }
993 }
994}
995
996impl<I: Interner, U: Interner, T> Lift<U> for Placeholder<I, T>
997where
998 T: Lift<U>,
999{
1000 type Lifted = Placeholder<U, T::Lifted>;
1001
1002 fn lift_to_interner(self, cx: U) -> Option<Self::Lifted> {
1003 Some(Placeholder {
1004 universe: self.universe,
1005 bound: self.bound.lift_to_interner(cx)?,
1006 _tcx: PhantomData,
1007 })
1008 }
1009}
1010
1011#[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)]
1012#[derive(const _: () =
{
impl<I: Interner, J> ::rustc_type_ir::lift::Lift<J> for
BoundRegionKind<I> where I: Interner, J: Interner,
I::Symbol: ::rustc_type_ir::lift::Lift<J, Lifted = J::Symbol>,
I::DefId: ::rustc_type_ir::lift::Lift<J, Lifted = J::DefId> {
type Lifted = BoundRegionKind<J>;
fn lift_to_interner(self, interner: J) -> Option<Self::Lifted> {
Some(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)]
1013#[cfg_attr(
1014 feature = "nightly",
1015 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, __CTX>
::rustc_data_structures::stable_hasher::HashStable<__CTX> for
BoundRegionKind<I> where
I::Symbol: ::rustc_data_structures::stable_hasher::HashStable<__CTX>,
I::DefId: ::rustc_data_structures::stable_hasher::HashStable<__CTX>
{
#[inline]
fn hash_stable(&self, __hcx: &mut __CTX,
__hasher:
&mut ::rustc_data_structures::stable_hasher::StableHasher) {
::std::mem::discriminant(self).hash_stable(__hcx, __hasher);
match *self {
BoundRegionKind::Anon => {}
BoundRegionKind::NamedForPrinting(ref __binding_0) => {
{ __binding_0.hash_stable(__hcx, __hasher); }
}
BoundRegionKind::Named(ref __binding_0) => {
{ __binding_0.hash_stable(__hcx, __hasher); }
}
BoundRegionKind::ClosureEnv => {}
}
}
}
};HashStable_NoContext)
1016)]
1017
1018pub enum BoundRegionKind<I: Interner> {
1019 Anon,
1021
1022 NamedForPrinting(I::Symbol),
1026
1027 Named(I::DefId),
1029
1030 ClosureEnv,
1033}
1034
1035impl<I: Interner> fmt::Debug for ty::BoundRegionKind<I> {
1036 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1037 match *self {
1038 ty::BoundRegionKind::Anon => f.write_fmt(format_args!("BrAnon"))write!(f, "BrAnon"),
1039 ty::BoundRegionKind::NamedForPrinting(name) => {
1040 f.write_fmt(format_args!("BrNamedForPrinting({0:?})", name))write!(f, "BrNamedForPrinting({:?})", name)
1041 }
1042 ty::BoundRegionKind::Named(did) => {
1043 f.write_fmt(format_args!("BrNamed({0:?})", did))write!(f, "BrNamed({did:?})")
1044 }
1045 ty::BoundRegionKind::ClosureEnv => f.write_fmt(format_args!("BrEnv"))write!(f, "BrEnv"),
1046 }
1047 }
1048}
1049
1050impl<I: Interner> BoundRegionKind<I> {
1051 pub fn is_named(&self, tcx: I) -> bool {
1052 self.get_name(tcx).is_some()
1053 }
1054
1055 pub fn get_name(&self, tcx: I) -> Option<I::Symbol> {
1056 match *self {
1057 ty::BoundRegionKind::Named(def_id) => {
1058 let name = tcx.item_name(def_id);
1059 if name.is_kw_underscore_lifetime() { None } else { Some(name) }
1060 }
1061 ty::BoundRegionKind::NamedForPrinting(name) => Some(name),
1062 _ => None,
1063 }
1064 }
1065
1066 pub fn get_id(&self) -> Option<I::DefId> {
1067 match *self {
1068 ty::BoundRegionKind::Named(id) => Some(id),
1069 _ => None,
1070 }
1071 }
1072}
1073
1074#[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)]
1075#[derive(const _: () =
{
impl<I: Interner, J> ::rustc_type_ir::lift::Lift<J> for BoundTyKind<I>
where I: Interner, J: Interner,
I::DefId: ::rustc_type_ir::lift::Lift<J, Lifted = J::DefId> {
type Lifted = BoundTyKind<J>;
fn lift_to_interner(self, interner: J) -> Option<Self::Lifted> {
Some(match self {
BoundTyKind::Anon => { BoundTyKind::Anon }
BoundTyKind::Param(__binding_0) => {
BoundTyKind::Param(__binding_0.lift_to_interner(interner)?)
}
})
}
}
};Lift_Generic)]
1076#[cfg_attr(
1077 feature = "nightly",
1078 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, __CTX>
::rustc_data_structures::stable_hasher::HashStable<__CTX> for
BoundTyKind<I> where
I::DefId: ::rustc_data_structures::stable_hasher::HashStable<__CTX>
{
#[inline]
fn hash_stable(&self, __hcx: &mut __CTX,
__hasher:
&mut ::rustc_data_structures::stable_hasher::StableHasher) {
::std::mem::discriminant(self).hash_stable(__hcx, __hasher);
match *self {
BoundTyKind::Anon => {}
BoundTyKind::Param(ref __binding_0) => {
{ __binding_0.hash_stable(__hcx, __hasher); }
}
}
}
}
};HashStable_NoContext)
1079)]
1080pub enum BoundTyKind<I: Interner> {
1081 Anon,
1082 Param(I::DefId),
1083}
1084
1085#[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)]
1086#[derive(const _: () =
{
impl<I: Interner, J> ::rustc_type_ir::lift::Lift<J> for
BoundVariableKind<I> where I: Interner, J: Interner,
BoundTyKind<I>: ::rustc_type_ir::lift::Lift<J, Lifted =
BoundTyKind<J>>,
BoundRegionKind<I>: ::rustc_type_ir::lift::Lift<J, Lifted =
BoundRegionKind<J>> {
type Lifted = BoundVariableKind<J>;
fn lift_to_interner(self, interner: J) -> Option<Self::Lifted> {
Some(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)]
1087#[cfg_attr(
1088 feature = "nightly",
1089 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, __CTX>
::rustc_data_structures::stable_hasher::HashStable<__CTX> for
BoundVariableKind<I> where
BoundTyKind<I>: ::rustc_data_structures::stable_hasher::HashStable<__CTX>,
BoundRegionKind<I>: ::rustc_data_structures::stable_hasher::HashStable<__CTX>
{
#[inline]
fn hash_stable(&self, __hcx: &mut __CTX,
__hasher:
&mut ::rustc_data_structures::stable_hasher::StableHasher) {
::std::mem::discriminant(self).hash_stable(__hcx, __hasher);
match *self {
BoundVariableKind::Ty(ref __binding_0) => {
{ __binding_0.hash_stable(__hcx, __hasher); }
}
BoundVariableKind::Region(ref __binding_0) => {
{ __binding_0.hash_stable(__hcx, __hasher); }
}
BoundVariableKind::Const => {}
}
}
}
};HashStable_NoContext)
1090)]
1091pub enum BoundVariableKind<I: Interner> {
1092 Ty(BoundTyKind<I>),
1093 Region(BoundRegionKind<I>),
1094 Const,
1095}
1096
1097impl<I: Interner> BoundVariableKind<I> {
1098 pub fn expect_region(self) -> BoundRegionKind<I> {
1099 match self {
1100 BoundVariableKind::Region(lt) => lt,
1101 _ => {
::core::panicking::panic_fmt(format_args!("expected a region, but found another kind"));
}panic!("expected a region, but found another kind"),
1102 }
1103 }
1104
1105 pub fn expect_ty(self) -> BoundTyKind<I> {
1106 match self {
1107 BoundVariableKind::Ty(ty) => ty,
1108 _ => {
::core::panicking::panic_fmt(format_args!("expected a type, but found another kind"));
}panic!("expected a type, but found another kind"),
1109 }
1110 }
1111
1112 pub fn expect_const(self) {
1113 match self {
1114 BoundVariableKind::Const => (),
1115 _ => {
::core::panicking::panic_fmt(format_args!("expected a const, but found another kind"));
}panic!("expected a const, but found another kind"),
1116 }
1117 }
1118}
1119
1120#[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)]
1121#[cfg_attr(
1122 feature = "nightly",
1123 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, __CTX>
::rustc_data_structures::stable_hasher::HashStable<__CTX> for
BoundRegion<I> where
BoundRegionKind<I>: ::rustc_data_structures::stable_hasher::HashStable<__CTX>
{
#[inline]
fn hash_stable(&self, __hcx: &mut __CTX,
__hasher:
&mut ::rustc_data_structures::stable_hasher::StableHasher) {
match *self {
BoundRegion { var: ref __binding_0, kind: ref __binding_1 }
=> {
{ __binding_0.hash_stable(__hcx, __hasher); }
{ __binding_1.hash_stable(__hcx, __hasher); }
}
}
}
}
};HashStable_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)
1124)]
1125pub struct BoundRegion<I: Interner> {
1126 pub var: ty::BoundVar,
1127 pub kind: BoundRegionKind<I>,
1128}
1129
1130impl<I: Interner> core::fmt::Debug for BoundRegion<I> {
1131 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1132 match self.kind {
1133 BoundRegionKind::Anon => f.write_fmt(format_args!("{0:?}", self.var))write!(f, "{:?}", self.var),
1134 BoundRegionKind::ClosureEnv => f.write_fmt(format_args!("{0:?}.Env", self.var))write!(f, "{:?}.Env", self.var),
1135 BoundRegionKind::Named(def) => {
1136 f.write_fmt(format_args!("{0:?}.Named({1:?})", self.var, def))write!(f, "{:?}.Named({:?})", self.var, def)
1137 }
1138 BoundRegionKind::NamedForPrinting(symbol) => {
1139 f.write_fmt(format_args!("{0:?}.NamedAnon({1:?})", self.var, symbol))write!(f, "{:?}.NamedAnon({:?})", self.var, symbol)
1140 }
1141 }
1142 }
1143}
1144
1145impl<I: Interner> BoundRegion<I> {
1146 pub fn var(self) -> ty::BoundVar {
1147 self.var
1148 }
1149
1150 pub fn assert_eq(self, var: BoundVariableKind<I>) {
1151 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())
1152 }
1153}
1154
1155pub type PlaceholderRegion<I> = ty::Placeholder<I, BoundRegion<I>>;
1156
1157impl<I: Interner> PlaceholderRegion<I> {
1158 pub fn universe(self) -> UniverseIndex {
1159 self.universe
1160 }
1161
1162 pub fn var(self) -> ty::BoundVar {
1163 self.bound.var()
1164 }
1165
1166 pub fn with_updated_universe(self, ui: UniverseIndex) -> Self {
1167 Self { universe: ui, bound: self.bound, _tcx: PhantomData }
1168 }
1169
1170 pub fn new(ui: UniverseIndex, bound: BoundRegion<I>) -> Self {
1171 Self { universe: ui, bound, _tcx: PhantomData }
1172 }
1173
1174 pub fn new_anon(ui: UniverseIndex, var: ty::BoundVar) -> Self {
1175 let bound = BoundRegion { var, kind: BoundRegionKind::Anon };
1176 Self { universe: ui, bound, _tcx: PhantomData }
1177 }
1178}
1179
1180#[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)]
1181#[cfg_attr(
1182 feature = "nightly",
1183 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, __CTX>
::rustc_data_structures::stable_hasher::HashStable<__CTX> for
BoundTy<I> where
BoundTyKind<I>: ::rustc_data_structures::stable_hasher::HashStable<__CTX>
{
#[inline]
fn hash_stable(&self, __hcx: &mut __CTX,
__hasher:
&mut ::rustc_data_structures::stable_hasher::StableHasher) {
match *self {
BoundTy { var: ref __binding_0, kind: ref __binding_1 } => {
{ __binding_0.hash_stable(__hcx, __hasher); }
{ __binding_1.hash_stable(__hcx, __hasher); }
}
}
}
}
};HashStable_NoContext)
1184)]
1185pub struct BoundTy<I: Interner> {
1186 pub var: ty::BoundVar,
1187 pub kind: BoundTyKind<I>,
1188}
1189
1190impl<I: Interner, U: Interner> Lift<U> for BoundTy<I>
1191where
1192 BoundTyKind<I>: Lift<U, Lifted = BoundTyKind<U>>,
1193{
1194 type Lifted = BoundTy<U>;
1195
1196 fn lift_to_interner(self, cx: U) -> Option<Self::Lifted> {
1197 Some(BoundTy { var: self.var, kind: self.kind.lift_to_interner(cx)? })
1198 }
1199}
1200
1201impl<I: Interner> fmt::Debug for ty::BoundTy<I> {
1202 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1203 match self.kind {
1204 ty::BoundTyKind::Anon => f.write_fmt(format_args!("{0:?}", self.var))write!(f, "{:?}", self.var),
1205 ty::BoundTyKind::Param(def_id) => f.write_fmt(format_args!("{0:?}", def_id))write!(f, "{def_id:?}"),
1206 }
1207 }
1208}
1209
1210impl<I: Interner> BoundTy<I> {
1211 pub fn var(self) -> ty::BoundVar {
1212 self.var
1213 }
1214
1215 pub fn assert_eq(self, var: BoundVariableKind<I>) {
1216 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())
1217 }
1218}
1219
1220pub type PlaceholderType<I> = ty::Placeholder<I, BoundTy<I>>;
1221
1222impl<I: Interner> PlaceholderType<I> {
1223 pub fn universe(self) -> UniverseIndex {
1224 self.universe
1225 }
1226
1227 pub fn var(self) -> ty::BoundVar {
1228 self.bound.var
1229 }
1230
1231 pub fn with_updated_universe(self, ui: UniverseIndex) -> Self {
1232 Self { universe: ui, bound: self.bound, _tcx: PhantomData }
1233 }
1234
1235 pub fn new(ui: UniverseIndex, bound: BoundTy<I>) -> Self {
1236 Self { universe: ui, bound, _tcx: PhantomData }
1237 }
1238
1239 pub fn new_anon(ui: UniverseIndex, var: ty::BoundVar) -> Self {
1240 let bound = BoundTy { var, kind: BoundTyKind::Anon };
1241 Self { universe: ui, bound, _tcx: PhantomData }
1242 }
1243}
1244
1245#[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)]
1246#[cfg_attr(
1247 feature = "nightly",
1248 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, __CTX>
::rustc_data_structures::stable_hasher::HashStable<__CTX> for
BoundConst<I> where
PhantomData<fn()
->
I>: ::rustc_data_structures::stable_hasher::HashStable<__CTX>
{
#[inline]
fn hash_stable(&self, __hcx: &mut __CTX,
__hasher:
&mut ::rustc_data_structures::stable_hasher::StableHasher) {
match *self {
BoundConst { var: ref __binding_0, _tcx: ref __binding_1 }
=> {
{ __binding_0.hash_stable(__hcx, __hasher); }
{ __binding_1.hash_stable(__hcx, __hasher); }
}
}
}
}
};HashStable_NoContext)
1249)]
1250pub struct BoundConst<I: Interner> {
1251 pub var: ty::BoundVar,
1252 #[derive_where(skip(Debug))]
1253 pub _tcx: PhantomData<fn() -> I>,
1254}
1255
1256impl<I: Interner> BoundConst<I> {
1257 pub fn var(self) -> ty::BoundVar {
1258 self.var
1259 }
1260
1261 pub fn assert_eq(self, var: BoundVariableKind<I>) {
1262 var.expect_const()
1263 }
1264
1265 pub fn new(var: ty::BoundVar) -> Self {
1266 Self { var, _tcx: PhantomData }
1267 }
1268}
1269
1270pub type PlaceholderConst<I> = ty::Placeholder<I, BoundConst<I>>;
1271
1272impl<I: Interner> PlaceholderConst<I> {
1273 pub fn universe(self) -> UniverseIndex {
1274 self.universe
1275 }
1276
1277 pub fn var(self) -> ty::BoundVar {
1278 self.bound.var
1279 }
1280
1281 pub fn with_updated_universe(self, ui: UniverseIndex) -> Self {
1282 Self { universe: ui, bound: self.bound, _tcx: PhantomData }
1283 }
1284
1285 pub fn new(ui: UniverseIndex, bound: BoundConst<I>) -> Self {
1286 Self { universe: ui, bound, _tcx: PhantomData }
1287 }
1288
1289 pub fn new_anon(ui: UniverseIndex, var: ty::BoundVar) -> Self {
1290 let bound = BoundConst::new(var);
1291 Self { universe: ui, bound, _tcx: PhantomData }
1292 }
1293
1294 pub fn find_const_ty_from_env(self, env: I::ParamEnv) -> I::Ty {
1295 let mut candidates = env.caller_bounds().iter().filter_map(|clause| {
1296 match clause.kind().skip_binder() {
1298 ty::ClauseKind::ConstArgHasType(placeholder_ct, ty) => {
1299 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());
1300
1301 match placeholder_ct.kind() {
1302 ty::ConstKind::Placeholder(placeholder_ct) if placeholder_ct == self => {
1303 Some(ty)
1304 }
1305 _ => None,
1306 }
1307 }
1308 _ => None,
1309 }
1310 });
1311
1312 let ty = candidates.next().unwrap_or_else(|| {
1319 {
::core::panicking::panic_fmt(format_args!("cannot find `{0:?}` in param-env: {1:#?}",
self, env));
};panic!("cannot find `{self:?}` in param-env: {env:#?}");
1320 });
1321 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!(
1322 candidates.next().is_none(),
1323 "did not expect duplicate `ConstParamHasTy` for `{self:?}` in param-env: {env:#?}"
1324 );
1325 ty
1326 }
1327}