Skip to content

Commit 30bf20a

Browse files
Unwrap the results of type folders
Co-authored-by: Alan Egerton <[email protected]>
1 parent 6dc3dae commit 30bf20a

File tree

34 files changed

+192
-168
lines changed

34 files changed

+192
-168
lines changed

compiler/rustc_const_eval/src/lib.rs

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

2829
#[macro_use]

compiler/rustc_const_eval/src/transform/validate.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@ pub fn equal_up_to_regions(
9595
// Leave consts and types unchanged.
9696
ct_op: |ct| ct,
9797
ty_op: |ty| ty,
98-
}),
98+
})
99+
.into_ok(),
99100
)
100101
};
101102
tcx.infer_ctxt().enter(|infcx| infcx.can_eq(param_env, normalize(src), normalize(dest)).is_ok())

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ impl<'cx, 'tcx> Canonicalizer<'cx, 'tcx> {
503503
indices: FxHashMap::default(),
504504
binder_index: ty::INNERMOST,
505505
};
506-
let out_value = value.fold_with(&mut canonicalizer);
506+
let out_value = value.fold_with(&mut canonicalizer).into_ok();
507507

508508
// Once we have canonicalized `out_value`, it should not
509509
// contain anything that ties it to this inference context
@@ -621,7 +621,7 @@ impl<'cx, 'tcx> Canonicalizer<'cx, 'tcx> {
621621
let infcx = self.infcx;
622622
let bound_to = infcx.shallow_resolve(ty_var);
623623
if bound_to != ty_var {
624-
self.fold_ty(bound_to)
624+
self.fold_ty(bound_to).into_ok()
625625
} else {
626626
let var = self.canonical_var(info, ty_var.into());
627627
self.tcx().mk_ty(ty::Bound(self.binder_index, var.into()))
@@ -640,12 +640,12 @@ impl<'cx, 'tcx> Canonicalizer<'cx, 'tcx> {
640640
let infcx = self.infcx;
641641
let bound_to = infcx.shallow_resolve(const_var);
642642
if bound_to != const_var {
643-
self.fold_const(bound_to)
643+
self.fold_const(bound_to).into_ok()
644644
} else {
645645
let var = self.canonical_var(info, const_var.into());
646646
self.tcx().mk_const(ty::Const {
647647
val: ty::ConstKind::Bound(self.binder_index, var),
648-
ty: self.fold_ty(const_var.ty),
648+
ty: self.fold_ty(const_var.ty).into_ok(),
649649
})
650650
}
651651
}

compiler/rustc_infer/src/infer/freshen.rs

+2-2
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);
75+
return ty.fold_with(self).into_ok();
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);
101+
return ct.fold_with(self).into_ok();
102102
}
103103

104104
match self.const_freshen_map.entry(key) {

compiler/rustc_infer/src/infer/fudge.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
161161
{
162162
Ok(value)
163163
} else {
164-
Ok(value.fold_with(&mut fudger))
164+
Ok(value.fold_with(&mut fudger).into_ok())
165165
}
166166
}
167167
}

compiler/rustc_infer/src/infer/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -681,7 +681,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
681681
}
682682

683683
pub fn freshen<T: TypeFoldable<'tcx>>(&self, t: T) -> T {
684-
t.fold_with(&mut self.freshener())
684+
t.fold_with(&mut self.freshener()).into_ok()
685685
}
686686

687687
/// Returns the origin of the type variable identified by `vid`, or `None`
@@ -1381,7 +1381,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
13811381
where
13821382
T: TypeFoldable<'tcx>,
13831383
{
1384-
value.fold_with(&mut ShallowResolver { infcx: self })
1384+
value.fold_with(&mut ShallowResolver { infcx: self }).into_ok()
13851385
}
13861386

13871387
pub fn root_var(&self, var: ty::TyVid) -> ty::TyVid {
@@ -1402,7 +1402,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
14021402
return value; // Avoid duplicated subst-folding.
14031403
}
14041404
let mut r = resolve::OpportunisticVarResolver::new(self);
1405-
value.fold_with(&mut r)
1405+
value.fold_with(&mut r).into_ok()
14061406
}
14071407

14081408
/// Returns the first unresolved variable contained in `T`. In the

compiler/rustc_infer/src/infer/opaque_types.rs

