@@ -18,7 +18,9 @@ use rustc_ast::Recovered;
1818use  rustc_data_structures:: captures:: Captures ; 
1919use  rustc_data_structures:: fx:: { FxHashSet ,  FxIndexMap } ; 
2020use  rustc_data_structures:: unord:: UnordMap ; 
21- use  rustc_errors:: { struct_span_code_err,  Applicability ,  Diag ,  ErrorGuaranteed ,  StashKey ,  E0228 } ; 
21+ use  rustc_errors:: { 
22+     struct_span_code_err,  Applicability ,  Diag ,  DiagCtxtHandle ,  ErrorGuaranteed ,  StashKey ,  E0228 , 
23+ } ; 
2224use  rustc_hir:: def:: DefKind ; 
2325use  rustc_hir:: def_id:: { DefId ,  LocalDefId } ; 
2426use  rustc_hir:: intravisit:: { self ,  walk_generics,  Visitor } ; 
@@ -161,7 +163,7 @@ pub struct CollectItemTypesVisitor<'tcx> {
161163/// and suggest adding type parameters in the appropriate place, taking into consideration any and 
162164/// all already existing generic type parameters to avoid suggesting a name that is already in use. 
163165pub ( crate )  fn  placeholder_type_error < ' tcx > ( 
164-     tcx :   TyCtxt < ' tcx > , 
166+     cx :   & dyn   HirTyLowerer < ' tcx > , 
165167    generics :  Option < & hir:: Generics < ' _ > > , 
166168    placeholder_types :  Vec < Span > , 
167169    suggest :  bool , 
@@ -172,21 +174,21 @@ pub(crate) fn placeholder_type_error<'tcx>(
172174        return ; 
173175    } 
174176
175-     placeholder_type_error_diag ( tcx ,  generics,  placeholder_types,  vec ! [ ] ,  suggest,  hir_ty,  kind) 
177+     placeholder_type_error_diag ( cx ,  generics,  placeholder_types,  vec ! [ ] ,  suggest,  hir_ty,  kind) 
176178        . emit ( ) ; 
177179} 
178180
179- pub ( crate )  fn  placeholder_type_error_diag < ' tcx > ( 
180-     tcx :   TyCtxt < ' tcx > , 
181+ pub ( crate )  fn  placeholder_type_error_diag < ' cx ,   ' tcx > ( 
182+     cx :   & ' cx   dyn   HirTyLowerer < ' tcx > , 
181183    generics :  Option < & hir:: Generics < ' _ > > , 
182184    placeholder_types :  Vec < Span > , 
183185    additional_spans :  Vec < Span > , 
184186    suggest :  bool , 
185187    hir_ty :  Option < & hir:: Ty < ' _ > > , 
186188    kind :  & ' static  str , 
187- )  -> Diag < ' tcx >  { 
189+ )  -> Diag < ' cx >  { 
188190    if  placeholder_types. is_empty ( )  { 
189-         return  bad_placeholder ( tcx ,  additional_spans,  kind) ; 
191+         return  bad_placeholder ( cx ,  additional_spans,  kind) ; 
190192    } 
191193
192194    let  params = generics. map ( |g| g. params ) . unwrap_or_default ( ) ; 
@@ -210,7 +212,7 @@ pub(crate) fn placeholder_type_error_diag<'tcx>(
210212    } 
211213
212214    let  mut  err =
213-         bad_placeholder ( tcx ,  placeholder_types. into_iter ( ) . chain ( additional_spans) . collect ( ) ,  kind) ; 
215+         bad_placeholder ( cx ,  placeholder_types. into_iter ( ) . chain ( additional_spans) . collect ( ) ,  kind) ; 
214216
215217    // Suggest, but only if it is not a function in const or static 
216218    if  suggest { 
@@ -224,7 +226,7 @@ pub(crate) fn placeholder_type_error_diag<'tcx>(
224226
225227            // Check if parent is const or static 
226228            is_const_or_static = matches ! ( 
227-                 tcx. parent_hir_node( hir_ty. hir_id) , 
229+                 cx . tcx( ) . parent_hir_node( hir_ty. hir_id) , 
228230                Node :: Item ( & hir:: Item  { 
229231                    kind:  hir:: ItemKind :: Const ( ..)  | hir:: ItemKind :: Static ( ..) , 
230232                    ..
@@ -267,7 +269,16 @@ fn reject_placeholder_type_signatures_in_item<'tcx>(
267269    let  mut  visitor = HirPlaceholderCollector :: default ( ) ; 
268270    visitor. visit_item ( item) ; 
269271
270-     placeholder_type_error ( tcx,  Some ( generics) ,  visitor. 0 ,  suggest,  None ,  item. kind . descr ( ) ) ; 
272+     let  icx = ItemCtxt :: new ( tcx,  item. owner_id . def_id ) ; 
273+ 
274+     placeholder_type_error ( 
275+         icx. lowerer ( ) , 
276+         Some ( generics) , 
277+         visitor. 0 , 
278+         suggest, 
279+         None , 
280+         item. kind . descr ( ) , 
281+     ) ; 
271282} 
272283
273284impl < ' tcx >  Visitor < ' tcx >  for  CollectItemTypesVisitor < ' tcx >  { 
@@ -329,15 +340,15 @@ impl<'tcx> Visitor<'tcx> for CollectItemTypesVisitor<'tcx> {
329340/////////////////////////////////////////////////////////////////////////// 
330341// Utility types and common code for the above passes. 
331342
332- fn  bad_placeholder < ' tcx > ( 
333-     tcx :   TyCtxt < ' tcx > , 
343+ fn  bad_placeholder < ' cx ,   ' tcx > ( 
344+     cx :   & ' cx   dyn   HirTyLowerer < ' tcx > , 
334345    mut  spans :  Vec < Span > , 
335346    kind :  & ' static  str , 
336- )  -> Diag < ' tcx >  { 
347+ )  -> Diag < ' cx >  { 
337348    let  kind = if  kind. ends_with ( 's' )  {  format ! ( "{kind}es" )  }  else  {  format ! ( "{kind}s" )  } ; 
338349
339350    spans. sort ( ) ; 
340-     tcx . dcx ( ) . create_err ( errors:: PlaceholderNotAllowedItemSignatures  {  spans,  kind } ) 
351+     cx . dcx ( ) . create_err ( errors:: PlaceholderNotAllowedItemSignatures  {  spans,  kind } ) 
341352} 
342353
343354impl < ' tcx >  ItemCtxt < ' tcx >  { 
@@ -370,21 +381,24 @@ impl<'tcx> HirTyLowerer<'tcx> for ItemCtxt<'tcx> {
370381        self . tcx 
371382    } 
372383
384+     fn  dcx ( & self )  -> DiagCtxtHandle < ' _ >  { 
385+         self . tcx . dcx ( ) . taintable_handle ( & self . tainted_by_errors ) 
386+     } 
387+ 
373388    fn  item_def_id ( & self )  -> LocalDefId  { 
374389        self . item_def_id 
375390    } 
376391
377392    fn  re_infer ( & self ,  span :  Span ,  reason :  RegionInferReason < ' _ > )  -> ty:: Region < ' tcx >  { 
378393        if  let  RegionInferReason :: BorrowedObjectLifetimeDefault  = reason { 
379394            let  e = struct_span_code_err ! ( 
380-                 self . tcx ( ) . dcx( ) , 
395+                 self . dcx( ) , 
381396                span, 
382397                E0228 , 
383398                "the lifetime bound for this object type cannot be deduced \  
384399                 from context; please supply an explicit bound"
385400            ) 
386401            . emit ( ) ; 
387-             self . set_tainted_by_errors ( e) ; 
388402            ty:: Region :: new_error ( self . tcx ( ) ,  e) 
389403        }  else  { 
390404            // This indicates an illegal lifetime in a non-assoc-trait position 
@@ -509,10 +523,6 @@ impl<'tcx> HirTyLowerer<'tcx> for ItemCtxt<'tcx> {
509523        None 
510524    } 
511525
512-     fn  set_tainted_by_errors ( & self ,  err :  ErrorGuaranteed )  { 
513-         self . tainted_by_errors . set ( Some ( err) ) ; 
514-     } 
515- 
516526    fn  lower_fn_sig ( 
517527        & self , 
518528        decl :  & hir:: FnDecl < ' tcx > , 
@@ -570,7 +580,7 @@ impl<'tcx> HirTyLowerer<'tcx> for ItemCtxt<'tcx> {
570580            // `ident_span` to not emit an error twice when we have `fn foo(_: fn() -> _)`. 
571581
572582            let  mut  diag = crate :: collect:: placeholder_type_error_diag ( 
573-                 tcx , 
583+                 self , 
574584                generics, 
575585                visitor. 0 , 
576586                infer_replacements. iter ( ) . map ( |( s,  _) | * s) . collect ( ) , 
@@ -590,7 +600,7 @@ impl<'tcx> HirTyLowerer<'tcx> for ItemCtxt<'tcx> {
590600                ) ; 
591601            } 
592602
593-             self . set_tainted_by_errors ( diag. emit ( ) ) ; 
603+             diag. emit ( ) ; 
594604        } 
595605
596606        ( input_tys,  output_ty) 
@@ -639,6 +649,7 @@ fn lower_item(tcx: TyCtxt<'_>, item_id: hir::ItemId) {
639649    let  it = tcx. hir ( ) . item ( item_id) ; 
640650    debug ! ( item = %it. ident,  id = %it. hir_id( ) ) ; 
641651    let  def_id = item_id. owner_id . def_id ; 
652+     let  icx = ItemCtxt :: new ( tcx,  def_id) ; 
642653
643654    match  & it. kind  { 
644655        // These don't define types. 
@@ -663,7 +674,7 @@ fn lower_item(tcx: TyCtxt<'_>, item_id: hir::ItemId) {
663674                        let  mut  visitor = HirPlaceholderCollector :: default ( ) ; 
664675                        visitor. visit_foreign_item ( item) ; 
665676                        placeholder_type_error ( 
666-                             tcx , 
677+                             icx . lowerer ( ) , 
667678                            None , 
668679                            visitor. 0 , 
669680                            false , 
@@ -742,7 +753,14 @@ fn lower_item(tcx: TyCtxt<'_>, item_id: hir::ItemId) {
742753            if  !ty. is_suggestable_infer_ty ( )  { 
743754                let  mut  visitor = HirPlaceholderCollector :: default ( ) ; 
744755                visitor. visit_item ( it) ; 
745-                 placeholder_type_error ( tcx,  None ,  visitor. 0 ,  false ,  None ,  it. kind . descr ( ) ) ; 
756+                 placeholder_type_error ( 
757+                     icx. lowerer ( ) , 
758+                     None , 
759+                     visitor. 0 , 
760+                     false , 
761+                     None , 
762+                     it. kind . descr ( ) , 
763+                 ) ; 
746764            } 
747765        } 
748766
@@ -760,6 +778,7 @@ fn lower_trait_item(tcx: TyCtxt<'_>, trait_item_id: hir::TraitItemId) {
760778    let  trait_item = tcx. hir ( ) . trait_item ( trait_item_id) ; 
761779    let  def_id = trait_item_id. owner_id ; 
762780    tcx. ensure ( ) . generics_of ( def_id) ; 
781+     let  icx = ItemCtxt :: new ( tcx,  def_id. def_id ) ; 
763782
764783    match  trait_item. kind  { 
765784        hir:: TraitItemKind :: Fn ( ..)  => { 
@@ -776,7 +795,14 @@ fn lower_trait_item(tcx: TyCtxt<'_>, trait_item_id: hir::TraitItemId) {
776795                // Account for `const C: _;`. 
777796                let  mut  visitor = HirPlaceholderCollector :: default ( ) ; 
778797                visitor. visit_trait_item ( trait_item) ; 
779-                 placeholder_type_error ( tcx,  None ,  visitor. 0 ,  false ,  None ,  "associated constant" ) ; 
798+                 placeholder_type_error ( 
799+                     icx. lowerer ( ) , 
800+                     None , 
801+                     visitor. 0 , 
802+                     false , 
803+                     None , 
804+                     "associated constant" , 
805+                 ) ; 
780806            } 
781807        } 
782808
@@ -787,7 +813,7 @@ fn lower_trait_item(tcx: TyCtxt<'_>, trait_item_id: hir::TraitItemId) {
787813            // Account for `type T = _;`. 
788814            let  mut  visitor = HirPlaceholderCollector :: default ( ) ; 
789815            visitor. visit_trait_item ( trait_item) ; 
790-             placeholder_type_error ( tcx ,  None ,  visitor. 0 ,  false ,  None ,  "associated type" ) ; 
816+             placeholder_type_error ( icx . lowerer ( ) ,  None ,  visitor. 0 ,  false ,  None ,  "associated type" ) ; 
791817        } 
792818
793819        hir:: TraitItemKind :: Type ( _,  None )  => { 
@@ -798,7 +824,7 @@ fn lower_trait_item(tcx: TyCtxt<'_>, trait_item_id: hir::TraitItemId) {
798824            let  mut  visitor = HirPlaceholderCollector :: default ( ) ; 
799825            visitor. visit_trait_item ( trait_item) ; 
800826
801-             placeholder_type_error ( tcx ,  None ,  visitor. 0 ,  false ,  None ,  "associated type" ) ; 
827+             placeholder_type_error ( icx . lowerer ( ) ,  None ,  visitor. 0 ,  false ,  None ,  "associated type" ) ; 
802828        } 
803829    } ; 
804830
@@ -811,6 +837,7 @@ fn lower_impl_item(tcx: TyCtxt<'_>, impl_item_id: hir::ImplItemId) {
811837    tcx. ensure ( ) . type_of ( def_id) ; 
812838    tcx. ensure ( ) . predicates_of ( def_id) ; 
813839    let  impl_item = tcx. hir ( ) . impl_item ( impl_item_id) ; 
840+     let  icx = ItemCtxt :: new ( tcx,  def_id. def_id ) ; 
814841    match  impl_item. kind  { 
815842        hir:: ImplItemKind :: Fn ( ..)  => { 
816843            tcx. ensure ( ) . codegen_fn_attrs ( def_id) ; 
@@ -821,14 +848,21 @@ fn lower_impl_item(tcx: TyCtxt<'_>, impl_item_id: hir::ImplItemId) {
821848            let  mut  visitor = HirPlaceholderCollector :: default ( ) ; 
822849            visitor. visit_impl_item ( impl_item) ; 
823850
824-             placeholder_type_error ( tcx ,  None ,  visitor. 0 ,  false ,  None ,  "associated type" ) ; 
851+             placeholder_type_error ( icx . lowerer ( ) ,  None ,  visitor. 0 ,  false ,  None ,  "associated type" ) ; 
825852        } 
826853        hir:: ImplItemKind :: Const ( ty,  _)  => { 
827854            // Account for `const T: _ = ..;` 
828855            if  !ty. is_suggestable_infer_ty ( )  { 
829856                let  mut  visitor = HirPlaceholderCollector :: default ( ) ; 
830857                visitor. visit_impl_item ( impl_item) ; 
831-                 placeholder_type_error ( tcx,  None ,  visitor. 0 ,  false ,  None ,  "associated constant" ) ; 
858+                 placeholder_type_error ( 
859+                     icx. lowerer ( ) , 
860+                     None , 
861+                     visitor. 0 , 
862+                     false , 
863+                     None , 
864+                     "associated constant" , 
865+                 ) ; 
832866            } 
833867        } 
834868    } 
@@ -1385,7 +1419,7 @@ fn fn_sig(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::EarlyBinder<'_, ty::PolyFn
13851419            ..
13861420        } ) 
13871421        | Item ( hir:: Item  {  kind :  ItemKind :: Fn ( sig,  generics,  _) ,  .. } )  => { 
1388-             infer_return_ty_for_fn_sig ( tcx ,   sig,  generics,  def_id,  & icx) 
1422+             infer_return_ty_for_fn_sig ( sig,  generics,  def_id,  & icx) 
13891423        } 
13901424
13911425        ImplItem ( hir:: ImplItem  {  kind :  ImplItemKind :: Fn ( sig,  _) ,  generics,  .. } )  => { 
@@ -1402,7 +1436,7 @@ fn fn_sig(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::EarlyBinder<'_, ty::PolyFn
14021436                    None , 
14031437                ) 
14041438            }  else  { 
1405-                 infer_return_ty_for_fn_sig ( tcx ,   sig,  generics,  def_id,  & icx) 
1439+                 infer_return_ty_for_fn_sig ( sig,  generics,  def_id,  & icx) 
14061440            } 
14071441        } 
14081442
@@ -1455,12 +1489,12 @@ fn fn_sig(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::EarlyBinder<'_, ty::PolyFn
14551489} 
14561490
14571491fn  infer_return_ty_for_fn_sig < ' tcx > ( 
1458-     tcx :  TyCtxt < ' tcx > , 
14591492    sig :  & hir:: FnSig < ' tcx > , 
14601493    generics :  & hir:: Generics < ' _ > , 
14611494    def_id :  LocalDefId , 
14621495    icx :  & ItemCtxt < ' tcx > , 
14631496)  -> ty:: PolyFnSig < ' tcx >  { 
1497+     let  tcx = icx. tcx ; 
14641498    let  hir_id = tcx. local_def_id_to_hir_id ( def_id) ; 
14651499
14661500    match  sig. decl . output . get_infer_ret_ty ( )  { 
@@ -1492,7 +1526,7 @@ fn infer_return_ty_for_fn_sig<'tcx>(
14921526            let  mut  visitor = HirPlaceholderCollector :: default ( ) ; 
14931527            visitor. visit_ty ( ty) ; 
14941528
1495-             let  mut  diag = bad_placeholder ( tcx ,  visitor. 0 ,  "return type" ) ; 
1529+             let  mut  diag = bad_placeholder ( icx . lowerer ( ) ,  visitor. 0 ,  "return type" ) ; 
14961530            let  ret_ty = fn_sig. output ( ) ; 
14971531            // Don't leak types into signatures unless they're nameable! 
14981532            // For example, if a function returns itself, we don't want that 
0 commit comments