Skip to content

Commit acbe444

Browse files
committed
Auto merge of #91318 - eggyal:reduce-boilerplate-around-infallible-folders, r=jackh726
Reduce boilerplate around infallible folders Further to #91230 (comment) r? `@jackh726`
2 parents e5038e2 + cf683e6 commit acbe444

File tree

43 files changed

+895
-731
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+895
-731
lines changed

compiler/rustc_const_eval/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ Rust MIR: a lowered representation of Rust.
2323
#![feature(trusted_len)]
2424
#![feature(trusted_step)]
2525
#![feature(try_blocks)]
26-
#![feature(unwrap_infallible)]
2726
#![recursion_limit = "256"]
2827

2928
#[macro_use]

compiler/rustc_const_eval/src/transform/validate.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,7 @@ pub fn equal_up_to_regions(
9494
// Leave consts and types unchanged.
9595
ct_op: |ct| ct,
9696
ty_op: |ty| ty,
97-
})
98-
.into_ok(),
97+
}),
9998
)
10099
};
101100
tcx.infer_ctxt().enter(|infcx| infcx.can_eq(param_env, normalize(src), normalize(dest)).is_ok())

compiler/rustc_data_structures/src/functor.rs

-8
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,6 @@ use std::mem;
44
pub trait IdFunctor: Sized {
55
type Inner;
66

7-
#[inline]
8-
fn map_id<F>(self, mut f: F) -> Self
9-
where
10-
F: FnMut(Self::Inner) -> Self::Inner,
11-
{
12-
self.try_map_id::<_, !>(|value| Ok(f(value))).into_ok()
13-
}
14-
157
fn try_map_id<F, E>(self, f: F) -> Result<Self, E>
168
where
179
F: FnMut(Self::Inner) -> Result<Self::Inner, E>;

compiler/rustc_data_structures/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
#![feature(once_cell)]
2626
#![feature(test)]
2727
#![feature(thread_id_value)]
28-
#![feature(unwrap_infallible)]
2928
#![allow(rustc::default_hash_types)]
3029
#![deny(unaligned_references)]
3130

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

+27-30
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>) -> Result<ty::Binder<'tcx, T>, Self::Error>
281+
fn fold_binder<T>(&mut self, t: ty::Binder<'tcx, T>) -> ty::Binder<'tcx, T>
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>) -> Result<ty::Region<'tcx>, Self::Error> {
291+
fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
292292
match *r {
293293
ty::ReLateBound(index, ..) => {
294294
if index >= self.binder_index {
295295
bug!("escaping late-bound region during canonicalization");
296296
} else {
297-
Ok(r)
297+
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-
Ok(self.canonicalize_region_mode.canonicalize_free_region(self, r))
314+
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 => Ok(self.canonicalize_region_mode.canonicalize_free_region(self, r)),
322+
| ty::ReErased => self.canonicalize_region_mode.canonicalize_free_region(self, r),
323323
}
324324
}
325325

326-
fn fold_ty(&mut self, t: Ty<'tcx>) -> Result<Ty<'tcx>, Self::Error> {
326+
fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
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-
Ok(self.canonicalize_ty_var(
342+
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(_)) => Ok(self.canonicalize_ty_var(
352+
ty::Infer(ty::IntVar(_)) => self.canonicalize_ty_var(
353353
CanonicalVarInfo { kind: CanonicalVarKind::Ty(CanonicalTyVarKind::Int) },
354354
t,
355-
)),
355+
),
356356

357-
ty::Infer(ty::FloatVar(_)) => Ok(self.canonicalize_ty_var(
357+
ty::Infer(ty::FloatVar(_)) => 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) => Ok(self.canonicalize_ty_var(
366+
ty::Placeholder(placeholder) => 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-
Ok(t)
375+
t
376376
}
377377
}
378378

@@ -403,16 +403,13 @@ 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-
Ok(t)
406+
t
407407
}
408408
}
409409
}
410410
}
411411

412-
fn fold_const(
413-
&mut self,
414-
ct: &'tcx ty::Const<'tcx>,
415-
) -> Result<&'tcx ty::Const<'tcx>, Self::Error> {
412+
fn fold_const(&mut self, ct: &'tcx ty::Const<'tcx>) -> &'tcx ty::Const<'tcx> {
416413
match ct.val {
417414
ty::ConstKind::Infer(InferConst::Var(vid)) => {
418415
debug!("canonical: const var found with vid {:?}", vid);
@@ -427,10 +424,10 @@ impl<'cx, 'tcx> TypeFolder<'tcx> for Canonicalizer<'cx, 'tcx> {
427424
Err(mut ui) => {
428425
// FIXME: perf problem described in #55921.
429426
ui = ty::UniverseIndex::ROOT;
430-
return Ok(self.canonicalize_const_var(
427+
return self.canonicalize_const_var(
431428
CanonicalVarInfo { kind: CanonicalVarKind::Const(ui) },
432429
ct,
433-
));
430+
);
434431
}
435432
}
436433
}
@@ -441,20 +438,20 @@ impl<'cx, 'tcx> TypeFolder<'tcx> for Canonicalizer<'cx, 'tcx> {
441438
if debruijn >= self.binder_index {
442439
bug!("escaping bound type during canonicalization")
443440
} else {
444-
return Ok(ct);
441+
return ct;
445442
}
446443
}
447444
ty::ConstKind::Placeholder(placeholder) => {
448-
return Ok(self.canonicalize_const_var(
445+
return self.canonicalize_const_var(
449446
CanonicalVarInfo { kind: CanonicalVarKind::PlaceholderConst(placeholder) },
450447
ct,
451-
));
448+
);
452449
}
453450
_ => {}
454451
}
455452

456453
let flags = FlagComputation::for_const(ct);
457-
if flags.intersects(self.needs_canonical_flags) { ct.super_fold_with(self) } else { Ok(ct) }
454+
if flags.intersects(self.needs_canonical_flags) { ct.super_fold_with(self) } else { ct }
458455
}
459456
}
460457

