Skip to content

Commit

Permalink
Print note with closure signature on type mismatch
Browse files Browse the repository at this point in the history
  • Loading branch information
wutchzone committed Apr 19, 2024
1 parent 9a7b6fc commit 4dd72f1
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
8 changes: 8 additions & 0 deletions compiler/rustc_infer/src/infer/error_reporting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1917,6 +1917,14 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
);
if !is_simple_error || terr.must_include_note() {
diag.note_expected_found(&expected_label, expected, &found_label, found);

if let Some(expected_type_found) = exp_found {
if let ty::Closure(_, args) = expected_type_found.found.kind() {
diag.note_closure_signature(
args.as_closure().print_as_impl_trait().to_string(),
);
}
}
}
}
}
Expand Down
11 changes: 11 additions & 0 deletions tests/ui/inference/hint-closure-signature-119266.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
fn main() {
let x = |a: u8, b: (usize, u32), c: fn() -> char| -> String { "I love beans.".to_string() };
//~^ NOTE: the found closure

let x: fn(i32) = x;
//~^ ERROR: 5:22: 5:23: mismatched types [E0308]
//~| NOTE: incorrect number of function parameters
//~| NOTE: expected due to this
//~| NOTE: expected fn pointer `fn(i32)`
//~| NOTE: closure has signature: `impl Fn(u8, (usize, u32), fn() -> char) -> String`
}
18 changes: 18 additions & 0 deletions tests/ui/inference/hint-closure-signature-119266.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
error[E0308]: mismatched types
--> $DIR/hint-closure-signature-119266.rs:5:22
|
LL | let x = |a: u8, b: (usize, u32), c: fn() -> char| -> String { "I love beans.".to_string() };
| --------------------------------------------------- the found closure
...
LL | let x: fn(i32) = x;
| ------- ^ incorrect number of function parameters
| |
| expected due to this
|
= note: expected fn pointer `fn(i32)`
found closure `{closure@$DIR/hint-closure-signature-119266.rs:2:13: 2:64}`
= note: closure has signature: `impl Fn(u8, (usize, u32), fn() -> char) -> String`

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0308`.

0 comments on commit 4dd72f1

Please sign in to comment.