Skip to content

Commit 0b20096

Browse files
committed
sort the errors from arguments checking so that suggestions are handled properly
1 parent ed7281e commit 0b20096

File tree

3 files changed

+49
-3
lines changed

3 files changed

+49
-3
lines changed

compiler/rustc_hir_typeck/src/fn_ctxt/arg_matrix.rs

+10-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use std::cmp;
2-
1+
use core::cmp::Ordering;
32
use rustc_index::IndexVec;
43
use rustc_middle::ty::error::TypeError;
4+
use std::cmp;
55

66
rustc_index::newtype_index! {
77
#[debug_format = "ExpectedIdx({})"]
@@ -177,7 +177,7 @@ impl<'tcx> ArgMatrix<'tcx> {
177177
// If an argument is unsatisfied, and the input in its position is useless
178178
// then the most likely explanation is that we just got the types wrong
179179
(true, true, true, true) => return Some(Issue::Invalid(i)),
180-
// Otherwise, if an input is useless, then indicate that this is an extra argument
180+
// Otherwise, if an input is useless then indicate that this is an extra input
181181
(true, _, true, _) => return Some(Issue::Extra(i)),
182182
// Otherwise, if an argument is unsatisfiable, indicate that it's missing
183183
(_, true, _, true) => return Some(Issue::Missing(i)),
@@ -376,6 +376,13 @@ impl<'tcx> ArgMatrix<'tcx> {
376376
};
377377
}
378378

379+
// sort errors with same type by the order they appear in the source
380+
// so that suggestion will be handled properly, see #112507
381+
errors.sort_by(|a, b| match (a, b) {
382+
(Error::Missing(i), Error::Missing(j)) => i.cmp(j),
383+
(Error::Extra(i), Error::Extra(j)) => i.cmp(j),
384+
_ => Ordering::Equal,
385+
});
379386
return (errors, matched_inputs);
380387
}
381388
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
pub enum Value {
2+
Float(Option<f64>),
3+
}
4+
5+
fn main() {
6+
let _a = Value::Float( //~ ERROR this enum variant takes 1 argument but 4 arguments were supplied
7+
0,
8+
None,
9+
None,
10+
0,
11+
);
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
error[E0061]: this enum variant takes 1 argument but 4 arguments were supplied
2+
--> $DIR/issue-112507.rs:6:14
3+
|
4+
LL | let _a = Value::Float(
5+
| ^^^^^^^^^^^^
6+
LL | 0,
7+
| - unexpected argument of type `{integer}`
8+
LL | None,
9+
LL | None,
10+
| ---- unexpected argument of type `Option<_>`
11+
LL | 0,
12+
| - unexpected argument of type `{integer}`
13+
|
14+
note: tuple variant defined here
15+
--> $DIR/issue-112507.rs:2:5
16+
|
17+
LL | Float(Option<f64>),
18+
| ^^^^^
19+
help: remove the extra arguments
20+
|
21+
LL ~ ,
22+
LL ~ None);
23+
|
24+
25+
error: aborting due to previous error
26+
27+
For more information about this error, try `rustc --explain E0061`.

0 commit comments

Comments
 (0)