Skip to content

Trait bound not satisfied should specify "fn item" vs "fn ptr" kind #99875

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

Closed
CAD97 opened this issue Jul 28, 2022 · 3 comments · Fixed by #100647
Closed

Trait bound not satisfied should specify "fn item" vs "fn ptr" kind #99875

CAD97 opened this issue Jul 28, 2022 · 3 comments · Fixed by #100647
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@CAD97
Copy link
Contributor

CAD97 commented Jul 28, 2022

Given the following code: [playground]

struct Argument;
struct Return;

fn function(_: Argument) -> Return { todo!() }

trait Trait {}
impl Trait for fn(Argument) -> Return {}

fn takes(_: impl Trait) {}

fn main() {
    takes(function);
    takes(|_: Argument| -> Return { todo!() });
}

The current output is:

error[E0277]: the trait bound `fn(Argument) -> Return {function}: Trait` is not satisfied
  --> src/main.rs:12:11
   |
12 |     takes(function);
   |     ----- ^^^^^^^^ the trait `Trait` is not implemented for `fn(Argument) -> Return {function}`
   |     |
   |     required by a bound introduced by this call
   |
   = help: the trait `Trait` is implemented for `fn(Argument) -> Return`
note: required by a bound in `takes`
  --> src/main.rs:9:18
   |
9  | fn takes(_: impl Trait) {}
   |                  ^^^^^ required by this bound in `takes`

error[E0277]: the trait bound `[closure@src/main.rs:13:11: 13:34]: Trait` is not satisfied
  --> src/main.rs:13:5
   |
13 |     takes(|_: Argument| -> Return { todo!() });
   |     ^^^^^ the trait `Trait` is not implemented for `[closure@src/main.rs:13:11: 13:34]`
   |
   = help: the trait `Trait` is implemented for `fn(Argument) -> Return`
note: required by a bound in `takes`
  --> src/main.rs:9:18
   |
9  | fn takes(_: impl Trait) {}
   |                  ^^^^^ required by this bound in `takes`

Compare E0308 mismatched types: [playground]

error[E0308]: mismatched types
  --> src/main.rs:12:21
   |
12 |     let _: fn(()) = function;
   |            ------   ^^^^^^^^ expected `()`, found struct `Argument`
   |            |
   |            expected due to this
   |
   = note: expected fn pointer `fn(())`
                 found fn item `fn(Argument) -> Return {function}`

error[E0308]: mismatched types
  --> src/main.rs:13:21
   |
13 |     let _: fn(()) = |_: Argument| -> Return { todo!() };
   |            ------   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `()`, found struct `Argument`
   |            |
   |            expected due to this
   |
   = note: expected fn pointer `fn(())`
                 found closure `[closure@src/main.rs:13:21: 13:44]`

Ideally the output (of the reported example) should look like:

error[[E0277]](https://doc.rust-lang.org/nightly/error-index.html#E0277): the trait bound `fn(Argument) -> Return {function}: Trait` is not satisfied
  --> src/main.rs:12:11
   |
12 |     takes(function);
   |     ----- ^^^^^^^^ the trait `Trait` is not implemented for fn item `fn(Argument) -> Return {function}`
   |     |
   |     required by a bound introduced by this call
   |
   = help: the trait `Trait` is implemented for fn pointer `fn(Argument) -> Return`
note: required by a bound in `takes`
  --> src/main.rs:9:18
   |
9  | fn takes(_: impl Trait) {}
   |                  ^^^^^ required by this bound in `takes`

error[[E0277]](https://doc.rust-lang.org/nightly/error-index.html#E0277): the trait bound `[closure@src/main.rs:13:11: 13:34]: Trait` is not satisfied
  --> src/main.rs:13:5
   |
13 |     takes(|_: Argument| -> Return { todo!() });
   |     ^^^^^ the trait `Trait` is not implemented for closure `[closure@src/main.rs:13:11: 13:34]`
   |
   = help: the trait `Trait` is implemented for fn pointer `fn(Argument) -> Return`
note: required by a bound in `takes`
  --> src/main.rs:9:18
   |
9  | fn takes(_: impl Trait) {}
   |                  ^^^^^ required by this bound in `takes`
@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 28, 2022
@CAD97 CAD97 changed the title Trait bound not satisfied should specify "fn item" vs "fn ptr" Trait bound not satisfied should specify "fn item" vs "fn ptr" kind Jul 28, 2022
@steffahn
Copy link
Member

steffahn commented Jul 28, 2022

I believe instead of "fixing" the error message, there's an underlying problem to be solved that function item types and function pointer types are rendered wayyy too similar. That should be changed, instead of working around it everywhere. I've expressed some thoughts on an alternative way to represent them in this post on IRLO.

@CAD97
Copy link
Contributor Author

CAD97 commented Jul 28, 2022

I agree that's the better the long-term fix, but this is a fairly simple incremental change in theory (just copy what E0308 is doing to E0277). And probably that makes it a good

@rustbot label E-easy

for entering in to diagnostics hacking.

@rustbot rustbot added the E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. label Jul 28, 2022
@obeis
Copy link
Contributor

obeis commented Jul 28, 2022

@rustbot claim

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 E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants