@@ -91,10 +91,11 @@ use rustc_middle::ty::error::{ExpectedFound, TypeError};
9191use rustc_middle:: ty:: { self , Ty , TyCtxt } ;
9292use rustc_middle:: ty:: { GenericArgs , GenericArgsRef } ;
9393use rustc_session:: parse:: feature_err;
94- use rustc_span:: symbol:: { kw, Ident } ;
95- use rustc_span:: { self , def_id:: CRATE_DEF_ID , BytePos , Span , Symbol , DUMMY_SP } ;
94+ use rustc_span:: symbol:: { kw, sym , Ident } ;
95+ use rustc_span:: { def_id:: CRATE_DEF_ID , BytePos , Span , Symbol , DUMMY_SP } ;
9696use rustc_target:: abi:: VariantIdx ;
9797use rustc_target:: spec:: abi:: Abi ;
98+ use rustc_trait_selection:: infer:: InferCtxtExt ;
9899use rustc_trait_selection:: traits:: error_reporting:: suggestions:: ReturnsVisitor ;
99100use rustc_trait_selection:: traits:: error_reporting:: TypeErrCtxtExt as _;
100101use rustc_trait_selection:: traits:: ObligationCtxt ;
@@ -466,13 +467,24 @@ fn fn_sig_suggestion<'tcx>(
466467 )
467468}
468469
469- pub fn ty_kind_suggestion ( ty : Ty < ' _ > ) -> Option < & ' static str > {
470+ pub fn ty_kind_suggestion < ' tcx > ( ty : Ty < ' tcx > , tcx : TyCtxt < ' tcx > ) -> Option < & ' static str > {
471+ let implements_default = |ty| {
472+ let Some ( default_trait) = tcx. get_diagnostic_item ( sym:: Default ) else {
473+ return false ;
474+ } ;
475+ let infcx = tcx. infer_ctxt ( ) . build ( ) ;
476+ infcx
477+ . type_implements_trait ( default_trait, [ ty] , ty:: ParamEnv :: reveal_all ( ) )
478+ . must_apply_modulo_regions ( )
479+ } ;
470480 Some ( match ty. kind ( ) {
471481 ty:: Bool => "true" ,
472482 ty:: Char => "'a'" ,
473483 ty:: Int ( _) | ty:: Uint ( _) => "42" ,
474484 ty:: Float ( _) => "3.14159" ,
475485 ty:: Error ( _) | ty:: Never => return None ,
486+ ty:: Adt ( def, _) if Some ( def. did ( ) ) == tcx. get_diagnostic_item ( sym:: Vec ) => "vec![]" ,
487+ ty:: Adt ( _, _) if implements_default ( ty) => "Default::default()" ,
476488 _ => "value" ,
477489 } )
478490}
@@ -511,7 +523,7 @@ fn suggestion_signature<'tcx>(
511523 }
512524 ty:: AssocKind :: Const => {
513525 let ty = tcx. type_of ( assoc. def_id ) . instantiate_identity ( ) ;
514- let val = ty_kind_suggestion ( ty) . unwrap_or ( "todo!()" ) ;
526+ let val = ty_kind_suggestion ( ty, tcx ) . unwrap_or ( "todo!()" ) ;
515527 format ! ( "const {}: {} = {};" , assoc. name, ty, val)
516528 }
517529 }
0 commit comments