Skip to content

Commit 7308b76

Browse files
Adapt TypeFolder implementors to return a Result
1 parent 0cb4a67 commit 7308b76

File tree

24 files changed

+332
-263
lines changed

24 files changed

+332
-263
lines changed

compiler/rustc_infer/src/infer/canonical/canonicalizer.rs

+26-23
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ impl<'cx, 'tcx> TypeFolder<'tcx> for Canonicalizer<'cx, 'tcx> {
293293
self.tcx
294294
}
295295

296-
fn fold_binder<T>(&mut self, t: ty::Binder<'tcx, T>) -> ty::Binder<'tcx, T>
296+
fn fold_binder<T>(&mut self, t: ty::Binder<'tcx, T>) -> Result<ty::Binder<'tcx, T>, Self::Error>
297297
where
298298
T: TypeFoldable<'tcx>,
299299
{
@@ -303,13 +303,13 @@ impl<'cx, 'tcx> TypeFolder<'tcx> for Canonicalizer<'cx, 'tcx> {
303303
t
304304
}
305305

306-
fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
306+
fn fold_region(&mut self, r: ty::Region<'tcx>) -> Result<ty::Region<'tcx>, Self::Error> {
307307
match *r {
308308
ty::ReLateBound(index, ..) => {
309309
if index >= self.binder_index {
310310
bug!("escaping late-bound region during canonicalization");
311311
} else {
312-
r
312+
Ok(r)
313313
}
314314
}
315315

@@ -327,19 +327,19 @@ impl<'cx, 'tcx> TypeFolder<'tcx> for Canonicalizer<'cx, 'tcx> {
327327
vid, r
328328
);
329329
let r = self.tcx.reuse_or_mk_region(r, ty::ReVar(resolved_vid));
330-
self.canonicalize_region_mode.canonicalize_free_region(self, r)
330+
Ok(self.canonicalize_region_mode.canonicalize_free_region(self, r))
331331
}
332332

333333
ty::ReStatic
334334
| ty::ReEarlyBound(..)
335335
| ty::ReFree(_)
336336
| ty::ReEmpty(_)
337337
| ty::RePlaceholder(..)
338-
| ty::ReErased => self.canonicalize_region_mode.canonicalize_free_region(self, r),
338+
| ty::ReErased => Ok(self.canonicalize_region_mode.canonicalize_free_region(self, r)),
339339
}
340340
}
341341

342-
fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
342+
fn fold_ty(&mut self, t: Ty<'tcx>) -> Result<Ty<'tcx>, Self::Error> {
343343
match *t.kind() {
344344
ty::Infer(ty::TyVar(vid)) => {
345345
debug!("canonical: type var found with vid {:?}", vid);
@@ -355,40 +355,40 @@ impl<'cx, 'tcx> TypeFolder<'tcx> for Canonicalizer<'cx, 'tcx> {
355355
Err(mut ui) => {
356356
// FIXME: perf problem described in #55921.
357357
ui = ty::UniverseIndex::ROOT;
358-
self.canonicalize_ty_var(
358+
Ok(self.canonicalize_ty_var(
359359
CanonicalVarInfo {
360360
kind: CanonicalVarKind::Ty(CanonicalTyVarKind::General(ui)),
361361
},
362362
t,
363-
)
363+
))
364364
}
365365
}
366366
}
367367

368-
ty::Infer(ty::IntVar(_)) => self.canonicalize_ty_var(
368+
ty::Infer(ty::IntVar(_)) => Ok(self.canonicalize_ty_var(
369369
CanonicalVarInfo { kind: CanonicalVarKind::Ty(CanonicalTyVarKind::Int) },
370370
t,
371-
),
371+
)),
372372

373-
ty::Infer(ty::FloatVar(_)) => self.canonicalize_ty_var(
373+
ty::Infer(ty::FloatVar(_)) => Ok(self.canonicalize_ty_var(
374374
CanonicalVarInfo { kind: CanonicalVarKind::Ty(CanonicalTyVarKind::Float) },
375375
t,
376-
),
376+
)),
377377

378378
ty::Infer(ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_)) => {
379379
bug!("encountered a fresh type during canonicalization")
380380
}
381381

382-
ty::Placeholder(placeholder) => self.canonicalize_ty_var(
382+
ty::Placeholder(placeholder) => Ok(self.canonicalize_ty_var(
383383
CanonicalVarInfo { kind: CanonicalVarKind::PlaceholderTy(placeholder) },
384384
t,
385-
),
385+
)),
386386

387387
ty::Bound(debruijn, _) => {
388388
if debruijn >= self.binder_index {
389389
bug!("escaping bound type during canonicalization")
390390
} else {
391-
t
391+
Ok(t)
392392
}
393393
}
394394

@@ -419,13 +419,16 @@ impl<'cx, 'tcx> TypeFolder<'tcx> for Canonicalizer<'cx, 'tcx> {
419419
if t.flags().intersects(self.needs_canonical_flags) {
420420
t.super_fold_with(self)
421421
} else {
422-
t
422+
Ok(t)
423423
}
424424
}
425425
}
426426
}
427427

