More accurate spans for arg removal suggestion

This commit is contained in:
Esteban Küber 2022-12-31 23:13:36 -08:00
parent 9bb6e60d1f
commit f02d8ec15e
23 changed files with 202 additions and 108 deletions

View File

@ -612,7 +612,7 @@ impl Diagnostic {
pub fn multipart_suggestion_with_style(
&mut self,
msg: impl Into<SubdiagnosticMessage>,
suggestion: Vec<(Span, String)>,
mut suggestion: Vec<(Span, String)>,
applicability: Applicability,
style: SuggestionStyle,
) -> &mut Self {
@ -634,6 +634,7 @@ impl Diagnostic {
None,
"suggestion must not have overlapping parts",
);
suggestion.sort_by_key(|(span, _)| (span.lo(), span.hi()));
self.push_suggestion(CodeSuggestion {
substitutions: vec![Substitution { parts }],

View File

@ -1768,7 +1768,7 @@ impl EmitterWriter {
// Render the replacements for each suggestion
let suggestions = suggestion.splice_lines(sm);
debug!("emit_suggestion_default: suggestions={:?}", suggestions);
debug!(?suggestions);
if suggestions.is_empty() {
// Suggestions coming from macros can have malformed spans. This is a heavy handed
@ -1797,6 +1797,7 @@ impl EmitterWriter {
for (complete, parts, highlights, only_capitalization) in
suggestions.iter().take(MAX_SUGGESTIONS)
{
debug!(?complete, ?parts, ?highlights);
notice_capitalization |= only_capitalization;
let has_deletion = parts.iter().any(|p| p.is_deletion(sm));

View File

@ -755,15 +755,23 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
}
errors.drain_filter(|error| {
let Error::Invalid(provided_idx, expected_idx, Compatibility::Incompatible(Some(e))) = error else { return false };
let (provided_ty, provided_span) = provided_arg_tys[*provided_idx];
let trace = mk_trace(provided_span, formal_and_expected_inputs[*expected_idx], provided_ty);
if !matches!(trace.cause.as_failure_code(*e), FailureCode::Error0308(_)) {
self.err_ctxt().report_and_explain_type_error(trace, *e).emit();
return true;
}
false
});
let Error::Invalid(
provided_idx,
expected_idx,
Compatibility::Incompatible(Some(e)),
) = error else { return false };
let (provided_ty, provided_span) = provided_arg_tys[*provided_idx];
let trace = mk_trace(
provided_span,
formal_and_expected_inputs[*expected_idx],
provided_ty,
);
if !matches!(trace.cause.as_failure_code(*e), FailureCode::Error0308(_)) {
self.err_ctxt().report_and_explain_type_error(trace, *e).emit();
return true;
}
false
});
// We're done if we found errors, but we already emitted them.
if errors.is_empty() {
@ -864,7 +872,27 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
}
let mut suggestion_text = SuggestionText::None;
let ty_to_snippet = |ty: Ty<'tcx>, expected_idx: ExpectedIdx| {
if ty.is_unit() {
"()".to_string()
} else if ty.is_suggestable(tcx, false) {
format!("/* {} */", ty)
} else if let Some(fn_def_id) = fn_def_id
&& self.tcx.def_kind(fn_def_id).is_fn_like()
&& let self_implicit =
matches!(call_expr.kind, hir::ExprKind::MethodCall(..)) as usize
&& let Some(arg) = self.tcx.fn_arg_names(fn_def_id)
.get(expected_idx.as_usize() + self_implicit)
&& arg.name != kw::SelfLower
{
format!("/* {} */", arg.name)
} else {
"/* value */".to_string()
}
};
let mut errors = errors.into_iter().peekable();
let mut suggestions = vec![];
while let Some(error) = errors.next() {
match error {
Error::Invalid(provided_idx, expected_idx, compatibility) => {
@ -906,6 +934,21 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
};
labels
.push((provided_span, format!("argument{} unexpected", provided_ty_name)));
let mut span = provided_span;
if let Some((_, next)) = provided_arg_tys.get(
ProvidedIdx::from_usize(arg_idx.index() + 1),
) {
// Include next comma
span = span.until(*next);
} else if arg_idx.index() > 0
&& let Some((_, prev)) = provided_arg_tys
.get(ProvidedIdx::from_usize(arg_idx.index() - 1)
) {
// Last argument, include previous comma
span = span.with_lo(prev.hi());
}
suggestions.push((span, String::new()));
suggestion_text = match suggestion_text {
SuggestionText::None => SuggestionText::Remove(false),
SuggestionText::Remove(_) => SuggestionText::Remove(true),
@ -1095,6 +1138,21 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
}
}
// Incorporate the argument changes in the removal suggestion.
let mut prev = -1;
for (expected_idx, provided_idx) in matched_inputs.iter_enumerated() {
if let Some(provided_idx) = provided_idx {
prev = provided_idx.index() as i64;
}
let idx = ProvidedIdx::from_usize((prev + 1) as usize);
if let None = provided_idx
&& let Some((_, arg_span)) = provided_arg_tys.get(idx)
{
let (_, expected_ty) = formal_and_expected_inputs[expected_idx];
suggestions.push((*arg_span, ty_to_snippet(expected_ty, expected_idx)));
}
}
// If we have less than 5 things to say, it would be useful to call out exactly what's wrong
if labels.len() <= 5 {
for (span, label) in labels {
@ -1112,7 +1170,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
Some(format!("provide the argument{}", if plural { "s" } else { "" }))
}
SuggestionText::Remove(plural) => {
Some(format!("remove the extra argument{}", if plural { "s" } else { "" }))
err.multipart_suggestion_verbose(
&format!("remove the extra argument{}", if plural { "s" } else { "" }),
suggestions,
Applicability::HasPlaceholders,
);
None
}
SuggestionText::Swap => Some("swap these arguments".to_string()),
SuggestionText::Reorder => Some("reorder these arguments".to_string()),
@ -1151,20 +1214,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
} else {
// Propose a placeholder of the correct type
let (_, expected_ty) = formal_and_expected_inputs[expected_idx];
if expected_ty.is_unit() {
"()".to_string()
} else if expected_ty.is_suggestable(tcx, false) {
format!("/* {} */", expected_ty)
} else if let Some(fn_def_id) = fn_def_id
&& self.tcx.def_kind(fn_def_id).is_fn_like()
&& let self_implicit = matches!(call_expr.kind, hir::ExprKind::MethodCall(..)) as usize
&& let Some(arg) = self.tcx.fn_arg_names(fn_def_id).get(expected_idx.as_usize() + self_implicit)
&& arg.name != kw::SelfLower
{
format!("/* {} */", arg.name)
} else {
"/* value */".to_string()
}
ty_to_snippet(expected_ty, expected_idx)
};
suggestion += &suggestion_text;
}

View File

@ -17,8 +17,10 @@ LL | fn oom() -> ! {
= note: this error originates in the attribute macro `alloc_error_handler` (in Nightly builds, run with -Z macro-backtrace for more info)
help: remove the extra argument
|
LL | fn oom() -> !() {
| ++
LL - fn oom() -> ! {
LL - loop {}
LL - }
|
error: aborting due to previous error

View File

@ -25,8 +25,9 @@ LL | fn extra() {}
| ^^^^^
help: remove the extra argument
|
LL | extra();
| ~~
LL - extra("");
LL + extra();
|
error[E0061]: this function takes 1 argument but 0 arguments were supplied
--> $DIR/basic.rs:22:5

View File

@ -11,8 +11,9 @@ LL | fn foo<T: Fn()>(t: T) {
| ^^^^
help: remove the extra argument
|
LL | t();
| ~~
LL - t(1i32);
LL + t();
|
error[E0057]: this function takes 0 arguments but 1 argument was supplied
--> $DIR/exotic-calls.rs:7:5
@ -27,8 +28,9 @@ LL | fn bar(t: impl Fn()) {
| ^^^^^^^^^
help: remove the extra argument
|
LL | t();
| ~~
LL - t(1i32);
LL + t();
|
error[E0057]: this function takes 0 arguments but 1 argument was supplied
--> $DIR/exotic-calls.rs:16:5
@ -43,8 +45,9 @@ LL | fn baz() -> impl Fn() {
| ^^^^^^^^^
help: remove the extra argument
|
LL | baz()()
| ~~
LL - baz()(1i32)
LL + baz()()
|
error[E0057]: this function takes 0 arguments but 1 argument was supplied
--> $DIR/exotic-calls.rs:22:5
@ -59,8 +62,9 @@ LL | let x = || {};
| ^^
help: remove the extra argument
|
LL | x();
| ~~
LL - x(1i32);
LL + x();
|
error: aborting due to 4 previous errors

View File

@ -11,8 +11,9 @@ LL | fn empty() {}
| ^^^^^
help: remove the extra argument
|
LL | empty();
| ~~
LL - empty("");
LL + empty();
|
error[E0061]: this function takes 1 argument but 2 arguments were supplied
--> $DIR/extra_arguments.rs:9:3
@ -27,8 +28,9 @@ LL | fn one_arg(_a: i32) {}
| ^^^^^^^ -------
help: remove the extra argument
|
LL | one_arg(1);
| ~~~
LL - one_arg(1, 1);
LL + one_arg(1);
|
error[E0061]: this function takes 1 argument but 2 arguments were supplied
--> $DIR/extra_arguments.rs:10:3
@ -43,8 +45,9 @@ LL | fn one_arg(_a: i32) {}
| ^^^^^^^ -------
help: remove the extra argument
|
LL | one_arg(1);
| ~~~
LL - one_arg(1, "");
LL + one_arg(1);
|
error[E0061]: this function takes 1 argument but 3 arguments were supplied
--> $DIR/extra_arguments.rs:11:3
@ -61,8 +64,9 @@ LL | fn one_arg(_a: i32) {}
| ^^^^^^^ -------
help: remove the extra arguments
|
LL | one_arg(1);
| ~~~
LL - one_arg(1, "", 1.0);
LL + one_arg(1, );
|
error[E0061]: this function takes 2 arguments but 3 arguments were supplied
--> $DIR/extra_arguments.rs:13:3
@ -77,8 +81,9 @@ LL | fn two_arg_same(_a: i32, _b: i32) {}
| ^^^^^^^^^^^^ ------- -------
help: remove the extra argument
|
LL | two_arg_same(1, 1);
| ~~~~~~
LL - two_arg_same(1, 1, 1);
LL + two_arg_same(1, 1);
|
error[E0061]: this function takes 2 arguments but 3 arguments were supplied
--> $DIR/extra_arguments.rs:14:3
@ -93,8 +98,9 @@ LL | fn two_arg_same(_a: i32, _b: i32) {}
| ^^^^^^^^^^^^ ------- -------
help: remove the extra argument
|
LL | two_arg_same(1, 1);
| ~~~~~~
LL - two_arg_same(1, 1, 1.0);
LL + two_arg_same(1, 1);
|
error[E0061]: this function takes 2 arguments but 3 arguments were supplied
--> $DIR/extra_arguments.rs:16:3
@ -109,8 +115,9 @@ LL | fn two_arg_diff(_a: i32, _b: &str) {}
| ^^^^^^^^^^^^ ------- --------
help: remove the extra argument
|
LL | two_arg_diff(1, "");
| ~~~~~~~
LL - two_arg_diff(1, 1, "");
LL + two_arg_diff(1, "");
|
error[E0061]: this function takes 2 arguments but 3 arguments were supplied
--> $DIR/extra_arguments.rs:17:3
@ -125,8 +132,9 @@ LL | fn two_arg_diff(_a: i32, _b: &str) {}
| ^^^^^^^^^^^^ ------- --------
help: remove the extra argument
|
LL | two_arg_diff(1, "");
| ~~~~~~~
LL - two_arg_diff(1, "", "");
LL + two_arg_diff(1, "");
|
error[E0061]: this function takes 2 arguments but 4 arguments were supplied
--> $DIR/extra_arguments.rs:18:3
@ -143,8 +151,9 @@ LL | fn two_arg_diff(_a: i32, _b: &str) {}
| ^^^^^^^^^^^^ ------- --------
help: remove the extra arguments
|
LL | two_arg_diff(1, "");
| ~~~~~~~
LL - two_arg_diff(1, 1, "", "");
LL + two_arg_diff(1, "");
|
error[E0061]: this function takes 2 arguments but 4 arguments were supplied
--> $DIR/extra_arguments.rs:19:3
@ -161,8 +170,9 @@ LL | fn two_arg_diff(_a: i32, _b: &str) {}
| ^^^^^^^^^^^^ ------- --------
help: remove the extra arguments
|
LL | two_arg_diff(1, "");
| ~~~~~~~
LL - two_arg_diff(1, "", 1, "");
LL + two_arg_diff(1, "", );
|
error[E0061]: this function takes 2 arguments but 3 arguments were supplied
--> $DIR/extra_arguments.rs:22:3
@ -177,8 +187,9 @@ LL | fn two_arg_same(_a: i32, _b: i32) {}
| ^^^^^^^^^^^^ ------- -------
help: remove the extra argument
|
LL | two_arg_same(1, 1);
| ~~~~~~
LL - two_arg_same(1, 1, "");
LL + two_arg_same(1, 1);
|
error[E0061]: this function takes 2 arguments but 3 arguments were supplied
--> $DIR/extra_arguments.rs:23:3
@ -193,8 +204,9 @@ LL | fn two_arg_diff(_a: i32, _b: &str) {}
| ^^^^^^^^^^^^ ------- --------
help: remove the extra argument
|
LL | two_arg_diff(1, "");
| ~~~~~~~
LL - two_arg_diff(1, 1, "");
LL + two_arg_diff(1, "");
|
error[E0061]: this function takes 2 arguments but 3 arguments were supplied
--> $DIR/extra_arguments.rs:24:3
@ -212,8 +224,9 @@ LL | fn two_arg_same(_a: i32, _b: i32) {}
| ^^^^^^^^^^^^ ------- -------
help: remove the extra argument
|
LL | two_arg_same(1, 1);
| ~~~~~~
LL - 1,
LL + 1
|
error[E0061]: this function takes 2 arguments but 3 arguments were supplied
--> $DIR/extra_arguments.rs:30:3
@ -231,8 +244,9 @@ LL | fn two_arg_diff(_a: i32, _b: &str) {}
| ^^^^^^^^^^^^ ------- --------
help: remove the extra argument
|
LL | two_arg_diff(1, "");
| ~~~~~~~
LL - 1,
LL + ""
|
error: aborting due to 14 previous errors

View File

@ -19,8 +19,9 @@ LL | foo(&&A, B, C, D, &E, F, G);
| ~~
help: remove the extra arguments
|
LL | foo(&&A, D, /* &E */, G);
| ~~~~~~~~~~~~~~~~~~~~~
LL - foo(&&A, B, C, D, E, F, G);
LL + foo(&&A, D, /* &E */, G);
|
error: aborting due to previous error

View File

@ -13,8 +13,9 @@ LL | fn two_args(_a: i32, _b: f32) {}
| ^^^^^^^^ ------- -------
help: remove the extra argument
|
LL | two_args(1, /* f32 */);
| ~~~~~~~~~~~~~~
LL - two_args(1, "", X {});
LL + two_args(1, /* f32 */);
|
error[E0061]: this function takes 3 arguments but 4 arguments were supplied
--> $DIR/mixed_cases.rs:11:3

View File

@ -27,8 +27,9 @@ LL | let f = |x| x * 3;
| ^^^
help: remove the extra argument
|
LL | let c = f(2);
| ~~~
LL - let c = f(2, 3);
LL + let c = f(2);
|
error: aborting due to 2 previous errors

View File

@ -11,8 +11,9 @@ LL | fn _foo<F: Fn()> (f: F) {
| ^^^^
help: remove the extra argument
|
LL | |t| f();
| ~~
LL - |t| f(t);
LL + |t| f();
|
error: aborting due to previous error

View File

@ -14,8 +14,9 @@ LL | fn some_function() {}
| ^^^^^^^^^^^^^
help: remove the extra argument
|
LL | some_function()
| ~~~~~~~~~~~~~~~
LL - $other(None)
LL + $other()
|
error: aborting due to previous error

View File

@ -11,8 +11,9 @@ LL | fn foo(a: usize) {}
| ^^^ --------
help: remove the extra argument
|
LL | fn main() { foo(5) }
| ~~~
LL - fn main() { foo(5, 6) }
LL + fn main() { foo(5) }
|
error: aborting due to previous error

View File

@ -11,8 +11,9 @@ LL | fn zero(self) -> Foo { self }
| ^^^^
help: remove the extra argument
|
LL | x.zero()
| ~~
LL - x.zero(0)
LL + x.zero()
|
error[E0061]: this method takes 1 argument but 0 arguments were supplied
--> $DIR/method-call-err-msg.rs:14:7

View File

@ -43,8 +43,9 @@ LL | impl FnMut<(isize,)> for S {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
help: remove the extra argument
|
LL | let ans = s(/* isize */);
| ~~~~~~~~~~~~~
LL - let ans = s("burma", "shave");
LL + let ans = s(/* isize */);
|
error[E0308]: mismatched types
--> $DIR/overloaded-calls-bad.rs:40:7

View File

@ -30,8 +30,9 @@ note: function defined here
--> $SRC_DIR/core/src/mem/mod.rs:LL:COL
help: remove the extra argument
|
LL | std::mem::size_of();
| ~~
LL - std::mem::size_of(u16);
LL + std::mem::size_of();
|
error: aborting due to 3 previous errors

View File

@ -63,8 +63,9 @@ LL | fn foo(Option<i32>, String) {}
| ^^^ ----------- ------
help: remove the extra argument
|
LL | foo(Some(42), 2);
| ~~~~~~~~~~~~~
LL - foo(Some(42), 2, "");
LL + foo(Some(42), 2);
|
error[E0308]: mismatched types
--> $DIR/issue-34264.rs:8:13
@ -93,8 +94,9 @@ LL | fn bar(x, y: usize) {}
| ^^^ - --------
help: remove the extra argument
|
LL | bar(1, 2);
| ~~~~~~
LL - bar(1, 2, 3);
LL + bar(1, 2);
|
error: aborting due to 6 previous errors

View File

@ -22,8 +22,9 @@ note: tuple variant defined here
--> $SRC_DIR/core/src/option.rs:LL:COL
help: remove the extra argument
|
LL | let _: Option<(i32, bool)> = Some(/* (i32, bool) */);
| ~~~~~~~~~~~~~~~~~~~
LL - let _: Option<(i32, bool)> = Some(1, 2);
LL + let _: Option<(i32, bool)> = Some(/* (i32, bool) */);
|
error[E0061]: this function takes 1 argument but 2 arguments were supplied
--> $DIR/args-instead-of-tuple-errors.rs:8:5
@ -45,8 +46,9 @@ LL | fn int_bool(_: (i32, bool)) {
| ^^^^^^^^ --------------
help: remove the extra argument
|
LL | int_bool(/* (i32, bool) */);
| ~~~~~~~~~~~~~~~~~~~
LL - int_bool(1, 2);
LL + int_bool(/* (i32, bool) */);
|
error[E0061]: this enum variant takes 1 argument but 0 arguments were supplied
--> $DIR/args-instead-of-tuple-errors.rs:11:28

View File

@ -15,8 +15,9 @@ note: associated function defined here
--> $SRC_DIR/alloc/src/vec/mod.rs:LL:COL
help: remove the extra argument
|
LL | groups.push(/* (Vec<String>, Vec<Process>) */);
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
LL - groups.push(new_group, vec![process]);
LL + groups.push(/* (Vec<String>, Vec<Process>) */);
|
error: aborting due to previous error

View File

@ -15,8 +15,9 @@ LL | (|| {})(|| {
| ^^
help: remove the extra argument
|
LL | (|| {})();
| ~~
LL - (|| {})(|| {
LL + (|| {})();
|
error: aborting due to previous error

View File

@ -17,8 +17,9 @@ note: associated function defined here
--> $SRC_DIR/alloc/src/vec/mod.rs:LL:COL
help: remove the extra argument
|
LL | let x: Vec::with_capacity(10);
| ~~~~
LL - let x: Vec::with_capacity(10, 20);
LL + let x: Vec::with_capacity(10);
|
error: aborting due to 2 previous errors

View File

@ -11,8 +11,9 @@ LL | fn l(_a: Vec<u8>) {}
| ^ -----------
help: remove the extra argument
|
LL | l(vec![])
| ~~~~~~~~
LL - l(vec![], vec![])
LL + l(vec![])
|
error: aborting due to previous error

View File

@ -8,8 +8,9 @@ note: tuple variant defined here
--> $SRC_DIR/core/src/option.rs:LL:COL
help: remove the extra argument
|
LL | let _ = Some(3);
| ~~~
LL - let _ = Some(3, 2);
LL + let _ = Some(3);
|
error[E0061]: this enum variant takes 1 argument but 3 arguments were supplied
--> $DIR/struct-enum-wrong-args.rs:7:13
@ -23,8 +24,9 @@ note: tuple variant defined here
--> $SRC_DIR/core/src/result.rs:LL:COL
help: remove the extra arguments
|
LL | let _ = Ok(3);
| ~~~
LL - let _ = Ok(3, 6, 2);
LL + let _ = Ok(3, );
|
error[E0061]: this enum variant takes 1 argument but 0 arguments were supplied
--> $DIR/struct-enum-wrong-args.rs:8:13
@ -68,8 +70,9 @@ LL | struct Wrapper(i32);
| ^^^^^^^
help: remove the extra argument
|
LL | let _ = Wrapper(5);
| ~~~
LL - let _ = Wrapper(5, 2);
LL + let _ = Wrapper(5);
|
error[E0061]: this struct takes 2 arguments but 0 arguments were supplied
--> $DIR/struct-enum-wrong-args.rs:11:13
@ -116,8 +119,9 @@ LL | struct DoubleWrapper(i32, i32);
| ^^^^^^^^^^^^^
help: remove the extra argument
|
LL | let _ = DoubleWrapper(5, 2);
| ~~~~~~
LL - let _ = DoubleWrapper(5, 2, 7);
LL + let _ = DoubleWrapper(5, 2);
|
error: aborting due to 8 previous errors