rustc_infer/infer/
freshen.rs1use std::collections::hash_map::Entry;
35
36use rustc_data_structures::fx::FxHashMap;
37use rustc_middle::bug;
38use rustc_middle::ty::fold::TypeFolder;
39use rustc_middle::ty::{self, Ty, TyCtxt, TypeFoldable, TypeSuperFoldable, TypeVisitableExt};
40
41use super::InferCtxt;
42
43pub struct TypeFreshener<'a, 'tcx> {
44 infcx: &'a InferCtxt<'tcx>,
45 ty_freshen_count: u32,
46 const_freshen_count: u32,
47 ty_freshen_map: FxHashMap<ty::InferTy, Ty<'tcx>>,
48 const_freshen_map: FxHashMap<ty::InferConst, ty::Const<'tcx>>,
49}
50
51impl<'a, 'tcx> TypeFreshener<'a, 'tcx> {
52 pub fn new(infcx: &'a InferCtxt<'tcx>) -> TypeFreshener<'a, 'tcx> {
53 TypeFreshener {
54 infcx,
55 ty_freshen_count: 0,
56 const_freshen_count: 0,
57 ty_freshen_map: Default::default(),
58 const_freshen_map: Default::default(),
59 }
60 }
61
62 fn freshen_ty<F>(&mut self, input: Result<Ty<'tcx>, ty::InferTy>, mk_fresh: F) -> Ty<'tcx>
63 where
64 F: FnOnce(u32) -> Ty<'tcx>,
65 {
66 match input {
67 Ok(ty) => ty.fold_with(self),
68 Err(key) => match self.ty_freshen_map.entry(key) {
69 Entry::Occupied(entry) => *entry.get(),
70 Entry::Vacant(entry) => {
71 let index = self.ty_freshen_count;
72 self.ty_freshen_count += 1;
73 let t = mk_fresh(index);
74 entry.insert(t);
75 t
76 }
77 },
78 }
79 }
80
81 fn freshen_const<F>(
82 &mut self,
83 input: Result<ty::Const<'tcx>, ty::InferConst>,
84 freshener: F,
85 ) -> ty::Const<'tcx>
86 where
87 F: FnOnce(u32) -> ty::InferConst,
88 {
89 match input {
90 Ok(ct) => ct.fold_with(self),
91 Err(key) => match self.const_freshen_map.entry(key) {
92 Entry::Occupied(entry) => *entry.get(),
93 Entry::Vacant(entry) => {
94 let index = self.const_freshen_count;
95 self.const_freshen_count += 1;
96 let ct = ty::Const::new_infer(self.infcx.tcx, freshener(index));
97 entry.insert(ct);
98 ct
99 }
100 },
101 }
102 }
103}
104
105impl<'a, 'tcx> TypeFolder<TyCtxt<'tcx>> for TypeFreshener<'a, 'tcx> {
106 fn cx(&self) -> TyCtxt<'tcx> {
107 self.infcx.tcx
108 }
109
110 fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
111 match *r {
112 ty::ReBound(..) => {
113 r
115 }
116
117 ty::ReEarlyParam(..)
118 | ty::ReLateParam(_)
119 | ty::ReVar(_)
120 | ty::RePlaceholder(..)
121 | ty::ReStatic
122 | ty::ReError(_)
123 | ty::ReErased => self.cx().lifetimes.re_erased,
124 }
125 }
126
127 #[inline]
128 fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
129 if !t.has_infer() && !t.has_erasable_regions() {
130 t
131 } else {
132 match *t.kind() {
133 ty::Infer(v) => self.fold_infer_ty(v).unwrap_or(t),
134
135 #[cfg(debug_assertions)]
138 ty::Placeholder(..) | ty::Bound(..) => bug!("unexpected type {:?}", t),
139
140 _ => t.super_fold_with(self),
141 }
142 }
143 }
144
145 fn fold_const(&mut self, ct: ty::Const<'tcx>) -> ty::Const<'tcx> {
146 match ct.kind() {
147 ty::ConstKind::Infer(ty::InferConst::Var(v)) => {
148 let mut inner = self.infcx.inner.borrow_mut();
149 let input =
150 inner.const_unification_table().probe_value(v).known().ok_or_else(|| {
151 ty::InferConst::Var(inner.const_unification_table().find(v).vid)
152 });
153 drop(inner);
154 self.freshen_const(input, ty::InferConst::Fresh)
155 }
156 ty::ConstKind::Infer(ty::InferConst::Fresh(i)) => {
157 if i >= self.const_freshen_count {
158 bug!(
159 "Encountered a freshend const with id {} \
160 but our counter is only at {}",
161 i,
162 self.const_freshen_count,
163 );
164 }
165 ct
166 }
167
168 ty::ConstKind::Bound(..) | ty::ConstKind::Placeholder(_) => {
169 bug!("unexpected const {:?}", ct)
170 }
171
172 ty::ConstKind::Param(_)
173 | ty::ConstKind::Value(_)
174 | ty::ConstKind::Unevaluated(..)
175 | ty::ConstKind::Expr(..)
176 | ty::ConstKind::Error(_) => ct.super_fold_with(self),
177 }
178 }
179}
180
181impl<'a, 'tcx> TypeFreshener<'a, 'tcx> {
182 #[inline(never)]
184 fn fold_infer_ty(&mut self, v: ty::InferTy) -> Option<Ty<'tcx>> {
185 match v {
186 ty::TyVar(v) => {
187 let mut inner = self.infcx.inner.borrow_mut();
188 let input = inner
189 .type_variables()
190 .probe(v)
191 .known()
192 .ok_or_else(|| ty::TyVar(inner.type_variables().root_var(v)));
193 drop(inner);
194 Some(self.freshen_ty(input, |n| Ty::new_fresh(self.infcx.tcx, n)))
195 }
196
197 ty::IntVar(v) => {
198 let mut inner = self.infcx.inner.borrow_mut();
199 let value = inner.int_unification_table().probe_value(v);
200 let input = match value {
201 ty::IntVarValue::IntType(ty) => Ok(Ty::new_int(self.infcx.tcx, ty)),
202 ty::IntVarValue::UintType(ty) => Ok(Ty::new_uint(self.infcx.tcx, ty)),
203 ty::IntVarValue::Unknown => {
204 Err(ty::IntVar(inner.int_unification_table().find(v)))
205 }
206 };
207 drop(inner);
208 Some(self.freshen_ty(input, |n| Ty::new_fresh_int(self.infcx.tcx, n)))
209 }
210
211 ty::FloatVar(v) => {
212 let mut inner = self.infcx.inner.borrow_mut();
213 let value = inner.float_unification_table().probe_value(v);
214 let input = match value {
215 ty::FloatVarValue::Known(ty) => Ok(Ty::new_float(self.infcx.tcx, ty)),
216 ty::FloatVarValue::Unknown => {
217 Err(ty::FloatVar(inner.float_unification_table().find(v)))
218 }
219 };
220 drop(inner);
221 Some(self.freshen_ty(input, |n| Ty::new_fresh_float(self.infcx.tcx, n)))
222 }
223
224 ty::FreshTy(ct) | ty::FreshIntTy(ct) | ty::FreshFloatTy(ct) => {
225 if ct >= self.ty_freshen_count {
226 bug!(
227 "Encountered a freshend type with id {} \
228 but our counter is only at {}",
229 ct,
230 self.ty_freshen_count
231 );
232 }
233 None
234 }
235 }
236 }
237}