Skip to content

Commit

Permalink
Use ordinal number in argument error
Browse files Browse the repository at this point in the history
  • Loading branch information
long-long-float committed May 26, 2024
1 parent 0a59f11 commit c71324b
Show file tree
Hide file tree
Showing 37 changed files with 123 additions and 108 deletions.
19 changes: 17 additions & 2 deletions compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1014,6 +1014,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
.peek()
.is_some_and(|first| matches!(first, Error::Extra(arg_idx) if arg_idx.index() == 0));
let mut suggestions = vec![];

while let Some(error) = errors.next() {
only_extras_so_far &= matches!(error, Error::Extra(_));

Expand Down Expand Up @@ -1055,7 +1056,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
} else {
"".to_string()
};
labels.push((provided_span, format!("unexpected argument{provided_ty_name}")));
let ord = if provided_arg_tys.len() == 1 {
"".to_string()
} else {
format!("#{} ", arg_idx.as_usize() + 1)
};
labels.push((
provided_span,
format!("unexpected {ord}argument{provided_ty_name}"),
));
let mut span = provided_span;
if span.can_be_used_for_suggestions()
&& error_span.can_be_used_for_suggestions()
Expand Down Expand Up @@ -1136,7 +1145,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
} else {
"".to_string()
};
labels.push((span, format!("an argument{rendered} is missing")));
let ord = if formal_and_expected_inputs.len() == 1 {
"an".to_string()
} else {
format!("#{}", expected_idx.as_usize() + 1)
};
labels.push((span, format!("{ord} argument{rendered} is missing")));

