Skip to content

Shrink suggestion span of argument mismatch error #101364

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

Merged
merged 1 commit into from
Sep 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions compiler/rustc_typeck/src/check/fn_ctxt/checks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -1088,7 +1099,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
}
suggestion += ")";
err.span_suggestion_verbose(
error_span,
suggestion_span,
&suggestion_text,
suggestion,
Applicability::HasPlaceholders,
Expand Down
10 changes: 5 additions & 5 deletions src/test/ui/argument-suggestions/basic.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ LL | fn extra() {}
help: remove the extra argument
|
LL | extra();
| ~~~~~~~
| ~~

error[E0061]: this function takes 1 argument but 0 arguments were supplied
--> $DIR/basic.rs:22:5
Expand All @@ -42,7 +42,7 @@ LL | fn missing(_i: u32) {}
help: provide the argument
|
LL | missing(/* u32 */);
| ~~~~~~~~~~~~~~~~~~
| ~~~~~~~~~~~

error[E0308]: arguments to this function are incorrect
--> $DIR/basic.rs:23:5
Expand All @@ -60,7 +60,7 @@ LL | fn swapped(_i: u32, _s: &str) {}
help: swap these arguments
|
LL | swapped(1, "");
| ~~~~~~~~~~~~~~
| ~~~~~~~

error[E0308]: arguments to this function are incorrect
--> $DIR/basic.rs:24:5
Expand All @@ -79,7 +79,7 @@ LL | fn permuted(_x: X, _y: Y, _z: Z) {}
help: reorder these arguments
|
LL | permuted(X {}, Y {}, Z {});
| ~~~~~~~~~~~~~~~~~~~~~~~~~~
| ~~~~~~~~~~~~~~~~~~

error[E0057]: this function takes 1 argument but 0 arguments were supplied
--> $DIR/basic.rs:27:5
Expand All @@ -95,7 +95,7 @@ LL | let closure = |x| x;
help: provide the argument
|
LL | closure(/* value */);
| ~~~~~~~~~~~~~~~~~~~~
| ~~~~~~~~~~~~~

error: aborting due to 6 previous errors

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/argument-suggestions/complex.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ LL | fn complex(_i: u32, _s: &str, _e: E, _f: F, _g: G, _x: X, _y: Y, _z: Z ) {}
help: did you mean
|
LL | complex(/* u32 */, &"", /* E */, F::X2, G{}, X {}, Y {}, Z {});
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

error: aborting due to previous error

Expand Down
8 changes: 4 additions & 4 deletions src/test/ui/argument-suggestions/exotic-calls.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ LL | fn foo<T: Fn()>(t: T) {
help: remove the extra argument
|
LL | t();
| ~~~
| ~~

error[E0057]: this function takes 0 arguments but 1 argument was supplied
--> $DIR/exotic-calls.rs:7:5
Expand All @@ -28,7 +28,7 @@ LL | fn bar(t: impl Fn()) {
help: remove the extra argument
|
LL | t();
| ~~~
| ~~

error[E0057]: this function takes 0 arguments but 1 argument was supplied
--> $DIR/exotic-calls.rs:16:5
Expand All @@ -44,7 +44,7 @@ LL | fn baz() -> impl Fn() {
help: remove the extra argument
|
LL | baz()()
|
| ~~

error[E0057]: this function takes 0 arguments but 1 argument was supplied
--> $DIR/exotic-calls.rs:22:5
Expand All @@ -60,7 +60,7 @@ LL | let x = || {};
help: remove the extra argument
|
LL | x();
| ~~~
| ~~

error: aborting due to 4 previous errors

Expand Down
28 changes: 14 additions & 14 deletions src/test/ui/argument-suggestions/extra_arguments.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ LL | fn empty() {}
help: remove the extra argument
|
LL | empty();
| ~~~~~~~
| ~~

error[E0061]: this function takes 1 argument but 2 arguments were supplied
--> $DIR/extra_arguments.rs:9:3
Expand All @@ -28,7 +28,7 @@ LL | fn one_arg(_a: i32) {}
help: remove the extra argument
|
LL | one_arg(1);
| ~~~~~~~~~~
| ~~~

error[E0061]: this function takes 1 argument but 2 arguments were supplied
--> $DIR/extra_arguments.rs:10:3
Expand All @@ -44,7 +44,7 @@ LL | fn one_arg(_a: i32) {}
help: remove the extra argument
|
LL | one_arg(1);
| ~~~~~~~~~~
| ~~~

error[E0061]: this function takes 1 argument but 3 arguments were supplied
--> $DIR/extra_arguments.rs:11:3
Expand All @@ -62,7 +62,7 @@ LL | fn one_arg(_a: i32) {}
help: remove the extra arguments
|
LL | one_arg(1);
| ~~~~~~~~~~
| ~~~

error[E0061]: this function takes 2 arguments but 3 arguments were supplied
--> $DIR/extra_arguments.rs:13:3
Expand All @@ -78,7 +78,7 @@ LL | fn two_arg_same(_a: i32, _b: i32) {}
help: remove the extra argument
|
LL | two_arg_same(1, 1);
| ~~~~~~~~~~~~~~~~~~
| ~~~~~~

error[E0061]: this function takes 2 arguments but 3 arguments were supplied
--> $DIR/extra_arguments.rs:14:3
Expand All @@ -94,7 +94,7 @@ LL | fn two_arg_same(_a: i32, _b: i32) {}
help: remove the extra argument
|
LL | two_arg_same(1, 1);
| ~~~~~~~~~~~~~~~~~~
| ~~~~~~

error[E0061]: this function takes 2 arguments but 3 arguments were supplied
--> $DIR/extra_arguments.rs:16:3
Expand All @@ -110,7 +110,7 @@ LL | fn two_arg_diff(_a: i32, _b: &str) {}
help: remove the extra argument
|
LL | two_arg_diff(1, "");
| ~~~~~~~~~~~~~~~~~~~
| ~~~~~~~

error[E0061]: this function takes 2 arguments but 3 arguments were supplied
--> $DIR/extra_arguments.rs:17:3
Expand All @@ -126,7 +126,7 @@ LL | fn two_arg_diff(_a: i32, _b: &str) {}
help: remove the extra argument
|
LL | two_arg_diff(1, "");
| ~~~~~~~~~~~~~~~~~~~
| ~~~~~~~

error[E0061]: this function takes 2 arguments but 4 arguments were supplied
--> $DIR/extra_arguments.rs:18:3
Expand All @@ -144,7 +144,7 @@ LL | fn two_arg_diff(_a: i32, _b: &str) {}
help: remove the extra arguments
|
LL | two_arg_diff(1, "");
| ~~~~~~~~~~~~~~~~~~~
| ~~~~~~~

error[E0061]: this function takes 2 arguments but 4 arguments were supplied
--> $DIR/extra_arguments.rs:19:3
Expand All @@ -162,7 +162,7 @@ LL | fn two_arg_diff(_a: i32, _b: &str) {}
help: remove the extra arguments
|
LL | two_arg_diff(1, "");
| ~~~~~~~~~~~~~~~~~~~
| ~~~~~~~

error[E0061]: this function takes 2 arguments but 3 arguments were supplied
--> $DIR/extra_arguments.rs:22:3
Expand All @@ -178,7 +178,7 @@ LL | fn two_arg_same(_a: i32, _b: i32) {}
help: remove the extra argument
|
LL | two_arg_same(1, 1);
| ~~~~~~~~~~~~~~~~~~
| ~~~~~~

error[E0061]: this function takes 2 arguments but 3 arguments were supplied
--> $DIR/extra_arguments.rs:23:3
Expand All @@ -194,7 +194,7 @@ LL | fn two_arg_diff(_a: i32, _b: &str) {}
help: remove the extra argument
|
LL | two_arg_diff(1, "");
| ~~~~~~~~~~~~~~~~~~~
| ~~~~~~~

error[E0061]: this function takes 2 arguments but 3 arguments were supplied
--> $DIR/extra_arguments.rs:24:3
Expand All @@ -213,7 +213,7 @@ LL | fn two_arg_same(_a: i32, _b: i32) {}
help: remove the extra argument
|
LL | two_arg_same(1, 1);
| ~~~~~~~~~~~~~~~~~~
| ~~~~~~

error[E0061]: this function takes 2 arguments but 3 arguments were supplied
--> $DIR/extra_arguments.rs:30:3
Expand All @@ -232,7 +232,7 @@ LL | fn two_arg_diff(_a: i32, _b: &str) {}
help: remove the extra argument
|
LL | two_arg_diff(1, "");
| ~~~~~~~~~~~~~~~~~~~
| ~~~~~~~

error: aborting due to 14 previous errors

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/argument-suggestions/issue-96638.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ LL | fn f(_: usize, _: &usize, _: usize) {}
help: provide the argument
|
LL | f(/* usize */, &x, /* usize */);
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

error: aborting due to previous error

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/argument-suggestions/issue-97197.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ LL | pub fn g(a1: (), a2: bool, a3: bool, a4: bool, a5: bool, a6: ()) -> () {}
help: provide the arguments
|
LL | g((), /* bool */, /* bool */, /* bool */, /* bool */, ());
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

error: aborting due to previous error

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/argument-suggestions/issue-97484.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ LL | foo(&&A, B, C, D, &E, F, G);
help: remove the extra arguments
|
LL | foo(&&A, D, /* &E */, G);
| ~~~~~~~~~~~~~~~~~~~~~~~~
| ~~~~~~~~~~~~~~~~~~~~~

error: aborting due to previous error

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/argument-suggestions/issue-98894.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ LL | (|_, ()| ())(if true {} else {return;});
help: provide the argument
|
LL | (|_, ()| ())(if true {} else {return;}, ());
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

error: aborting due to previous error

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/argument-suggestions/issue-98897.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ LL | (|_, ()| ())([return, ()]);
help: provide the argument
|
LL | (|_, ()| ())([return, ()], ());
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| ~~~~~~~~~~~~~~~~~~

error: aborting due to previous error

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/argument-suggestions/issue-99482.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ LL | let f = |_: (), f: fn()| f;
help: provide the argument
|
LL | let _f = f((), main);
| ~~~~~~~~~~~
| ~~~~~~~~~~

error: aborting due to previous error

Expand Down
Loading