428-
fn fold_const(&mut self, ct: &'tcx ty::Const<'tcx>) -> &'tcx ty::Const<'tcx> {
428+
fn fold_const(
429+
&mut self,
430+
ct: &'tcx ty::Const<'tcx>,
431+
) -> Result<&'tcx ty::Const<'tcx>, Self::Error> {
429432
match ct.val {
430433
ty::ConstKind::Infer(InferConst::Var(vid)) => {
431434
debug!("canonical: const var found with vid {:?}", vid);
@@ -440,10 +443,10 @@ impl<'cx, 'tcx> TypeFolder<'tcx> for Canonicalizer<'cx, 'tcx> {
440443
Err(mut ui) => {
441444
// FIXME: perf problem described in #55921.
442445
ui = ty::UniverseIndex::ROOT;
443-
return self.canonicalize_const_var(
446+
return Ok(self.canonicalize_const_var(
444447
CanonicalVarInfo { kind: CanonicalVarKind::Const(ui) },
445448
ct,
446-
);
449+
));
447450
}
448451
}
449452
}
@@ -454,20 +457,20 @@ impl<'cx, 'tcx> TypeFolder<'tcx> for Canonicalizer<'cx, 'tcx> {
454457
if debruijn >= self.binder_index {
455458
bug!("escaping bound type during canonicalization")
456459
} else {
457-
return ct;
460+
return Ok(ct);
458461
}
459462
}
460463
ty::ConstKind::Placeholder(placeholder) => {
461-
return self.canonicalize_const_var(
464+
return Ok(self.canonicalize_const_var(
462465
CanonicalVarInfo { kind: CanonicalVarKind::PlaceholderConst(placeholder) },
463466
ct,
464-
);
467+
));
465468
}
466469
_ => {}
467470
}
468471

469472
let flags = FlagComputation::for_const(ct);
470-
if flags.intersects(self.needs_canonical_flags) { ct.super_fold_with(self) } else { ct }
473+
if flags.intersects(self.needs_canonical_flags) { ct.super_fold_with(self) } else { Ok(ct) }
471474
}
472475
}
473476

compiler/rustc_infer/src/infer/freshen.rs

+18-15
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,11 @@ impl<'a, 'tcx> TypeFolder<'tcx> for TypeFreshener<'a, 'tcx> {
117117
self.infcx.tcx
118118
}
119119