+100-96
Original file line numberDiff line numberDiff line change
@@ -418,92 +418,94 @@ struct Instantiator<'a, 'tcx> {
418418
impl<'a, 'tcx> Instantiator<'a, 'tcx> {
419419
fn instantiate_opaque_types_in_map<T: TypeFoldable<'tcx>>(&mut self, value: T) -> T {
420420
let tcx = self.infcx.tcx;
421-
value.fold_with(&mut BottomUpFolder {
422-
tcx,
423-
ty_op: |ty| {
424-
if ty.references_error() {
425-
return tcx.ty_error();
426-
} else if let ty::Opaque(def_id, substs) = ty.kind() {
427-
// Check that this is `impl Trait` type is
428-
// declared by `parent_def_id` -- i.e., one whose
429-
// value we are inferring. At present, this is
430-
// always true during the first phase of
431-
// type-check, but not always true later on during
432-
// NLL. Once we support named opaque types more fully,
433-
// this same scenario will be able to arise during all phases.
434-
//
435-
// Here is an example using type alias `impl Trait`
436-
// that indicates the distinction we are checking for:
437-
//
438-
// ```rust
439-
// mod a {
440-
// pub type Foo = impl Iterator;
441-
// pub fn make_foo() -> Foo { .. }
442-
// }
443-
//
444-
// mod b {
445-
// fn foo() -> a::Foo { a::make_foo() }
446-
// }
447-
// ```
448-
//
449-
// Here, the return type of `foo` references an
450-
// `Opaque` indeed, but not one whose value is
451-
// presently being inferred. You can get into a
452-
// similar situation with closure return types
453-
// today:
454-
//
455-
// ```rust
456-
// fn foo() -> impl Iterator { .. }
457-
// fn bar() {
458-
// let x = || foo(); // returns the Opaque assoc with `foo`
459-
// }
460-
// ```
461-
if let Some(def_id) = def_id.as_local() {
462-
let opaque_hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
463-
let parent_def_id = self.infcx.defining_use_anchor;
464-
let def_scope_default = || {
465-
let opaque_parent_hir_id = tcx.hir().get_parent_item(opaque_hir_id);
466-
parent_def_id == tcx.hir().local_def_id(opaque_parent_hir_id)
467-
};
468-
let (in_definition_scope, origin) =
469-
match tcx.hir().expect_item(opaque_hir_id).kind {
470-
// Anonymous `impl Trait`
471-
hir::ItemKind::OpaqueTy(hir::OpaqueTy {
472-
impl_trait_fn: Some(parent),
473-
origin,
474-
..
475-
}) => (parent == parent_def_id.to_def_id(), origin),
476-
// Named `type Foo = impl Bar;`
477-
hir::ItemKind::OpaqueTy(hir::OpaqueTy {
478-
impl_trait_fn: None,
479-
origin,
480-
..
481-
}) => (
482-
may_define_opaque_type(tcx, parent_def_id, opaque_hir_id),
483-
origin,
484-
),
485-
_ => (def_scope_default(), hir::OpaqueTyOrigin::TyAlias),
421+
value
422+
.fold_with(&mut BottomUpFolder {
423+
tcx,
424+
ty_op: |ty| {
425+
if ty.references_error() {
426+
return tcx.ty_error();
427+
} else if let ty::Opaque(def_id, substs) = ty.kind() {
428+
// Check that this is `impl Trait` type is
429+
// declared by `parent_def_id` -- i.e., one whose
430+
// value we are inferring. At present, this is
431+
// always true during the first phase of
432+
// type-check, but not always true later on during
433+
// NLL. Once we support named opaque types more fully,
434+
// this same scenario will be able to arise during all phases.
435+
//
436+
// Here is an example using type alias `impl Trait`
437+
// that indicates the distinction we are checking for:
438+
//
439+
// ```rust
440+
// mod a {
441+
// pub type Foo = impl Iterator;
442+
// pub fn make_foo() -> Foo { .. }
443+
// }
444+
//
445+
// mod b {
446+
// fn foo() -> a::Foo { a::make_foo() }
447+
// }
448+
// ```
449+
//
450+
// Here, the return type of `foo` references an
451+
// `Opaque` indeed, but not one whose value is
452+
// presently being inferred. You can get into a
453+
// similar situation with closure return types
454+
// today:
455+
//
456+
// ```rust
457+
// fn foo() -> impl Iterator { .. }
458+
// fn bar() {
459+
// let x = || foo(); // returns the Opaque assoc with `foo`
460+
// }
461+
// ```
462+
if let Some(def_id) = def_id.as_local() {
463+
let opaque_hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
464+
let parent_def_id = self.infcx.defining_use_anchor;
465+
let def_scope_default = || {
466+
let opaque_parent_hir_id = tcx.hir().get_parent_item(opaque_hir_id);
467+
parent_def_id == tcx.hir().local_def_id(opaque_parent_hir_id)
486468
};
487-
if in_definition_scope {
488-
let opaque_type_key =
489-
OpaqueTypeKey { def_id: def_id.to_def_id(), substs };
490-
return self.fold_opaque_ty(ty, opaque_type_key, origin);
491-
}
492-
493-
debug!(
494-
"instantiate_opaque_types_in_map: \
469+
let (in_definition_scope, origin) =
470+
match tcx.hir().expect_item(opaque_hir_id).kind {
471+
// Anonymous `impl Trait`
472+
hir::ItemKind::OpaqueTy(hir::OpaqueTy {
473+
impl_trait_fn: Some(parent),
474+
origin,
475+
..
476+
}) => (parent == parent_def_id.to_def_id(), origin),
477+
// Named `type Foo = impl Bar;`
478+
hir::ItemKind::OpaqueTy(hir::OpaqueTy {
479+
impl_trait_fn: None,
480+
origin,
481+
..
482+
}) => (
483+
may_define_opaque_type(tcx, parent_def_id, opaque_hir_id),
484+
origin,
485+
),
486+
_ => (def_scope_default(), hir::OpaqueTyOrigin::TyAlias),
487+
};
488+
if in_definition_scope {
489+
let opaque_type_key =
490+
OpaqueTypeKey { def_id: def_id.to_def_id(), substs };
491+
return self.fold_opaque_ty(ty, opaque_type_key, origin);
492+
}
493+
494+
debug!(
495+
"instantiate_opaque_types_in_map: \
495496
encountered opaque outside its definition scope \
496497
def_id={:?}",
497-
def_id,
498-
);
498+
def_id,
499+
);
500+
}
499501
}
500-
}
501502

502-
ty
503-
},
504-
lt_op: |lt| lt,
505-
ct_op: |ct| ct,
506-
})
503+
ty
504+
},
505+
lt_op: |lt| lt,
506+
ct_op: |ct| ct,
507+
})
508+
.into_ok()
507509
}
508510

509511
#[instrument(skip(self), level = "debug")]
@@ -556,21 +558,23 @@ impl<'a, 'tcx> Instantiator<'a, 'tcx> {
556558
debug!(?predicate);
557559

558560
// We can't normalize associated types from `rustc_infer`, but we can eagerly register inference variables for them.
559-
let predicate = predicate.fold_with(&mut BottomUpFolder {
560-
tcx,
561-
ty_op: |ty| match ty.kind() {
562-
ty::Projection(projection_ty) => infcx.infer_projection(
563-
self.param_env,
564-
*projection_ty,
565-
traits::ObligationCause::misc(self.value_span, self.body_id),
566-
0,
567-
&mut self.obligations,
568-
),
569-
_ => ty,
570-
},
571-
lt_op: |lt| lt,
572-
ct_op: |ct| ct,
573-
});
561+
let predicate = predicate
562+
.fold_with(&mut BottomUpFolder {
563+
tcx,
564+
ty_op: |ty| match ty.kind() {
565+
ty::Projection(projection_ty) => infcx.infer_projection(
566+
self.param_env,
567+
*projection_ty,
568+
traits::ObligationCause::misc(self.value_span, self.body_id),
569+
0,
570+
&mut self.obligations,
571+
),
572+
_ => ty,
573+
},
574+
lt_op: |lt| lt,
575+
ct_op: |ct| ct,
576+
})
577+
.into_ok();
574578
debug!(?predicate);
575579

576580
if let ty::PredicateKind::Projection(projection) = predicate.kind().skip_binder() {

compiler/rustc_infer/src/infer/resolve.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ where
182182
T: TypeFoldable<'tcx>,
183183
{
184184
let mut full_resolver = FullTypeResolver { infcx, err: None };
185-
let result = value.fold_with(&mut full_resolver);
185+
let result = value.fold_with(&mut full_resolver).into_ok();
186186
match full_resolver.err {
187187
None => Ok(result),
188188
Some(e) => Err(e),

compiler/rustc_infer/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#![feature(control_flow_enum)]
2525
#![feature(min_specialization)]
2626
#![feature(label_break_value)]
27+
#![feature(unwrap_infallible)]
2728
#![recursion_limit = "512"] // For rustdoc
2829

2930
#[macro_use]

compiler/rustc_middle/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
#![feature(try_blocks)]
5757
#![feature(try_reserve_kind)]
5858
#![feature(nonzero_ops)]
59+
#![feature(unwrap_infallible)]
5960
#![recursion_limit = "512"]
6061

6162
#[macro_use]

compiler/rustc_middle/src/ty/erase_regions.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ pub(super) fn provide(providers: &mut ty::query::Providers) {
99
fn erase_regions_ty<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> Ty<'tcx> {
1010
// N.B., use `super_fold_with` here. If we used `fold_with`, it
1111
// could invoke the `erase_regions_ty` query recursively.
12-
ty.super_fold_with(&mut RegionEraserVisitor { tcx })
12+
ty.super_fold_with(&mut RegionEraserVisitor { tcx }).into_ok()
1313
}
1414

1515
impl<'tcx> TyCtxt<'tcx> {
@@ -27,7 +27,7 @@ impl<'tcx> TyCtxt<'tcx> {
2727
return value;
2828
}
2929
debug!("erase_regions({:?})", value);
30-
let value1 = value.fold_with(&mut RegionEraserVisitor { tcx: self });
30+
let value1 = value.fold_with(&mut RegionEraserVisitor { tcx: self }).into_ok();
3131
debug!("erase_regions = {:?}", value1);
3232
value1
3333
}

0 commit comments

Comments
 (0)