@@ -77,11 +77,9 @@ impl InferenceDiagnosticsData {
7777 !( self . name == "_" && matches ! ( self . kind, UnderspecifiedArgKind :: Type { .. } ) )
7878 }
7979
80- fn where_x_is_kind ( & self , in_type : Ty < ' _ > , is_collect : bool ) -> & ' static str {
81- if is_collect {
82- "empty"
83- } else if in_type. is_ty_infer ( ) {
84- "anon"
80+ fn where_x_is_kind ( & self , in_type : Ty < ' _ > ) -> & ' static str {
81+ if in_type. is_ty_infer ( ) {
82+ ""
8583 } else if self . name == "_" {
8684 // FIXME: Consider specializing this message if there is a single `_`
8785 // in the type.
@@ -185,14 +183,20 @@ fn fmt_printer<'a, 'tcx>(infcx: &'a InferCtxt<'tcx>, ns: Namespace) -> FmtPrinte
185183 printer
186184}
187185
188- fn ty_to_string < ' tcx > ( infcx : & InferCtxt < ' tcx > , ty : Ty < ' tcx > ) -> String {
186+ fn ty_to_string < ' tcx > ( infcx : & InferCtxt < ' tcx > , ty : Ty < ' tcx > , def_id : Option < DefId > ) -> String {
189187 let printer = fmt_printer ( infcx, Namespace :: TypeNS ) ;
190188 let ty = infcx. resolve_vars_if_possible ( ty) ;
191- match ty. kind ( ) {
189+ match ( ty. kind ( ) , def_id ) {
192190 // We don't want the regular output for `fn`s because it includes its path in
193191 // invalid pseudo-syntax, we want the `fn`-pointer output instead.
194- ty:: FnDef ( ..) => ty. fn_sig ( infcx. tcx ) . print ( printer) . unwrap ( ) . into_buffer ( ) ,
195- _ if ty. is_ty_infer ( ) => "Type" . to_string ( ) ,
192+ ( ty:: FnDef ( ..) , _) => ty. fn_sig ( infcx. tcx ) . print ( printer) . unwrap ( ) . into_buffer ( ) ,
193+ ( _, Some ( def_id) )
194+ if ty. is_ty_infer ( )
195+ && infcx. tcx . get_diagnostic_item ( sym:: iterator_collect_fn) == Some ( def_id) =>
196+ {
197+ "Vec<_>" . to_string ( )
198+ }
199+ _ if ty. is_ty_infer ( ) => "/* Type */" . to_string ( ) ,
196200 // FIXME: The same thing for closures, but this only works when the closure
197201 // does not capture anything.
198202 //
@@ -216,15 +220,15 @@ fn closure_as_fn_str<'tcx>(infcx: &InferCtxt<'tcx>, ty: Ty<'tcx>) -> String {
216220 . map ( |args| {
217221 args. tuple_fields ( )
218222 . iter ( )
219- . map ( |arg| ty_to_string ( infcx, arg) )
223+ . map ( |arg| ty_to_string ( infcx, arg, None ) )
220224 . collect :: < Vec < _ > > ( )
221225 . join ( ", " )
222226 } )
223227 . unwrap_or_default ( ) ;
224228 let ret = if fn_sig. output ( ) . skip_binder ( ) . is_unit ( ) {
225229 String :: new ( )
226230 } else {
227- format ! ( " -> {}" , ty_to_string( infcx, fn_sig. output( ) . skip_binder( ) ) )
231+ format ! ( " -> {}" , ty_to_string( infcx, fn_sig. output( ) . skip_binder( ) , None ) )
228232 } ;
229233 format ! ( "fn({}){}" , args, ret)
230234}
@@ -410,32 +414,28 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
410414 let mut infer_subdiags = Vec :: new ( ) ;
411415 let mut multi_suggestions = Vec :: new ( ) ;
412416 match kind {
413- InferSourceKind :: LetBinding { insert_span, pattern_name, ty, is_collect } => {
417+ InferSourceKind :: LetBinding { insert_span, pattern_name, ty, def_id } => {
414418 infer_subdiags. push ( SourceKindSubdiag :: LetLike {
415419 span : insert_span,
416420 name : pattern_name. map ( |name| name. to_string ( ) ) . unwrap_or_else ( String :: new) ,
417- x_kind : arg_data. where_x_is_kind ( ty, is_collect ) ,
421+ x_kind : arg_data. where_x_is_kind ( ty) ,
418422 prefix_kind : arg_data. kind . clone ( ) ,
419423 prefix : arg_data. kind . try_get_prefix ( ) . unwrap_or_default ( ) ,
420424 arg_name : arg_data. name ,
421425 kind : if pattern_name. is_some ( ) { "with_pattern" } else { "other" } ,
422- type_name : if is_collect {
423- "Vec<_>" . to_string ( )
424- } else {
425- ty_to_string ( self , ty)
426- } ,
426+ type_name : ty_to_string ( self , ty, def_id) ,
427427 } ) ;
428428 }
429429 InferSourceKind :: ClosureArg { insert_span, ty } => {
430430 infer_subdiags. push ( SourceKindSubdiag :: LetLike {
431431 span : insert_span,
432432 name : String :: new ( ) ,
433- x_kind : arg_data. where_x_is_kind ( ty, false ) ,
433+ x_kind : arg_data. where_x_is_kind ( ty) ,
434434 prefix_kind : arg_data. kind . clone ( ) ,
435435 prefix : arg_data. kind . try_get_prefix ( ) . unwrap_or_default ( ) ,
436436 arg_name : arg_data. name ,
437437 kind : "closure" ,
438- type_name : ty_to_string ( self , ty) ,
438+ type_name : ty_to_string ( self , ty, None ) ,
439439 } ) ;
440440 }
441441 InferSourceKind :: GenericArg {
@@ -534,7 +534,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
534534 ) ) ;
535535 }
536536 InferSourceKind :: ClosureReturn { ty, data, should_wrap_expr } => {
537- let ty_info = ty_to_string ( self , ty) ;
537+ let ty_info = ty_to_string ( self , ty, None ) ;
538538 multi_suggestions. push ( SourceKindMultiSuggestion :: new_closure_return (
539539 ty_info,
540540 data,
@@ -622,7 +622,7 @@ enum InferSourceKind<'tcx> {
622622 insert_span : Span ,
623623 pattern_name : Option < Ident > ,
624624 ty : Ty < ' tcx > ,
625- is_collect : bool ,
625+ def_id : Option < DefId > ,
626626 } ,
627627 ClosureArg {
628628 insert_span : Span ,
@@ -677,7 +677,7 @@ impl<'tcx> InferSourceKind<'tcx> {
677677 if ty. is_closure ( ) {
678678 ( "closure" , closure_as_fn_str ( infcx, ty) )
679679 } else if !ty. is_ty_infer ( ) {
680- ( "normal" , ty_to_string ( infcx, ty) )
680+ ( "normal" , ty_to_string ( infcx, ty, None ) )
681681 } else {
682682 ( "other" , String :: new ( ) )
683683 }
@@ -807,14 +807,13 @@ impl<'a, 'tcx> FindInferSourceVisitor<'a, 'tcx> {
807807 let cost = self . source_cost ( & new_source) + self . attempt ;
808808 debug ! ( ?cost) ;
809809 self . attempt += 1 ;
810- if let Some ( InferSource { kind : InferSourceKind :: GenericArg { def_id, ..} , .. } ) = self . infer_source
811- && self . infcx . tcx . get_diagnostic_item ( sym:: iterator_collect_fn) == Some ( def_id)
812- && let InferSourceKind :: LetBinding { ref ty, ref mut is_collect, ..} = new_source. kind
810+ if let Some ( InferSource { kind : InferSourceKind :: GenericArg { def_id : did, ..} , .. } ) = self . infer_source
811+ && let InferSourceKind :: LetBinding { ref ty, ref mut def_id, ..} = new_source. kind
813812 && ty. is_ty_infer ( )
814813 {
815814 // Customize the output so we talk about `let x: Vec<_> = iter.collect();` instead of
816815 // `let x: _ = iter.collect();`, as this is a very common case.
817- * is_collect = true ;
816+ * def_id = Some ( did ) ;
818817 }
819818 if cost < self . infer_source_cost {
820819 self . infer_source_cost = cost;
@@ -1113,7 +1112,7 @@ impl<'a, 'tcx> Visitor<'tcx> for FindInferSourceVisitor<'a, 'tcx> {
11131112 insert_span : local. pat . span . shrink_to_hi ( ) ,
11141113 pattern_name : local. pat . simple_ident ( ) ,
11151114 ty,
1116- is_collect : false ,
1115+ def_id : None ,
11171116 } ,
11181117 } )
11191118 }
0 commit comments