Skip to content

Commit 6dc3dae

Browse files
Adapt TypeFolder implementors to return a Result
Co-authored-by: Alan Egerton <[email protected]>
1 parent 6e3fa20 commit 6dc3dae

File tree

24 files changed

+387
-305
lines changed

24 files changed

+387
-305
lines changed

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

+26-23
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ impl<'cx, 'tcx> TypeFolder<'tcx> for Canonicalizer<'cx, 'tcx> {
278278
self.tcx
279279
}
280280

281-
fn fold_binder<T>(&mut self, t: ty::Binder<'tcx, T>) -> ty::Binder<'tcx, T>
281+
fn fold_binder<T>(&mut self, t: ty::Binder<'tcx, T>) -> Result<ty::Binder<'tcx, T>, Self::Error>
282282
where
283283
T: TypeFoldable<'tcx>,
284284
{
@@ -288,13 +288,13 @@ impl<'cx, 'tcx> TypeFolder<'tcx> for Canonicalizer<'cx, 'tcx> {
288288
t
289289
}
290290

291-
fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
291+
fn fold_region(&mut self, r: ty::Region<'tcx>) -> Result<ty::Region<'tcx>, Self::Error> {
292292
match *r {
293293
ty::ReLateBound(index, ..) => {
294294
if index >= self.binder_index {
295295
bug!("escaping late-bound region during canonicalization");
296296
} else {
297-
r
297+
Ok(r)
298298
}
299299
}
300300

@@ -311,19 +311,19 @@ impl<'cx, 'tcx> TypeFolder<'tcx> for Canonicalizer<'cx, 'tcx> {
311311
vid, r
312312
);
313313
let r = self.tcx.reuse_or_mk_region(r, ty::ReVar(resolved_vid));
314-
self.canonicalize_region_mode.canonicalize_free_region(self, r)
314+
Ok(self.canonicalize_region_mode.canonicalize_free_region(self, r))
315315
}
316316

317317
ty::ReStatic
318318
| ty::ReEarlyBound(..)
319319
| ty::ReFree(_)
320320
| ty::ReEmpty(_)
321321
| ty::RePlaceholder(..)
322-
| ty::ReErased => self.canonicalize_region_mode.canonicalize_free_region(self, r),
322+
| ty::ReErased => Ok(self.canonicalize_region_mode.canonicalize_free_region(self, r)),
323323
}
324324
}
325325

326-
fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
326+
fn fold_ty(&mut self, t: Ty<'tcx>) -> Result<Ty<'tcx>, Self::Error> {
327327
match *t.kind() {
328328
ty::Infer(ty::TyVar(vid)) => {
329329
debug!("canonical: type var found with vid {:?}", vid);
@@ -339,40 +339,40 @@ impl<'cx, 'tcx> TypeFolder<'tcx> for Canonicalizer<'cx, 'tcx> {
339339
Err(mut ui) => {
340340
// FIXME: perf problem described in #55921.
341341
ui = ty::UniverseIndex::ROOT;
342-
self.canonicalize_ty_var(
342+
Ok(self.canonicalize_ty_var(
343343
CanonicalVarInfo {
344344
kind: CanonicalVarKind::Ty(CanonicalTyVarKind::General(ui)),
345345
},
346346
t,
347-
)
347+
))
348348
}
349349
}
350350
}
351351

352-
ty::Infer(ty::IntVar(_)) => self.canonicalize_ty_var(
352+
ty::Infer(ty::IntVar(_)) => Ok(self.canonicalize_ty_var(
353353
CanonicalVarInfo { kind: CanonicalVarKind::Ty(CanonicalTyVarKind::Int) },
354354
t,
355-
),
355+
)),
356356

357-
ty::Infer(ty::FloatVar(_)) => self.canonicalize_ty_var(
357+
ty::Infer(ty::FloatVar(_)) => Ok(self.canonicalize_ty_var(
358358
CanonicalVarInfo { kind: CanonicalVarKind::Ty(CanonicalTyVarKind::Float) },
359359
t,
360-
),
360+
)),
361361

362362
ty::Infer(ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_)) => {
363363
bug!("encountered a fresh type during canonicalization")
364364
}
365365

366-
ty::Placeholder(placeholder) => self.canonicalize_ty_var(
366+
ty::Placeholder(placeholder) => Ok(self.canonicalize_ty_var(
367367
CanonicalVarInfo { kind: CanonicalVarKind::PlaceholderTy(placeholder) },
368368
t,
369-
),
369+
)),
370370

371371
ty::Bound(debruijn, _) => {
372372
if debruijn >= self.binder_index {
373373
bug!("escaping bound type during canonicalization")
374374
} else {
375-
t
375+
Ok(t)
376376
}
377377
}
378378

@@ -403,13 +403,16 @@ impl<'cx, 'tcx> TypeFolder<'tcx> for Canonicalizer<'cx, 'tcx> {
403403
if t.flags().intersects(self.needs_canonical_flags) {
404404
t.super_fold_with(self)
405405
} else {
406-
t
406+
Ok(t)
407407
}
408408
}
409409
}
410410
}
411411

