Skip to content

Commit c015382

Browse files
Use ordinal number in argument error
1 parent 336e89b commit c015382

32 files changed

+209
-85
lines changed

compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1053,6 +1053,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10531053
.peek()
10541054
.is_some_and(|first| matches!(first, Error::Extra(arg_idx) if arg_idx.index() == 0));
10551055
let mut suggestions = vec![];
1056+
10561057
while let Some(error) = errors.next() {
10571058
only_extras_so_far &= matches!(error, Error::Extra(_));
10581059

@@ -1094,7 +1095,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10941095
} else {
10951096
"".to_string()
10961097
};
1097-
labels.push((provided_span, format!("unexpected argument{provided_ty_name}")));
1098+
let ord = if provided_arg_tys.len() == 1 {
1099+
"".to_string()
1100+
} else {
1101+
format!("#{} ", arg_idx.as_usize() + 1)
1102+
};
1103+
labels.push((
1104+
provided_span,
1105+
format!("unexpected {ord}argument{provided_ty_name}"),
1106+
));
10981107
let mut span = provided_span;
10991108
if span.can_be_used_for_suggestions()
11001109
&& error_span.can_be_used_for_suggestions()
@@ -1175,7 +1184,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
11751184
} else {
11761185
"".to_string()
11771186
};
1178-
labels.push((span, format!("an argument{rendered} is missing")));
1187+
let ord = if formal_and_expected_inputs.len() == 1 {
1188+
"an".to_string()
1189+
} else {
1190+
format!("#{}", expected_idx.as_usize() + 1)
1191+
};
1192+
labels.push((span, format!("{ord} argument{rendered} is missing")));
1193+
11791194
suggestion_text = match suggestion_text {
11801195
SuggestionText::None => SuggestionText::Provide(false),
11811196
SuggestionText::Provide(_) => SuggestionText::Provide(true),

tests/ui/argument-suggestions/extern-fn-arg-names.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ error[E0061]: this function takes 2 arguments but 1 argument was supplied
88
--> $DIR/extern-fn-arg-names.rs:7:5
99
|
1010
LL | dstfn(1);
11-
| ^^^^^--- an argument is missing
11+
| ^^^^^--- #2 argument is missing
1212
|
1313
note: function defined here
1414
--> $DIR/extern-fn-arg-names.rs:2:8

tests/ui/argument-suggestions/extra_arguments.stderr

Lines changed: 123 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ error[E0061]: this function takes 0 arguments but 2 arguments were supplied
1919
--> $DIR/extra_arguments.rs:20:3
2020
|
2121
LL | empty(1, 1);
22-
| ^^^^^ - - unexpected argument of type `{integer}`
22+
| ^^^^^ - - unexpected #2 argument of type `{integer}`
2323
| |
24-
| unexpected argument of type `{integer}`
24+
| unexpected #1 argument of type `{integer}`
2525
|
2626
note: function defined here
2727
--> $DIR/extra_arguments.rs:1:4
@@ -38,7 +38,14 @@ error[E0061]: this function takes 1 argument but 2 arguments were supplied
3838
--> $DIR/extra_arguments.rs:22:3
3939
|
4040
LL | one_arg(1, 1);
41+
<<<<<<< HEAD
4142
| ^^^^^^^ - unexpected argument of type `{integer}`
43+
=======
44+
| ^^^^^^^ ---
45+
| | |
46+
| | unexpected #2 argument of type `{integer}`
47+
| help: remove the extra argument
48+
>>>>>>> c71324b4859 (Use ordinal number in argument error)
4249
|
4350
note: function defined here
4451
--> $DIR/extra_arguments.rs:2:4
@@ -55,7 +62,14 @@ error[E0061]: this function takes 1 argument but 2 arguments were supplied
5562
--> $DIR/extra_arguments.rs:23:3
5663
|
5764
LL | one_arg(1, "");
65+
<<<<<<< HEAD
5866
| ^^^^^^^ -- unexpected argument of type `&'static str`
67+
=======
68+
| ^^^^^^^ ----
69+
| | |
70+
| | unexpected #2 argument of type `&'static str`
71+
| help: remove the extra argument
72+
>>>>>>> c71324b4859 (Use ordinal number in argument error)
5973
|
6074
note: function defined here
6175
--> $DIR/extra_arguments.rs:2:4
@@ -72,9 +86,9 @@ error[E0061]: this function takes 1 argument but 3 arguments were supplied
7286
--> $DIR/extra_arguments.rs:24:3
7387
|
7488
LL | one_arg(1, "", 1.0);
75-
| ^^^^^^^ -- --- unexpected argument of type `{float}`
89+
| ^^^^^^^ -- --- unexpected #3 argument of type `{float}`
7690
| |
77-
| unexpected argument of type `&'static str`
91+
| unexpected #2 argument of type `&'static str`
7892
|
7993
note: function defined here
8094
--> $DIR/extra_arguments.rs:2:4
@@ -91,7 +105,14 @@ error[E0061]: this function takes 2 arguments but 3 arguments were supplied
91105
--> $DIR/extra_arguments.rs:26:3
92106
|
93107
LL | two_arg_same(1, 1, 1);
108+
<<<<<<< HEAD
94109
| ^^^^^^^^^^^^ - unexpected argument of type `{integer}`
110+
=======
111+
| ^^^^^^^^^^^^ ---
112+
| | |
113+
| | unexpected #3 argument of type `{integer}`
114+
| help: remove the extra argument
115+
>>>>>>> c71324b4859 (Use ordinal number in argument error)
95116
|
96117
note: function defined here
97118
--> $DIR/extra_arguments.rs:3:4
@@ -108,7 +129,14 @@ error[E0061]: this function takes 2 arguments but 3 arguments were supplied
108129
--> $DIR/extra_arguments.rs:27:3
109130
|
110131
LL | two_arg_same(1, 1, 1.0);
132+
<<<<<<< HEAD
111133
| ^^^^^^^^^^^^ --- unexpected argument of type `{float}`
134+
=======
135+
| ^^^^^^^^^^^^ -----
136+
| | |
137+
| | unexpected #3 argument of type `{float}`
138+
| help: remove the extra argument
139+
>>>>>>> c71324b4859 (Use ordinal number in argument error)
112140
|
113141
note: function defined here
114142
--> $DIR/extra_arguments.rs:3:4
@@ -125,7 +153,14 @@ error[E0061]: this function takes 2 arguments but 3 arguments were supplied
125153
--> $DIR/extra_arguments.rs:29:3
126154
|
127155
LL | two_arg_diff(1, 1, "");
156+
<<<<<<< HEAD
128157
| ^^^^^^^^^^^^ - unexpected argument of type `{integer}`
158+
=======
159+
| ^^^^^^^^^^^^ ---
160+
| | |
161+
| | unexpected #2 argument of type `{integer}`
162+
| help: remove the extra argument
163+
>>>>>>> c71324b4859 (Use ordinal number in argument error)
129164
|
130165
note: function defined here
131166
--> $DIR/extra_arguments.rs:4:4
@@ -142,7 +177,14 @@ error[E0061]: this function takes 2 arguments but 3 arguments were supplied
142177
--> $DIR/extra_arguments.rs:30:3
143178
|
144179
LL | two_arg_diff(1, "", "");
180+
<<<<<<< HEAD
145181
| ^^^^^^^^^^^^ -- unexpected argument of type `&'static str`
182+
=======
183+
| ^^^^^^^^^^^^ ----
184+
| | |
185+
| | unexpected #3 argument of type `&'static str`
186+
| help: remove the extra argument
187+
>>>>>>> c71324b4859 (Use ordinal number in argument error)
146188
|
147189
note: function defined here
148190
--> $DIR/extra_arguments.rs:4:4
@@ -159,9 +201,9 @@ error[E0061]: this function takes 2 arguments but 4 arguments were supplied
159201
--> $DIR/extra_arguments.rs:31:3
160202
|
161203
LL | two_arg_diff(1, 1, "", "");
162-
| ^^^^^^^^^^^^ - -- unexpected argument of type `&'static str`
204+
| ^^^^^^^^^^^^ - -- unexpected #4 argument of type `&'static str`
163205
| |
164-
| unexpected argument of type `{integer}`
206+
| unexpected #2 argument of type `{integer}`
165207
|
166208
note: function defined here
167209
--> $DIR/extra_arguments.rs:4:4
@@ -178,9 +220,9 @@ error[E0061]: this function takes 2 arguments but 4 arguments were supplied
178220
--> $DIR/extra_arguments.rs:32:3
179221
|
180222
LL | two_arg_diff(1, "", 1, "");
181-
| ^^^^^^^^^^^^ - -- unexpected argument of type `&'static str`
223+
| ^^^^^^^^^^^^ - -- unexpected #4 argument of type `&'static str`
182224
| |
183-
| unexpected argument of type `{integer}`
225+
| unexpected #3 argument of type `{integer}`
184226
|
185227
note: function defined here
186228
--> $DIR/extra_arguments.rs:4:4
@@ -197,7 +239,14 @@ error[E0061]: this function takes 2 arguments but 3 arguments were supplied
197239
--> $DIR/extra_arguments.rs:35:3
198240
|
199241
LL | two_arg_same(1, 1, "");
242+
<<<<<<< HEAD
200243
| ^^^^^^^^^^^^ -- unexpected argument of type `&'static str`
244+
=======
245+
| ^^^^^^^^^^^^ --------
246+
| | |
247+
| | unexpected #3 argument of type `&'static str`
248+
| help: remove the extra argument
249+
>>>>>>> c71324b4859 (Use ordinal number in argument error)
201250
|
202251
note: function defined here
203252
--> $DIR/extra_arguments.rs:3:4
@@ -214,7 +263,14 @@ error[E0061]: this function takes 2 arguments but 3 arguments were supplied
214263
--> $DIR/extra_arguments.rs:36:3
215264
|
216265
LL | two_arg_diff(1, 1, "");
266+
<<<<<<< HEAD
217267
| ^^^^^^^^^^^^ - unexpected argument of type `{integer}`
268+
=======
269+
| ^^^^^^^^^^^^ ---
270+
| | |
271+
| | unexpected #2 argument of type `{integer}`
272+
| help: remove the extra argument
273+
>>>>>>> c71324b4859 (Use ordinal number in argument error)
218274
|
219275
note: function defined here
220276
--> $DIR/extra_arguments.rs:4:4
@@ -230,11 +286,24 @@ LL + two_arg_diff(1, "");
230286
error[E0061]: this function takes 2 arguments but 3 arguments were supplied
231287
--> $DIR/extra_arguments.rs:37:3
232288
|
289+
<<<<<<< HEAD
233290
LL | two_arg_same(
234291
| ^^^^^^^^^^^^
235292
...
236293
LL | ""
237294
| -- unexpected argument of type `&'static str`
295+
=======
296+
LL | two_arg_same(
297+
| ^^^^^^^^^^^^
298+
LL | 1,
299+
LL | 1,
300+
| ______-
301+
LL | | ""
302+
| | --
303+
| |_____||
304+
| |help: remove the extra argument
305+
| unexpected #3 argument of type `&'static str`
306+
>>>>>>> c71324b4859 (Use ordinal number in argument error)
238307
|
239308
note: function defined here
240309
--> $DIR/extra_arguments.rs:3:4
@@ -251,11 +320,23 @@ LL + 1
251320
error[E0061]: this function takes 2 arguments but 3 arguments were supplied
252321
--> $DIR/extra_arguments.rs:43:3
253322
|
323+
<<<<<<< HEAD
254324
LL | two_arg_diff(
255325
| ^^^^^^^^^^^^
256326
LL | 1,
257327
LL | 1,
258328
| - unexpected argument of type `{integer}`
329+
=======
330+
LL | two_arg_diff(
331+
| ^^^^^^^^^^^^
332+
LL | 1,
333+
| ______-
334+
LL | | 1,
335+
| | -
336+
| | |
337+
| |_____unexpected #2 argument of type `{integer}`
338+
| help: remove the extra argument
339+
>>>>>>> c71324b4859 (Use ordinal number in argument error)
259340
|
260341
note: function defined here
261342
--> $DIR/extra_arguments.rs:4:4
@@ -271,12 +352,12 @@ error[E0061]: this function takes 0 arguments but 2 arguments were supplied
271352
--> $DIR/extra_arguments.rs:8:9
272353
|
273354
LL | empty($x, 1);
274-
| ^^^^^ - unexpected argument of type `{integer}`
355+
| ^^^^^ - unexpected #2 argument of type `{integer}`
275356
...
276357
LL | foo!(1, ~);
277358
| ----------
278359
| | |
279-
| | unexpected argument of type `{integer}`
360+
| | unexpected #1 argument of type `{integer}`
280361
| in this macro invocation
281362
|
282363
note: function defined here
@@ -290,12 +371,12 @@ error[E0061]: this function takes 0 arguments but 2 arguments were supplied
290371
--> $DIR/extra_arguments.rs:14:9
291372
|
292373
LL | empty(1, $y);
293-
| ^^^^^ - unexpected argument of type `{integer}`
374+
| ^^^^^ - unexpected #1 argument of type `{integer}`
294375
...
295376
LL | foo!(~, 1);
296377
| ----------
297378
| | |
298-
| | unexpected argument of type `{integer}`
379+
| | unexpected #2 argument of type `{integer}`
299380
| in this macro invocation
300381
|
301382
note: function defined here
@@ -314,8 +395,8 @@ LL | empty($x, $y);
314395
LL | foo!(1, 1);
315396
| ----------
316397
| | | |
317-
| | | unexpected argument of type `{integer}`
318-
| | unexpected argument of type `{integer}`
398+
| | | unexpected #2 argument of type `{integer}`
399+
| | unexpected #1 argument of type `{integer}`
319400
| in this macro invocation
320401
|
321402
note: function defined here
@@ -329,7 +410,14 @@ error[E0061]: this function takes 1 argument but 2 arguments were supplied
329410
--> $DIR/extra_arguments.rs:53:3
330411
|
331412
LL | one_arg(1, panic!());
413+
<<<<<<< HEAD
332414
| ^^^^^^^ -------- unexpected argument
415+
=======
416+
| ^^^^^^^ ----------
417+
| | |
418+
| | unexpected #2 argument
419+
| help: remove the extra argument
420+
>>>>>>> c71324b4859 (Use ordinal number in argument error)
333421
|
334422
note: function defined here
335423
--> $DIR/extra_arguments.rs:2:4
@@ -346,7 +434,14 @@ error[E0061]: this function takes 1 argument but 2 arguments were supplied
346434
--> $DIR/extra_arguments.rs:54:3
347435
|
348436
LL | one_arg(panic!(), 1);
437+
<<<<<<< HEAD
349438
| ^^^^^^^ - unexpected argument of type `{integer}`
439+
=======
440+
| ^^^^^^^ ---
441+
| | |
442+
| | unexpected #2 argument of type `{integer}`
443+
| help: remove the extra argument
444+
>>>>>>> c71324b4859 (Use ordinal number in argument error)
350445
|
351446
note: function defined here
352447
--> $DIR/extra_arguments.rs:2:4
@@ -363,7 +458,14 @@ error[E0061]: this function takes 1 argument but 2 arguments were supplied
363458
--> $DIR/extra_arguments.rs:55:3
364459
|
365460
LL | one_arg(stringify!($e), 1);
461+
<<<<<<< HEAD
366462
| ^^^^^^^ - unexpected argument of type `{integer}`
463+
=======
464+
| ^^^^^^^ ---
465+
| | |
466+
| | unexpected #2 argument of type `{integer}`
467+
| help: remove the extra argument
468+
>>>>>>> c71324b4859 (Use ordinal number in argument error)
367469
|
368470
note: function defined here
369471
--> $DIR/extra_arguments.rs:2:4
@@ -380,7 +482,14 @@ error[E0061]: this function takes 1 argument but 2 arguments were supplied
380482
--> $DIR/extra_arguments.rs:60:3
381483
|
382484
LL | one_arg(for _ in 1.. {}, 1);
485+
<<<<<<< HEAD
383486
| ^^^^^^^ - unexpected argument of type `{integer}`
487+
=======
488+
| ^^^^^^^ ---
489+
| | |
490+
| | unexpected #2 argument of type `{integer}`
491+
| help: remove the extra argument
492+
>>>>>>> c71324b4859 (Use ordinal number in argument error)
384493
|
385494
note: function defined here
386495
--> $DIR/extra_arguments.rs:2:4

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ error[E0061]: this function takes 3 arguments but 1 argument was supplied
44
LL | three_diff(T2::new(0));
55
| ^^^^^^^^^^------------
66
| ||
7-
| |an argument of type `T1` is missing
8-
| an argument of type `T3` is missing
7+
| |#1 argument of type `T1` is missing
8+
| #3 argument of type `T3` is missing
99
|
1010
note: function defined here
1111
--> $DIR/issue-100478.rs:30:4
@@ -63,7 +63,7 @@ LL | foo(
6363
| ^^^
6464
...
6565
LL | p3, p4, p5, p6, p7, p8,
66-
| -- an argument of type `Arc<T2>` is missing
66+
| -- #2 argument of type `Arc<T2>` is missing
6767
|
6868
note: function defined here
6969
--> $DIR/issue-100478.rs:29:4

0 commit comments

Comments
 (0)