Skip to content

Commit

Permalink
use var_debug_info for arg_name
Browse files Browse the repository at this point in the history
  • Loading branch information
chenyukang committed Jul 4, 2024
1 parent ab7b29d commit 1767432
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use rustc_hir::{CoroutineKind, CoroutineSource, LangItem};
use rustc_middle::bug;
use rustc_middle::hir::nested_filter::OnlyBodies;
use rustc_middle::mir::tcx::PlaceTy;
use rustc_middle::mir::VarDebugInfoContents;
use rustc_middle::mir::{
self, AggregateKind, BindingForm, BorrowKind, CallSource, ClearCrossCrate, ConstraintCategory,
FakeBorrowKind, FakeReadCause, LocalDecl, LocalInfo, LocalKind, Location, MutBorrowKind,
Expand Down Expand Up @@ -495,7 +496,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, '_, 'infcx, 'tcx> {
}
}

self.suggest_ref_for_dbg_args(expr, span, move_span, err);
self.suggest_ref_for_dbg_args(expr, place, move_span, err);

// it's useless to suggest inserting `ref` when the span don't comes from local code
if let Some(pat) = finder.pat
Expand All @@ -522,22 +523,23 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, '_, 'infcx, 'tcx> {
fn suggest_ref_for_dbg_args(
&self,
body: &hir::Expr<'_>,
span: Option<Span>,
place: &Place<'tcx>,
move_span: Span,
err: &mut Diag<'tcx>,
err: &mut Diag<'infcx>,
) {
let sm = self.infcx.tcx.sess.source_map();
let arg_code = if let Some(span) = span
&& let Ok(code) = sm.span_to_snippet(span)
{
code
let var_info = self.body.var_debug_info.iter().find(|info| match info.value {
VarDebugInfoContents::Place(ref p) => p == place,
_ => false,
});
let arg_name = if let Some(var_info) = var_info {
var_info.name.to_string()
} else {
return;
};
struct MatchArgFinder {
expr_span: Span,
match_arg_span: Option<Span>,
arg_code: String,
arg_name: String,
}
impl Visitor<'_> for MatchArgFinder {
fn visit_expr(&mut self, e: &hir::Expr<'_>) {
Expand All @@ -547,7 +549,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, '_, 'infcx, 'tcx> {
_,
path @ Path { segments: [seg], .. },
)) = &expr.kind
&& seg.ident.name.as_str() == &self.arg_code
&& seg.ident.name.as_str() == &self.arg_name
&& self.expr_span.source_callsite().contains(expr.span)
{
self.match_arg_span = Some(path.span);
Expand All @@ -556,7 +558,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, '_, 'infcx, 'tcx> {
}
}

let mut finder = MatchArgFinder { expr_span: move_span, match_arg_span: None, arg_code };
let mut finder = MatchArgFinder { expr_span: move_span, match_arg_span: None, arg_name };
finder.visit_expr(body);
if let Some(macro_arg_span) = finder.match_arg_span {
err.span_suggestion_verbose(
Expand Down

0 comments on commit 1767432

Please sign in to comment.