@@ -91,10 +91,11 @@ use rustc_middle::ty::error::{ExpectedFound, TypeError};
91
91
use rustc_middle:: ty:: { self , Ty , TyCtxt } ;
92
92
use rustc_middle:: ty:: { GenericArgs , GenericArgsRef } ;
93
93
use 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 } ;
96
96
use rustc_target:: abi:: VariantIdx ;
97
97
use rustc_target:: spec:: abi:: Abi ;
98
+ use rustc_trait_selection:: infer:: InferCtxtExt ;
98
99
use rustc_trait_selection:: traits:: error_reporting:: suggestions:: ReturnsVisitor ;
99
100
use rustc_trait_selection:: traits:: error_reporting:: TypeErrCtxtExt as _;
100
101
use rustc_trait_selection:: traits:: ObligationCtxt ;
@@ -466,13 +467,24 @@ fn fn_sig_suggestion<'tcx>(
466
467
)
467
468
}
468
469
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
+ } ;
470
480
Some ( match ty. kind ( ) {
471
481
ty:: Bool => "true" ,
472
482
ty:: Char => "'a'" ,
473
483
ty:: Int ( _) | ty:: Uint ( _) => "42" ,
474
484
ty:: Float ( _) => "3.14159" ,
475
485
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()" ,
476
488
_ => "value" ,
477
489
} )
478
490
}
@@ -511,7 +523,7 @@ fn suggestion_signature<'tcx>(
511
523
}
512
524
ty:: AssocKind :: Const => {
513
525
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!()" ) ;
515
527
format ! ( "const {}: {} = {};" , assoc. name, ty, val)
516
528
}
517
529
}
0 commit comments