120-
fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
120+
fn fold_region(&mut self, r: ty::Region<'tcx>) -> Result<ty::Region<'tcx>, Self::Error> {
121121
match *r {
122122
ty::ReLateBound(..) => {
123123
// leave bound regions alone
124-
r
124+
Ok(r)
125125
}
126126

127127
ty::ReStatic
@@ -132,25 +132,25 @@ impl<'a, 'tcx> TypeFolder<'tcx> for TypeFreshener<'a, 'tcx> {
132132
| ty::ReEmpty(_)
133133
| ty::ReErased => {
134134
// replace all free regions with 'erased
135-
self.tcx().lifetimes.re_erased
135+
Ok(self.tcx().lifetimes.re_erased)
136136
}
137137
}
138138
}
139139

140-
fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
140+
fn fold_ty(&mut self, t: Ty<'tcx>) -> Result<Ty<'tcx>, Self::Error> {
141141
if !t.needs_infer() && !t.has_erasable_regions() {
142-
return t;
142+
return Ok(t);
143143
}
144144

145145
let tcx = self.infcx.tcx;
146146

147147
match *t.kind() {
148148
ty::Infer(ty::TyVar(v)) => {
149149
let opt_ty = self.infcx.inner.borrow_mut().type_variables().probe(v).known();
150-
self.freshen_ty(opt_ty, ty::TyVar(v), ty::FreshTy)
150+
Ok(self.freshen_ty(opt_ty, ty::TyVar(v), ty::FreshTy))
151151
}
152152

153-
ty::Infer(ty::IntVar(v)) => self.freshen_ty(
153+
ty::Infer(ty::IntVar(v)) => Ok(self.freshen_ty(
154154
self.infcx
155155
.inner
156156
.borrow_mut()
@@ -159,9 +159,9 @@ impl<'a, 'tcx> TypeFolder<'tcx> for TypeFreshener<'a, 'tcx> {
159159
.map(|v| v.to_type(tcx)),
160160
ty::IntVar(v),
161161
ty::FreshIntTy,
162-
),
162+
)),
163163

164-
ty::Infer(ty::FloatVar(v)) => self.freshen_ty(
164+
ty::Infer(ty::FloatVar(v)) => Ok(self.freshen_ty(
165165
self.infcx
166166
.inner
167167
.borrow_mut()
@@ -170,7 +170,7 @@ impl<'a, 'tcx> TypeFolder<'tcx> for TypeFreshener<'a, 'tcx> {
170170
.map(|v| v.to_type(tcx)),
171171
ty::FloatVar(v),
172172
ty::FreshFloatTy,
173-
),
173+
)),
174174

175175
ty::Infer(ty::FreshTy(ct) | ty::FreshIntTy(ct) | ty::FreshFloatTy(ct)) => {
176176
if ct >= self.ty_freshen_count {
@@ -181,7 +181,7 @@ impl<'a, 'tcx> TypeFolder<'tcx> for TypeFreshener<'a, 'tcx> {
181181
self.ty_freshen_count
182182
);
183183
}
184-
t
184+
Ok(t)
185185
}
186186

187187
ty::Generator(..)
@@ -213,7 +213,10 @@ impl<'a, 'tcx> TypeFolder<'tcx> for TypeFreshener<'a, 'tcx> {
213213
}
214214
}
215215

216-
fn fold_const(&mut self, ct: &'tcx ty::Const<'tcx>) -> &'tcx ty::Const<'tcx> {
216+
fn fold_const(
217+
&mut self,
218+
ct: &'tcx ty::Const<'tcx>,
219+
) -> Result<&'tcx ty::Const<'tcx>, Self::Error> {
217220
match ct.val {
218221
ty::ConstKind::Infer(ty::InferConst::Var(v)) => {
219222
let opt_ct = self
@@ -224,12 +227,12 @@ impl<'a, 'tcx> TypeFolder<'tcx> for TypeFreshener<'a, 'tcx> {
224227
.probe_value(v)
225228
.val
226229
.known();
227-
return self.freshen_const(
230+
return Ok(self.freshen_const(
228231
opt_ct,
229232
ty::InferConst::Var(v),
230233
ty::InferConst::Fresh,
231234
ct.ty,
232-
);
235+
));
233236
}
234237
ty::ConstKind::Infer(ty::InferConst::Fresh(i)) => {
235238
if i >= self.const_freshen_count {
@@ -240,7 +243,7 @@ impl<'a, 'tcx> TypeFolder<'tcx> for TypeFreshener<'a, 'tcx> {
240243
self.const_freshen_count,
241244
);
242245
}
243-
return ct;
246+
return Ok(ct);
244247
}
245248

246249
ty::ConstKind::Bound(..) | ty::ConstKind::Placeholder(_) => {

compiler/rustc_infer/src/infer/fudge.rs

+14-19
Original file line numberDiff line numberDiff line change
@@ -181,15 +181,15 @@ impl<'a, 'tcx> TypeFolder<'tcx> for InferenceFudger<'a, 'tcx> {
181181
self.infcx.tcx
182182
}
183183

184-
fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
184+
fn fold_ty(&mut self, ty: Ty<'tcx>) -> Result<Ty<'tcx>, Self::Error> {
185185
match *ty.kind() {
186186
ty::Infer(ty::InferTy::TyVar(vid)) => {
187187
if self.type_vars.0.contains(&vid) {
188188
// This variable was created during the fudging.
189189
// Recreate it with a fresh variable here.
190190
let idx = (vid.index - self.type_vars.0.start.index) as usize;
191191
let origin = self.type_vars.1[idx];
192-
self.infcx.next_ty_var(origin)
192+
Ok(self.infcx.next_ty_var(origin))
193193
} else {
194194
// This variable was created before the
195195
// "fudging". Since we refresh all type
@@ -199,48 +199,43 @@ impl<'a, 'tcx> TypeFolder<'tcx> for InferenceFudger<'a, 'tcx> {
199199
debug_assert!(
200200
self.infcx.inner.borrow_mut().type_variables().probe(vid).is_unknown()
201201
);
202-
ty
202+
Ok(ty)
203203
}
204204
}
205205
ty::Infer(ty::InferTy::IntVar(vid)) => {
206-
if self.int_vars.contains(&vid) {
207-
self.infcx.next_int_var()
208-
} else {
209-
ty
210-
}
206+
Ok(if self.int_vars.contains(&vid) { self.infcx.next_int_var() } else { ty })
211207
}
212208
ty::Infer(ty::InferTy::FloatVar(vid)) => {
213-
if self.float_vars.contains(&vid) {
214-
self.infcx.next_float_var()
215-
} else {
216-
ty
217-
}
209+
Ok(if self.float_vars.contains(&vid) { self.infcx.next_float_var() } else { ty })
218210
}
219211
_ => ty.super_fold_with(self),
220212
}
221213
}
222214

223-
fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
215+
fn fold_region(&mut self, r: ty::Region<'tcx>) -> Result<ty::Region<'tcx>, Self::Error> {
224216
if let ty::ReVar(vid) = *r {
225217
if self.region_vars.0.contains(&vid) {
226218
let idx = vid.index() - self.region_vars.0.start.index();
227219
let origin = self.region_vars.1[idx];
228-
return self.infcx.next_region_var(origin);
220+
return Ok(self.infcx.next_region_var(origin));
229221
}
230222
}
231-
r
223+
Ok(r)
232224
}
233225

234-
fn fold_const(&mut self, ct: &'tcx ty::Const<'tcx>) -> &'tcx ty::Const<'tcx> {
226+
fn fold_const(
227+
&mut self,
228+
ct: &'tcx ty::Const<'tcx>,
229+
) -> Result<&'tcx ty::Const<'tcx>, Self::Error> {
235230
if let ty::Const { val: ty::ConstKind::Infer(ty::InferConst::Var(vid)), ty } = ct {
236231
if self.const_vars.0.contains(&vid) {
237232
// This variable was created during the fudging.
238233
// Recreate it with a fresh variable here.
239234
let idx = (vid.index - self.const_vars.0.start.index) as usize;
240235
let origin = self.const_vars.1[idx];
241-
self.infcx.next_const_var(ty, origin)
236+
Ok(self.infcx.next_const_var(ty, origin))
242237
} else {
243-
ct
238+
Ok(ct)
244239
}
245240
} else {
246241
ct.super_fold_with(self)

compiler/rustc_infer/src/infer/mod.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -1651,12 +1651,15 @@ impl<'a, 'tcx> TypeFolder<'tcx> for ShallowResolver<'a, 'tcx> {
16511651
self.infcx.tcx
16521652
}
16531653

1654-
fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
1655-
self.infcx.shallow_resolve_ty(ty)
1654+
fn fold_ty(&mut self, ty: Ty<'tcx>) -> Result<Ty<'tcx>, Self::Error> {
1655+
Ok(self.infcx.shallow_resolve_ty(ty))
16561656
}
16571657

1658-
fn fold_const(&mut self, ct: &'tcx ty::Const<'tcx>) -> &'tcx ty::Const<'tcx> {
1659-
if let ty::Const { val: ty::ConstKind::Infer(InferConst::Var(vid)), .. } = ct {
1658+
fn fold_const(
1659+
&mut self,
1660+
ct: &'tcx ty::Const<'tcx>,
1661+
) -> Result<&'tcx ty::Const<'tcx>, Self::Error> {
1662+
Ok(if let ty::Const { val: ty::ConstKind::Infer(InferConst::Var(vid)), .. } = ct {
16601663
self.infcx
16611664
.inner
16621665
.borrow_mut()
@@ -1667,7 +1670,7 @@ impl<'a, 'tcx> TypeFolder<'tcx> for ShallowResolver<'a, 'tcx> {
16671670
.unwrap_or(ct)
16681671
} else {
16691672
ct
1670-
}
1673+
})
16711674
}
16721675
}
16731676

0 commit comments

Comments
 (0)