Skip to content

Commit 8febd42

Browse files
Use ordinal number in argument error
1 parent 0a59f11 commit 8febd42

File tree

1 file changed

+16
-2
lines changed
  • compiler/rustc_hir_typeck/src/fn_ctxt

1 file changed

+16
-2
lines changed

compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,6 +1014,19 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10141014
.peek()
10151015
.is_some_and(|first| matches!(first, Error::Extra(arg_idx) if arg_idx.index() == 0));
10161016
let mut suggestions = vec![];
1017+
1018+
// Convert the given number into the corresponding ordinal
1019+
// This is copied from rustc_resolve::diagnostics::ordinalize because it is too much to link rustc_resolve for this small function.
1020+
fn ordinalize(v: usize) -> String {
1021+
let suffix = match ((11..=13).contains(&(v % 100)), v % 10) {
1022+
(false, 1) => "st",
1023+
(false, 2) => "nd",
1024+
(false, 3) => "rd",
1025+
_ => "th",
1026+
};
1027+
format!("{v}{suffix}")
1028+
}
1029+
10171030
while let Some(error) = errors.next() {
10181031
only_extras_so_far &= matches!(error, Error::Extra(_));
10191032

@@ -1055,7 +1068,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10551068
} else {
10561069
"".to_string()
10571070
};
1058-
labels.push((provided_span, format!("unexpected argument{provided_ty_name}")));
1071+
labels.push((provided_span, format!("unexpected {} argument{provided_ty_name}", ordinalize(arg_idx.as_usize() + 1))));
10591072
let mut span = provided_span;
10601073
if span.can_be_used_for_suggestions()
10611074
&& error_span.can_be_used_for_suggestions()
@@ -1136,7 +1149,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
11361149
} else {
11371150
"".to_string()
11381151
};
1139-
labels.push((span, format!("an argument{rendered} is missing")));
1152+
labels.push((span, format!("{} argument{rendered} is missing", ordinalize(expected_idx.as_usize() + 1))));
1153+
11401154
suggestion_text = match suggestion_text {
11411155
SuggestionText::None => SuggestionText::Provide(false),
11421156
SuggestionText::Provide(_) => SuggestionText::Provide(true),

0 commit comments

Comments
 (0)