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

Named functions do not implement traits implemented on fn() #121632

Open
Dragoncraft89 opened this issue Feb 26, 2024 · 4 comments
Open

Named functions do not implement traits implemented on fn() #121632

Dragoncraft89 opened this issue Feb 26, 2024 · 4 comments
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. D-papercut Diagnostics: An error or lint that needs small tweaks.

Comments

@Dragoncraft89
Copy link

Dragoncraft89 commented Feb 26, 2024

I tried this code:

trait Foo {}

impl Foo for fn() {}

fn main() {
    let x: &dyn Foo = &main;
}

playground link

I expected this code to compile, as main is a fn() pointer, which implements the trait Foo

However, rustc seems to need additiontal casting to convert a named fn() to a unnamed one:

   Compiling playground v0.0.1 (/playground)
error[E0277]: the trait bound `fn() {main}: Foo` is not satisfied
 --> src/lib.rs:6:23
  |
6 |     let x: &dyn Foo = &main;
  |                       ^^^^^ the trait `Foo` is not implemented for fn item `fn() {main}`
  |
  = note: required for the cast from `&fn() {main}` to `&dyn Foo`
help: the trait `Foo` is implemented for fn pointer `fn()`, try casting using `as`
  |
6 |     let x: &dyn Foo = &main as fn();
  |                             +++++++

For more information about this error, try `rustc --explain E0277`.
error: could not compile `playground` (lib) due to 1 previous error

On a side note, the solution suggested by rustc is missing parentheses. The correct way would be &(main as fn()). When following the suggestions given by rustc, the code goes &main -> &main as fn() -> &(&main as fn()). The last code suggestion is an invalid cast, because you cannot cast &fn() to fn().

Meta

rustc --version --verbose:

rustc 1.76.0 (07dca489a 2024-02-04)
binary: rustc
commit-hash: 07dca489ac2d933c78d3c5158e3f43beefeb02ce
commit-date: 2024-02-04
host: x86_64-unknown-linux-gnu
release: 1.76.0
LLVM version: 17.0.6

(also happens on beta and nightly)

@Dragoncraft89 Dragoncraft89 added the C-bug Category: This is a bug. label Feb 26, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Feb 26, 2024
@asquared31415
Copy link
Contributor

asquared31415 commented Feb 26, 2024

main is a fn() pointer

This is not true, and that's what the diagnostic is trying to explain.

fn item fn() {main}

main (and all other functions) are what's called a "function item" which is a unique type per function that can coerce to a fn pointer, but only at certain coercion sites. This is not one of those places, so the compiler is trying to tell you to force the coercion.

The suggestion is incorrect though, it needs parentheses.

An alternative is to do impl<T: Fn()> Foo for T {}, since function items and function pointers both implement the Fn trait family where possible. (note that closures also implement this trait, which may be helpful, but also may not).

@rustbot label -needs-triage +A-diagnostics +D-papercut +D-incorrect

@rustbot
Copy link
Collaborator

rustbot commented Feb 26, 2024

Error: Parsing relabel command in comment failed: ...'papercut +' | error: empty label at >| ' D-incorre'...

Please file an issue on GitHub at triagebot if there's a problem with this bot, or reach out on #t-infra on Zulip.

@asquared31415
Copy link
Contributor

asquared31415 commented Feb 26, 2024

@rustbot claim

I'll work on fixing the suggestion at least, and possibly see about clarifying the diagnostic

@rustbot
Copy link
Collaborator

rustbot commented Feb 26, 2024

Error: Parsing assign command in comment failed: ...' claim' | error: expected end of command at >| ' I'll work'...

Please file an issue on GitHub at triagebot if there's a problem with this bot, or reach out on #t-infra on Zulip.

@rustbot rustbot added A-diagnostics Area: Messages for errors, warnings, and lints D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. D-papercut Diagnostics: An error or lint that needs small tweaks. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Feb 26, 2024
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 C-bug Category: This is a bug. D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. D-papercut Diagnostics: An error or lint that needs small tweaks.
Projects
None yet
Development

No branches or pull requests

3 participants