@@ -7,9 +7,10 @@ use rustc_hir as hir;
7
7
use rustc_hir:: def:: { DefKind , Res } ;
8
8
use rustc_hir:: def_id:: DefId ;
9
9
use rustc_lint_defs:: builtin:: UNUSED_ASSOCIATED_TYPE_BOUNDS ;
10
- use rustc_middle:: ty:: { self , Ty } ;
10
+ use rustc_middle:: ty:: fold:: BottomUpFolder ;
11
+ use rustc_middle:: ty:: { self , Ty , TyCtxt , TypeFoldable } ;
11
12
use rustc_middle:: ty:: { DynKind , ToPredicate } ;
12
- use rustc_span:: Span ;
13
+ use rustc_span:: { ErrorGuaranteed , Span } ;
13
14
use rustc_trait_selection:: traits:: error_reporting:: report_object_safety_error;
14
15
use rustc_trait_selection:: traits:: { self , hir_ty_lowering_object_safety_violations} ;
15
16
@@ -267,12 +268,17 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
267
268
if arg == dummy_self. into ( ) {
268
269
let param = & generics. params [ index] ;
269
270
missing_type_params. push ( param. name ) ;
270
- return Ty :: new_misc_error ( tcx) . into ( ) ;
271
+ Ty :: new_misc_error ( tcx) . into ( )
271
272
} else if arg. walk ( ) . any ( |arg| arg == dummy_self. into ( ) ) {
272
273
references_self = true ;
273
- return Ty :: new_misc_error ( tcx) . into ( ) ;
274
+ let guar = tcx. dcx ( ) . span_delayed_bug (
275
+ span,
276
+ "trait object trait bounds reference `Self`" ,
277
+ ) ;
278
+ replace_dummy_self_with_error ( tcx, arg, guar)
279
+ } else {
280
+ arg
274
281
}
275
- arg
276
282
} )
277
283
. collect ( ) ;
278
284
let args = tcx. mk_args ( & args) ;
@@ -327,18 +333,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
327
333
let guar = tcx
328
334
. dcx ( )
329
335
. span_delayed_bug ( span, "trait object projection bounds reference `Self`" ) ;
330
- let args: Vec < _ > = b
331
- . projection_ty
332
- . args
333
- . iter ( )
334
- . map ( |arg| {
335
- if arg. walk ( ) . any ( |arg| arg == dummy_self. into ( ) ) {
336
- return Ty :: new_error ( tcx, guar) . into ( ) ;
337
- }
338
- arg
339
- } )
340
- . collect ( ) ;
341
- b. projection_ty . args = tcx. mk_args ( & args) ;
336
+ b. projection_ty = replace_dummy_self_with_error ( tcx, b. projection_ty , guar) ;
342
337
}
343
338
344
339
ty:: ExistentialProjection :: erase_self_ty ( tcx, b)
@@ -396,3 +391,18 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
396
391
Ty :: new_dynamic ( tcx, existential_predicates, region_bound, representation)
397
392
}
398
393
}
394
+
395
+ fn replace_dummy_self_with_error < ' tcx , T : TypeFoldable < TyCtxt < ' tcx > > > (
396
+ tcx : TyCtxt < ' tcx > ,
397
+ t : T ,
398
+ guar : ErrorGuaranteed ,
399
+ ) -> T {
400
+ t. fold_with ( & mut BottomUpFolder {
401
+ tcx,
402
+ ty_op : |ty| {
403
+ if ty == tcx. types . trait_object_dummy_self { Ty :: new_error ( tcx, guar) } else { ty }
404
+ } ,
405
+ lt_op : |lt| lt,
406
+ ct_op : |ct| ct,
407
+ } )
408
+ }
0 commit comments