Skip to content

Commit 1383f0e

Browse files
committed
Make the trait bound is not satisfied specify kind
1 parent 7a42ca9 commit 1383f0e

18 files changed

+111
-39
lines changed

compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -450,12 +450,27 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
450450
{
451451
"consider using `()`, or a `Result`".to_owned()
452452
} else {
453-
format!(
454-
"{}the trait `{}` is not implemented for `{}`",
455-
pre_message,
456-
trait_predicate.print_modifiers_and_trait_path(),
457-
trait_ref.skip_binder().self_ty(),
458-
)
453+
let ty_desc = match trait_ref.skip_binder().self_ty().kind() {
454+
ty::FnDef(_, _) => Some("fn item"),
455+
ty::Closure(_, _) => Some("closure"),
456+
_ => None,
457+
};
458+
459+
match ty_desc {
460+
Some(desc) => format!(
461+
"{}the trait `{}` is not implemented for {} `{}`",
462+
pre_message,
463+
trait_predicate.print_modifiers_and_trait_path(),
464+
desc,
465+
trait_ref.skip_binder().self_ty(),
466+
),
467+
None => format!(
468+
"{}the trait `{}` is not implemented for `{}`",
469+
pre_message,
470+
trait_predicate.print_modifiers_and_trait_path(),
471+
trait_ref.skip_binder().self_ty(),
472+
),
473+
}
459474
};
460475

