@@ -131,19 +131,25 @@ pub struct ItemCtxt<'tcx> {
131
131
///////////////////////////////////////////////////////////////////////////
132
132
133
133
#[ derive( Default ) ]
134
- pub ( crate ) struct HirPlaceholderCollector ( pub ( crate ) Vec < Span > ) ;
134
+ pub ( crate ) struct HirPlaceholderCollector {
135
+ pub spans : Vec < Span > ,
136
+ // If any of the spans points to a const infer var, then suppress any messages
137
+ // that may try to turn that const infer into a type parameter.
138
+ pub may_contain_const_infer : bool ,
139
+ }
135
140
136
141
impl < ' v > Visitor < ' v > for HirPlaceholderCollector {
137
142
fn visit_ty ( & mut self , t : & ' v hir:: Ty < ' v > ) {
138
143
if let hir:: TyKind :: Infer = t. kind {
139
- self . 0 . push ( t. span ) ;
144
+ self . spans . push ( t. span ) ;
140
145
}
141
146
intravisit:: walk_ty ( self , t)
142
147
}
143
148
fn visit_generic_arg ( & mut self , generic_arg : & ' v hir:: GenericArg < ' v > ) {
144
149
match generic_arg {
145
150
hir:: GenericArg :: Infer ( inf) => {
146
- self . 0 . push ( inf. span ) ;
151
+ self . spans . push ( inf. span ) ;
152
+ self . may_contain_const_infer = true ;
147
153
intravisit:: walk_inf ( self , inf) ;
148
154
}
149
155
hir:: GenericArg :: Type ( t) => self . visit_ty ( t) ,
@@ -152,7 +158,8 @@ impl<'v> Visitor<'v> for HirPlaceholderCollector {
152
158
}
153
159
fn visit_const_arg ( & mut self , const_arg : & ' v hir:: ConstArg < ' v > ) {
154
160
if let hir:: ConstArgKind :: Infer ( span) = const_arg. kind {
155
- self . 0 . push ( span) ;
161
+ self . may_contain_const_infer = true ;
162
+ self . spans . push ( span) ;
156
163
}
157
164
intravisit:: walk_const_arg ( self , const_arg)
158
165
}
@@ -277,8 +284,8 @@ fn reject_placeholder_type_signatures_in_item<'tcx>(
277
284
placeholder_type_error (
278
285
icx. lowerer ( ) ,
279
286
Some ( generics) ,
280
- visitor. 0 ,
281
- suggest,
287
+ visitor. spans ,
288
+ suggest && !visitor . may_contain_const_infer ,
282
289
None ,
283
290
item. kind . descr ( ) ,
284
291
) ;
@@ -607,16 +614,16 @@ impl<'tcx> HirTyLowerer<'tcx> for ItemCtxt<'tcx> {
607
614
hir:: FnRetTy :: DefaultReturn ( ..) => tcx. types . unit ,
608
615
} ;
609
616
610
- if !( visitor. 0 . is_empty ( ) && infer_replacements. is_empty ( ) ) {
617
+ if !( visitor. spans . is_empty ( ) && infer_replacements. is_empty ( ) ) {
611
618
// We check for the presence of
612
619
// `ident_span` to not emit an error twice when we have `fn foo(_: fn() -> _)`.
613
620
614
621
let mut diag = crate :: collect:: placeholder_type_error_diag (
615
622
self ,
616
623
generics,
617
- visitor. 0 ,
624
+ visitor. spans ,
618
625
infer_replacements. iter ( ) . map ( |( s, _) | * s) . collect ( ) ,
619
- true ,
626
+ !visitor . may_contain_const_infer ,
620
627
hir_ty,
621
628
"function" ,
622
629
) ;
@@ -712,7 +719,7 @@ fn lower_item(tcx: TyCtxt<'_>, item_id: hir::ItemId) {
712
719
placeholder_type_error (
713
720
icx. lowerer ( ) ,
714
721
None ,
715
- visitor. 0 ,
722
+ visitor. spans ,
716
723
false ,
717
724
None ,
718
725
"static variable" ,
@@ -780,7 +787,7 @@ fn lower_item(tcx: TyCtxt<'_>, item_id: hir::ItemId) {
780
787
placeholder_type_error (
781
788
icx. lowerer ( ) ,
782
789
None ,
783
- visitor. 0 ,
790
+ visitor. spans ,
784
791
false ,
785
792
None ,
786
793
it. kind . descr ( ) ,
@@ -822,7 +829,7 @@ fn lower_trait_item(tcx: TyCtxt<'_>, trait_item_id: hir::TraitItemId) {
822
829
placeholder_type_error (
823
830
icx. lowerer ( ) ,
824
831
None ,
825
- visitor. 0 ,
832
+ visitor. spans ,
826
833
false ,
827
834
None ,
828
835
"associated constant" ,
@@ -837,7 +844,14 @@ fn lower_trait_item(tcx: TyCtxt<'_>, trait_item_id: hir::TraitItemId) {
837
844
// Account for `type T = _;`.
838
845
let mut visitor = HirPlaceholderCollector :: default ( ) ;
839
846
visitor. visit_trait_item ( trait_item) ;
840
- placeholder_type_error ( icx. lowerer ( ) , None , visitor. 0 , false , None , "associated type" ) ;
847
+ placeholder_type_error (
848
+ icx. lowerer ( ) ,
849
+ None ,
850
+ visitor. spans ,
851
+ false ,
852
+ None ,
853
+ "associated type" ,
854
+ ) ;
841
855
}
842
856
843
857
hir:: TraitItemKind :: Type ( _, None ) => {
@@ -848,7 +862,14 @@ fn lower_trait_item(tcx: TyCtxt<'_>, trait_item_id: hir::TraitItemId) {
848
862
let mut visitor = HirPlaceholderCollector :: default ( ) ;
849
863
visitor. visit_trait_item ( trait_item) ;
850
864
851
- placeholder_type_error ( icx. lowerer ( ) , None , visitor. 0 , false , None , "associated type" ) ;
865
+ placeholder_type_error (
866
+ icx. lowerer ( ) ,
867
+ None ,
868
+ visitor. spans ,
869
+ false ,
870
+ None ,
871
+ "associated type" ,
872
+ ) ;
852
873
}
853
874
} ;
854
875
@@ -872,7 +893,14 @@ fn lower_impl_item(tcx: TyCtxt<'_>, impl_item_id: hir::ImplItemId) {
872
893
let mut visitor = HirPlaceholderCollector :: default ( ) ;
873
894
visitor. visit_impl_item ( impl_item) ;
874
895
875
- placeholder_type_error ( icx. lowerer ( ) , None , visitor. 0 , false , None , "associated type" ) ;
896
+ placeholder_type_error (
897
+ icx. lowerer ( ) ,
898
+ None ,
899
+ visitor. spans ,
900
+ false ,
901
+ None ,
902
+ "associated type" ,
903
+ ) ;
876
904
}
877
905
hir:: ImplItemKind :: Const ( ty, _) => {
878
906
// Account for `const T: _ = ..;`
@@ -882,7 +910,7 @@ fn lower_impl_item(tcx: TyCtxt<'_>, impl_item_id: hir::ImplItemId) {
882
910
placeholder_type_error (
883
911
icx. lowerer ( ) ,
884
912
None ,
885
- visitor. 0 ,
913
+ visitor. spans ,
886
914
false ,
887
915
None ,
888
916
"associated constant" ,
@@ -1371,7 +1399,7 @@ fn lower_fn_sig_recovering_infer_ret_ty<'tcx>(
1371
1399
generics : & ' tcx hir:: Generics < ' tcx > ,
1372
1400
def_id : LocalDefId ,
1373
1401
) -> ty:: PolyFnSig < ' tcx > {
1374
- if let Some ( infer_ret_ty) = sig. decl . output . get_infer_ret_ty ( ) {
1402
+ if let Some ( infer_ret_ty) = sig. decl . output . is_suggestable_infer_ty ( ) {
1375
1403
return recover_infer_ret_ty ( icx, infer_ret_ty, generics, def_id) ;
1376
1404
}
1377
1405
@@ -1422,7 +1450,7 @@ fn recover_infer_ret_ty<'tcx>(
1422
1450
let mut visitor = HirPlaceholderCollector :: default ( ) ;
1423
1451
visitor. visit_ty ( infer_ret_ty) ;
1424
1452
1425
- let mut diag = bad_placeholder ( icx. lowerer ( ) , visitor. 0 , "return type" ) ;
1453
+ let mut diag = bad_placeholder ( icx. lowerer ( ) , visitor. spans , "return type" ) ;
1426
1454
let ret_ty = fn_sig. output ( ) ;
1427
1455
1428
1456
// Don't leak types into signatures unless they're nameable!
0 commit comments