@@ -503,7 +500,7 @@ impl<'cx, 'tcx> Canonicalizer<'cx, 'tcx> {
503500
indices: FxHashMap::default(),
504501
binder_index: ty::INNERMOST,
505502
};
506-
let out_value = value.fold_with(&mut canonicalizer).into_ok();
503+
let out_value = value.fold_with(&mut canonicalizer);
507504

508505
// Once we have canonicalized `out_value`, it should not
509506
// contain anything that ties it to this inference context
@@ -621,7 +618,7 @@ impl<'cx, 'tcx> Canonicalizer<'cx, 'tcx> {
621618
let infcx = self.infcx;
622619
let bound_to = infcx.shallow_resolve(ty_var);
623620
if bound_to != ty_var {
624-
self.fold_ty(bound_to).into_ok()
621+
self.fold_ty(bound_to)
625622
} else {
626623
let var = self.canonical_var(info, ty_var.into());
627624
self.tcx().mk_ty(ty::Bound(self.binder_index, var.into()))
@@ -640,12 +637,12 @@ impl<'cx, 'tcx> Canonicalizer<'cx, 'tcx> {
640637
let infcx = self.infcx;
641638
let bound_to = infcx.shallow_resolve(const_var);
642639
if bound_to != const_var {
643-
self.fold_const(bound_to).into_ok()
640+
self.fold_const(bound_to)
644641
} else {
645642
let var = self.canonical_var(info, const_var.into());
646643
self.tcx().mk_const(ty::Const {
647644
val: ty::ConstKind::Bound(self.binder_index, var),
648-
ty: self.fold_ty(const_var.ty).into_ok(),
645+
ty: self.fold_ty(const_var.ty),
649646
})
650647
}
651648
}

compiler/rustc_infer/src/infer/freshen.rs

+19-22
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ impl<'a, 'tcx> TypeFreshener<'a, 'tcx> {
7272
F: FnOnce(u32) -> ty::InferTy,
7373
{
7474
if let Some(ty) = opt_ty {
75-
return ty.fold_with(self).into_ok();
75+
return ty.fold_with(self);
7676
}
7777

7878
match self.ty_freshen_map.entry(key) {
@@ -98,7 +98,7 @@ impl<'a, 'tcx> TypeFreshener<'a, 'tcx> {
9898
F: FnOnce(u32) -> ty::InferConst<'tcx>,
9999
{
100100
if let Some(ct) = opt_ct {
101-
return ct.fold_with(self).into_ok();
101+
return ct.fold_with(self);
102102
}
103103

104104
match self.const_freshen_map.entry(key) {
@@ -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>) -> Result<ty::Region<'tcx>, Self::Error> {
122+
fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
123123
match *r {
124124
ty::ReLateBound(..) => {
125125
// leave bound regions alone
126-
Ok(r)
126+
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-
Ok(self.tcx().lifetimes.re_erased)
136+
self.tcx().lifetimes.re_erased
137137
}
138138
ty::ReStatic => {
139139
if self.keep_static {
140-
Ok(r)
140+
r
141141
} else {
142-
Ok(self.tcx().lifetimes.re_erased)
142+
self.tcx().lifetimes.re_erased
143143
}
144144
}
145145
}
146146
}
147147

148-
fn fold_ty(&mut self, t: Ty<'tcx>) -> Result<Ty<'tcx>, Self::Error> {
148+
fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
149149
if !t.needs_infer() && !t.has_erasable_regions(self.tcx()) {
150-
return Ok(t);
150+
return 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-
Ok(self.freshen_ty(opt_ty, ty::TyVar(v), ty::FreshTy))
158+
self.freshen_ty(opt_ty, ty::TyVar(v), ty::FreshTy)
159159
}
160160

161-
ty::Infer(ty::IntVar(v)) => Ok(self.freshen_ty(
161+
ty::Infer(ty::IntVar(v)) => 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)) => Ok(self.freshen_ty(
172+
ty::Infer(ty::FloatVar(v)) => 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-
Ok(t)
192+
t
193193
}
194194

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

224-
fn fold_const(
225-
&mut self,
226-
ct: &'tcx ty::Const<'tcx>,
227-
) -> Result<&'tcx ty::Const<'tcx>, Self::Error> {
224+
fn fold_const(&mut self, ct: &'tcx ty::Const<'tcx>) -> &'tcx ty::Const<'tcx> {
228225
match ct.val {
229226
ty::ConstKind::Infer(ty::InferConst::Var(v)) => {
230227
let opt_ct = self
@@ -235,12 +232,12 @@ impl<'a, 'tcx> TypeFolder<'tcx> for TypeFreshener<'a, 'tcx> {
235232
.probe_value(v)
236233
.val
237234
.known();
238-
return Ok(self.freshen_const(
235+
return self.freshen_const(
239236
opt_ct,
240237
ty::InferConst::Var(v),
241238
ty::InferConst::Fresh,
242239
ct.ty,
243-
));
240+
);
244241
}
245242
ty::ConstKind::Infer(ty::InferConst::Fresh(i)) => {
246243
if i >= self.const_freshen_count {
@@ -251,7 +248,7 @@ impl<'a, 'tcx> TypeFolder<'tcx> for TypeFreshener<'a, 'tcx> {
251248
self.const_freshen_count,
252249
);
253250
}
254-
return Ok(ct);
251+
return ct;
255252
}
256253

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

0 commit comments

Comments
 (0)