@@ -6,9 +6,10 @@ use rustc_hir as hir;
6
6
use rustc_hir:: def:: { DefKind , Res } ;
7
7
use rustc_hir:: def_id:: DefId ;
8
8
use 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 } ;
10
11
use rustc_middle:: ty:: { DynKind , ToPredicate } ;
11
- use rustc_span:: Span ;
12
+ use rustc_span:: { ErrorGuaranteed , Span } ;
12
13
use rustc_trait_selection:: traits:: error_reporting:: report_object_safety_error;
13
14
use rustc_trait_selection:: traits:: { self , hir_ty_lowering_object_safety_violations} ;
14
15
@@ -228,12 +229,17 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
228
229
if arg == dummy_self. into ( ) {
229
230
let param = & generics. params [ index] ;
230
231
missing_type_params. push ( param. name ) ;
231
- return Ty :: new_misc_error ( tcx) . into ( ) ;
232
+ Ty :: new_misc_error ( tcx) . into ( )
232
233
} else if arg. walk ( ) . any ( |arg| arg == dummy_self. into ( ) ) {
233
234
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
235
242
}
236
- arg
237
243
} )
238
244
. collect ( ) ;
239
245
let args = tcx. mk_args ( & args) ;
@@ -288,18 +294,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
288
294
let guar = tcx
289
295
. dcx ( )
290
296
. 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) ;
303
298
}
304
299
305
300
ty:: ExistentialProjection :: erase_self_ty ( tcx, b)
@@ -357,3 +352,18 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
357
352
Ty :: new_dynamic ( tcx, existential_predicates, region_bound, representation)
358
353
}
359
354
}
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