Skip to content

Commit 9337d4f

Browse files
committed
Print closure signatures when reporting placeholder errors
1 parent 94c11df commit 9337d4f

File tree

5 files changed

+38
-12
lines changed

5 files changed

+38
-12
lines changed

compiler/rustc_infer/src/infer/error_reporting/nice_region_error/placeholder_error.rs

+32-6
Original file line numberDiff line numberDiff line change
@@ -386,12 +386,38 @@ impl NiceRegionError<'me, 'tcx> {
386386
let mut note = if same_self_type {
387387
let mut self_ty = expected_trait_ref.map(|tr| tr.self_ty());
388388
self_ty.highlight.maybe_highlighting_region(vid, actual_has_vid);
389-
format!(
390-
"{}`{}` must implement `{}`",
391-
if leading_ellipsis { "..." } else { "" },
392-
self_ty,
393-
expected_trait_ref.map(|tr| tr.print_only_trait_path()),
394-
)
389+
390+
if self_ty.value.is_closure()
391+
&& self
392+
.tcx()
393+
.fn_trait_kind_from_lang_item(expected_trait_ref.value.def_id)
394+
.is_some()
395+
{
396+
let closure_sig = self_ty.map(|closure| {
397+
if let ty::Closure(_, substs) = closure.kind() {
398+
self.tcx().signature_unclosure(
399+
substs.as_closure().sig(),
400+
rustc_hir::Unsafety::Normal,
401+
)
402+
} else {
403+
bug!("type is not longer closure");
404+
}
405+
});
406+
407+
format!(
408+
"{}closure with signature `{}` must implement `{}`",
409+
if leading_ellipsis { "..." } else { "" },
410+
closure_sig,
411+
expected_trait_ref.map(|tr| tr.print_only_trait_path()),
412+
)
413+
} else {
414+
format!(
415+
"{}`{}` must implement `{}`",
416+
if leading_ellipsis { "..." } else { "" },
417+
self_ty,
418+
expected_trait_ref.map(|tr| tr.print_only_trait_path()),
419+
)
420+
}
395421
} else if passive_voice {
396422
format!(
397423
"{}`{}` would have to be implemented for the type `{}`",

src/test/ui/issues/issue-57843.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error: implementation of `FnOnce` is not general enough
44
LL | Foo(Box::new(|_| ()));
55
| ^^^^^^^^^^^^^^^^ implementation of `FnOnce` is not general enough
66
|
7-
= note: `[closure@$DIR/issue-57843.rs:25:18: 25:24]` must implement `FnOnce<(&'1 bool,)>`, for any lifetime `'1`...
7+
= note: closure with signature `fn(&'2 bool)` must implement `FnOnce<(&'1 bool,)>`, for any lifetime `'1`...
88
= note: ...but it actually implements `FnOnce<(&'2 bool,)>`, for some specific lifetime `'2`
99

1010
error: aborting due to previous error

src/test/ui/lifetimes/issue-79187.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error: implementation of `FnOnce` is not general enough
44
LL | thing(f);
55
| ^^^^^ implementation of `FnOnce` is not general enough
66
|
7-
= note: `[closure@$DIR/issue-79187.rs:4:13: 4:19]` must implement `FnOnce<(&'1 u32,)>`, for any lifetime `'1`...
7+
= note: closure with signature `fn(&'2 u32)` must implement `FnOnce<(&'1 u32,)>`, for any lifetime `'1`...
88
= note: ...but it actually implements `FnOnce<(&'2 u32,)>`, for some specific lifetime `'2`
99

1010
error: aborting due to previous error

src/test/ui/type-alias-impl-trait/issue-57611-trait-alias.nll.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ error: implementation of `FnOnce` is not general enough
3030
LL | type Bar = impl Baz<Self, Self>;
3131
| ^^^^^^^^^^^^^^^^^^^^ implementation of `FnOnce` is not general enough
3232
|
33-
= note: `[closure@$DIR/issue-57611-trait-alias.rs:25:9: 25:14]` must implement `FnOnce<(&'1 X,)>`, for any lifetime `'1`...
33+
= note: closure with signature `fn(&'2 X) -> &'2 X` must implement `FnOnce<(&'1 X,)>`, for any lifetime `'1`...
3434
= note: ...but it actually implements `FnOnce<(&'2 X,)>`, for some specific lifetime `'2`
3535

3636
error: aborting due to 4 previous errors

src/test/ui/type-alias-impl-trait/issue-57611-trait-alias.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error: implementation of `FnOnce` is not general enough
44
LL | type Bar = impl Baz<Self, Self>;
55
| ^^^^^^^^^^^^^^^^^^^^ implementation of `FnOnce` is not general enough
66
|
7-
= note: `[closure@$DIR/issue-57611-trait-alias.rs:25:9: 25:14]` must implement `FnOnce<(&'1 X,)>`, for any lifetime `'1`...
7+
= note: closure with signature `fn(&'2 X) -> &X` must implement `FnOnce<(&'1 X,)>`, for any lifetime `'1`...
88
= note: ...but it actually implements `FnOnce<(&'2 X,)>`, for some specific lifetime `'2`
99

1010
error[E0308]: mismatched types
@@ -27,7 +27,7 @@ error: implementation of `FnOnce` is not general enough
2727
LL | type Bar = impl Baz<Self, Self>;
2828
| ^^^^^^^^^^^^^^^^^^^^ implementation of `FnOnce` is not general enough
2929
|
30-
= note: `[closure@$DIR/issue-57611-trait-alias.rs:25:9: 25:14]` must implement `FnOnce<(&'1 X,)>`, for any lifetime `'1`...
30+
= note: closure with signature `fn(&'2 X) -> &'2 X` must implement `FnOnce<(&'1 X,)>`, for any lifetime `'1`...
3131
= note: ...but it actually implements `FnOnce<(&'2 X,)>`, for some specific lifetime `'2`
3232

3333
error[E0308]: mismatched types
@@ -50,7 +50,7 @@ error: implementation of `FnOnce` is not general enough
5050
LL | type Bar = impl Baz<Self, Self>;
5151
| ^^^^^^^^^^^^^^^^^^^^ implementation of `FnOnce` is not general enough
5252
|
53-
= note: `[closure@$DIR/issue-57611-trait-alias.rs:25:9: 25:14]` must implement `FnOnce<(&'1 X,)>`, for any lifetime `'1`...
53+
= note: closure with signature `fn(&'2 X) -> &'2 X` must implement `FnOnce<(&'1 X,)>`, for any lifetime `'1`...
5454
= note: ...but it actually implements `FnOnce<(&'2 X,)>`, for some specific lifetime `'2`
5555

5656
error: aborting due to 5 previous errors

0 commit comments

Comments
 (0)