@@ -6,9 +6,10 @@ use rustc_hir as hir;
66use rustc_hir:: def:: { DefKind , Res } ;
77use rustc_hir:: def_id:: DefId ;
88use rustc_lint_defs:: builtin:: UNUSED_ASSOCIATED_TYPE_BOUNDS ;
9- use rustc_middle:: ty:: { self , Ty } ;
9+ use rustc_middle:: ty:: fold:: BottomUpFolder ;
10+ use rustc_middle:: ty:: { self , Ty , TyCtxt , TypeFoldable } ;
1011use rustc_middle:: ty:: { DynKind , ToPredicate } ;
11- use rustc_span:: Span ;
12+ use rustc_span:: { ErrorGuaranteed , Span } ;
1213use rustc_trait_selection:: traits:: error_reporting:: report_object_safety_error;
1314use rustc_trait_selection:: traits:: { self , hir_ty_lowering_object_safety_violations} ;
1415
@@ -228,12 +229,17 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
228229 if arg == dummy_self. into ( ) {
229230 let param = & generics. params [ index] ;
230231 missing_type_params. push ( param. name ) ;
231- return Ty :: new_misc_error ( tcx) . into ( ) ;
232+ Ty :: new_misc_error ( tcx) . into ( )
232233 } else if arg. walk ( ) . any ( |arg| arg == dummy_self. into ( ) ) {
233234 references_self = true ;
234- return Ty :: new_misc_error ( tcx) . into ( ) ;
235+ let guar = tcx. dcx ( ) . span_delayed_bug (
236+ span,
237+ "trait object trait bounds reference `Self`" ,
238+ ) ;
239+ replace_dummy_self_with_error ( tcx, arg, guar)
240+ } else {
241+ arg
235242 }
236- arg
237243 } )
238244 . collect ( ) ;
239245 let args = tcx. mk_args ( & args) ;
@@ -288,18 +294,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
288294 let guar = tcx
289295 . dcx ( )
290296 . span_delayed_bug ( span, "trait object projection bounds reference `Self`" ) ;
291- let args: Vec < _ > = b
292- . projection_ty
293- . args
294- . iter ( )
295- . map ( |arg| {
296- if arg. walk ( ) . any ( |arg| arg == dummy_self. into ( ) ) {
297- return Ty :: new_error ( tcx, guar) . into ( ) ;
298- }
299- arg
300- } )
301- . collect ( ) ;
302- b. projection_ty . args = tcx. mk_args ( & args) ;
297+ b. projection_ty = replace_dummy_self_with_error ( tcx, b. projection_ty , guar) ;
303298 }
304299
305300 ty:: ExistentialProjection :: erase_self_ty ( tcx, b)
@@ -357,3 +352,18 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
357352 Ty :: new_dynamic ( tcx, existential_predicates, region_bound, representation)
358353 }
359354}
355+
356+ fn replace_dummy_self_with_error < ' tcx , T : TypeFoldable < TyCtxt < ' tcx > > > (
357+ tcx : TyCtxt < ' tcx > ,
358+ t : T ,
359+ guar : ErrorGuaranteed ,
360+ ) -> T {
361+ t. fold_with ( & mut BottomUpFolder {
362+ tcx,
363+ ty_op : |ty| {
364+ if ty == tcx. types . trait_object_dummy_self { Ty :: new_error ( tcx, guar) } else { ty }
365+ } ,
366+ lt_op : |lt| lt,
367+ ct_op : |ct| ct,
368+ } )
369+ }
0 commit comments