Skip to content

Commit

Permalink
Make TypeFolder::fold_* return Result
Browse files Browse the repository at this point in the history
  • Loading branch information
LeSeulArtichaut authored and eggyal committed Nov 26, 2021
1 parent 9adfd9d commit c5f0d0e
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions compiler/rustc_middle/src/ty/fold.rs
Expand Up @@ -46,8 +46,8 @@ use std::ops::ControlFlow;
///
/// To implement this conveniently, use the derive macro located in `rustc_macros`.
pub trait TypeFoldable<'tcx>: fmt::Debug + Clone {
fn super_fold_with<F: TypeFolder<'tcx>>(self, folder: &mut F) -> Self;
fn fold_with<F: TypeFolder<'tcx>>(self, folder: &mut F) -> Self {
fn super_fold_with<F: TypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error>;
fn fold_with<F: TypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
self.super_fold_with(folder)
}

Expand Down Expand Up @@ -193,32 +193,43 @@ impl TypeFoldable<'tcx> for hir::Constness {
/// identity fold, it should invoke `foo.fold_with(self)` to fold each
/// sub-item.
pub trait TypeFolder<'tcx>: Sized {
type Error = !;

fn tcx<'a>(&'a self) -> TyCtxt<'tcx>;

fn fold_binder<T>(&mut self, t: Binder<'tcx, T>) -> Binder<'tcx, T>
fn fold_binder<T>(&mut self, t: Binder<'tcx, T>) -> Result<Binder<'tcx, T>, 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<Ty<'tcx>, 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<ty::Region<'tcx>, 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<ty::Predicate<'tcx>, 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<mir::ConstantKind<'tcx>, Self::Error> {
bug!("most type folders should not be folding MIR datastructures: {:?}", c)
}
}
Expand Down

0 comments on commit c5f0d0e

Please sign in to comment.