Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unsatisfied trait bound on fn item should suggest as fn() if it is satisfied for fn pointer #99922

Closed
CAD97 opened this issue Jul 29, 2022 · 2 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@CAD97
Copy link
Contributor

CAD97 commented Jul 29, 2022

Given the following code: [playground]

trait Trait {}
impl Trait for fn() {}
fn take(_: impl Trait) {}

fn f() {}
fn main() {
    take(f);
}

The current output is:

error[[E0277]](https://doc.rust-lang.org/stable/error-index.html#E0277): the trait bound `fn() {f}: Trait` is not satisfied
 --> src/main.rs:7:10
  |
7 |     take(f);
  |     ---- ^ the trait `Trait` is not implemented for `fn() {f}`
  |     |
  |     required by a bound introduced by this call
  |
  = help: the trait `Trait` is implemented for `fn()`
note: required by a bound in `take`
 --> src/main.rs:3:17
  |
3 | fn take(_: impl Trait) {}
  |                 ^^^^^ required by this bound in `take`

Ideally the output should look like:

error[[E0277]](https://doc.rust-lang.org/stable/error-index.html#E0277): the trait bound `fn() {f}: Trait` is not satisfied
 --> src/main.rs:7:10
  |
7 |     take(f);
  |     ---- ^ the trait `Trait` is not implemented for fn item `fn() {f}`
  |     |
  |     required by a bound introduced by this call
  |
  = help: the trait `Trait` is implemented for fn pointer `fn()`
note: required by a bound in `take`
 --> src/main.rs:3:17
  |
3 | fn take(_: impl Trait) {}
  |                 ^^^^^ required by this bound in `take`
  |
help: Convert the fn item to a fn pointer with `as`
  |
7 |     take(f as fn());
  |           ++++++++
A more involved diagnostic example:

[playground]

trait Trait {}
impl<A: PartialEq> Trait for A {}
fn take(_: impl Trait) {}

fn f() {}
fn main() {
    take(f);
}

The current output is:

error[[E0277]](https://doc.rust-lang.org/nightly/error-index.html#E0277): can't compare `fn() {f}` with `fn() {f}`
 --> src/main.rs:7:10
  |
7 |     take(f);
  |     ---- ^ no implementation for `fn() {f} == fn() {f}`
  |     |
  |     required by a bound introduced by this call
  |
  = help: the trait `PartialEq` is not implemented for `fn() {f}`
  = help: the following other types implement trait `PartialEq<Rhs>`:
            extern "C" fn() -> Ret
            extern "C" fn(A, B) -> Ret
            extern "C" fn(A, B, ...) -> Ret
            extern "C" fn(A, B, C) -> Ret
            extern "C" fn(A, B, C, ...) -> Ret
            extern "C" fn(A, B, C, D) -> Ret
            extern "C" fn(A, B, C, D, ...) -> Ret
            extern "C" fn(A, B, C, D, E) -> Ret
          and 68 others
note: required because of the requirements on the impl of `Trait` for `fn() {f}`
 --> src/main.rs:2:20
  |
2 | impl<A: PartialEq> Trait for A {}
  |                    ^^^^^     ^
note: required by a bound in `take`
 --> src/main.rs:3:17
  |
3 | fn take(_: impl Trait) {}
  |                 ^^^^^ required by this bound in `take`

Ideally the output should look like:

error[[E0277]](https://doc.rust-lang.org/nightly/error-index.html#E0277): can't compare `fn() {f}` with `fn() {f}`
 --> src/main.rs:7:10
  |
7 |     take(f);
  |     ---- ^ no implementation for `fn() {f} == fn() {f}`
  |     |
  |     required by a bound introduced by this call
  |
  = help: the trait `PartialEq` is not implemented for fn item `fn() {f}`
  = help: the trait `PartialEq` is implemented for fn pointer `fn()`
note: required because of the requirements on the impl of `Trait` for `fn() {f}`
 --> src/main.rs:2:20
  |
2 | impl<A: PartialEq> Trait for A {}
  |                    ^^^^^     ^
note: required by a bound in `take`
 --> src/main.rs:3:17
  |
3 | fn take(_: impl Trait) {}
  |                 ^^^^^ required by this bound in `take`
  |
help: Convert the fn item to a fn pointer with `as`
  |
7 |     take(f as fn());
  |           ++++++++

(bonus side effect: hiding the giant list of explicit variadic fn pointer impls)

@CAD97 CAD97 added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jul 29, 2022
@fmease
Copy link
Member

fmease commented Mar 20, 2025

Output today:

error[E0277]: the trait bound `fn() {f}: Trait` is not satisfied
 --> src/main.rs:7:10
  |
7 |     take(f);
  |     ---- ^ the trait `Trait` is not implemented for fn item `fn() {f}`
  |     |
  |     required by a bound introduced by this call
  |
note: required by a bound in `take`
 --> src/main.rs:3:17
  |
3 | fn take(_: impl Trait) {}
  |                 ^^^^^ required by this bound in `take`
help: the trait `Trait` is implemented for fn pointer `fn()`, try casting using `as`
  |
7 |     take(f as fn());
  |            +++++++

@fmease
Copy link
Member

fmease commented Mar 21, 2025

We already have a test for this IINM: tests/ui/traits/fn-pointer/fn-trait-cast-diagnostic.rs
Fixed back in #106665.

@fmease fmease closed this as completed Mar 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

2 participants