Skip to content

Fix overlapping spans in removing extra arguments #108239

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
Feb 22, 2023
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: 10 additions & 13 deletions compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -932,25 +932,22 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
labels
.push((provided_span, format!("unexpected argument{}", provided_ty_name)));
let mut span = provided_span;
if arg_idx.index() > 0
if span.can_be_used_for_suggestions() {
if arg_idx.index() > 0
&& let Some((_, prev)) = provided_arg_tys
.get(ProvidedIdx::from_usize(arg_idx.index() - 1)
) {
// Include previous comma
span = span.with_lo(prev.hi());
} else if let Some((_, next)) = provided_arg_tys.get(
ProvidedIdx::from_usize(arg_idx.index() + 1),
) {
// Include next comma
span = span.until(*next);
span = prev.shrink_to_hi().to(span);
}
suggestions.push((span, String::new()));
suggestions.push((span, String::new()));

suggestion_text = match suggestion_text {
SuggestionText::None => SuggestionText::Remove(false),
SuggestionText::Remove(_) => SuggestionText::Remove(true),
_ => SuggestionText::DidYouMean,
};
suggestion_text = match suggestion_text {
SuggestionText::None => SuggestionText::Remove(false),
SuggestionText::Remove(_) => SuggestionText::Remove(true),
_ => SuggestionText::DidYouMean,
};
}
}
Error::Missing(expected_idx) => {
// If there are multiple missing arguments adjacent to each other,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ LL | fn oom() -> ! {
| _-^^^^^^^^^^^^
LL | | loop {}
LL | | }
| | -
| | |
| |_unexpected argument of type `core::alloc::Layout`
| help: remove the extra argument
| |_- unexpected argument of type `core::alloc::Layout`
|
note: function defined here
--> $DIR/alloc-error-handler-bad-signature-3.rs:10:4
Expand Down
8 changes: 8 additions & 0 deletions tests/ui/argument-suggestions/extra_arguments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,15 @@ fn one_arg(_a: i32) {}
fn two_arg_same(_a: i32, _b: i32) {}
fn two_arg_diff(_a: i32, _b: &str) {}

macro_rules! foo {
($x:expr) => {
empty($x, 1); //~ ERROR function takes
}
}

fn main() {
empty(""); //~ ERROR function takes
empty(1, 1); //~ ERROR function takes

one_arg(1, 1); //~ ERROR function takes
one_arg(1, ""); //~ ERROR function takes
Expand Down Expand Up @@ -32,4 +39,5 @@ fn main() {
1,
""
);
foo!(1);
}
69 changes: 54 additions & 15 deletions tests/ui/argument-suggestions/extra_arguments.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error[E0061]: this function takes 0 arguments but 1 argument was supplied
--> $DIR/extra_arguments.rs:7:3
--> $DIR/extra_arguments.rs:13:3
|
LL | empty("");
| ^^^^^ --
Expand All @@ -13,8 +13,27 @@ note: function defined here
LL | fn empty() {}
| ^^^^^

error[E0061]: this function takes 0 arguments but 2 arguments were supplied
--> $DIR/extra_arguments.rs:14:3
|
LL | empty(1, 1);
| ^^^^^ - - unexpected argument of type `{integer}`
| |
| unexpected argument of type `{integer}`
|
note: function defined here
--> $DIR/extra_arguments.rs:1:4
|
LL | fn empty() {}
| ^^^^^
help: remove the extra arguments
|
LL - empty(1, 1);
LL + empty();
|

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

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

error[E0061]: this function takes 1 argument but 3 arguments were supplied
--> $DIR/extra_arguments.rs:11:3
--> $DIR/extra_arguments.rs:18:3
|
LL | one_arg(1, "", 1.0);
| ^^^^^^^ -- --- unexpected argument of type `{float}`
Expand All @@ -63,7 +82,7 @@ LL + one_arg(1);
|

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

