Skip to content

Commit a6ba988

Browse files
committed
Added more scenarios where commas need to be removed
1 parent d7f6eba commit a6ba988

File tree

5 files changed

+88
-9
lines changed

5 files changed

+88
-9
lines changed

compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,6 +1013,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10131013
let mut only_extras_so_far = errors
10141014
.peek()
10151015
.is_some_and(|first| matches!(first, Error::Extra(arg_idx) if arg_idx.index() == 0));
1016+
let mut prev_extra_idx = None;
10161017
let mut suggestions = vec![];
10171018
while let Some(error) = errors.next() {
10181019
only_extras_so_far &= matches!(error, Error::Extra(_));
@@ -1074,11 +1075,41 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10741075
// fn f() {}
10751076
// - f(0, 1,)
10761077
// + f()
1077-
if only_extras_so_far
1078-
&& !errors
1079-
.peek()
1080-
.is_some_and(|next_error| matches!(next_error, Error::Extra(_)))
1081-
{
1078+
let trim_next_comma = {
1079+
if only_extras_so_far {
1080+
if let Some(next_error) = errors.peek() {
1081+
match next_error {
1082+
// If the next Error::Extra ("next") doesn't next to current ("current"),
1083+
// fn foo(_: (), _: u32) {}
1084+
// - foo("current", (), 1u32, "next")
1085+
// + foo((), 1u32)
1086+
// If the previous error is not a `Error::Extra`, then do not trim the next comma
1087+
// - foo((), "current", 42u32, "next")
1088+
// + foo((), 42u32)
1089+
Error::Extra(provided_idx) => {
1090+
if provided_idx.index() > arg_idx.index() + 1 {
1091+
if let Some(prev_extra_idx) = prev_extra_idx {
1092+
prev_extra_idx + 1 == arg_idx.index()
1093+
} else {
1094+
true
1095+
}
1096+
} else {
1097+
false
1098+
}
1099+
}
1100+
// Not sure if other error type need to be handled as well
1101+
_ => false,
1102+
}
1103+
} else {
1104+
// If no error left, we need to delete the next comma
1105+
true
1106+
}
1107+
} else {
1108+
false
1109+
}
1110+
};
1111+
1112+
if trim_next_comma {
10821113
let next = provided_arg_tys
10831114
.get(arg_idx + 1)
10841115
.map(|&(_, sp)| sp)
@@ -1098,6 +1129,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10981129
SuggestionText::Remove(_) => SuggestionText::Remove(true),
10991130
_ => SuggestionText::DidYouMean,
11001131
};
1132+
prev_extra_idx = Some(arg_idx.index())
11011133
}
11021134
}
11031135
Error::Missing(expected_idx) => {

tests/ui/argument-suggestions/issue-109425.fixed

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ fn main() {
1515
//~^ error: this function takes 1 argument but 3 arguments were supplied
1616
is(0, ""); // is(0, "")
1717
//~^ error: this function takes 2 arguments but 4 arguments were supplied
18+
is(1, "");
19+
//~^ error: this function takes 2 arguments but 4 arguments were supplied
20+
is(1, "");
21+
//~^ error: this function takes 2 arguments but 4 arguments were supplied
1822
s(""); // s("")
1923
//~^ error: this function takes 1 argument but 3 arguments were supplied
2024
}

tests/ui/argument-suggestions/issue-109425.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ fn main() {
1515
//~^ error: this function takes 1 argument but 3 arguments were supplied
1616
is(0, 1, 2, ""); // is(0, "")
1717
//~^ error: this function takes 2 arguments but 4 arguments were supplied
18+
is((), 1, "", ());
19+
//~^ error: this function takes 2 arguments but 4 arguments were supplied
20+
is(1, (), "", ());
21+
//~^ error: this function takes 2 arguments but 4 arguments were supplied
1822
s(0, 1, ""); // s("")
1923
//~^ error: this function takes 1 argument but 3 arguments were supplied
2024
}

tests/ui/argument-suggestions/issue-109425.stderr

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,47 @@ LL - is(0, 1, 2, ""); // is(0, "")
7474
LL + is(0, ""); // is(0, "")
7575
|
7676

77-
error[E0061]: this function takes 1 argument but 3 arguments were supplied
77+
error[E0061]: this function takes 2 arguments but 4 arguments were supplied
7878
--> $DIR/issue-109425.rs:18:5
7979
|
80+
LL | is((), 1, "", ());
81+
| ^^ -- -- unexpected argument of type `()`
82+
| |
83+
| unexpected argument of type `()`
84+
|
85+
note: function defined here
86+
--> $DIR/issue-109425.rs:5:4
87+
|
88+
LL | fn is(_: u32, _: &str) {}
89+
| ^^ ------ -------
90+
help: remove the extra arguments
91+
|
92+
LL - is((), 1, "", ());
93+
LL + is(1, "");
94+
|
95+
96+
error[E0061]: this function takes 2 arguments but 4 arguments were supplied
97+
--> $DIR/issue-109425.rs:20:5
98+
|
99+
LL | is(1, (), "", ());
100+
| ^^ -- -- unexpected argument of type `()`
101+
| |
102+
| unexpected argument of type `()`
103+
|
104+
note: function defined here
105+
--> $DIR/issue-109425.rs:5:4
106+
|
107+
LL | fn is(_: u32, _: &str) {}
108+
| ^^ ------ -------
109+
help: remove the extra arguments
110+
|
111+
LL - is(1, (), "", ());
112+
LL + is(1, "");
113+
|
114+
115+
error[E0061]: this function takes 1 argument but 3 arguments were supplied
116+
--> $DIR/issue-109425.rs:22:5
117+
|
80118
LL | s(0, 1, ""); // s("")
81119
| ^ - - unexpected argument of type `{integer}`
82120
| |
@@ -93,6 +131,6 @@ LL - s(0, 1, ""); // s("")
93131
LL + s(""); // s("")
94132
|
95133

96-
error: aborting due to 5 previous errors
134+
error: aborting due to 7 previous errors
97135

98136
For more information about this error, try `rustc --explain E0061`.

tests/ui/argument-suggestions/issue-112507.stderr

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ LL | Float(Option<f64>),
1818
| ^^^^^
1919
help: remove the extra arguments
2020
|
21-
LL ~ ,
22-
LL ~ None);
21+
LL - 0,
22+
LL - None,
23+
LL + None);
2324
|
2425

2526
error: aborting due to 1 previous error

0 commit comments

Comments
 (0)