@@ -606,12 +606,60 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
606606 }
607607 }
608608 Some ( ( false , err_label_span, message) ) => {
609- err. span_label (
610- err_label_span,
611- & format ! (
612- "consider changing this binding's type to be: `{message}`"
613- ) ,
614- ) ;
609+ struct V {
610+ span : Span ,
611+ hir_id : Option < hir:: HirId > ,
612+ }
613+
614+ impl < ' tcx > Visitor < ' tcx > for V {
615+ fn visit_stmt ( & mut self , s : & ' tcx hir:: Stmt < ' tcx > ) {
616+ if let hir:: StmtKind :: Local ( local) = s. kind {
617+ if local. pat . span == self . span {
618+ self . hir_id = Some ( local. hir_id ) ;
619+ }
620+ }
621+ hir:: intravisit:: walk_stmt ( self , s) ;
622+ }
623+ }
624+ let hir_map = self . infcx . tcx . hir ( ) ;
625+ let pat = loop {
626+ // Poor man's try block
627+ let def_id = self . body . source . def_id ( ) ;
628+ let hir_id =
629+ hir_map. local_def_id_to_hir_id ( def_id. as_local ( ) . unwrap ( ) ) ;
630+ let node = hir_map. find ( hir_id) ;
631+ let Some ( hir:: Node :: Item ( item) ) = node else { break None ; } ;
632+ let hir:: ItemKind :: Fn ( .., body_id) = item. kind else { break None ; } ;
633+ let body = self . infcx . tcx . hir ( ) . body ( body_id) ;
634+ let mut v = V { span : err_label_span, hir_id : None } ;
635+ v. visit_body ( body) ;
636+ break v. hir_id ;
637+ } ;
638+ if let Some ( hir_id) = pat
639+ && let Some ( hir:: Node :: Local ( local) ) = hir_map. find ( hir_id)
640+ {
641+ let ( changing, span, sugg) = match local. ty {
642+ Some ( ty) => ( "changing" , ty. span , message) ,
643+ None => (
644+ "specifying" ,
645+ local. pat . span . shrink_to_hi ( ) ,
646+ format ! ( ": {message}" ) ,
647+ ) ,
648+ } ;
649+ err. span_suggestion_verbose (
650+ span,
651+ & format ! ( "consider {changing} this binding's type" ) ,
652+ sugg,
653+ Applicability :: HasPlaceholders ,
654+ ) ;
655+ } else {
656+ err. span_label (
657+ err_label_span,
658+ & format ! (
659+ "consider changing this binding's type to be: `{message}`"
660+ ) ,
661+ ) ;
662+ }
615663 }
616664 None => { }
617665 }
0 commit comments