-
Notifications
You must be signed in to change notification settings - Fork 14.1k
Provide structured suggestion for binding needing type on E0594 #107646
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -606,12 +606,60 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> { | |
| } | ||
| } | ||
| Some((false, err_label_span, message)) => { | ||
| err.span_label( | ||
| err_label_span, | ||
| &format!( | ||
| "consider changing this binding's type to be: `{message}`" | ||
| ), | ||
| ); | ||
| struct V { | ||
| span: Span, | ||
| hir_id: Option<hir::HirId>, | ||
| } | ||
|
|
||
| impl<'tcx> Visitor<'tcx> for V { | ||
| fn visit_stmt(&mut self, s: &'tcx hir::Stmt<'tcx>) { | ||
| if let hir::StmtKind::Local(local) = s.kind { | ||
| if local.pat.span == self.span { | ||
| self.hir_id = Some(local.hir_id); | ||
| } | ||
| } | ||
| hir::intravisit::walk_stmt(self, s); | ||
| } | ||
| } | ||
| let hir_map = self.infcx.tcx.hir(); | ||
| let pat = loop { | ||
| // Poor man's try block | ||
|
||
| let def_id = self.body.source.def_id(); | ||
| let hir_id = | ||
| hir_map.local_def_id_to_hir_id(def_id.as_local().unwrap()); | ||
estebank marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| let node = hir_map.find(hir_id); | ||
| let Some(hir::Node::Item(item)) = node else { break None; }; | ||
| let hir::ItemKind::Fn(.., body_id) = item.kind else { break None; }; | ||
| let body = self.infcx.tcx.hir().body(body_id); | ||
estebank marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| let mut v = V { span: err_label_span, hir_id: None }; | ||
| v.visit_body(body); | ||
| break v.hir_id; | ||
| }; | ||
| if let Some(hir_id) = pat | ||
| && let Some(hir::Node::Local(local)) = hir_map.find(hir_id) | ||
| { | ||
| let (changing, span, sugg) = match local.ty { | ||
| Some(ty) => ("changing", ty.span, message), | ||
| None => ( | ||
| "specifying", | ||
| local.pat.span.shrink_to_hi(), | ||
| format!(": {message}"), | ||
| ), | ||
| }; | ||
| err.span_suggestion_verbose( | ||
| span, | ||
| &format!("consider {changing} this binding's type"), | ||
| sugg, | ||
| Applicability::HasPlaceholders, | ||
| ); | ||
| } else { | ||
| err.span_label( | ||
| err_label_span, | ||
| &format!( | ||
| "consider changing this binding's type to be: `{message}`" | ||
| ), | ||
| ); | ||
| } | ||
| } | ||
| None => {} | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.