412-
fn fold_const(&mut self, ct: &'tcx ty::Const<'tcx>) -> &'tcx ty::Const<'tcx> {
412+
fn fold_const(
413+
&mut self,
414+
ct: &'tcx ty::Const<'tcx>,
415+
) -> Result<&'tcx ty::Const<'tcx>, Self::Error> {
413416
match ct.val {
414417
ty::ConstKind::Infer(InferConst::Var(vid)) => {
415418
debug!("canonical: const var found with vid {:?}", vid);
@@ -424,10 +427,10 @@ impl<'cx, 'tcx> TypeFolder<'tcx> for Canonicalizer<'cx, 'tcx> {
424427
Err(mut ui) => {
425428
// FIXME: perf problem described in #55921.
426429
ui = ty::UniverseIndex::ROOT;
427-
return self.canonicalize_const_var(
430+
return Ok(self.canonicalize_const_var(
428431
CanonicalVarInfo { kind: CanonicalVarKind::Const(ui) },
429432
ct,
430-
);
433+
));
431434
}
432435
}
433436
}
@@ -438,20 +441,20 @@ impl<'cx, 'tcx> TypeFolder<'tcx> for Canonicalizer<'cx, 'tcx> {
438441
if debruijn >= self.binder_index {
439442
bug!("escaping bound type during canonicalization")
440443
} else {
441-
return ct;
444+
return Ok(ct);
442445
}
443446
}
444447
ty::ConstKind::Placeholder(placeholder) => {
445-
return self.canonicalize_const_var(
448+
return Ok(self.canonicalize_const_var(
446449
CanonicalVarInfo { kind: CanonicalVarKind::PlaceholderConst(placeholder) },
447450
ct,
448-
);
451+
));
449452
}
450453
_ => {}
451454
}
452455

453456
let flags = FlagComputation::for_const(ct);
454-
if flags.intersects(self.needs_canonical_flags) { ct.super_fold_with(self) } else { ct }
457+
if flags.intersects(self.needs_canonical_flags) { ct.super_fold_with(self) } else { Ok(ct) }
455458
}
456459
}
457460

compiler/rustc_infer/src/infer/freshen.rs

+20-17
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,11 @@ impl<'a, 'tcx> TypeFolder<'tcx> for TypeFreshener<'a, 'tcx> {
119119
self.infcx.tcx
120120
}
121121

