From c5f0d0ebb4eca79491d30bfb6f32a4541faeaa25 Mon Sep 17 00:00:00 2001 From: LeSeulArtichaut Date: Wed, 19 May 2021 13:20:39 +0200 Subject: [PATCH] Make `TypeFolder::fold_*` return `Result` --- compiler/rustc_middle/src/ty/fold.rs | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/compiler/rustc_middle/src/ty/fold.rs b/compiler/rustc_middle/src/ty/fold.rs index e16491dcc90..a306656984f 100644 --- a/compiler/rustc_middle/src/ty/fold.rs +++ b/compiler/rustc_middle/src/ty/fold.rs @@ -46,8 +46,8 @@ /// /// To implement this conveniently, use the derive macro located in `rustc_macros`. pub trait TypeFoldable<'tcx>: fmt::Debug + Clone { - fn super_fold_with>(self, folder: &mut F) -> Self; - fn fold_with>(self, folder: &mut F) -> Self { + fn super_fold_with>(self, folder: &mut F) -> Result; + fn fold_with>(self, folder: &mut F) -> Result { self.super_fold_with(folder) } @@ -193,32 +193,43 @@ fn super_visit_with>(&self, _: &mut V) -> ControlFlow: Sized { + type Error = !; + fn tcx<'a>(&'a self) -> TyCtxt<'tcx>; - fn fold_binder(&mut self, t: Binder<'tcx, T>) -> Binder<'tcx, T> + fn fold_binder(&mut self, t: Binder<'tcx, T>) -> Result, Self::Error> where T: TypeFoldable<'tcx>, { t.super_fold_with(self) } - fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> { + fn fold_ty(&mut self, t: Ty<'tcx>) -> Result, Self::Error> { t.super_fold_with(self) } - fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> { + fn fold_region(&mut self, r: ty::Region<'tcx>) -> Result, Self::Error> { r.super_fold_with(self) } - fn fold_const(&mut self, c: &'tcx ty::Const<'tcx>) -> &'tcx ty::Const<'tcx> { + fn fold_const( + &mut self, + c: &'tcx ty::Const<'tcx>, + ) -> Result<&'tcx ty::Const<'tcx>, Self::Error> { c.super_fold_with(self) } - fn fold_predicate(&mut self, p: ty::Predicate<'tcx>) -> ty::Predicate<'tcx> { + fn fold_predicate( + &mut self, + p: ty::Predicate<'tcx>, + ) -> Result, Self::Error> { p.super_fold_with(self) } - fn fold_mir_const(&mut self, c: mir::ConstantKind<'tcx>) -> mir::ConstantKind<'tcx> { + fn fold_mir_const( + &mut self, + c: mir::ConstantKind<'tcx>, + ) -> Result, Self::Error> { bug!("most type folders should not be folding MIR datastructures: {:?}", c) } }