Skip to content

Commit 0b9a3e2

Browse files
committed
Fix overlapping spans in removing extra arguments
1 parent eebdfb5 commit 0b9a3e2

File tree

6 files changed

+76
-39
lines changed

6 files changed

+76
-39
lines changed

Diff for: compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs

+10-13
Original file line numberDiff line numberDiff line change
@@ -932,25 +932,22 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
932932
labels
933933
.push((provided_span, format!("unexpected argument{}", provided_ty_name)));
934934
let mut span = provided_span;
935-
if arg_idx.index() > 0
935+
if span.can_be_used_for_suggestions() {
936+
if arg_idx.index() > 0
936937
&& let Some((_, prev)) = provided_arg_tys
937938
.get(ProvidedIdx::from_usize(arg_idx.index() - 1)
938939
) {
939940
// Include previous comma
940-
span = span.with_lo(prev.hi());
941-
} else if let Some((_, next)) = provided_arg_tys.get(
942-
ProvidedIdx::from_usize(arg_idx.index() + 1),
943-
) {
944-
// Include next comma
945-
span = span.until(*next);
941+
span = prev.shrink_to_hi().to(span);
946942
}
947-
suggestions.push((span, String::new()));
943+
suggestions.push((span, String::new()));
948944

949-
suggestion_text = match suggestion_text {
950-
SuggestionText::None => SuggestionText::Remove(false),
951-
SuggestionText::Remove(_) => SuggestionText::Remove(true),
952-
_ => SuggestionText::DidYouMean,
953-
};
945+
suggestion_text = match suggestion_text {
946+
SuggestionText::None => SuggestionText::Remove(false),
947+
SuggestionText::Remove(_) => SuggestionText::Remove(true),
948+
_ => SuggestionText::DidYouMean,
949+
};
950+
}
954951
}
955952
Error::Missing(expected_idx) => {
956953
// If there are multiple missing arguments adjacent to each other,

Diff for: tests/ui/alloc-error/alloc-error-handler-bad-signature-3.stderr

+1-4
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@ LL | fn oom() -> ! {
77
| _-^^^^^^^^^^^^
88
LL | | loop {}
99
LL | | }
10-
| | -
11-
| | |
12-
| |_unexpected argument of type `core::alloc::Layout`
13-
| help: remove the extra argument
10+
| |_- unexpected argument of type `core::alloc::Layout`
1411
|
1512
note: function defined here
1613
--> $DIR/alloc-error-handler-bad-signature-3.rs:10:4

Diff for: tests/ui/argument-suggestions/extra_arguments.rs

+8
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,15 @@ fn one_arg(_a: i32) {}
33
fn two_arg_same(_a: i32, _b: i32) {}
44
fn two_arg_diff(_a: i32, _b: &str) {}
55

6+
macro_rules! foo {
7+
($x:expr) => {
8+
empty($x, 1); //~ ERROR function takes
9+
}
10+
}
11+
612
fn main() {
713
empty(""); //~ ERROR function takes
14+
empty(1, 1); //~ ERROR function takes
815

916
one_arg(1, 1); //~ ERROR function takes
1017
one_arg(1, ""); //~ ERROR function takes
@@ -32,4 +39,5 @@ fn main() {
3239
1,
3340
""
3441
);
42+
foo!(1);
3543
}

Diff for: tests/ui/argument-suggestions/extra_arguments.stderr

+54-15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0061]: this function takes 0 arguments but 1 argument was supplied
2-
--> $DIR/extra_arguments.rs:7:3
2+
--> $DIR/extra_arguments.rs:13:3
33
|
44
LL | empty("");
55
| ^^^^^ --
@@ -13,8 +13,27 @@ note: function defined here
1313
LL | fn empty() {}
1414
| ^^^^^
1515

16+
error[E0061]: this function takes 0 arguments but 2 arguments were supplied
17+
--> $DIR/extra_arguments.rs:14:3
18+
|
19+
LL | empty(1, 1);
20+
| ^^^^^ - - unexpected argument of type `{integer}`
21+
| |
22+
| unexpected argument of type `{integer}`
23+
|
24+
note: function defined here
25+
--> $DIR/extra_arguments.rs:1:4
26+
|
27+
LL | fn empty() {}
28+
| ^^^^^
29+
help: remove the extra arguments
30+
|
31+
LL - empty(1, 1);
32+
LL + empty();
33+
|
34+
1635
error[E0061]: this function takes 1 argument but 2 arguments were supplied
17-
--> $DIR/extra_arguments.rs:9:3
36+
--> $DIR/extra_arguments.rs:16:3
1837
|
1938
LL | one_arg(1, 1);
2039
| ^^^^^^^ ---
@@ -29,7 +48,7 @@ LL | fn one_arg(_a: i32) {}
2948
| ^^^^^^^ -------
3049

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

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

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

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

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

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

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

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

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

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

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

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

232-
error: aborting due to 14 previous errors
251+
error[E0061]: this function takes 0 arguments but 2 arguments were supplied
252+
--> $DIR/extra_arguments.rs:8:9
253+
|
254+
LL | empty($x, 1);
255+
| ^^^^^ - unexpected argument of type `{integer}`
256+
...
257+
LL | foo!(1);
258+
| -------
259+
| | |
260+
| | unexpected argument of type `{integer}`
261+
| | help: remove the extra argument
262+
| in this macro invocation
263+
|
264+
note: function defined here
265+
--> $DIR/extra_arguments.rs:1:4
266+
|
267+
LL | fn empty() {}
268+
| ^^^^^
269+
= note: this error originates in the macro `foo` (in Nightly builds, run with -Z macro-backtrace for more info)
270+
271+
error: aborting due to 16 previous errors
233272

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

Diff for: tests/ui/issues/issue-26094.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
macro_rules! some_macro {
2-
($other: expr) => ({
2+
($other: expr) => {{
33
$other(None) //~ NOTE unexpected argument of type `Option<_>`
4-
})
4+
}};
55
}
66

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

99
fn main() {
1010
some_macro!(some_function);
1111
//~^ ERROR function takes 0 arguments but 1 argument was supplied
12-
//~| NOTE in this expansion of some_macro!
1312
}

Diff for: tests/ui/issues/issue-26094.stderr

+1-4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@ error[E0061]: this function takes 0 arguments but 1 argument was supplied
22
--> $DIR/issue-26094.rs:10:17
33
|
44
LL | $other(None)
5-
| ----
6-
| |
7-
| unexpected argument of type `Option<_>`
8-
| help: remove the extra argument
5+
| ---- unexpected argument of type `Option<_>`
96
...
107
LL | some_macro!(some_function);
118
| ^^^^^^^^^^^^^

0 commit comments

Comments
 (0)