Skip to content

Commit 9ff4dab

Browse files
authored
Rollup merge of #126588 - linyihai:trim-extra-comma, r=petrochenkov
Added more scenarios where comma to be removed in the function arg This is an attempt to address the problem methion in #106304 (comment). Copy the annotation to explain the fix If the next Error::Extra ("next") doesn't next to current ("current") ``` fn foo(_: (), _: u32) {} - foo("current", (), 1u32, "next") + foo((), 1u32) ``` If the previous error is not a `Error::Extra`, then do not trim the next comma ``` - foo((), "current", 42u32, "next") + foo((), 42u32) ``` Frankly, this is a fix from a test case and may not cover all scenarios
2 parents fb32dd4 + f107082 commit 9ff4dab

File tree

5 files changed

+76
-9
lines changed

5 files changed

+76
-9
lines changed

compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs

+25-5
Original file line numberDiff line numberDiff line change
@@ -1097,6 +1097,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10971097
let mut only_extras_so_far = errors
10981098
.peek()
10991099
.is_some_and(|first| matches!(first, Error::Extra(arg_idx) if arg_idx.index() == 0));
1100+
let mut prev_extra_idx = None;
11001101
let mut suggestions = vec![];
11011102
while let Some(error) = errors.next() {
11021103
only_extras_so_far &= matches!(error, Error::Extra(_));
@@ -1165,11 +1166,29 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
11651166
// fn f() {}
11661167
// - f(0, 1,)
11671168
// + f()
1168-
if only_extras_so_far
1169-
&& !errors
1170-
.peek()
1171-
.is_some_and(|next_error| matches!(next_error, Error::Extra(_)))
1172-
{
1169+
let trim_next_comma = match errors.peek() {
1170+
Some(Error::Extra(provided_idx))
1171+
if only_extras_so_far
1172+
&& provided_idx.index() > arg_idx.index() + 1 =>
1173+
// If the next Error::Extra ("next") doesn't next to current ("current"),
1174+
// fn foo(_: (), _: u32) {}
1175+
// - foo("current", (), 1u32, "next")
1176+
// + foo((), 1u32)
1177+
// If the previous error is not a `Error::Extra`, then do not trim the next comma
1178+
// - foo((), "current", 42u32, "next")
1179+
// + foo((), 42u32)
1180+
{
1181+
prev_extra_idx.map_or(true, |prev_extra_idx| {
1182+
prev_extra_idx + 1 == arg_idx.index()
1183+
})
1184+
}
1185+
// If no error left, we need to delete the next comma
1186+
None if only_extras_so_far => true,
1187+
// Not sure if other error type need to be handled as well
1188+
_ => false,
1189+
};
1190+
1191+
if trim_next_comma {
11731192
let next = provided_arg_tys
11741193
.get(arg_idx + 1)
11751194
.map(|&(_, sp)| sp)
@@ -1192,6 +1211,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
11921211
SuggestionText::Remove(_) => SuggestionText::Remove(true),
11931212
_ => SuggestionText::DidYouMean,
11941213
};
1214+
prev_extra_idx = Some(arg_idx.index())
11951215
}
11961216
}
11971217
Error::Missing(expected_idx) => {

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

+4
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

+4
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

+40-2
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 #4 of type `()`
82+
| |
83+
| unexpected argument #1 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 #4 of type `()`
101+
| |
102+
| unexpected argument #2 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 #2 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

+3-2
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)