Skip to content

Commit 8b21f79

Browse files
committed
Print note with closure signature on type mismatch
1 parent 5adde18 commit 8b21f79

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

compiler/rustc_infer/src/infer/error_reporting/mod.rs

+8
Original file line numberDiff line numberDiff line change
@@ -1917,6 +1917,14 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
19171917
);
19181918
if !is_simple_error || terr.must_include_note() {
19191919
diag.note_expected_found(&expected_label, expected, &found_label, found);
1920+
1921+
if let Some(expected_type_found) = exp_found {
1922+
if let ty::Closure(_, args) = expected_type_found.found.kind() {
1923+
diag.note_closure_signature(
1924+
args.as_closure().print_signature().to_string(),
1925+
);
1926+
}
1927+
}
19201928
}
19211929
}
19221930
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
fn main() {
2+
let x = |a: u8, b: (usize, u32), c: fn() -> char| -> String { "I love beans.".to_string() };
3+
//~^ NOTE: the found closure
4+
5+
let x: fn(i32) = x;
6+
//~^ ERROR: 5:22: 5:23: mismatched types [E0308]
7+
//~| NOTE: incorrect number of function parameters
8+
//~| NOTE: expected due to this
9+
//~| NOTE: expected fn pointer `fn(i32)`
10+
//~| NOTE: closure has signature: `fn(u8, (usize, u32), fn() -> char) -> String`
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/hint-closure-signature-119266.rs:5:22
3+
|
4+
LL | let x = |a: u8, b: (usize, u32), c: fn() -> char| -> String { "I love beans.".to_string() };
5+
| --------------------------------------------------- the found closure
6+
...
7+
LL | let x: fn(i32) = x;
8+
| ------- ^ incorrect number of function parameters
9+
| |
10+
| expected due to this
11+
|
12+
= note: expected fn pointer `fn(i32)`
13+
found closure `{closure@$DIR/hint-closure-signature-119266.rs:2:13: 2:64}`
14+
= note: closure has signature: `fn(u8, (usize, u32), fn() -> char) -> String`
15+
16+
error: aborting due to 1 previous error
17+
18+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)