@@ -606,12 +606,63 @@ 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 BindingFinder {
610+ span : Span ,
611+ hir_id : Option < hir:: HirId > ,
612+ }
613+
614+ impl < ' tcx > Visitor < ' tcx > for BindingFinder {
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 def_id = self . body . source . def_id ( ) ;
626+ let hir_id = hir_map. local_def_id_to_hir_id ( def_id. expect_local ( ) ) ;
627+ let node = hir_map. find ( hir_id) ;
628+ let hir_id = if let Some ( hir:: Node :: Item ( item) ) = node
629+ && let hir:: ItemKind :: Fn ( .., body_id) = item. kind
630+ {
631+ let body = hir_map. body ( body_id) ;
632+ let mut v = BindingFinder {
633+ span : err_label_span,
634+ hir_id : None ,
635+ } ;
636+ v. visit_body ( body) ;
637+ v. hir_id
638+ } else {
639+ None
640+ } ;
641+ if let Some ( hir_id) = hir_id
642+ && let Some ( hir:: Node :: Local ( local) ) = hir_map. find ( hir_id)
643+ {
644+ let ( changing, span, sugg) = match local. ty {
645+ Some ( ty) => ( "changing" , ty. span , message) ,
646+ None => (
647+ "specifying" ,
648+ local. pat . span . shrink_to_hi ( ) ,
649+ format ! ( ": {message}" ) ,
650+ ) ,
651+ } ;
652+ err. span_suggestion_verbose (
653+ span,
654+ & format ! ( "consider {changing} this binding's type" ) ,
655+ sugg,
656+ Applicability :: HasPlaceholders ,
657+ ) ;
658+ } else {
659+ err. span_label (
660+ err_label_span,
661+ & format ! (
662+ "consider changing this binding's type to be: `{message}`"
663+ ) ,
664+ ) ;
665+ }
615666 }
616667 None => { }
617668 }
0 commit comments