@@ -46,8 +46,8 @@ use std::ops::ControlFlow;
4646///
4747/// To implement this conveniently, use the derive macro located in `rustc_macros`.
4848pub trait TypeFoldable < ' tcx > : fmt:: Debug + Clone {
49- fn super_fold_with < F : TypeFolder < ' tcx > > ( self , folder : & mut F ) -> Self ;
50- fn fold_with < F : TypeFolder < ' tcx > > ( self , folder : & mut F ) -> Self {
49+ fn super_fold_with < F : TypeFolder < ' tcx > > ( self , folder : & mut F ) -> Result < Self , F :: Error > ;
50+ fn fold_with < F : TypeFolder < ' tcx > > ( self , folder : & mut F ) -> Result < Self , F :: Error > {
5151 self . super_fold_with ( folder)
5252 }
5353
@@ -193,32 +193,43 @@ impl TypeFoldable<'tcx> for hir::Constness {
193193/// identity fold, it should invoke `foo.fold_with(self)` to fold each
194194/// sub-item.
195195pub trait TypeFolder < ' tcx > : Sized {
196+ type Error = !;
197+
196198 fn tcx < ' a > ( & ' a self ) -> TyCtxt < ' tcx > ;
197199
198- fn fold_binder < T > ( & mut self , t : Binder < ' tcx , T > ) -> Binder < ' tcx , T >
200+ fn fold_binder < T > ( & mut self , t : Binder < ' tcx , T > ) -> Result < Binder < ' tcx , T > , Self :: Error >
199201 where
200202 T : TypeFoldable < ' tcx > ,
201203 {
202204 t. super_fold_with ( self )
203205 }
204206
205- fn fold_ty ( & mut self , t : Ty < ' tcx > ) -> Ty < ' tcx > {
207+ fn fold_ty ( & mut self , t : Ty < ' tcx > ) -> Result < Ty < ' tcx > , Self :: Error > {
206208 t. super_fold_with ( self )
207209 }
208210
209- fn fold_region ( & mut self , r : ty:: Region < ' tcx > ) -> ty:: Region < ' tcx > {
211+ fn fold_region ( & mut self , r : ty:: Region < ' tcx > ) -> Result < ty:: Region < ' tcx > , Self :: Error > {
210212 r. super_fold_with ( self )
211213 }
212214
213- fn fold_const ( & mut self , c : & ' tcx ty:: Const < ' tcx > ) -> & ' tcx ty:: Const < ' tcx > {
215+ fn fold_const (
216+ & mut self ,
217+ c : & ' tcx ty:: Const < ' tcx > ,
218+ ) -> Result < & ' tcx ty:: Const < ' tcx > , Self :: Error > {
214219 c. super_fold_with ( self )
215220 }
216221
217- fn fold_predicate ( & mut self , p : ty:: Predicate < ' tcx > ) -> ty:: Predicate < ' tcx > {
222+ fn fold_predicate (
223+ & mut self ,
224+ p : ty:: Predicate < ' tcx > ,
225+ ) -> Result < ty:: Predicate < ' tcx > , Self :: Error > {
218226 p. super_fold_with ( self )
219227 }
220228
221- fn fold_mir_const ( & mut self , c : mir:: ConstantKind < ' tcx > ) -> mir:: ConstantKind < ' tcx > {
229+ fn fold_mir_const (
230+ & mut self ,
231+ c : mir:: ConstantKind < ' tcx > ,
232+ ) -> Result < mir:: ConstantKind < ' tcx > , Self :: Error > {
222233 bug ! ( "most type folders should not be folding MIR datastructures: {:?}" , c)
223234 }
224235}
0 commit comments