suggestion_text = match suggestion_text {
SuggestionText::None => SuggestionText::Provide(false),
SuggestionText::Provide(_) => SuggestionText::Provide(true),
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/argument-suggestions/extern-fn-arg-names.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ error[E0061]: this function takes 2 arguments but 1 argument was supplied
--> $DIR/extern-fn-arg-names.rs:7:5
|
LL | dstfn(1);
| ^^^^^--- an argument is missing
| ^^^^^--- #2 argument is missing
|
note: function defined here
--> $DIR/extern-fn-arg-names.rs:2:8
Expand Down
56 changes: 28 additions & 28 deletions tests/ui/argument-suggestions/extra_arguments.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ error[E0061]: this function takes 0 arguments but 2 arguments were supplied
--> $DIR/extra_arguments.rs:20:3
|
LL | empty(1, 1);
| ^^^^^ - - unexpected argument of type `{integer}`
| ^^^^^ - - unexpected #2 argument of type `{integer}`
| |
| unexpected argument of type `{integer}`
| unexpected #1 argument of type `{integer}`
|
note: function defined here
--> $DIR/extra_arguments.rs:1:4
Expand All @@ -38,7 +38,7 @@ error[E0061]: this function takes 1 argument but 2 arguments were supplied
LL | one_arg(1, 1);
| ^^^^^^^ ---
| | |
| | unexpected argument of type `{integer}`
| | unexpected #2 argument of type `{integer}`
| help: remove the extra argument
|
note: function defined here
Expand All @@ -53,7 +53,7 @@ error[E0061]: this function takes 1 argument but 2 arguments were supplied
LL | one_arg(1, "");
| ^^^^^^^ ----
| | |
| | unexpected argument of type `&'static str`
| | unexpected #2 argument of type `&'static str`
| help: remove the extra argument
|
note: function defined here
Expand All @@ -66,9 +66,9 @@ error[E0061]: this function takes 1 argument but 3 arguments were supplied
--> $DIR/extra_arguments.rs:24:3
|
LL | one_arg(1, "", 1.0);
| ^^^^^^^ -- --- unexpected argument of type `{float}`
| ^^^^^^^ -- --- unexpected #3 argument of type `{float}`
| |
| unexpected argument of type `&'static str`
| unexpected #2 argument of type `&'static str`
|
note: function defined here
--> $DIR/extra_arguments.rs:2:4
Expand All @@ -87,7 +87,7 @@ error[E0061]: this function takes 2 arguments but 3 arguments were supplied
LL | two_arg_same(1, 1, 1);
| ^^^^^^^^^^^^ ---
| | |
| | unexpected argument of type `{integer}`
| | unexpected #3 argument of type `{integer}`
| help: remove the extra argument
|
note: function defined here
Expand All @@ -102,7 +102,7 @@ error[E0061]: this function takes 2 arguments but 3 arguments were supplied
LL | two_arg_same(1, 1, 1.0);
| ^^^^^^^^^^^^ -----
| | |
| | unexpected argument of type `{float}`
| | unexpected #3 argument of type `{float}`
| help: remove the extra argument
|
note: function defined here
Expand All @@ -117,7 +117,7 @@ error[E0061]: this function takes 2 arguments but 3 arguments were supplied
LL | two_arg_diff(1, 1, "");
| ^^^^^^^^^^^^ ---
| | |
| | unexpected argument of type `{integer}`
| | unexpected #2 argument of type `{integer}`
| help: remove the extra argument
|
note: function defined here
Expand All @@ -132,7 +132,7 @@ error[E0061]: this function takes 2 arguments but 3 arguments were supplied
LL | two_arg_diff(1, "", "");
| ^^^^^^^^^^^^ ----
| | |
| | unexpected argument of type `&'static str`
| | unexpected #3 argument of type `&'static str`
| help: remove the extra argument
|
note: function defined here
Expand All @@ -145,9 +145,9 @@ error[E0061]: this function takes 2 arguments but 4 arguments were supplied
--> $DIR/extra_arguments.rs:31:3
|
LL | two_arg_diff(1, 1, "", "");
| ^^^^^^^^^^^^ - -- unexpected argument of type `&'static str`
| ^^^^^^^^^^^^ - -- unexpected #4 argument of type `&'static str`
| |
| unexpected argument of type `{integer}`
| unexpected #2 argument of type `{integer}`
|
note: function defined here
--> $DIR/extra_arguments.rs:4:4
Expand All @@ -164,9 +164,9 @@ error[E0061]: this function takes 2 arguments but 4 arguments were supplied
--> $DIR/extra_arguments.rs:32:3
|
LL | two_arg_diff(1, "", 1, "");
| ^^^^^^^^^^^^ - -- unexpected argument of type `&'static str`
| ^^^^^^^^^^^^ - -- unexpected #4 argument of type `&'static str`
| |
| unexpected argument of type `{integer}`
| unexpected #3 argument of type `{integer}`
|
note: function defined here
--> $DIR/extra_arguments.rs:4:4
Expand All @@ -185,7 +185,7 @@ error[E0061]: this function takes 2 arguments but 3 arguments were supplied
LL | two_arg_same(1, 1, "");
| ^^^^^^^^^^^^ --------
| | |
| | unexpected argument of type `&'static str`
| | unexpected #3 argument of type `&'static str`
| help: remove the extra argument
|
note: function defined here
Expand All @@ -200,7 +200,7 @@ error[E0061]: this function takes 2 arguments but 3 arguments were supplied
LL | two_arg_diff(1, 1, "");
| ^^^^^^^^^^^^ ---
| | |
| | unexpected argument of type `{integer}`
| | unexpected #2 argument of type `{integer}`
| help: remove the extra argument
|
note: function defined here
Expand All @@ -221,7 +221,7 @@ LL | | ""
| | --
| |_____||
| |help: remove the extra argument
| unexpected argument of type `&'static str`
| unexpected #3 argument of type `&'static str`
|
note: function defined here
--> $DIR/extra_arguments.rs:3:4
Expand All @@ -239,7 +239,7 @@ LL | 1,
LL | | 1,
| | -
| | |
| |_____unexpected argument of type `{integer}`
| |_____unexpected #2 argument of type `{integer}`
| help: remove the extra argument
|
note: function defined here
Expand All @@ -252,12 +252,12 @@ 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}`
| ^^^^^ - unexpected #2 argument of type `{integer}`
...
LL | foo!(1, ~);
| ----------
| | |
| | unexpected argument of type `{integer}`
| | unexpected #1 argument of type `{integer}`
| in this macro invocation
|
note: function defined here
Expand All @@ -271,12 +271,12 @@ error[E0061]: this function takes 0 arguments but 2 arguments were supplied
--> $DIR/extra_arguments.rs:14:9
|
LL | empty(1, $y);
| ^^^^^ - unexpected argument of type `{integer}`
| ^^^^^ - unexpected #1 argument of type `{integer}`
...
LL | foo!(~, 1);
| ----------
| | |
| | unexpected argument of type `{integer}`
| | unexpected #2 argument of type `{integer}`
| in this macro invocation
|
note: function defined here
Expand All @@ -295,8 +295,8 @@ LL | empty($x, $y);
LL | foo!(1, 1);
| ----------
| | | |
| | | unexpected argument of type `{integer}`
| | unexpected argument of type `{integer}`
| | | unexpected #2 argument of type `{integer}`
| | unexpected #1 argument of type `{integer}`
| in this macro invocation
|
note: function defined here
Expand All @@ -312,7 +312,7 @@ error[E0061]: this function takes 1 argument but 2 arguments were supplied
LL | one_arg(1, panic!());
| ^^^^^^^ ----------
| | |
| | unexpected argument
| | unexpected #2 argument
| help: remove the extra argument
|
note: function defined here
Expand All @@ -327,7 +327,7 @@ error[E0061]: this function takes 1 argument but 2 arguments were supplied
LL | one_arg(panic!(), 1);
| ^^^^^^^ ---
| | |
| | unexpected argument of type `{integer}`
| | unexpected #2 argument of type `{integer}`
| help: remove the extra argument
|
note: function defined here
Expand All @@ -342,7 +342,7 @@ error[E0061]: this function takes 1 argument but 2 arguments were supplied
LL | one_arg(stringify!($e), 1);
| ^^^^^^^ ---
| | |
| | unexpected argument of type `{integer}`
| | unexpected #2 argument of type `{integer}`
| help: remove the extra argument
|
note: function defined here
Expand All @@ -357,7 +357,7 @@ error[E0061]: this function takes 1 argument but 2 arguments were supplied
LL | one_arg(for _ in 1.. {}, 1);
| ^^^^^^^ ---
| | |
| | unexpected argument of type `{integer}`
| | unexpected #2 argument of type `{integer}`
| help: remove the extra argument
|
note: function defined here
Expand Down
6 changes: 3 additions & 3 deletions tests/ui/argument-suggestions/issue-100478.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ error[E0061]: this function takes 3 arguments but 1 argument was supplied
LL | three_diff(T2::new(0));
| ^^^^^^^^^^------------
| ||
| |an argument of type `T1` is missing
| an argument of type `T3` is missing
| |#1 argument of type `T1` is missing
| #3 argument of type `T3` is missing
|
note: function defined here
--> $DIR/issue-100478.rs:30:4
Expand Down Expand Up @@ -63,7 +63,7 @@ LL | foo(
| ^^^
...
LL | p3, p4, p5, p6, p7, p8,
| -- an argument of type `Arc<T2>` is missing
| -- #2 argument of type `Arc<T2>` is missing
|
note: function defined here
--> $DIR/issue-100478.rs:29:4
Expand Down
6 changes: 3 additions & 3 deletions tests/ui/argument-suggestions/issue-101097.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0061]: this function takes 6 arguments but 7 arguments were supplied
LL | f(C, A, A, A, B, B, C);
| ^ - - - - expected `C`, found `B`
| | | |
| | | unexpected argument of type `A`
| | | unexpected #4 argument of type `A`
| | expected `B`, found `A`
| expected `A`, found `C`
|
Expand Down Expand Up @@ -64,8 +64,8 @@ error[E0308]: arguments to this function are incorrect
LL | f(A, A, D, D, B, B);
| ^ - - ---- two arguments of type `C` and `C` are missing
| | |
| | unexpected argument of type `D`
| unexpected argument of type `D`
| | unexpected #4 argument of type `D`
| unexpected #3 argument of type `D`
|
note: function defined here
--> $DIR/issue-101097.rs:6:4
Expand Down
20 changes: 10 additions & 10 deletions tests/ui/argument-suggestions/issue-109425.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ error[E0061]: this function takes 0 arguments but 2 arguments were supplied
--> $DIR/issue-109425.rs:10:5
|
LL | f(0, 1,); // f()
| ^ - - unexpected argument of type `{integer}`
| ^ - - unexpected #2 argument of type `{integer}`
| |
| unexpected argument of type `{integer}`
| unexpected #1 argument of type `{integer}`
|
note: function defined here
--> $DIR/issue-109425.rs:3:4
Expand All @@ -21,9 +21,9 @@ error[E0061]: this function takes 1 argument but 3 arguments were supplied
--> $DIR/issue-109425.rs:12:5
|
LL | i(0, 1, 2,); // i(0,)
| ^ - - unexpected argument of type `{integer}`
| ^ - - unexpected #3 argument of type `{integer}`
| |
| unexpected argument of type `{integer}`
| unexpected #2 argument of type `{integer}`
|
note: function defined here
--> $DIR/issue-109425.rs:4:4
Expand All @@ -40,9 +40,9 @@ error[E0061]: this function takes 1 argument but 3 arguments were supplied
--> $DIR/issue-109425.rs:14:5
|
LL | i(0, 1, 2); // i(0)
| ^ - - unexpected argument of type `{integer}`
| ^ - - unexpected #3 argument of type `{integer}`
| |
| unexpected argument of type `{integer}`
| unexpected #2 argument of type `{integer}`
|
note: function defined here
--> $DIR/issue-109425.rs:4:4
Expand All @@ -59,9 +59,9 @@ error[E0061]: this function takes 2 arguments but 4 arguments were supplied
--> $DIR/issue-109425.rs:16:5
|
LL | is(0, 1, 2, ""); // is(0, "")
| ^^ - - unexpected argument of type `{integer}`
| ^^ - - unexpected #3 argument of type `{integer}`
| |
| unexpected argument of type `{integer}`
| unexpected #2 argument of type `{integer}`
|
note: function defined here
--> $DIR/issue-109425.rs:5:4
Expand All @@ -78,9 +78,9 @@ error[E0061]: this function takes 1 argument but 3 arguments were supplied
--> $DIR/issue-109425.rs:18:5
|
LL | s(0, 1, ""); // s("")
| ^ - - unexpected argument of type `{integer}`
| ^ - - unexpected #2 argument of type `{integer}`
| |
| unexpected argument of type `{integer}`
| unexpected #1 argument of type `{integer}`
|
note: function defined here
--> $DIR/issue-109425.rs:6:4
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/argument-suggestions/issue-109831.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ error[E0061]: this function takes 3 arguments but 4 arguments were supplied
--> $DIR/issue-109831.rs:7:5
|
LL | f(A, A, B, C);
| ^ - - - unexpected argument
| ^ - - - unexpected #4 argument
| | |
| | expected `B`, found `A`
| expected `B`, found `A`
Expand Down
6 changes: 3 additions & 3 deletions tests/ui/argument-suggestions/issue-112507.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ error[E0061]: this enum variant takes 1 argument but 4 arguments were supplied
LL | let _a = Value::Float(
| ^^^^^^^^^^^^
LL | 0,
| - unexpected argument of type `{integer}`
| - unexpected #1 argument of type `{integer}`
LL | None,
LL | None,
| ---- unexpected argument of type `Option<_>`
| ---- unexpected #3 argument of type `Option<_>`
LL | 0,
| - unexpected argument of type `{integer}`
| - unexpected #4 argument of type `{integer}`
|
note: tuple variant defined here
--> $DIR/issue-112507.rs:2:5
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/argument-suggestions/issue-96638.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0061]: this function takes 3 arguments but 2 arguments were supplied
LL | f(&x, "");
| ^ -- -- expected `usize`, found `&str`
| |
| an argument of type `usize` is missing
| #1 argument of type `usize` is missing
|
note: function defined here
--> $DIR/issue-96638.rs:1:4
Expand Down
Loading

0 comments on commit c71324b

Please sign in to comment.