461476
if self.suggest_add_reference_to_arg(
@@ -1805,13 +1820,21 @@ impl<'a, 'tcx> InferCtxtPrivExt<'a, 'tcx> for InferCtxt<'a, 'tcx> {
18051820
return false;
18061821
}
18071822
if candidates.len() == 1 {
1823+
let ty_desc = match candidates[0].self_ty().kind() {
1824+
ty::FnPtr(_) => Some("fn pointer"),
1825+
_ => None,
1826+
};
1827+
let the_desc = match ty_desc {
1828+
Some(desc) => format!(" implemented for {} `", desc),
1829+
None => " implemented for `".to_string(),
1830+
};
18081831
err.highlighted_help(vec![
18091832
(
18101833
format!("the trait `{}` ", candidates[0].print_only_trait_path()),
18111834
Style::NoStyle,
18121835
),
18131836
("is".to_string(), Style::Highlight),
1814-
(" implemented for `".to_string(), Style::NoStyle),
1837+
(the_desc, Style::NoStyle),
18151838
(candidates[0].self_ty().to_string(), Style::Highlight),
18161839
("`".to_string(), Style::NoStyle),
18171840
]);

src/test/ui/async-await/issues/issue-62009-1.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ error[E0277]: `[closure@$DIR/issue-62009-1.rs:12:6: 12:9]` is not a future
3030
LL | (|_| 2333).await;
3131
| ^^^^^^ `[closure@$DIR/issue-62009-1.rs:12:6: 12:9]` is not a future
3232
|
33-
= help: the trait `Future` is not implemented for `[closure@$DIR/issue-62009-1.rs:12:6: 12:9]`
33+
= help: the trait `Future` is not implemented for closure `[closure@$DIR/issue-62009-1.rs:12:6: 12:9]`
3434
= note: [closure@$DIR/issue-62009-1.rs:12:6: 12:9] must be a future or must implement `IntoFuture` to be awaited
3535
= note: required for `[closure@$DIR/issue-62009-1.rs:12:6: 12:9]` to implement `IntoFuture`
3636
help: remove the `.await`

src/test/ui/binop/issue-77910-1.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ LL | fn foo(s: &i32) -> &i32 {
1818
LL | assert_eq!(foo, y);
1919
| ^^^^^^^^^^^^^^^^^^ `for<'r> fn(&'r i32) -> &'r i32 {foo}` cannot be formatted using `{:?}` because it doesn't implement `Debug`
2020
|
21-
= help: the trait `Debug` is not implemented for `for<'r> fn(&'r i32) -> &'r i32 {foo}`
21+
= help: the trait `Debug` is not implemented for fn item `for<'r> fn(&'r i32) -> &'r i32 {foo}`
2222
= help: use parentheses to call the function: `foo(s)`
2323
= note: this error originates in the macro `assert_eq` (in Nightly builds, run with -Z macro-backtrace for more info)
2424

src/test/ui/closures/coerce-unsafe-to-closure.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ LL | let x: Option<&[u8]> = Some("foo").map(std::mem::transmute);
66
| |
77
| required by a bound introduced by this call
88
|
9-
= help: the trait `FnOnce<(&str,)>` is not implemented for `unsafe extern "rust-intrinsic" fn(_) -> _ {transmute::<_, _>}`
9+
= help: the trait `FnOnce<(&str,)>` is not implemented for fn item `unsafe extern "rust-intrinsic" fn(_) -> _ {transmute::<_, _>}`
1010
= note: unsafe function cannot be called generically without an unsafe block
1111
note: required by a bound in `Option::<T>::map`
1212
--> $SRC_DIR/core/src/option.rs:LL:COL

src/test/ui/extern/extern-wrong-value-type.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ LL | is_fn(f);
66
| |
77
| required by a bound introduced by this call
88
|
9-
= help: the trait `Fn<()>` is not implemented for `extern "C" fn() {f}`
9+
= help: the trait `Fn<()>` is not implemented for fn item `extern "C" fn() {f}`
1010
= note: wrap the `extern "C" fn() {f}` in a closure with no arguments: `|| { /* code */ }`
1111
note: required by a bound in `is_fn`
1212
--> $DIR/extern-wrong-value-type.rs:4:28

src/test/ui/intrinsics/const-eval-select-bad.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ LL | const_eval_select((), || {}, || {});
66
| |
77
| required by a bound introduced by this call
88
|
9-
= help: the trait `~const FnOnce<()>` is not implemented for `[closure@$DIR/const-eval-select-bad.rs:7:27: 7:29]`
9+
= help: the trait `~const FnOnce<()>` is not implemented for closure `[closure@$DIR/const-eval-select-bad.rs:7:27: 7:29]`
1010
note: the trait `FnOnce<()>` is implemented for `[closure@$DIR/const-eval-select-bad.rs:7:27: 7:29]`, but that implementation is not `const`
1111
--> $DIR/const-eval-select-bad.rs:7:27
1212
|

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ error[E0277]: `fn(usize) -> Foo {Foo::Bar}` doesn't implement `Debug`
9393
LL | assert_eq!(Foo::Bar, i);
9494
| ^^^^^^^^^^^^^^^^^^^^^^^ `fn(usize) -> Foo {Foo::Bar}` cannot be formatted using `{:?}` because it doesn't implement `Debug`
9595
|
96-
= help: the trait `Debug` is not implemented for `fn(usize) -> Foo {Foo::Bar}`
96+
= help: the trait `Debug` is not implemented for fn item `fn(usize) -> Foo {Foo::Bar}`
9797
= help: the following other types implement trait `Debug`:
9898
extern "C" fn() -> Ret
9999
extern "C" fn(A, B) -> Ret
@@ -112,7 +112,7 @@ error[E0277]: `fn(usize) -> Foo {Foo::Bar}` doesn't implement `Debug`
112112
LL | assert_eq!(Foo::Bar, i);
113113
| ^^^^^^^^^^^^^^^^^^^^^^^ `fn(usize) -> Foo {Foo::Bar}` cannot be formatted using `{:?}` because it doesn't implement `Debug`
114114
|
115-
= help: the trait `Debug` is not implemented for `fn(usize) -> Foo {Foo::Bar}`
115+
= help: the trait `Debug` is not implemented for fn item `fn(usize) -> Foo {Foo::Bar}`
116116
= help: the following other types implement trait `Debug`:
117117
extern "C" fn() -> Ret
118118
extern "C" fn(A, B) -> Ret

src/test/ui/issues/issue-70724-add_type_neq_err_label-unwrap.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ LL | fn a() -> i32 {
3333
LL | assert_eq!(a, 0);
3434
| ^^^^^^^^^^^^^^^^ `fn() -> i32 {a}` cannot be formatted using `{:?}` because it doesn't implement `Debug`
3535
|
36-
= help: the trait `Debug` is not implemented for `fn() -> i32 {a}`
36+
= help: the trait `Debug` is not implemented for fn item `fn() -> i32 {a}`
3737
= help: use parentheses to call the function: `a()`
3838
= note: this error originates in the macro `assert_eq` (in Nightly builds, run with -Z macro-backtrace for more info)
3939

src/test/ui/issues/issue-99875.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
struct Argument;
2+
struct Return;
3+
4+
fn function(_: Argument) -> Return { todo!() }
5+
6+
trait Trait {}
7+
impl Trait for fn(Argument) -> Return {}
8+
9+
fn takes(_: impl Trait) {}
10+
11+
fn main() {
12+
takes(function);
13+
//~^ ERROR the trait bound
14+
takes(|_: Argument| -> Return { todo!() });
15+
//~^ ERROR the trait bound
16+
}

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
error[E0277]: the trait bound `fn(Argument) -> Return {function}: Trait` is not satisfied
2+
--> $DIR/issue-99875.rs:12:11
3+
|
4+
LL | takes(function);
5+
| ----- ^^^^^^^^ the trait `Trait` is not implemented for fn item `fn(Argument) -> Return {function}`
6+
| |
7+
| required by a bound introduced by this call
8+
|
9+
= help: the trait `Trait` is implemented for fn pointer `fn(Argument) -> Return`
10+
note: required by a bound in `takes`
11+
--> $DIR/issue-99875.rs:9:18
12+
|
13+
LL | fn takes(_: impl Trait) {}
14+
| ^^^^^ required by this bound in `takes`
15+
16+
error[E0277]: the trait bound `[closure@$DIR/issue-99875.rs:14:11: 14:34]: Trait` is not satisfied
17+
--> $DIR/issue-99875.rs:14:11
18+
|
19+
LL | takes(|_: Argument| -> Return { todo!() });
20+
| ----- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Trait` is not implemented for closure `[closure@$DIR/issue-99875.rs:14:11: 14:34]`
21+
| |
22+
| required by a bound introduced by this call
23+
|
24+
= help: the trait `Trait` is implemented for fn pointer `fn(Argument) -> Return`
25+
note: required by a bound in `takes`
26+
--> $DIR/issue-99875.rs:9:18
27+
|
28+
LL | fn takes(_: impl Trait) {}
29+
| ^^^^^ required by this bound in `takes`
30+
31+
error: aborting due to 2 previous errors
32+
33+
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)