]>
Commit | Line | Data |
---|---|---|
dc9dc135 | 1 | use super::type_variable::{TypeVariableOrigin, TypeVariableOriginKind}; |
dfeec247 | 2 | use super::{FixupError, FixupResult, InferCtxt, Span}; |
487cf647 | 3 | use rustc_middle::infer::unify_key::{ConstVariableOrigin, ConstVariableOriginKind}; |
064997fb FG |
4 | use rustc_middle::ty::fold::{FallibleTypeFolder, TypeFolder, TypeSuperFoldable}; |
5 | use rustc_middle::ty::visit::{TypeSuperVisitable, TypeVisitor}; | |
6 | use rustc_middle::ty::{self, Const, InferConst, Ty, TyCtxt, TypeFoldable, TypeVisitable}; | |
1a4d82fc | 7 | |
29967ef6 XL |
8 | use std::ops::ControlFlow; |
9 | ||
1a4d82fc | 10 | /////////////////////////////////////////////////////////////////////////// |
dc9dc135 | 11 | // OPPORTUNISTIC VAR RESOLVER |
1a4d82fc | 12 | |
dc9dc135 XL |
13 | /// The opportunistic resolver can be used at any time. It simply replaces |
14 | /// type/const variables that have been unified with the things they have | |
1a4d82fc JJ |
15 | /// been unified with (similar to `shallow_resolve`, but deep). This is |
16 | /// useful for printing messages etc but also required at various | |
17 | /// points for correctness. | |
dc9dc135 | 18 | pub struct OpportunisticVarResolver<'a, 'tcx> { |
2b03887a | 19 | infcx: &'a InferCtxt<'tcx>, |
1a4d82fc JJ |
20 | } |
21 | ||
dc9dc135 | 22 | impl<'a, 'tcx> OpportunisticVarResolver<'a, 'tcx> { |
a1dfa0c6 | 23 | #[inline] |
2b03887a | 24 | pub fn new(infcx: &'a InferCtxt<'tcx>) -> Self { |
dc9dc135 | 25 | OpportunisticVarResolver { infcx } |
1a4d82fc JJ |
26 | } |
27 | } | |
28 | ||
dc9dc135 XL |
29 | impl<'a, 'tcx> TypeFolder<'tcx> for OpportunisticVarResolver<'a, 'tcx> { |
30 | fn tcx<'b>(&'b self) -> TyCtxt<'tcx> { | |
1a4d82fc JJ |
31 | self.infcx.tcx |
32 | } | |
33 | ||
34 | fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> { | |
2b03887a | 35 | if !t.has_non_region_infer() { |
1a4d82fc JJ |
36 | t // micro-optimize -- if there is nothing in this type that this fold affects... |
37 | } else { | |
dc9dc135 XL |
38 | let t = self.infcx.shallow_resolve(t); |
39 | t.super_fold_with(self) | |
40 | } | |
41 | } | |
42 | ||
5099ac24 | 43 | fn fold_const(&mut self, ct: Const<'tcx>) -> Const<'tcx> { |
2b03887a | 44 | if !ct.has_non_region_infer() { |
dc9dc135 XL |
45 | ct // micro-optimize -- if there is nothing in this const that this fold affects... |
46 | } else { | |
47 | let ct = self.infcx.shallow_resolve(ct); | |
48 | ct.super_fold_with(self) | |
9cc50fc6 SL |
49 | } |
50 | } | |
51 | } | |
52 | ||
f035d41b XL |
53 | /// The opportunistic region resolver opportunistically resolves regions |
54 | /// variables to the variable with the least variable id. It is used when | |
5e7ed085 | 55 | /// normalizing projections to avoid hitting the recursion limit by creating |
f035d41b XL |
56 | /// many versions of a predicate for types that in the end have to unify. |
57 | /// | |
58 | /// If you want to resolve type and const variables as well, call | |
59 | /// [InferCtxt::resolve_vars_if_possible] first. | |
60 | pub struct OpportunisticRegionResolver<'a, 'tcx> { | |
2b03887a | 61 | infcx: &'a InferCtxt<'tcx>, |
9cc50fc6 SL |
62 | } |
63 | ||
f035d41b | 64 | impl<'a, 'tcx> OpportunisticRegionResolver<'a, 'tcx> { |
2b03887a | 65 | pub fn new(infcx: &'a InferCtxt<'tcx>) -> Self { |
f035d41b | 66 | OpportunisticRegionResolver { infcx } |
9cc50fc6 SL |
67 | } |
68 | } | |
69 | ||
f035d41b | 70 | impl<'a, 'tcx> TypeFolder<'tcx> for OpportunisticRegionResolver<'a, 'tcx> { |
dc9dc135 | 71 | fn tcx<'b>(&'b self) -> TyCtxt<'tcx> { |
9cc50fc6 SL |
72 | self.infcx.tcx |
73 | } | |
74 | ||
75 | fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> { | |
f035d41b | 76 | if !t.has_infer_regions() { |
9cc50fc6 SL |
77 | t // micro-optimize -- if there is nothing in this type that this fold affects... |
78 | } else { | |
f035d41b | 79 | t.super_fold_with(self) |
9cc50fc6 SL |
80 | } |
81 | } | |
82 | ||
7cac9316 | 83 | fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> { |
9e0c209e | 84 | match *r { |
f035d41b XL |
85 | ty::ReVar(rid) => { |
86 | let resolved = self | |
87 | .infcx | |
88 | .inner | |
89 | .borrow_mut() | |
90 | .unwrap_region_constraints() | |
91 | .opportunistic_resolve_var(rid); | |
064997fb | 92 | TypeFolder::tcx(self).reuse_or_mk_region(r, ty::ReVar(resolved)) |
f035d41b | 93 | } |
dfeec247 | 94 | _ => r, |
1a4d82fc JJ |
95 | } |
96 | } | |
48663c56 | 97 | |
5099ac24 | 98 | fn fold_const(&mut self, ct: ty::Const<'tcx>) -> ty::Const<'tcx> { |
f035d41b | 99 | if !ct.has_infer_regions() { |
48663c56 XL |
100 | ct // micro-optimize -- if there is nothing in this const that this fold affects... |
101 | } else { | |
f035d41b | 102 | ct.super_fold_with(self) |
48663c56 XL |
103 | } |
104 | } | |
1a4d82fc JJ |
105 | } |
106 | ||
3b2f2976 XL |
107 | /////////////////////////////////////////////////////////////////////////// |
108 | // UNRESOLVED TYPE FINDER | |
109 | ||
48663c56 XL |
110 | /// The unresolved type **finder** walks a type searching for |
111 | /// type variables that don't yet have a value. The first unresolved type is stored. | |
112 | /// It does not construct the fully resolved type (which might | |
3b2f2976 | 113 | /// involve some hashing and so forth). |
487cf647 | 114 | pub struct UnresolvedTypeOrConstFinder<'a, 'tcx> { |
2b03887a | 115 | infcx: &'a InferCtxt<'tcx>, |
3b2f2976 XL |
116 | } |
117 | ||
487cf647 | 118 | impl<'a, 'tcx> UnresolvedTypeOrConstFinder<'a, 'tcx> { |
2b03887a | 119 | pub fn new(infcx: &'a InferCtxt<'tcx>) -> Self { |
487cf647 | 120 | UnresolvedTypeOrConstFinder { infcx } |
3b2f2976 XL |
121 | } |
122 | } | |
123 | ||
487cf647 FG |
124 | impl<'a, 'tcx> TypeVisitor<'tcx> for UnresolvedTypeOrConstFinder<'a, 'tcx> { |
125 | type BreakTy = (ty::Term<'tcx>, Option<Span>); | |
fc512014 | 126 | fn visit_ty(&mut self, t: Ty<'tcx>) -> ControlFlow<Self::BreakTy> { |
3b2f2976 | 127 | let t = self.infcx.shallow_resolve(t); |
487cf647 FG |
128 | if let ty::Infer(infer_ty) = *t.kind() { |
129 | // Since we called `shallow_resolve` above, this must | |
130 | // be an (as yet...) unresolved inference variable. | |
131 | let ty_var_span = if let ty::TyVar(ty_vid) = infer_ty { | |
132 | let mut inner = self.infcx.inner.borrow_mut(); | |
133 | let ty_vars = &inner.type_variables(); | |
134 | if let TypeVariableOrigin { | |
135 | kind: TypeVariableOriginKind::TypeParameterDefinition(_, _), | |
136 | span, | |
137 | } = *ty_vars.var_origin(ty_vid) | |
138 | { | |
139 | Some(span) | |
48663c56 XL |
140 | } else { |
141 | None | |
487cf647 | 142 | } |
3b2f2976 | 143 | } else { |
487cf647 FG |
144 | None |
145 | }; | |
146 | ControlFlow::Break((t.into(), ty_var_span)) | |
147 | } else if !t.has_non_region_infer() { | |
148 | // All const/type variables in inference types must already be resolved, | |
149 | // no need to visit the contents. | |
9c376795 | 150 | ControlFlow::Continue(()) |
3b2f2976 | 151 | } else { |
487cf647 FG |
152 | // Otherwise, keep visiting. |
153 | t.super_visit_with(self) | |
154 | } | |
155 | } | |
156 | ||
157 | fn visit_const(&mut self, ct: ty::Const<'tcx>) -> ControlFlow<Self::BreakTy> { | |
158 | let ct = self.infcx.shallow_resolve(ct); | |
159 | if let ty::ConstKind::Infer(i) = ct.kind() { | |
160 | // Since we called `shallow_resolve` above, this must | |
161 | // be an (as yet...) unresolved inference variable. | |
162 | let ct_var_span = if let ty::InferConst::Var(vid) = i { | |
163 | let mut inner = self.infcx.inner.borrow_mut(); | |
164 | let ct_vars = &mut inner.const_unification_table(); | |
165 | if let ConstVariableOrigin { | |
166 | span, | |
167 | kind: ConstVariableOriginKind::ConstParameterDefinition(_, _), | |
168 | } = ct_vars.probe_value(vid).origin | |
169 | { | |
170 | Some(span) | |
171 | } else { | |
172 | None | |
173 | } | |
174 | } else { | |
175 | None | |
176 | }; | |
177 | ControlFlow::Break((ct.into(), ct_var_span)) | |
178 | } else if !ct.has_non_region_infer() { | |
179 | // All const/type variables in inference types must already be resolved, | |
180 | // no need to visit the contents. | |
9c376795 | 181 | ControlFlow::Continue(()) |
487cf647 FG |
182 | } else { |
183 | // Otherwise, keep visiting. | |
184 | ct.super_visit_with(self) | |
3b2f2976 XL |
185 | } |
186 | } | |
187 | } | |
188 | ||
1a4d82fc JJ |
189 | /////////////////////////////////////////////////////////////////////////// |
190 | // FULL TYPE RESOLUTION | |
191 | ||
192 | /// Full type resolution replaces all type and region variables with | |
193 | /// their concrete results. If any variable cannot be replaced (never unified, etc) | |
194 | /// then an `Err` result is returned. | |
2b03887a | 195 | pub fn fully_resolve<'tcx, T>(infcx: &InferCtxt<'tcx>, value: T) -> FixupResult<'tcx, T> |
dc9dc135 XL |
196 | where |
197 | T: TypeFoldable<'tcx>, | |
1a4d82fc | 198 | { |
a2a8927a | 199 | value.try_fold_with(&mut FullTypeResolver { infcx }) |
1a4d82fc JJ |
200 | } |
201 | ||
202 | // N.B. This type is not public because the protocol around checking the | |
74b04a01 | 203 | // `err` field is not enforceable otherwise. |
dc9dc135 | 204 | struct FullTypeResolver<'a, 'tcx> { |
2b03887a | 205 | infcx: &'a InferCtxt<'tcx>, |
1a4d82fc JJ |
206 | } |
207 | ||
064997fb | 208 | impl<'a, 'tcx> FallibleTypeFolder<'tcx> for FullTypeResolver<'a, 'tcx> { |
a2a8927a XL |
209 | type Error = FixupError<'tcx>; |
210 | ||
dc9dc135 | 211 | fn tcx<'b>(&'b self) -> TyCtxt<'tcx> { |
1a4d82fc JJ |
212 | self.infcx.tcx |
213 | } | |
214 | ||
a2a8927a | 215 | fn try_fold_ty(&mut self, t: Ty<'tcx>) -> Result<Ty<'tcx>, Self::Error> { |
ba9703b0 | 216 | if !t.needs_infer() { |
a2a8927a | 217 | Ok(t) // micro-optimize -- if there is nothing in this type that this fold affects... |
1a4d82fc JJ |
218 | } else { |
219 | let t = self.infcx.shallow_resolve(t); | |
1b1a35ee | 220 | match *t.kind() { |
a2a8927a XL |
221 | ty::Infer(ty::TyVar(vid)) => Err(FixupError::UnresolvedTy(vid)), |
222 | ty::Infer(ty::IntVar(vid)) => Err(FixupError::UnresolvedIntTy(vid)), | |
223 | ty::Infer(ty::FloatVar(vid)) => Err(FixupError::UnresolvedFloatTy(vid)), | |
b7449926 | 224 | ty::Infer(_) => { |
54a0048b | 225 | bug!("Unexpected type in full type resolver: {:?}", t); |
1a4d82fc | 226 | } |
a2a8927a | 227 | _ => t.try_super_fold_with(self), |
1a4d82fc JJ |
228 | } |
229 | } | |
230 | } | |
231 | ||
a2a8927a | 232 | fn try_fold_region(&mut self, r: ty::Region<'tcx>) -> Result<ty::Region<'tcx>, Self::Error> { |
9e0c209e | 233 | match *r { |
064997fb | 234 | ty::ReVar(_) => Ok(self |
dfeec247 XL |
235 | .infcx |
236 | .lexical_region_resolutions | |
237 | .borrow() | |
238 | .as_ref() | |
239 | .expect("region resolution not performed") | |
064997fb | 240 | .resolve_region(self.infcx.tcx, r)), |
a2a8927a | 241 | _ => Ok(r), |
1a4d82fc JJ |
242 | } |
243 | } | |
48663c56 | 244 | |
5099ac24 | 245 | fn try_fold_const(&mut self, c: ty::Const<'tcx>) -> Result<ty::Const<'tcx>, Self::Error> { |
ba9703b0 | 246 | if !c.needs_infer() { |
a2a8927a | 247 | Ok(c) // micro-optimize -- if there is nothing in this const that this fold affects... |
48663c56 XL |
248 | } else { |
249 | let c = self.infcx.shallow_resolve(c); | |
923072b8 | 250 | match c.kind() { |
60c5eb7d | 251 | ty::ConstKind::Infer(InferConst::Var(vid)) => { |
a2a8927a | 252 | return Err(FixupError::UnresolvedConst(vid)); |
48663c56 | 253 | } |
60c5eb7d | 254 | ty::ConstKind::Infer(InferConst::Fresh(_)) => { |
48663c56 XL |
255 | bug!("Unexpected const in full const resolver: {:?}", c); |
256 | } | |
257 | _ => {} | |
258 | } | |
a2a8927a | 259 | c.try_super_fold_with(self) |
48663c56 XL |
260 | } |
261 | } | |
1a4d82fc | 262 | } |