diff --git a/compiler/rustc_const_eval/src/lib.rs b/compiler/rustc_const_eval/src/lib.rs index f308e764e861d..0d36466f6e3e7 100644 --- a/compiler/rustc_const_eval/src/lib.rs +++ b/compiler/rustc_const_eval/src/lib.rs @@ -23,6 +23,7 @@ Rust MIR: a lowered representation of Rust. #![feature(trusted_len)] #![feature(trusted_step)] #![feature(try_blocks)] +#![feature(unwrap_infallible)] #![recursion_limit = "256"] #[macro_use] diff --git a/compiler/rustc_const_eval/src/transform/validate.rs b/compiler/rustc_const_eval/src/transform/validate.rs index b09b2227f3e88..0ab077cf2bf40 100644 --- a/compiler/rustc_const_eval/src/transform/validate.rs +++ b/compiler/rustc_const_eval/src/transform/validate.rs @@ -95,7 +95,8 @@ pub fn equal_up_to_regions( // Leave consts and types unchanged. ct_op: |ct| ct, ty_op: |ty| ty, - }), + }) + .into_ok(), ) }; tcx.infer_ctxt().enter(|infcx| infcx.can_eq(param_env, normalize(src), normalize(dest)).is_ok()) diff --git a/compiler/rustc_infer/src/infer/canonical/canonicalizer.rs b/compiler/rustc_infer/src/infer/canonical/canonicalizer.rs index dd1f0d63aff05..27e73738b7f35 100644 --- a/compiler/rustc_infer/src/infer/canonical/canonicalizer.rs +++ b/compiler/rustc_infer/src/infer/canonical/canonicalizer.rs @@ -503,7 +503,7 @@ impl<'cx, 'tcx> Canonicalizer<'cx, 'tcx> { indices: FxHashMap::default(), binder_index: ty::INNERMOST, }; - let out_value = value.fold_with(&mut canonicalizer); + let out_value = value.fold_with(&mut canonicalizer).into_ok(); // Once we have canonicalized `out_value`, it should not // contain anything that ties it to this inference context @@ -621,7 +621,7 @@ impl<'cx, 'tcx> Canonicalizer<'cx, 'tcx> { let infcx = self.infcx; let bound_to = infcx.shallow_resolve(ty_var); if bound_to != ty_var { - self.fold_ty(bound_to) + self.fold_ty(bound_to).into_ok() } else { let var = self.canonical_var(info, ty_var.into()); self.tcx().mk_ty(ty::Bound(self.binder_index, var.into())) @@ -640,12 +640,12 @@ impl<'cx, 'tcx> Canonicalizer<'cx, 'tcx> { let infcx = self.infcx; let bound_to = infcx.shallow_resolve(const_var); if bound_to != const_var { - self.fold_const(bound_to) + self.fold_const(bound_to).into_ok() } else { let var = self.canonical_var(info, const_var.into()); self.tcx().mk_const(ty::Const { val: ty::ConstKind::Bound(self.binder_index, var), - ty: self.fold_ty(const_var.ty), + ty: self.fold_ty(const_var.ty).into_ok(), }) } } diff --git a/compiler/rustc_infer/src/infer/freshen.rs b/compiler/rustc_infer/src/infer/freshen.rs index fce6403575b79..7599e98167fb5 100644 --- a/compiler/rustc_infer/src/infer/freshen.rs +++ b/compiler/rustc_infer/src/infer/freshen.rs @@ -72,7 +72,7 @@ impl<'a, 'tcx> TypeFreshener<'a, 'tcx> { F: FnOnce(u32) -> ty::InferTy, { if let Some(ty) = opt_ty { - return ty.fold_with(self); + return ty.fold_with(self).into_ok(); } match self.ty_freshen_map.entry(key) { @@ -98,7 +98,7 @@ impl<'a, 'tcx> TypeFreshener<'a, 'tcx> { F: FnOnce(u32) -> ty::InferConst<'tcx>, { if let Some(ct) = opt_ct { - return ct.fold_with(self); + return ct.fold_with(self).into_ok(); } match self.const_freshen_map.entry(key) { diff --git a/compiler/rustc_infer/src/infer/fudge.rs b/compiler/rustc_infer/src/infer/fudge.rs index c889abf3d3c88..4e6f1315d1024 100644 --- a/compiler/rustc_infer/src/infer/fudge.rs +++ b/compiler/rustc_infer/src/infer/fudge.rs @@ -161,7 +161,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> { { Ok(value) } else { - Ok(value.fold_with(&mut fudger)) + Ok(value.fold_with(&mut fudger).into_ok()) } } } diff --git a/compiler/rustc_infer/src/infer/mod.rs b/compiler/rustc_infer/src/infer/mod.rs index afac40b0ebd0d..4a9a63e1c7602 100644 --- a/compiler/rustc_infer/src/infer/mod.rs +++ b/compiler/rustc_infer/src/infer/mod.rs @@ -681,7 +681,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> { } pub fn freshen>(&self, t: T) -> T { - t.fold_with(&mut self.freshener()) + t.fold_with(&mut self.freshener()).into_ok() } /// Returns the origin of the type variable identified by `vid`, or `None` @@ -1381,7 +1381,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> { where T: TypeFoldable<'tcx>, { - value.fold_with(&mut ShallowResolver { infcx: self }) + value.fold_with(&mut ShallowResolver { infcx: self }).into_ok() } pub fn root_var(&self, var: ty::TyVid) -> ty::TyVid { @@ -1402,7 +1402,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> { return value; // Avoid duplicated subst-folding. } let mut r = resolve::OpportunisticVarResolver::new(self); - value.fold_with(&mut r) + value.fold_with(&mut r).into_ok() } /// Returns the first unresolved variable contained in `T`. In the diff --git a/compiler/rustc_infer/src/infer/opaque_types.rs b/compiler/rustc_infer/src/infer/opaque_types.rs index e2e07f2072e49..932f26d555010 100644 --- a/compiler/rustc_infer/src/infer/opaque_types.rs +++ b/compiler/rustc_infer/src/infer/opaque_types.rs @@ -418,92 +418,94 @@ struct Instantiator<'a, 'tcx> { impl<'a, 'tcx> Instantiator<'a, 'tcx> { fn instantiate_opaque_types_in_map>(&mut self, value: T) -> T { let tcx = self.infcx.tcx; - value.fold_with(&mut BottomUpFolder { - tcx, - ty_op: |ty| { - if ty.references_error() { - return tcx.ty_error(); - } else if let ty::Opaque(def_id, substs) = ty.kind() { - // Check that this is `impl Trait` type is - // declared by `parent_def_id` -- i.e., one whose - // value we are inferring. At present, this is - // always true during the first phase of - // type-check, but not always true later on during - // NLL. Once we support named opaque types more fully, - // this same scenario will be able to arise during all phases. - // - // Here is an example using type alias `impl Trait` - // that indicates the distinction we are checking for: - // - // ```rust - // mod a { - // pub type Foo = impl Iterator; - // pub fn make_foo() -> Foo { .. } - // } - // - // mod b { - // fn foo() -> a::Foo { a::make_foo() } - // } - // ``` - // - // Here, the return type of `foo` references an - // `Opaque` indeed, but not one whose value is - // presently being inferred. You can get into a - // similar situation with closure return types - // today: - // - // ```rust - // fn foo() -> impl Iterator { .. } - // fn bar() { - // let x = || foo(); // returns the Opaque assoc with `foo` - // } - // ``` - if let Some(def_id) = def_id.as_local() { - let opaque_hir_id = tcx.hir().local_def_id_to_hir_id(def_id); - let parent_def_id = self.infcx.defining_use_anchor; - let def_scope_default = || { - let opaque_parent_hir_id = tcx.hir().get_parent_item(opaque_hir_id); - parent_def_id == tcx.hir().local_def_id(opaque_parent_hir_id) - }; - let (in_definition_scope, origin) = - match tcx.hir().expect_item(opaque_hir_id).kind { - // Anonymous `impl Trait` - hir::ItemKind::OpaqueTy(hir::OpaqueTy { - impl_trait_fn: Some(parent), - origin, - .. - }) => (parent == parent_def_id.to_def_id(), origin), - // Named `type Foo = impl Bar;` - hir::ItemKind::OpaqueTy(hir::OpaqueTy { - impl_trait_fn: None, - origin, - .. - }) => ( - may_define_opaque_type(tcx, parent_def_id, opaque_hir_id), - origin, - ), - _ => (def_scope_default(), hir::OpaqueTyOrigin::TyAlias), + value + .fold_with(&mut BottomUpFolder { + tcx, + ty_op: |ty| { + if ty.references_error() { + return tcx.ty_error(); + } else if let ty::Opaque(def_id, substs) = ty.kind() { + // Check that this is `impl Trait` type is + // declared by `parent_def_id` -- i.e., one whose + // value we are inferring. At present, this is + // always true during the first phase of + // type-check, but not always true later on during + // NLL. Once we support named opaque types more fully, + // this same scenario will be able to arise during all phases. + // + // Here is an example using type alias `impl Trait` + // that indicates the distinction we are checking for: + // + // ```rust + // mod a { + // pub type Foo = impl Iterator; + // pub fn make_foo() -> Foo { .. } + // } + // + // mod b { + // fn foo() -> a::Foo { a::make_foo() } + // } + // ``` + // + // Here, the return type of `foo` references an + // `Opaque` indeed, but not one whose value is + // presently being inferred. You can get into a + // similar situation with closure return types + // today: + // + // ```rust + // fn foo() -> impl Iterator { .. } + // fn bar() { + // let x = || foo(); // returns the Opaque assoc with `foo` + // } + // ``` + if let Some(def_id) = def_id.as_local() { + let opaque_hir_id = tcx.hir().local_def_id_to_hir_id(def_id); + let parent_def_id = self.infcx.defining_use_anchor; + let def_scope_default = || { + let opaque_parent_hir_id = tcx.hir().get_parent_item(opaque_hir_id); + parent_def_id == tcx.hir().local_def_id(opaque_parent_hir_id) }; - if in_definition_scope { - let opaque_type_key = - OpaqueTypeKey { def_id: def_id.to_def_id(), substs }; - return self.fold_opaque_ty(ty, opaque_type_key, origin); - } - - debug!( - "instantiate_opaque_types_in_map: \ + let (in_definition_scope, origin) = + match tcx.hir().expect_item(opaque_hir_id).kind { + // Anonymous `impl Trait` + hir::ItemKind::OpaqueTy(hir::OpaqueTy { + impl_trait_fn: Some(parent), + origin, + .. + }) => (parent == parent_def_id.to_def_id(), origin), + // Named `type Foo = impl Bar;` + hir::ItemKind::OpaqueTy(hir::OpaqueTy { + impl_trait_fn: None, + origin, + .. + }) => ( + may_define_opaque_type(tcx, parent_def_id, opaque_hir_id), + origin, + ), + _ => (def_scope_default(), hir::OpaqueTyOrigin::TyAlias), + }; + if in_definition_scope { + let opaque_type_key = + OpaqueTypeKey { def_id: def_id.to_def_id(), substs }; + return self.fold_opaque_ty(ty, opaque_type_key, origin); + } + + debug!( + "instantiate_opaque_types_in_map: \ encountered opaque outside its definition scope \ def_id={:?}", - def_id, - ); + def_id, + ); + } } - } - ty - }, - lt_op: |lt| lt, - ct_op: |ct| ct, - }) + ty + }, + lt_op: |lt| lt, + ct_op: |ct| ct, + }) + .into_ok() } #[instrument(skip(self), level = "debug")] @@ -556,21 +558,23 @@ impl<'a, 'tcx> Instantiator<'a, 'tcx> { debug!(?predicate); // We can't normalize associated types from `rustc_infer`, but we can eagerly register inference variables for them. - let predicate = predicate.fold_with(&mut BottomUpFolder { - tcx, - ty_op: |ty| match ty.kind() { - ty::Projection(projection_ty) => infcx.infer_projection( - self.param_env, - *projection_ty, - traits::ObligationCause::misc(self.value_span, self.body_id), - 0, - &mut self.obligations, - ), - _ => ty, - }, - lt_op: |lt| lt, - ct_op: |ct| ct, - }); + let predicate = predicate + .fold_with(&mut BottomUpFolder { + tcx, + ty_op: |ty| match ty.kind() { + ty::Projection(projection_ty) => infcx.infer_projection( + self.param_env, + *projection_ty, + traits::ObligationCause::misc(self.value_span, self.body_id), + 0, + &mut self.obligations, + ), + _ => ty, + }, + lt_op: |lt| lt, + ct_op: |ct| ct, + }) + .into_ok(); debug!(?predicate); if let ty::PredicateKind::Projection(projection) = predicate.kind().skip_binder() { diff --git a/compiler/rustc_infer/src/infer/resolve.rs b/compiler/rustc_infer/src/infer/resolve.rs index d09c585a02598..554c5b162f755 100644 --- a/compiler/rustc_infer/src/infer/resolve.rs +++ b/compiler/rustc_infer/src/infer/resolve.rs @@ -182,7 +182,7 @@ where T: TypeFoldable<'tcx>, { let mut full_resolver = FullTypeResolver { infcx, err: None }; - let result = value.fold_with(&mut full_resolver); + let result = value.fold_with(&mut full_resolver).into_ok(); match full_resolver.err { None => Ok(result), Some(e) => Err(e), diff --git a/compiler/rustc_infer/src/lib.rs b/compiler/rustc_infer/src/lib.rs index e4b407e7c112d..5153427954ccc 100644 --- a/compiler/rustc_infer/src/lib.rs +++ b/compiler/rustc_infer/src/lib.rs @@ -24,6 +24,7 @@ #![feature(control_flow_enum)] #![feature(min_specialization)] #![feature(label_break_value)] +#![feature(unwrap_infallible)] #![recursion_limit = "512"] // For rustdoc #[macro_use] diff --git a/compiler/rustc_middle/src/lib.rs b/compiler/rustc_middle/src/lib.rs index 9ce9f65a49066..b67ad8b770ea1 100644 --- a/compiler/rustc_middle/src/lib.rs +++ b/compiler/rustc_middle/src/lib.rs @@ -56,6 +56,7 @@ #![feature(try_blocks)] #![feature(try_reserve_kind)] #![feature(nonzero_ops)] +#![feature(unwrap_infallible)] #![recursion_limit = "512"] #[macro_use] diff --git a/compiler/rustc_middle/src/ty/erase_regions.rs b/compiler/rustc_middle/src/ty/erase_regions.rs index 559534a7aa4e3..25b460cf16d3c 100644 --- a/compiler/rustc_middle/src/ty/erase_regions.rs +++ b/compiler/rustc_middle/src/ty/erase_regions.rs @@ -9,7 +9,7 @@ pub(super) fn provide(providers: &mut ty::query::Providers) { fn erase_regions_ty<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> Ty<'tcx> { // N.B., use `super_fold_with` here. If we used `fold_with`, it // could invoke the `erase_regions_ty` query recursively. - ty.super_fold_with(&mut RegionEraserVisitor { tcx }) + ty.super_fold_with(&mut RegionEraserVisitor { tcx }).into_ok() } impl<'tcx> TyCtxt<'tcx> { @@ -27,7 +27,7 @@ impl<'tcx> TyCtxt<'tcx> { return value; } debug!("erase_regions({:?})", value); - let value1 = value.fold_with(&mut RegionEraserVisitor { tcx: self }); + let value1 = value.fold_with(&mut RegionEraserVisitor { tcx: self }).into_ok(); debug!("erase_regions = {:?}", value1); value1 } diff --git a/compiler/rustc_middle/src/ty/fold.rs b/compiler/rustc_middle/src/ty/fold.rs index 639606f6b891e..31055c03a5d36 100644 --- a/compiler/rustc_middle/src/ty/fold.rs +++ b/compiler/rustc_middle/src/ty/fold.rs @@ -336,7 +336,7 @@ impl<'tcx> TyCtxt<'tcx> { where T: TypeFoldable<'tcx>, { - value.fold_with(&mut RegionFolder::new(self, skipped_regions, &mut f)) + value.fold_with(&mut RegionFolder::new(self, skipped_regions, &mut f)).into_ok() } /// Invoke `callback` on every region appearing free in `value`. @@ -638,7 +638,7 @@ impl<'tcx> TyCtxt<'tcx> { value } else { let mut replacer = BoundVarReplacer::new(self, Some(&mut real_fld_r), None, None); - value.fold_with(&mut replacer) + value.fold_with(&mut replacer).into_ok() }; (value, region_map) } @@ -664,7 +664,7 @@ impl<'tcx> TyCtxt<'tcx> { } else { let mut replacer = BoundVarReplacer::new(self, Some(&mut fld_r), Some(&mut fld_t), Some(&mut fld_c)); - value.fold_with(&mut replacer) + value.fold_with(&mut replacer).into_ok() } } @@ -1030,7 +1030,7 @@ where { debug!("shift_vars(value={:?}, amount={})", value, amount); - value.fold_with(&mut Shifter::new(tcx, amount)) + value.fold_with(&mut Shifter::new(tcx, amount)).into_ok() } #[derive(Debug, PartialEq, Eq, Copy, Clone)] @@ -1293,7 +1293,7 @@ impl<'tcx> TyCtxt<'tcx> { /// /// FIXME(@lcnr): explain this function a bit more pub fn expose_default_const_substs>(self, v: T) -> T { - v.fold_with(&mut ExposeDefaultConstSubstsFolder { tcx: self }) + v.fold_with(&mut ExposeDefaultConstSubstsFolder { tcx: self }).into_ok() } } diff --git a/compiler/rustc_middle/src/ty/instance.rs b/compiler/rustc_middle/src/ty/instance.rs index 23863bf389e5a..15931b8d2c86a 100644 --- a/compiler/rustc_middle/src/ty/instance.rs +++ b/compiler/rustc_middle/src/ty/instance.rs @@ -669,7 +669,7 @@ fn polymorphize<'tcx>( // ..and polymorphize any closures/generators captured as upvars. let upvars_ty = upvars_ty.unwrap(); let polymorphized_upvars_ty = upvars_ty.fold_with( - &mut PolymorphizationFolder { tcx }); + &mut PolymorphizationFolder { tcx }).into_ok(); debug!("polymorphize: polymorphized_upvars_ty={:?}", polymorphized_upvars_ty); ty::GenericArg::from(polymorphized_upvars_ty) }, diff --git a/compiler/rustc_middle/src/ty/normalize_erasing_regions.rs b/compiler/rustc_middle/src/ty/normalize_erasing_regions.rs index c5d845f97559d..e6f67adae93da 100644 --- a/compiler/rustc_middle/src/ty/normalize_erasing_regions.rs +++ b/compiler/rustc_middle/src/ty/normalize_erasing_regions.rs @@ -35,7 +35,9 @@ impl<'tcx> TyCtxt<'tcx> { if !value.has_projections() { value } else { - value.fold_with(&mut NormalizeAfterErasingRegionsFolder { tcx: self, param_env }) + value + .fold_with(&mut NormalizeAfterErasingRegionsFolder { tcx: self, param_env }) + .into_ok() } } diff --git a/compiler/rustc_middle/src/ty/print/pretty.rs b/compiler/rustc_middle/src/ty/print/pretty.rs index 13c6b4d6b01ba..f454b95862bab 100644 --- a/compiler/rustc_middle/src/ty/print/pretty.rs +++ b/compiler/rustc_middle/src/ty/print/pretty.rs @@ -2193,7 +2193,7 @@ impl FmtPrinter<'_, 'tcx, F> { name: &mut name, region_map: BTreeMap::new(), }; - let new_value = value.clone().skip_binder().fold_with(&mut folder); + let new_value = value.clone().skip_binder().fold_with(&mut folder).into_ok(); let region_map = folder.region_map; start_or_continue(&mut self, "", "> "); (new_value, region_map) diff --git a/compiler/rustc_middle/src/ty/subst.rs b/compiler/rustc_middle/src/ty/subst.rs index 419cf164db3b6..0767224ad9288 100644 --- a/compiler/rustc_middle/src/ty/subst.rs +++ b/compiler/rustc_middle/src/ty/subst.rs @@ -439,7 +439,7 @@ impl<'tcx, T: TypeFoldable<'tcx>> Subst<'tcx> for T { span: Option, ) -> T { let mut folder = SubstFolder { tcx, substs, span, binders_passed: 0 }; - self.fold_with(&mut folder) + self.fold_with(&mut folder).into_ok() } } diff --git a/compiler/rustc_middle/src/ty/util.rs b/compiler/rustc_middle/src/ty/util.rs index b90123b4cee82..5137f9650633a 100644 --- a/compiler/rustc_middle/src/ty/util.rs +++ b/compiler/rustc_middle/src/ty/util.rs @@ -574,14 +574,14 @@ impl<'tcx> OpaqueTypeExpander<'tcx> { if self.found_any_recursion { return None; } - let substs = substs.fold_with(self); + let substs = substs.fold_with(self).into_ok(); if !self.check_recursion || self.seen_opaque_tys.insert(def_id) { let expanded_ty = match self.expanded_cache.get(&(def_id, substs)) { Some(expanded_ty) => expanded_ty, None => { let generic_ty = self.tcx.type_of(def_id); let concrete_ty = generic_ty.subst(self.tcx, substs); - let expanded_ty = self.fold_ty(concrete_ty); + let expanded_ty = self.fold_ty(concrete_ty).into_ok(); self.expanded_cache.insert((def_id, substs), expanded_ty); expanded_ty } @@ -1092,7 +1092,7 @@ pub fn normalize_opaque_types( check_recursion: false, tcx, }; - val.fold_with(&mut visitor) + val.fold_with(&mut visitor).into_ok() } pub fn provide(providers: &mut ty::query::Providers) { diff --git a/compiler/rustc_trait_selection/src/lib.rs b/compiler/rustc_trait_selection/src/lib.rs index 1820e33b19bf4..ecc352c1a49b5 100644 --- a/compiler/rustc_trait_selection/src/lib.rs +++ b/compiler/rustc_trait_selection/src/lib.rs @@ -22,6 +22,7 @@ #![feature(never_type)] #![feature(crate_visibility_modifier)] #![feature(control_flow_enum)] +#![feature(unwrap_infallible)] #![recursion_limit = "512"] // For rustdoc #[macro_use] diff --git a/compiler/rustc_trait_selection/src/opaque_types.rs b/compiler/rustc_trait_selection/src/opaque_types.rs index eb7293ae86e69..9052dff0aaa83 100644 --- a/compiler/rustc_trait_selection/src/opaque_types.rs +++ b/compiler/rustc_trait_selection/src/opaque_types.rs @@ -65,14 +65,16 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> { // Convert the type from the function into a type valid outside // the function, by replacing invalid regions with 'static, // after producing an error for each of them. - let definition_ty = instantiated_ty.fold_with(&mut ReverseMapper::new( - self.tcx, - self.is_tainted_by_errors(), - def_id, - map, - instantiated_ty, - span, - )); + let definition_ty = instantiated_ty + .fold_with(&mut ReverseMapper::new( + self.tcx, + self.is_tainted_by_errors(), + def_id, + map, + instantiated_ty, + span, + )) + .into_ok(); debug!(?definition_ty); definition_ty @@ -123,14 +125,14 @@ impl ReverseMapper<'tcx> { ) -> GenericArg<'tcx> { assert!(!self.map_missing_regions_to_empty); self.map_missing_regions_to_empty = true; - let kind = kind.fold_with(self); + let kind = kind.fold_with(self).into_ok(); self.map_missing_regions_to_empty = false; kind } fn fold_kind_normally(&mut self, kind: GenericArg<'tcx>) -> GenericArg<'tcx> { assert!(!self.map_missing_regions_to_empty); - kind.fold_with(self) + kind.fold_with(self).into_ok() } } diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs index 1876b0ce80eed..866bcde1bfd4f 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs @@ -1916,8 +1916,9 @@ impl<'a, 'tcx> InferCtxtPrivExt<'tcx> for InferCtxt<'a, 'tcx> { self.probe(|_| { let mut selcx = SelectionContext::new(self); - let cleaned_pred = - pred.fold_with(&mut ParamToVarFolder { infcx: self, var_map: Default::default() }); + let cleaned_pred = pred + .fold_with(&mut ParamToVarFolder { infcx: self, var_map: Default::default() }) + .into_ok(); let cleaned_pred = super::project::normalize( &mut selcx, diff --git a/compiler/rustc_trait_selection/src/traits/project.rs b/compiler/rustc_trait_selection/src/traits/project.rs index 2baa21a43d61d..0911b2c529ab2 100644 --- a/compiler/rustc_trait_selection/src/traits/project.rs +++ b/compiler/rustc_trait_selection/src/traits/project.rs @@ -339,7 +339,7 @@ impl<'a, 'b, 'tcx> AssocTypeNormalizer<'a, 'b, 'tcx> { if !needs_normalization(&value, self.param_env.reveal()) { value } else { - value.fold_with(self) + value.fold_with(self).into_ok() } } } @@ -555,7 +555,7 @@ impl<'me, 'tcx> BoundVarReplacer<'me, 'tcx> { universe_indices, }; - let value = value.super_fold_with(&mut replacer); + let value = value.super_fold_with(&mut replacer).into_ok(); (value, replacer.mapped_regions, replacer.mapped_types, replacer.mapped_consts) } @@ -681,7 +681,7 @@ impl<'me, 'tcx> PlaceholderReplacer<'me, 'tcx> { universe_indices, current_index: ty::INNERMOST, }; - value.super_fold_with(&mut replacer) + value.super_fold_with(&mut replacer).into_ok() } } @@ -1546,7 +1546,8 @@ fn confirm_candidate<'cx, 'tcx>( // when possible for this to work. See `auto-trait-projection-recursion.rs` // for a case where this matters. if progress.ty.has_infer_regions() { - progress.ty = OpportunisticRegionResolver::new(selcx.infcx()).fold_ty(progress.ty); + progress.ty = + OpportunisticRegionResolver::new(selcx.infcx()).fold_ty(progress.ty).into_ok(); } progress } diff --git a/compiler/rustc_trait_selection/src/traits/query/normalize.rs b/compiler/rustc_trait_selection/src/traits/query/normalize.rs index f3a90935a9fdf..dd9cd51936ec3 100644 --- a/compiler/rustc_trait_selection/src/traits/query/normalize.rs +++ b/compiler/rustc_trait_selection/src/traits/query/normalize.rs @@ -88,7 +88,7 @@ impl<'cx, 'tcx> AtExt<'tcx> for At<'cx, 'tcx> { normalizer.universes.extend((0..max_visitor.escaping).map(|_| None)); } } - let result = value.fold_with(&mut normalizer); + let result = value.fold_with(&mut normalizer).into_ok(); info!( "normalize::<{}>: result={:?} with {} obligations", std::any::type_name::(), diff --git a/compiler/rustc_trait_selection/src/traits/select/mod.rs b/compiler/rustc_trait_selection/src/traits/select/mod.rs index 2aa214694cb14..767cb1618bb67 100644 --- a/compiler/rustc_trait_selection/src/traits/select/mod.rs +++ b/compiler/rustc_trait_selection/src/traits/select/mod.rs @@ -2222,6 +2222,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { .predicate .to_poly_trait_ref() .fold_with(&mut self.freshener) + .into_ok() .with_constness(obligation.predicate.skip_binder().constness); let dfn = previous_stack.cache.next_dfn(); diff --git a/compiler/rustc_traits/src/chalk/db.rs b/compiler/rustc_traits/src/chalk/db.rs index 1d457d6761fd0..fdff07302c240 100644 --- a/compiler/rustc_traits/src/chalk/db.rs +++ b/compiler/rustc_traits/src/chalk/db.rs @@ -45,7 +45,7 @@ impl<'tcx> RustIrDatabase<'tcx> { predicates .iter() .map(|(wc, _)| wc.subst(self.interner.tcx, bound_vars)) - .map(|wc| wc.fold_with(&mut regions_substitutor)) + .map(|wc| wc.fold_with(&mut regions_substitutor).into_ok()) .filter_map(|wc| LowerInto::>>>::lower_into(wc, &self.interner)).collect() } @@ -287,7 +287,7 @@ impl<'tcx> chalk_solve::RustIrDatabase> for RustIrDatabase<'t let trait_ref = trait_ref.subst(self.interner.tcx, bound_vars); let mut regions_substitutor = lowering::RegionsSubstitutor::new(self.interner.tcx, self.reempty_placeholder); - let trait_ref = trait_ref.fold_with(&mut regions_substitutor); + let trait_ref = trait_ref.fold_with(&mut regions_substitutor).into_ok(); let where_clauses = self.where_clauses_for(def_id, bound_vars); @@ -335,7 +335,7 @@ impl<'tcx> chalk_solve::RustIrDatabase> for RustIrDatabase<'t let self_ty = self_ty.subst(self.interner.tcx, bound_vars); let mut regions_substitutor = lowering::RegionsSubstitutor::new(self.interner.tcx, self.reempty_placeholder); - let self_ty = self_ty.fold_with(&mut regions_substitutor); + let self_ty = self_ty.fold_with(&mut regions_substitutor).into_ok(); let lowered_ty = self_ty.lower_into(&self.interner); parameters[0].assert_ty_ref(&self.interner).could_match( @@ -501,22 +501,24 @@ impl<'tcx> chalk_solve::RustIrDatabase> for RustIrDatabase<'t .iter() .map(|(bound, _)| bound.subst(self.interner.tcx, &bound_vars)) .map(|bound| { - bound.fold_with(&mut ty::fold::BottomUpFolder { - tcx: self.interner.tcx, - ty_op: |ty| { - if let ty::Opaque(def_id, substs) = *ty.kind() { - if def_id == opaque_ty_id.0 && substs == identity_substs { - return self.interner.tcx.mk_ty(ty::Bound( - ty::INNERMOST, - ty::BoundTy::from(ty::BoundVar::from_u32(0)), - )); + bound + .fold_with(&mut ty::fold::BottomUpFolder { + tcx: self.interner.tcx, + ty_op: |ty| { + if let ty::Opaque(def_id, substs) = *ty.kind() { + if def_id == opaque_ty_id.0 && substs == identity_substs { + return self.interner.tcx.mk_ty(ty::Bound( + ty::INNERMOST, + ty::BoundTy::from(ty::BoundVar::from_u32(0)), + )); + } } - } - ty - }, - lt_op: |lt| lt, - ct_op: |ct| ct, - }) + ty + }, + lt_op: |lt| lt, + ct_op: |ct| ct, + }) + .into_ok() }) .filter_map(|bound| { LowerInto::< diff --git a/compiler/rustc_traits/src/chalk/lowering.rs b/compiler/rustc_traits/src/chalk/lowering.rs index 033a94658425b..66073facf4b0c 100644 --- a/compiler/rustc_traits/src/chalk/lowering.rs +++ b/compiler/rustc_traits/src/chalk/lowering.rs @@ -817,7 +817,7 @@ crate fn collect_bound_vars<'tcx, T: TypeFoldable<'tcx>>( .collect(); let mut bound_var_substitutor = NamedBoundVarSubstitutor::new(tcx, &named_parameters); - let new_ty = ty.skip_binder().fold_with(&mut bound_var_substitutor); + let new_ty = ty.skip_binder().fold_with(&mut bound_var_substitutor).into_ok(); for var in named_parameters.values() { parameters.insert(*var, chalk_ir::VariableKind::Lifetime); diff --git a/compiler/rustc_traits/src/chalk/mod.rs b/compiler/rustc_traits/src/chalk/mod.rs index b7275bac19048..a6f7c4d7988f6 100644 --- a/compiler/rustc_traits/src/chalk/mod.rs +++ b/compiler/rustc_traits/src/chalk/mod.rs @@ -49,12 +49,12 @@ crate fn evaluate_goal<'tcx>( let mut params_substitutor = ParamsSubstitutor::new(tcx, placeholders_collector.next_ty_placeholder); - let obligation = obligation.fold_with(&mut params_substitutor); + let obligation = obligation.fold_with(&mut params_substitutor).into_ok(); // FIXME(chalk): we really should be substituting these back in the solution let _params: FxHashMap = params_substitutor.params; let mut regions_substitutor = RegionsSubstitutor::new(tcx, reempty_placeholder); - let obligation = obligation.fold_with(&mut regions_substitutor); + let obligation = obligation.fold_with(&mut regions_substitutor).into_ok(); let max_universe = obligation.max_universe.index(); diff --git a/compiler/rustc_traits/src/lib.rs b/compiler/rustc_traits/src/lib.rs index 8612499623be6..ea70a8d9e3a0d 100644 --- a/compiler/rustc_traits/src/lib.rs +++ b/compiler/rustc_traits/src/lib.rs @@ -4,6 +4,7 @@ #![feature(crate_visibility_modifier)] #![feature(in_band_lifetimes)] #![feature(nll)] +#![feature(unwrap_infallible)] #![recursion_limit = "256"] #[macro_use] diff --git a/compiler/rustc_typeck/src/check/op.rs b/compiler/rustc_typeck/src/check/op.rs index 8f852c0b51c98..1c4e0522bef8f 100644 --- a/compiler/rustc_typeck/src/check/op.rs +++ b/compiler/rustc_typeck/src/check/op.rs @@ -442,8 +442,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { let mut eraser = TypeParamEraser(self, expr.span); let needs_bound = self .lookup_op_method( - eraser.fold_ty(lhs_ty), - &[eraser.fold_ty(rhs_ty)], + eraser.fold_ty(lhs_ty).into_ok(), + &[eraser.fold_ty(rhs_ty).into_ok()], Op::Binary(op, is_assign), ) .is_ok(); diff --git a/compiler/rustc_typeck/src/check/writeback.rs b/compiler/rustc_typeck/src/check/writeback.rs index ecf0332db7b8b..a5965411020a4 100644 --- a/compiler/rustc_typeck/src/check/writeback.rs +++ b/compiler/rustc_typeck/src/check/writeback.rs @@ -658,7 +658,7 @@ impl<'cx, 'tcx> WritebackCx<'cx, 'tcx> { T: TypeFoldable<'tcx>, { let mut resolver = Resolver::new(self.fcx, span, self.body); - let x = x.fold_with(&mut resolver); + let x = x.fold_with(&mut resolver).into_ok(); if cfg!(debug_assertions) && x.needs_infer() { span_bug!(span.to_span(self.fcx.tcx), "writeback: `{:?}` has inference variables", x); } diff --git a/compiler/rustc_typeck/src/collect/type_of.rs b/compiler/rustc_typeck/src/collect/type_of.rs index 1dce63416dc96..1ea379c57389a 100644 --- a/compiler/rustc_typeck/src/collect/type_of.rs +++ b/compiler/rustc_typeck/src/collect/type_of.rs @@ -761,7 +761,7 @@ fn infer_placeholder_type<'a>( // Suggesting unnameable types won't help. let mut mk_nameable = MakeNameable::new(tcx); - let ty = mk_nameable.fold_ty(ty); + let ty = mk_nameable.fold_ty(ty).into_ok(); let sugg_ty = if mk_nameable.success { Some(ty) } else { None }; if let Some(sugg_ty) = sugg_ty { err.span_suggestion( @@ -785,7 +785,7 @@ fn infer_placeholder_type<'a>( if !ty.references_error() { let mut mk_nameable = MakeNameable::new(tcx); - let ty = mk_nameable.fold_ty(ty); + let ty = mk_nameable.fold_ty(ty).into_ok(); let sugg_ty = if mk_nameable.success { Some(ty) } else { None }; if let Some(sugg_ty) = sugg_ty { diag.span_suggestion( diff --git a/compiler/rustc_typeck/src/hir_wf_check.rs b/compiler/rustc_typeck/src/hir_wf_check.rs index 5c229c86ddc1a..f9de6376b0ff5 100644 --- a/compiler/rustc_typeck/src/hir_wf_check.rs +++ b/compiler/rustc_typeck/src/hir_wf_check.rs @@ -71,8 +71,11 @@ fn diagnostic_hir_wf_check<'tcx>( fn visit_ty(&mut self, ty: &'tcx hir::Ty<'tcx>) { self.tcx.infer_ctxt().enter(|infcx| { let mut fulfill = traits::FulfillmentContext::new(); - let tcx_ty = - self.icx.to_ty(ty).fold_with(&mut EraseAllBoundRegions { tcx: self.tcx }); + let tcx_ty = self + .icx + .to_ty(ty) + .fold_with(&mut EraseAllBoundRegions { tcx: self.tcx }) + .into_ok(); let cause = traits::ObligationCause::new( ty.span, self.hir_id, diff --git a/compiler/rustc_typeck/src/lib.rs b/compiler/rustc_typeck/src/lib.rs index 0881cf07586b3..c0f0b3fe7046e 100644 --- a/compiler/rustc_typeck/src/lib.rs +++ b/compiler/rustc_typeck/src/lib.rs @@ -71,6 +71,7 @@ This API is completely unstable and subject to change. #![feature(slice_partition_dedup)] #![feature(control_flow_enum)] #![feature(hash_drain_filter)] +#![feature(unwrap_infallible)] #![recursion_limit = "256"] #[macro_use] diff --git a/src/librustdoc/clean/auto_trait.rs b/src/librustdoc/clean/auto_trait.rs index 3a6f611660ece..04fe1dccce44b 100644 --- a/src/librustdoc/clean/auto_trait.rs +++ b/src/librustdoc/clean/auto_trait.rs @@ -449,7 +449,7 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> { _ => false, } }) - .map(|p| p.fold_with(&mut replacer)); + .map(|p| p.fold_with(&mut replacer).into_ok()); let mut generic_params = (tcx.generics_of(item_def_id), tcx.explicit_predicates_of(item_def_id)) diff --git a/src/librustdoc/lib.rs b/src/librustdoc/lib.rs index b6311abb5c3e8..6ba56fe01a935 100644 --- a/src/librustdoc/lib.rs +++ b/src/librustdoc/lib.rs @@ -18,6 +18,7 @@ #![feature(type_ascription)] #![feature(iter_intersperse)] #![recursion_limit = "256"] +#![feature(unwrap_infallible)] #![warn(rustc::internal)] #[macro_use]