122-
fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
122+
fn fold_region(&mut self, r: ty::Region<'tcx>) -> Result<ty::Region<'tcx>, Self::Error> {
123123
match *r {
124124
ty::ReLateBound(..) => {
125125
// leave bound regions alone
126-
r
126+
Ok(r)
127127
}
128128

129129
ty::ReEarlyBound(..)
@@ -133,32 +133,32 @@ impl<'a, 'tcx> TypeFolder<'tcx> for TypeFreshener<'a, 'tcx> {
133133
| ty::ReEmpty(_)
134134
| ty::ReErased => {
135135
// replace all free regions with 'erased
136-
self.tcx().lifetimes.re_erased
136+
Ok(self.tcx().lifetimes.re_erased)
137137
}
138138
ty::ReStatic => {
139139
if self.keep_static {
140-
r
140+
Ok(r)
141141
} else {
142-
self.tcx().lifetimes.re_erased
142+
Ok(self.tcx().lifetimes.re_erased)
143143
}
144144
}
145145
}
146146
}
147147

148-
fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
148+
fn fold_ty(&mut self, t: Ty<'tcx>) -> Result<Ty<'tcx>, Self::Error> {
149149
if !t.needs_infer() && !t.has_erasable_regions(self.tcx()) {
150-
return t;
150+
return Ok(t);
151151
}
152152

153153
let tcx = self.infcx.tcx;
154154

155155
match *t.kind() {
156156
ty::Infer(ty::TyVar(v)) => {
157157
let opt_ty = self.infcx.inner.borrow_mut().type_variables().probe(v).known();
158-
self.freshen_ty(opt_ty, ty::TyVar(v), ty::FreshTy)
158+
Ok(self.freshen_ty(opt_ty, ty::TyVar(v), ty::FreshTy))
159159
}
160160

161-
ty::Infer(ty::IntVar(v)) => self.freshen_ty(
161+
ty::Infer(ty::IntVar(v)) => Ok(self.freshen_ty(
162162
self.infcx
163163
.inner
164164
.borrow_mut()
@@ -167,9 +167,9 @@ impl<'a, 'tcx> TypeFolder<'tcx> for TypeFreshener<'a, 'tcx> {
167167
.map(|v| v.to_type(tcx)),
168168
ty::IntVar(v),
169169
ty::FreshIntTy,
170-
),
170+
)),
171171

172-
ty::Infer(ty::FloatVar(v)) => self.freshen_ty(
172+
ty::Infer(ty::FloatVar(v)) => Ok(self.freshen_ty(
173173
self.infcx
174174
.inner
175175
.borrow_mut()
@@ -178,7 +178,7 @@ impl<'a, 'tcx> TypeFolder<'tcx> for TypeFreshener<'a, 'tcx> {
178178
.map(|v| v.to_type(tcx)),
179179
ty::FloatVar(v),
180180
ty::FreshFloatTy,
181-
),
181+
)),
182182

183183
ty::Infer(ty::FreshTy(ct) | ty::FreshIntTy(ct) | ty::FreshFloatTy(ct)) => {
184184
if ct >= self.ty_freshen_count {
@@ -189,7 +189,7 @@ impl<'a, 'tcx> TypeFolder<'tcx> for TypeFreshener<'a, 'tcx> {
189189
self.ty_freshen_count
190190
);
191191
}
192-
t
192+
Ok(t)
193193
}
194194

195195
ty::Generator(..)
@@ -221,7 +221,10 @@ impl<'a, 'tcx> TypeFolder<'tcx> for TypeFreshener<'a, 'tcx> {
221221
}
222222
}
223223

224-
fn fold_const(&mut self, ct: &'tcx ty::Const<'tcx>) -> &'tcx ty::Const<'tcx> {
224+
fn fold_const(
225+
&mut self,
226+
ct: &'tcx ty::Const<'tcx>,
227+
) -> Result<&'tcx ty::Const<'tcx>, Self::Error> {
225228
match ct.val {
226229
ty::ConstKind::Infer(ty::InferConst::Var(v)) => {
227230
let opt_ct = self
@@ -232,12 +235,12 @@ impl<'a, 'tcx> TypeFolder<'tcx> for TypeFreshener<'a, 'tcx> {
232235
.probe_value(v)
233236
.val
234237
.known();
235-
return self.freshen_const(
238+
return Ok(self.freshen_const(
236239
opt_ct,
237240
ty::InferConst::Var(v),
238241
ty::InferConst::Fresh,
239242
ct.ty,
240-
);
243+
));
241244
}
242245
ty::ConstKind::Infer(ty::InferConst::Fresh(i)) => {
243246
if i >= self.const_freshen_count {
@@ -248,7 +251,7 @@ impl<'a, 'tcx> TypeFolder<'tcx> for TypeFreshener<'a, 'tcx> {
248251
self.const_freshen_count,
249252
);
250253
}
251-
return ct;
254+
return Ok(ct);
252255
}
253256

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

compiler/rustc_infer/src/infer/fudge.rs

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

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

222-
fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
214+
fn fold_region(&mut self, r: ty::Region<'tcx>) -> Result<ty::Region<'tcx>, Self::Error> {
223215
if let ty::ReVar(vid) = *r {
224216
if self.region_vars.0.contains(&vid) {
225217
let idx = vid.index() - self.region_vars.0.start.index();
226218
let origin = self.region_vars.1[idx];
227-
return self.infcx.next_region_var(origin);
219+
return Ok(self.infcx.next_region_var(origin));
228220
}
229221
}
230-
r
222+
Ok(r)
231223
}
232224

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

compiler/rustc_infer/src/infer/mod.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -1745,12 +1745,15 @@ impl<'a, 'tcx> TypeFolder<'tcx> for ShallowResolver<'a, 'tcx> {
17451745
self.infcx.tcx
17461746
}
17471747

1748-
fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
1749-
self.infcx.shallow_resolve_ty(ty)
1748+
fn fold_ty(&mut self, ty: Ty<'tcx>) -> Result<Ty<'tcx>, Self::Error> {
1749+
Ok(self.infcx.shallow_resolve_ty(ty))
17501750
}
17511751

1752-
fn fold_const(&mut self, ct: &'tcx ty::Const<'tcx>) -> &'tcx ty::Const<'tcx> {
1753-
if let ty::Const { val: ty::ConstKind::Infer(InferConst::Var(vid)), .. } = ct {
1752+
fn fold_const(
1753+
&mut self,
1754+
ct: &'tcx ty::Const<'tcx>,
1755+
) -> Result<&'tcx ty::Const<'tcx>, Self::Error> {
1756+
Ok(if let ty::Const { val: ty::ConstKind::Infer(InferConst::Var(vid)), .. } = ct {
17541757
self.infcx
17551758
.inner
17561759
.borrow_mut()
@@ -1761,7 +1764,7 @@ impl<'a, 'tcx> TypeFolder<'tcx> for ShallowResolver<'a, 'tcx> {
17611764
.unwrap_or(ct)
17621765
} else {
17631766
ct
1764-
}
1767+
})
17651768
}
17661769
}
17671770

0 commit comments

Comments
 (0)