@@ -18,7 +18,9 @@ use rustc_ast::Recovered;
18
18
use rustc_data_structures:: captures:: Captures ;
19
19
use rustc_data_structures:: fx:: { FxHashSet , FxIndexMap } ;
20
20
use 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
+ } ;
22
24
use rustc_hir:: def:: DefKind ;
23
25
use rustc_hir:: def_id:: { DefId , LocalDefId } ;
24
26
use rustc_hir:: intravisit:: { self , walk_generics, Visitor } ;
@@ -161,7 +163,7 @@ pub struct CollectItemTypesVisitor<'tcx> {
161
163
/// and suggest adding type parameters in the appropriate place, taking into consideration any and
162
164
/// all already existing generic type parameters to avoid suggesting a name that is already in use.
163
165
pub ( crate ) fn placeholder_type_error < ' tcx > (
164
- tcx : TyCtxt < ' tcx > ,
166
+ cx : & dyn HirTyLowerer < ' tcx > ,
165
167
generics : Option < & hir:: Generics < ' _ > > ,
166
168
placeholder_types : Vec < Span > ,
167
169
suggest : bool ,
@@ -172,21 +174,21 @@ pub(crate) fn placeholder_type_error<'tcx>(
172
174
return ;
173
175
}
174
176
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)
176
178
. emit ( ) ;
177
179
}
178
180
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 > ,
181
183
generics : Option < & hir:: Generics < ' _ > > ,
182
184
placeholder_types : Vec < Span > ,
183
185
additional_spans : Vec < Span > ,
184
186
suggest : bool ,
185
187
hir_ty : Option < & hir:: Ty < ' _ > > ,
186
188
kind : & ' static str ,
187
- ) -> Diag < ' tcx > {
189
+ ) -> Diag < ' cx > {
188
190
if placeholder_types. is_empty ( ) {
189
- return bad_placeholder ( tcx , additional_spans, kind) ;
191
+ return bad_placeholder ( cx , additional_spans, kind) ;
190
192
}
191
193
192
194
let params = generics. map ( |g| g. params ) . unwrap_or_default ( ) ;
@@ -210,7 +212,7 @@ pub(crate) fn placeholder_type_error_diag<'tcx>(
210
212
}
211
213
212
214
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) ;
214
216
215
217
// Suggest, but only if it is not a function in const or static
216
218
if suggest {
@@ -224,7 +226,7 @@ pub(crate) fn placeholder_type_error_diag<'tcx>(
224
226
225
227
// Check if parent is const or static
226
228
is_const_or_static = matches ! (
227
- tcx. parent_hir_node( hir_ty. hir_id) ,
229
+ cx . tcx( ) . parent_hir_node( hir_ty. hir_id) ,
228
230
Node :: Item ( & hir:: Item {
229
231
kind: hir:: ItemKind :: Const ( ..) | hir:: ItemKind :: Static ( ..) ,
230
232
..
@@ -267,7 +269,16 @@ fn reject_placeholder_type_signatures_in_item<'tcx>(
267
269
let mut visitor = HirPlaceholderCollector :: default ( ) ;
268
270
visitor. visit_item ( item) ;
269
271
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
+ ) ;
271
282
}
272
283
273
284
impl < ' tcx > Visitor < ' tcx > for CollectItemTypesVisitor < ' tcx > {
@@ -329,15 +340,15 @@ impl<'tcx> Visitor<'tcx> for CollectItemTypesVisitor<'tcx> {
329
340
///////////////////////////////////////////////////////////////////////////
330
341
// Utility types and common code for the above passes.
331
342
332
- fn bad_placeholder < ' tcx > (
333
- tcx : TyCtxt < ' tcx > ,
343
+ fn bad_placeholder < ' cx , ' tcx > (
344
+ cx : & ' cx dyn HirTyLowerer < ' tcx > ,
334
345
mut spans : Vec < Span > ,
335
346
kind : & ' static str ,
336
- ) -> Diag < ' tcx > {
347
+ ) -> Diag < ' cx > {
337
348
let kind = if kind. ends_with ( 's' ) { format ! ( "{kind}es" ) } else { format ! ( "{kind}s" ) } ;
338
349
339
350
spans. sort ( ) ;
340
- tcx . dcx ( ) . create_err ( errors:: PlaceholderNotAllowedItemSignatures { spans, kind } )
351
+ cx . dcx ( ) . create_err ( errors:: PlaceholderNotAllowedItemSignatures { spans, kind } )
341
352
}
342
353
343
354
impl < ' tcx > ItemCtxt < ' tcx > {
@@ -370,21 +381,24 @@ impl<'tcx> HirTyLowerer<'tcx> for ItemCtxt<'tcx> {
370
381
self . tcx
371
382
}
372
383
384
+ fn dcx ( & self ) -> DiagCtxtHandle < ' _ > {
385
+ self . tcx . dcx ( ) . taintable_handle ( & self . tainted_by_errors )
386
+ }
387
+
373
388
fn item_def_id ( & self ) -> LocalDefId {
374
389
self . item_def_id
375
390
}
376
391
377
392
fn re_infer ( & self , span : Span , reason : RegionInferReason < ' _ > ) -> ty:: Region < ' tcx > {
378
393
if let RegionInferReason :: BorrowedObjectLifetimeDefault = reason {
379
394
let e = struct_span_code_err ! (
380
- self . tcx ( ) . dcx( ) ,
395
+ self . dcx( ) ,
381
396
span,
382
397
E0228 ,
383
398
"the lifetime bound for this object type cannot be deduced \
384
399
from context; please supply an explicit bound"
385
400
)
386
401
. emit ( ) ;
387
- self . set_tainted_by_errors ( e) ;
388
402
ty:: Region :: new_error ( self . tcx ( ) , e)
389
403
} else {
390
404
// This indicates an illegal lifetime in a non-assoc-trait position
@@ -509,10 +523,6 @@ impl<'tcx> HirTyLowerer<'tcx> for ItemCtxt<'tcx> {
509
523
None
510
524
}
511
525
512
- fn set_tainted_by_errors ( & self , err : ErrorGuaranteed ) {
513
- self . tainted_by_errors . set ( Some ( err) ) ;
514
- }
515
-
516
526
fn lower_fn_sig (
517
527
& self ,
518
528
decl : & hir:: FnDecl < ' tcx > ,
@@ -570,7 +580,7 @@ impl<'tcx> HirTyLowerer<'tcx> for ItemCtxt<'tcx> {
570
580
// `ident_span` to not emit an error twice when we have `fn foo(_: fn() -> _)`.
571
581
572
582
let mut diag = crate :: collect:: placeholder_type_error_diag (
573
- tcx ,
583
+ self ,
574
584
generics,
575
585
visitor. 0 ,
576
586
infer_replacements. iter ( ) . map ( |( s, _) | * s) . collect ( ) ,
@@ -590,7 +600,7 @@ impl<'tcx> HirTyLowerer<'tcx> for ItemCtxt<'tcx> {
590
600
) ;
591
601
}
592
602
593
- self . set_tainted_by_errors ( diag. emit ( ) ) ;
603
+ diag. emit ( ) ;
594
604
}
595
605
596
606
( input_tys, output_ty)
@@ -639,6 +649,7 @@ fn lower_item(tcx: TyCtxt<'_>, item_id: hir::ItemId) {
639
649
let it = tcx. hir ( ) . item ( item_id) ;
640
650
debug ! ( item = %it. ident, id = %it. hir_id( ) ) ;
641
651
let def_id = item_id. owner_id . def_id ;
652
+ let icx = ItemCtxt :: new ( tcx, def_id) ;
642
653
643
654
match & it. kind {
644
655
// These don't define types.
@@ -663,7 +674,7 @@ fn lower_item(tcx: TyCtxt<'_>, item_id: hir::ItemId) {
663
674
let mut visitor = HirPlaceholderCollector :: default ( ) ;
664
675
visitor. visit_foreign_item ( item) ;
665
676
placeholder_type_error (
666
- tcx ,
677
+ icx . lowerer ( ) ,
667
678
None ,
668
679
visitor. 0 ,
669
680
false ,
@@ -742,7 +753,14 @@ fn lower_item(tcx: TyCtxt<'_>, item_id: hir::ItemId) {
742
753
if !ty. is_suggestable_infer_ty ( ) {
743
754
let mut visitor = HirPlaceholderCollector :: default ( ) ;
744
755
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
+ ) ;
746
764
}
747
765
}
748
766
@@ -760,6 +778,7 @@ fn lower_trait_item(tcx: TyCtxt<'_>, trait_item_id: hir::TraitItemId) {
760
778
let trait_item = tcx. hir ( ) . trait_item ( trait_item_id) ;
761
779
let def_id = trait_item_id. owner_id ;
762
780
tcx. ensure ( ) . generics_of ( def_id) ;
781
+ let icx = ItemCtxt :: new ( tcx, def_id. def_id ) ;
763
782
764
783
match trait_item. kind {
765
784
hir:: TraitItemKind :: Fn ( ..) => {
@@ -776,7 +795,14 @@ fn lower_trait_item(tcx: TyCtxt<'_>, trait_item_id: hir::TraitItemId) {
776
795
// Account for `const C: _;`.
777
796
let mut visitor = HirPlaceholderCollector :: default ( ) ;
778
797
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
+ ) ;
780
806
}
781
807
}
782
808
@@ -787,7 +813,7 @@ fn lower_trait_item(tcx: TyCtxt<'_>, trait_item_id: hir::TraitItemId) {
787
813
// Account for `type T = _;`.
788
814
let mut visitor = HirPlaceholderCollector :: default ( ) ;
789
815
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" ) ;
791
817
}
792
818
793
819
hir:: TraitItemKind :: Type ( _, None ) => {
@@ -798,7 +824,7 @@ fn lower_trait_item(tcx: TyCtxt<'_>, trait_item_id: hir::TraitItemId) {
798
824
let mut visitor = HirPlaceholderCollector :: default ( ) ;
799
825
visitor. visit_trait_item ( trait_item) ;
800
826
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" ) ;
802
828
}
803
829
} ;
804
830
@@ -811,6 +837,7 @@ fn lower_impl_item(tcx: TyCtxt<'_>, impl_item_id: hir::ImplItemId) {
811
837
tcx. ensure ( ) . type_of ( def_id) ;
812
838
tcx. ensure ( ) . predicates_of ( def_id) ;
813
839
let impl_item = tcx. hir ( ) . impl_item ( impl_item_id) ;
840
+ let icx = ItemCtxt :: new ( tcx, def_id. def_id ) ;
814
841
match impl_item. kind {
815
842
hir:: ImplItemKind :: Fn ( ..) => {
816
843
tcx. ensure ( ) . codegen_fn_attrs ( def_id) ;
@@ -821,14 +848,21 @@ fn lower_impl_item(tcx: TyCtxt<'_>, impl_item_id: hir::ImplItemId) {
821
848
let mut visitor = HirPlaceholderCollector :: default ( ) ;
822
849
visitor. visit_impl_item ( impl_item) ;
823
850
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" ) ;
825
852
}
826
853
hir:: ImplItemKind :: Const ( ty, _) => {
827
854
// Account for `const T: _ = ..;`
828
855
if !ty. is_suggestable_infer_ty ( ) {
829
856
let mut visitor = HirPlaceholderCollector :: default ( ) ;
830
857
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
+ ) ;
832
866
}
833
867
}
834
868
}
@@ -1385,7 +1419,7 @@ fn fn_sig(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::EarlyBinder<'_, ty::PolyFn
1385
1419
..
1386
1420
} )
1387
1421
| 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)
1389
1423
}
1390
1424
1391
1425
ImplItem ( hir:: ImplItem { kind : ImplItemKind :: Fn ( sig, _) , generics, .. } ) => {
@@ -1402,7 +1436,7 @@ fn fn_sig(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::EarlyBinder<'_, ty::PolyFn
1402
1436
None ,
1403
1437
)
1404
1438
} 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)
1406
1440
}
1407
1441
}
1408
1442
@@ -1455,12 +1489,12 @@ fn fn_sig(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::EarlyBinder<'_, ty::PolyFn
1455
1489
}
1456
1490
1457
1491
fn infer_return_ty_for_fn_sig < ' tcx > (
1458
- tcx : TyCtxt < ' tcx > ,
1459
1492
sig : & hir:: FnSig < ' tcx > ,
1460
1493
generics : & hir:: Generics < ' _ > ,
1461
1494
def_id : LocalDefId ,
1462
1495
icx : & ItemCtxt < ' tcx > ,
1463
1496
) -> ty:: PolyFnSig < ' tcx > {
1497
+ let tcx = icx. tcx ;
1464
1498
let hir_id = tcx. local_def_id_to_hir_id ( def_id) ;
1465
1499
1466
1500
match sig. decl . output . get_infer_ret_ty ( ) {
@@ -1492,7 +1526,7 @@ fn infer_return_ty_for_fn_sig<'tcx>(
1492
1526
let mut visitor = HirPlaceholderCollector :: default ( ) ;
1493
1527
visitor. visit_ty ( ty) ;
1494
1528
1495
- let mut diag = bad_placeholder ( tcx , visitor. 0 , "return type" ) ;
1529
+ let mut diag = bad_placeholder ( icx . lowerer ( ) , visitor. 0 , "return type" ) ;
1496
1530
let ret_ty = fn_sig. output ( ) ;
1497
1531
// Don't leak types into signatures unless they're nameable!
1498
1532
// For example, if a function returns itself, we don't want that
0 commit comments