]> git.proxmox.com Git - rustc.git/blob - vendor/chalk-ir-0.55.0/src/fold/subst.rs
New upstream version 1.58.1+dfsg1
[rustc.git] / vendor / chalk-ir-0.55.0 / src / fold / subst.rs
1 use super::*;
2 use crate::fold::shift::Shift;
3
4 /// Substitution used during folding
5 pub struct Subst<'s, 'i, I: Interner> {
6 /// Values to substitute. A reference to a free variable with
7 /// index `i` will be mapped to `parameters[i]` -- if `i >
8 /// parameters.len()`, then we will leave the variable untouched.
9 parameters: &'s [GenericArg<I>],
10 interner: &'i I,
11 }
12
13 impl<I: Interner> Subst<'_, '_, I> {
14 /// Applies the substitution by folding
15 pub fn apply<T: Fold<I>>(interner: &I, parameters: &[GenericArg<I>], value: T) -> T::Result {
16 value
17 .fold_with(
18 &mut Subst {
19 parameters,
20 interner,
21 },
22 DebruijnIndex::INNERMOST,
23 )
24 .unwrap()
25 }
26 }
27
28 impl<'i, I: Interner> Folder<'i, I> for Subst<'_, 'i, I> {
29 fn as_dyn(&mut self) -> &mut dyn Folder<'i, I> {
30 self
31 }
32
33 /// We are eliminating one binder, but binders outside of that get preserved.
34 ///
35 /// So e.g. consider this:
36 ///
37 /// ```notrust
38 /// for<A, B> { for<C> { [A, C] } }
39 /// // ^ the binder we are substituing with `[u32]`
40 /// ```
41 ///
42 /// Here, `A` would be `^1.0` and `C` would be `^0.0`. We will replace `^0.0` with the
43 /// 0th index from the list (`u32`). We will convert `^1.0` (A) to `^0.0` -- i.e., shift
44 /// it **out** of one level of binder (the `for<C>` binder we are eliminating).
45 ///
46 /// This gives us as a result:
47 ///
48 /// ```notrust
49 /// for<A, B> { [A, u32] }
50 /// ^ represented as `^0.0`
51 /// ```
52 fn fold_free_var_ty(
53 &mut self,
54 bound_var: BoundVar,
55 outer_binder: DebruijnIndex,
56 ) -> Fallible<Ty<I>> {
57 if let Some(index) = bound_var.index_if_innermost() {
58 match self.parameters[index].data(self.interner()) {
59 GenericArgData::Ty(t) => {
60 Ok(t.clone().shifted_in_from(self.interner(), outer_binder))
61 }
62 _ => panic!("mismatched kinds in substitution"),
63 }
64 } else {
65 Ok(bound_var
66 .shifted_out()
67 .expect("cannot fail because this is not the innermost")
68 .shifted_in_from(outer_binder)
69 .to_ty(self.interner()))
70 }
71 }
72
73 /// see `fold_free_var_ty`
74 fn fold_free_var_lifetime(
75 &mut self,
76 bound_var: BoundVar,
77 outer_binder: DebruijnIndex,
78 ) -> Fallible<Lifetime<I>> {
79 if let Some(index) = bound_var.index_if_innermost() {
80 match self.parameters[index].data(self.interner()) {
81 GenericArgData::Lifetime(l) => {
82 Ok(l.clone().shifted_in_from(self.interner(), outer_binder))
83 }
84 _ => panic!("mismatched kinds in substitution"),
85 }
86 } else {
87 Ok(bound_var
88 .shifted_out()
89 .unwrap()
90 .shifted_in_from(outer_binder)
91 .to_lifetime(self.interner()))
92 }
93 }
94
95 /// see `fold_free_var_ty`
96 fn fold_free_var_const(
97 &mut self,
98 ty: Ty<I>,
99 bound_var: BoundVar,
100 outer_binder: DebruijnIndex,
101 ) -> Fallible<Const<I>> {
102 if let Some(index) = bound_var.index_if_innermost() {
103 match self.parameters[index].data(self.interner()) {
104 GenericArgData::Const(c) => {
105 Ok(c.clone().shifted_in_from(self.interner(), outer_binder))
106 }
107 _ => panic!("mismatched kinds in substitution"),
108 }
109 } else {
110 Ok(bound_var
111 .shifted_out()
112 .unwrap()
113 .shifted_in_from(outer_binder)
114 .to_const(self.interner(), ty))
115 }
116 }
117
118 fn interner(&self) -> &'i I {
119 self.interner
120 }
121 }