]>
Commit | Line | Data |
---|---|---|
dc9dc135 | 1 | use super::type_variable::{TypeVariableOrigin, TypeVariableOriginKind}; |
dfeec247 | 2 | use super::{FixupError, FixupResult, InferCtxt, Span}; |
ba9703b0 XL |
3 | use rustc_middle::ty::fold::{TypeFolder, TypeVisitor}; |
4 | use rustc_middle::ty::{self, Const, InferConst, Ty, TyCtxt, TypeFoldable}; | |
1a4d82fc JJ |
5 | |
6 | /////////////////////////////////////////////////////////////////////////// | |
dc9dc135 | 7 | // OPPORTUNISTIC VAR RESOLVER |
1a4d82fc | 8 | |
dc9dc135 XL |
9 | /// The opportunistic resolver can be used at any time. It simply replaces |
10 | /// type/const variables that have been unified with the things they have | |
1a4d82fc JJ |
11 | /// been unified with (similar to `shallow_resolve`, but deep). This is |
12 | /// useful for printing messages etc but also required at various | |
13 | /// points for correctness. | |
dc9dc135 XL |
14 | pub struct OpportunisticVarResolver<'a, 'tcx> { |
15 | infcx: &'a InferCtxt<'a, 'tcx>, | |
1a4d82fc JJ |
16 | } |
17 | ||
dc9dc135 | 18 | impl<'a, 'tcx> OpportunisticVarResolver<'a, 'tcx> { |
a1dfa0c6 | 19 | #[inline] |
dc9dc135 XL |
20 | pub fn new(infcx: &'a InferCtxt<'a, 'tcx>) -> Self { |
21 | OpportunisticVarResolver { infcx } | |
1a4d82fc JJ |
22 | } |
23 | } | |
24 | ||
dc9dc135 XL |
25 | impl<'a, 'tcx> TypeFolder<'tcx> for OpportunisticVarResolver<'a, 'tcx> { |
26 | fn tcx<'b>(&'b self) -> TyCtxt<'tcx> { | |
1a4d82fc JJ |
27 | self.infcx.tcx |
28 | } | |
29 | ||
30 | fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> { | |
74b04a01 | 31 | if !t.has_infer_types_or_consts() { |
1a4d82fc JJ |
32 | t // micro-optimize -- if there is nothing in this type that this fold affects... |
33 | } else { | |
dc9dc135 XL |
34 | let t = self.infcx.shallow_resolve(t); |
35 | t.super_fold_with(self) | |
36 | } | |
37 | } | |
38 | ||
39 | fn fold_const(&mut self, ct: &'tcx Const<'tcx>) -> &'tcx Const<'tcx> { | |
74b04a01 | 40 | if !ct.has_infer_types_or_consts() { |
dc9dc135 XL |
41 | ct // micro-optimize -- if there is nothing in this const that this fold affects... |
42 | } else { | |
43 | let ct = self.infcx.shallow_resolve(ct); | |
44 | ct.super_fold_with(self) | |
9cc50fc6 SL |
45 | } |
46 | } | |
47 | } | |
48 | ||
49 | /// The opportunistic type and region resolver is similar to the | |
3b2f2976 | 50 | /// opportunistic type resolver, but also opportunistically resolves |
9cc50fc6 | 51 | /// regions. It is useful for canonicalization. |
dc9dc135 XL |
52 | pub struct OpportunisticTypeAndRegionResolver<'a, 'tcx> { |
53 | infcx: &'a InferCtxt<'a, 'tcx>, | |
9cc50fc6 SL |
54 | } |
55 | ||
dc9dc135 XL |
56 | impl<'a, 'tcx> OpportunisticTypeAndRegionResolver<'a, 'tcx> { |
57 | pub fn new(infcx: &'a InferCtxt<'a, 'tcx>) -> Self { | |
a1dfa0c6 | 58 | OpportunisticTypeAndRegionResolver { infcx } |
9cc50fc6 SL |
59 | } |
60 | } | |
61 | ||
dc9dc135 XL |
62 | impl<'a, 'tcx> TypeFolder<'tcx> for OpportunisticTypeAndRegionResolver<'a, 'tcx> { |
63 | fn tcx<'b>(&'b self) -> TyCtxt<'tcx> { | |
9cc50fc6 SL |
64 | self.infcx.tcx |
65 | } | |
66 | ||
67 | fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> { | |
68 | if !t.needs_infer() { | |
69 | t // micro-optimize -- if there is nothing in this type that this fold affects... | |
70 | } else { | |
71 | let t0 = self.infcx.shallow_resolve(t); | |
72 | t0.super_fold_with(self) | |
73 | } | |
74 | } | |
75 | ||
7cac9316 | 76 | fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> { |
9e0c209e | 77 | match *r { |
74b04a01 XL |
78 | ty::ReVar(rid) => self |
79 | .infcx | |
80 | .inner | |
81 | .borrow_mut() | |
82 | .unwrap_region_constraints() | |
83 | .opportunistic_resolve_var(self.tcx(), rid), | |
dfeec247 | 84 | _ => r, |
1a4d82fc JJ |
85 | } |
86 | } | |
48663c56 XL |
87 | |
88 | fn fold_const(&mut self, ct: &'tcx ty::Const<'tcx>) -> &'tcx ty::Const<'tcx> { | |
89 | if !ct.needs_infer() { | |
90 | ct // micro-optimize -- if there is nothing in this const that this fold affects... | |
91 | } else { | |
92 | let c0 = self.infcx.shallow_resolve(ct); | |
93 | c0.super_fold_with(self) | |
94 | } | |
95 | } | |
1a4d82fc JJ |
96 | } |
97 | ||
3b2f2976 XL |
98 | /////////////////////////////////////////////////////////////////////////// |
99 | // UNRESOLVED TYPE FINDER | |
100 | ||
48663c56 XL |
101 | /// The unresolved type **finder** walks a type searching for |
102 | /// type variables that don't yet have a value. The first unresolved type is stored. | |
103 | /// It does not construct the fully resolved type (which might | |
3b2f2976 | 104 | /// involve some hashing and so forth). |
dc9dc135 XL |
105 | pub struct UnresolvedTypeFinder<'a, 'tcx> { |
106 | infcx: &'a InferCtxt<'a, 'tcx>, | |
48663c56 XL |
107 | |
108 | /// Used to find the type parameter name and location for error reporting. | |
dc9dc135 | 109 | pub first_unresolved: Option<(Ty<'tcx>, Option<Span>)>, |
3b2f2976 XL |
110 | } |
111 | ||
dc9dc135 XL |
112 | impl<'a, 'tcx> UnresolvedTypeFinder<'a, 'tcx> { |
113 | pub fn new(infcx: &'a InferCtxt<'a, 'tcx>) -> Self { | |
48663c56 | 114 | UnresolvedTypeFinder { infcx, first_unresolved: None } |
3b2f2976 XL |
115 | } |
116 | } | |
117 | ||
dc9dc135 | 118 | impl<'a, 'tcx> TypeVisitor<'tcx> for UnresolvedTypeFinder<'a, 'tcx> { |
3b2f2976 XL |
119 | fn visit_ty(&mut self, t: Ty<'tcx>) -> bool { |
120 | let t = self.infcx.shallow_resolve(t); | |
121 | if t.has_infer_types() { | |
e74abb32 | 122 | if let ty::Infer(infer_ty) = t.kind { |
3b2f2976 XL |
123 | // Since we called `shallow_resolve` above, this must |
124 | // be an (as yet...) unresolved inference variable. | |
dfeec247 | 125 | let ty_var_span = if let ty::TyVar(ty_vid) = infer_ty { |
f9f354fc XL |
126 | let mut inner = self.infcx.inner.borrow_mut(); |
127 | let ty_vars = &inner.type_variables(); | |
dc9dc135 | 128 | if let TypeVariableOrigin { |
dfeec247 | 129 | kind: TypeVariableOriginKind::TypeParameterDefinition(_, _), |
dc9dc135 XL |
130 | span, |
131 | } = *ty_vars.var_origin(ty_vid) | |
48663c56 XL |
132 | { |
133 | Some(span) | |
134 | } else { | |
135 | None | |
136 | } | |
137 | } else { | |
138 | None | |
139 | }; | |
140 | self.first_unresolved = Some((t, ty_var_span)); | |
dfeec247 | 141 | true // Halt visiting. |
3b2f2976 XL |
142 | } else { |
143 | // Otherwise, visit its contents. | |
144 | t.super_visit_with(self) | |
145 | } | |
146 | } else { | |
48663c56 XL |
147 | // All type variables in inference types must already be resolved, |
148 | // - no need to visit the contents, continue visiting. | |
3b2f2976 XL |
149 | false |
150 | } | |
151 | } | |
152 | } | |
153 | ||
1a4d82fc JJ |
154 | /////////////////////////////////////////////////////////////////////////// |
155 | // FULL TYPE RESOLUTION | |
156 | ||
157 | /// Full type resolution replaces all type and region variables with | |
158 | /// their concrete results. If any variable cannot be replaced (never unified, etc) | |
159 | /// then an `Err` result is returned. | |
dc9dc135 XL |
160 | pub fn fully_resolve<'a, 'tcx, T>(infcx: &InferCtxt<'a, 'tcx>, value: &T) -> FixupResult<'tcx, T> |
161 | where | |
162 | T: TypeFoldable<'tcx>, | |
1a4d82fc | 163 | { |
74b04a01 | 164 | let mut full_resolver = FullTypeResolver { infcx, err: None }; |
1a4d82fc JJ |
165 | let result = value.fold_with(&mut full_resolver); |
166 | match full_resolver.err { | |
167 | None => Ok(result), | |
168 | Some(e) => Err(e), | |
169 | } | |
170 | } | |
171 | ||
172 | // N.B. This type is not public because the protocol around checking the | |
74b04a01 | 173 | // `err` field is not enforceable otherwise. |
dc9dc135 XL |
174 | struct FullTypeResolver<'a, 'tcx> { |
175 | infcx: &'a InferCtxt<'a, 'tcx>, | |
48663c56 | 176 | err: Option<FixupError<'tcx>>, |
1a4d82fc JJ |
177 | } |
178 | ||
dc9dc135 XL |
179 | impl<'a, 'tcx> TypeFolder<'tcx> for FullTypeResolver<'a, 'tcx> { |
180 | fn tcx<'b>(&'b self) -> TyCtxt<'tcx> { | |
1a4d82fc JJ |
181 | self.infcx.tcx |
182 | } | |
183 | ||
184 | fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> { | |
ba9703b0 | 185 | if !t.needs_infer() { |
1a4d82fc JJ |
186 | t // micro-optimize -- if there is nothing in this type that this fold affects... |
187 | } else { | |
188 | let t = self.infcx.shallow_resolve(t); | |
e74abb32 | 189 | match t.kind { |
b7449926 | 190 | ty::Infer(ty::TyVar(vid)) => { |
c1a9b12d | 191 | self.err = Some(FixupError::UnresolvedTy(vid)); |
1a4d82fc JJ |
192 | self.tcx().types.err |
193 | } | |
b7449926 | 194 | ty::Infer(ty::IntVar(vid)) => { |
c1a9b12d | 195 | self.err = Some(FixupError::UnresolvedIntTy(vid)); |
1a4d82fc JJ |
196 | self.tcx().types.err |
197 | } | |
b7449926 | 198 | ty::Infer(ty::FloatVar(vid)) => { |
c1a9b12d | 199 | self.err = Some(FixupError::UnresolvedFloatTy(vid)); |
1a4d82fc JJ |
200 | self.tcx().types.err |
201 | } | |
b7449926 | 202 | ty::Infer(_) => { |
54a0048b | 203 | bug!("Unexpected type in full type resolver: {:?}", t); |
1a4d82fc | 204 | } |
dfeec247 | 205 | _ => t.super_fold_with(self), |
1a4d82fc JJ |
206 | } |
207 | } | |
208 | } | |
209 | ||
7cac9316 | 210 | fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> { |
9e0c209e | 211 | match *r { |
dfeec247 XL |
212 | ty::ReVar(rid) => self |
213 | .infcx | |
214 | .lexical_region_resolutions | |
215 | .borrow() | |
216 | .as_ref() | |
217 | .expect("region resolution not performed") | |
218 | .resolve_var(rid), | |
9e0c209e | 219 | _ => r, |
1a4d82fc JJ |
220 | } |
221 | } | |
48663c56 XL |
222 | |
223 | fn fold_const(&mut self, c: &'tcx ty::Const<'tcx>) -> &'tcx ty::Const<'tcx> { | |
ba9703b0 | 224 | if !c.needs_infer() { |
48663c56 | 225 | c // micro-optimize -- if there is nothing in this const that this fold affects... |
48663c56 XL |
226 | } else { |
227 | let c = self.infcx.shallow_resolve(c); | |
228 | match c.val { | |
60c5eb7d | 229 | ty::ConstKind::Infer(InferConst::Var(vid)) => { |
48663c56 | 230 | self.err = Some(FixupError::UnresolvedConst(vid)); |
ba9703b0 | 231 | return self.tcx().mk_const(ty::Const { val: ty::ConstKind::Error, ty: c.ty }); |
48663c56 | 232 | } |
60c5eb7d | 233 | ty::ConstKind::Infer(InferConst::Fresh(_)) => { |
48663c56 XL |
234 | bug!("Unexpected const in full const resolver: {:?}", c); |
235 | } | |
236 | _ => {} | |
237 | } | |
238 | c.super_fold_with(self) | |
239 | } | |
240 | } | |
1a4d82fc | 241 | } |