error[E0061]: this function takes 2 arguments but 3 arguments were supplied
--> $DIR/extra_arguments.rs:14:3
--> $DIR/extra_arguments.rs:21:3
|
LL | two_arg_same(1, 1, 1.0);
| ^^^^^^^^^^^^ -----
Expand All @@ -93,7 +112,7 @@ LL | fn two_arg_same(_a: i32, _b: i32) {}
| ^^^^^^^^^^^^ ------- -------

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

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

error[E0061]: this function takes 2 arguments but 4 arguments were supplied
--> $DIR/extra_arguments.rs:18:3
--> $DIR/extra_arguments.rs:25:3
|
LL | two_arg_diff(1, 1, "", "");
| ^^^^^^^^^^^^ - -- unexpected argument of type `&'static str`
Expand All @@ -142,7 +161,7 @@ LL + two_arg_diff(1, "");
|

error[E0061]: this function takes 2 arguments but 4 arguments were supplied
--> $DIR/extra_arguments.rs:19:3
--> $DIR/extra_arguments.rs:26:3
|
LL | two_arg_diff(1, "", 1, "");
| ^^^^^^^^^^^^ - -- unexpected argument of type `&'static str`
Expand All @@ -161,7 +180,7 @@ LL + two_arg_diff(1, "");
|

error[E0061]: this function takes 2 arguments but 3 arguments were supplied
--> $DIR/extra_arguments.rs:22:3
--> $DIR/extra_arguments.rs:29:3
|
LL | two_arg_same(1, 1, "");
| ^^^^^^^^^^^^ --------
Expand All @@ -176,7 +195,7 @@ LL | fn two_arg_same(_a: i32, _b: i32) {}
| ^^^^^^^^^^^^ ------- -------

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

error[E0061]: this function takes 2 arguments but 3 arguments were supplied
--> $DIR/extra_arguments.rs:24:3
--> $DIR/extra_arguments.rs:31:3
|
LL | two_arg_same(
| ^^^^^^^^^^^^
Expand All @@ -211,7 +230,7 @@ LL | fn two_arg_same(_a: i32, _b: i32) {}
| ^^^^^^^^^^^^ ------- -------

error[E0061]: this function takes 2 arguments but 3 arguments were supplied
--> $DIR/extra_arguments.rs:30:3
--> $DIR/extra_arguments.rs:37:3
|
LL | two_arg_diff(
| ^^^^^^^^^^^^
Expand All @@ -229,6 +248,26 @@ note: function defined here
LL | fn two_arg_diff(_a: i32, _b: &str) {}
| ^^^^^^^^^^^^ ------- --------

error: aborting due to 14 previous errors
error[E0061]: this function takes 0 arguments but 2 arguments were supplied
--> $DIR/extra_arguments.rs:8:9
|
LL | empty($x, 1);
| ^^^^^ - unexpected argument of type `{integer}`
...
LL | foo!(1);
| -------
| | |
| | unexpected argument of type `{integer}`
| | help: remove the extra argument
| in this macro invocation
|
note: function defined here
--> $DIR/extra_arguments.rs:1:4
|
LL | fn empty() {}
| ^^^^^
= note: this error originates in the macro `foo` (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 16 previous errors

For more information about this error, try `rustc --explain E0061`.
5 changes: 2 additions & 3 deletions tests/ui/issues/issue-26094.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
macro_rules! some_macro {
($other: expr) => ({
($other: expr) => {{
$other(None) //~ NOTE unexpected argument of type `Option<_>`
})
}};
}

fn some_function() {} //~ NOTE defined here

fn main() {
some_macro!(some_function);
//~^ ERROR function takes 0 arguments but 1 argument was supplied
//~| NOTE in this expansion of some_macro!
}
5 changes: 1 addition & 4 deletions tests/ui/issues/issue-26094.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ error[E0061]: this function takes 0 arguments but 1 argument was supplied
--> $DIR/issue-26094.rs:10:17
|
LL | $other(None)
| ----
| |
| unexpected argument of type `Option<_>`
| help: remove the extra argument
| ---- unexpected argument of type `Option<_>`
...
LL | some_macro!(some_function);
| ^^^^^^^^^^^^^
Expand Down