1
Fork 0

Rollup merge of #101364 - compiler-errors:arg-suggestion-spans, r=wesleywiser

Shrink suggestion span of argument mismatch error

This doesn't really help with #101242, but I wanted to put this up while I work on other fixes.
This commit is contained in:
Matthias Krüger 2022-09-03 14:20:52 +02:00 committed by GitHub
commit 3c2780cf16
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
38 changed files with 125 additions and 114 deletions

View file

@ -1056,11 +1056,22 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
};
if let Some(suggestion_text) = suggestion_text {
let source_map = self.sess().source_map();
let mut suggestion = format!(
"{}(",
source_map.span_to_snippet(full_call_span).unwrap_or_else(|_| fn_def_id
.map_or("".to_string(), |fn_def_id| tcx.item_name(fn_def_id).to_string()))
);
let (mut suggestion, suggestion_span) =
if let Some(call_span) = full_call_span.find_ancestor_inside(error_span) {
("(".to_string(), call_span.shrink_to_hi().to(error_span.shrink_to_hi()))
} else {
(
format!(
"{}(",
source_map.span_to_snippet(full_call_span).unwrap_or_else(|_| {
fn_def_id.map_or("".to_string(), |fn_def_id| {
tcx.item_name(fn_def_id).to_string()
})
})
),
error_span,
)
};
let mut needs_comma = false;
for (expected_idx, provided_idx) in matched_inputs.iter_enumerated() {
if needs_comma {
@ -1088,7 +1099,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
}
suggestion += ")";
err.span_suggestion_verbose(
error_span,
suggestion_span,
&suggestion_text,
suggestion,
Applicability::HasPlaceholders,