@@ -81,7 +81,6 @@ use rustc_errors::ErrorGuaranteed;
8181use rustc_errors:: { pluralize, struct_span_code_err, Diag } ;
8282use rustc_hir:: def_id:: { DefId , LocalDefId } ;
8383use rustc_hir:: intravisit:: Visitor ;
84- use rustc_hir:: Mutability ;
8584use rustc_index:: bit_set:: BitSet ;
8685use rustc_infer:: infer:: error_reporting:: ObligationCauseExt as _;
8786use rustc_infer:: infer:: outlives:: env:: OutlivesEnvironment ;
@@ -96,8 +95,9 @@ use rustc_span::symbol::{kw, sym, Ident};
9695use rustc_span:: { def_id:: CRATE_DEF_ID , BytePos , Span , Symbol , DUMMY_SP } ;
9796use rustc_target:: abi:: VariantIdx ;
9897use rustc_target:: spec:: abi:: Abi ;
99- use rustc_trait_selection:: infer:: InferCtxtExt ;
100- use rustc_trait_selection:: traits:: error_reporting:: suggestions:: ReturnsVisitor ;
98+ use rustc_trait_selection:: traits:: error_reporting:: suggestions:: {
99+ ReturnsVisitor , TypeErrCtxtExt as _,
100+ } ;
101101use rustc_trait_selection:: traits:: error_reporting:: TypeErrCtxtExt as _;
102102use rustc_trait_selection:: traits:: ObligationCtxt ;
103103
@@ -467,67 +467,6 @@ fn fn_sig_suggestion<'tcx>(
467467 )
468468}
469469
470- pub fn ty_kind_suggestion < ' tcx > ( ty : Ty < ' tcx > , tcx : TyCtxt < ' tcx > ) -> Option < String > {
471- // Keep in sync with `rustc_borrowck/src/diagnostics/conflict_errors.rs:ty_kind_suggestion`.
472- // FIXME: deduplicate the above.
473- let implements_default = |ty| {
474- let Some ( default_trait) = tcx. get_diagnostic_item ( sym:: Default ) else {
475- return false ;
476- } ;
477- let infcx = tcx. infer_ctxt ( ) . build ( ) ;
478- infcx
479- . type_implements_trait ( default_trait, [ ty] , ty:: ParamEnv :: reveal_all ( ) )
480- . must_apply_modulo_regions ( )
481- } ;
482- Some ( match ty. kind ( ) {
483- ty:: Never | ty:: Error ( _) => return None ,
484- ty:: Bool => "false" . to_string ( ) ,
485- ty:: Char => "\' x\' " . to_string ( ) ,
486- ty:: Int ( _) | ty:: Uint ( _) => "42" . into ( ) ,
487- ty:: Float ( _) => "3.14159" . into ( ) ,
488- ty:: Slice ( _) => "[]" . to_string ( ) ,
489- ty:: Adt ( def, _) if Some ( def. did ( ) ) == tcx. get_diagnostic_item ( sym:: Vec ) => {
490- "vec![]" . to_string ( )
491- }
492- ty:: Adt ( def, _) if Some ( def. did ( ) ) == tcx. get_diagnostic_item ( sym:: String ) => {
493- "String::new()" . to_string ( )
494- }
495- ty:: Adt ( def, args) if def. is_box ( ) => {
496- format ! ( "Box::new({})" , ty_kind_suggestion( args[ 0 ] . expect_ty( ) , tcx) ?)
497- }
498- ty:: Adt ( def, _) if Some ( def. did ( ) ) == tcx. get_diagnostic_item ( sym:: Option ) => {
499- "None" . to_string ( )
500- }
501- ty:: Adt ( def, args) if Some ( def. did ( ) ) == tcx. get_diagnostic_item ( sym:: Result ) => {
502- format ! ( "Ok({})" , ty_kind_suggestion( args[ 0 ] . expect_ty( ) , tcx) ?)
503- }
504- ty:: Adt ( _, _) if implements_default ( ty) => "Default::default()" . to_string ( ) ,
505- ty:: Ref ( _, ty, mutability) => {
506- if let ( ty:: Str , Mutability :: Not ) = ( ty. kind ( ) , mutability) {
507- "\" \" " . to_string ( )
508- } else {
509- let Some ( ty) = ty_kind_suggestion ( * ty, tcx) else {
510- return None ;
511- } ;
512- format ! ( "&{}{ty}" , mutability. prefix_str( ) )
513- }
514- }
515- ty:: Array ( ty, len) => format ! (
516- "[{}; {}]" ,
517- ty_kind_suggestion( * ty, tcx) ?,
518- len. eval_target_usize( tcx, ty:: ParamEnv :: reveal_all( ) ) ,
519- ) ,
520- ty:: Tuple ( tys) => format ! (
521- "({})" ,
522- tys. iter( )
523- . map( |ty| ty_kind_suggestion( ty, tcx) )
524- . collect:: <Option <Vec <String >>>( ) ?
525- . join( ", " )
526- ) ,
527- _ => "value" . to_string ( ) ,
528- } )
529- }
530-
531470/// Return placeholder code for the given associated item.
532471/// Similar to `ty::AssocItem::suggestion`, but appropriate for use as the code snippet of a
533472/// structured suggestion.
@@ -562,7 +501,12 @@ fn suggestion_signature<'tcx>(
562501 }
563502 ty:: AssocKind :: Const => {
564503 let ty = tcx. type_of ( assoc. def_id ) . instantiate_identity ( ) ;
565- let val = ty_kind_suggestion ( ty, tcx) . unwrap_or_else ( || "value" . to_string ( ) ) ;
504+ let val = tcx
505+ . infer_ctxt ( )
506+ . build ( )
507+ . err_ctxt ( )
508+ . ty_kind_suggestion ( tcx. param_env ( assoc. def_id ) , ty)
509+ . unwrap_or_else ( || "value" . to_string ( ) ) ;
566510 format ! ( "const {}: {} = {};" , assoc. name, ty, val)
567511 }
568512 }
0 commit comments