Skip to content

Error when casting a function is not informative #28959

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

Open
jdm opened this issue Oct 10, 2015 · 8 comments
Open

Error when casting a function is not informative #28959

jdm opened this issue Oct 10, 2015 · 8 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@jdm
Copy link
Contributor

jdm commented Oct 10, 2015

fn heap_size(_: &usize) -> usize {
    0
}

const f: *const fn(*const usize) -> usize = (&heap_size as *const fn(&usize) -> usize) as *const fn(*const usize) -> usize;

fn main() {
}

yields

<anon>:5:46: 5:86 error: casting `&fn(&usize) -> usize {heap_size}` as `*const fn(&usize) -> usize` is invalid
<anon>:5 const f: *const fn(*const usize) -> usize = (&heap_size as *const fn(&usize) -> usize) as *const fn(*const usize) -> usize;
                                                      ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
error: aborting due to previous error

This error tells me nothing about why it's invalid, and there's no extended explanation.

@jdm jdm added the A-diagnostics Area: Messages for errors, warnings, and lints label Oct 10, 2015
@arielb1
Copy link
Contributor

arielb1 commented Oct 11, 2015

This is because no cast rule is legal.

@DemiMarie
Copy link
Contributor

Still an unhelpful diagnostic that should be better.

@arielb1
Copy link
Contributor

arielb1 commented Oct 11, 2015

I don't really think it would be improved by "expected &-pointer, found *-pointer" (the issue is that an fn item does not necessarily have the same representation as an fn pointer, which makes the &T -> *const T coercion not take place).

@alilleybrinker
Copy link
Contributor

It may be desirable then to include an error for this particular case indicating that there is no valid version of the cast.

@aidanhs
Copy link
Member

aidanhs commented Jul 6, 2016

It'd also be nice if there was some treatment of "functions are function items until you explicitly cast them to function pointer types" in the docs.

@alilleybrinker
Copy link
Contributor

@aidanhs: Hmm... that's a good point. I don't know that the distinction is made in the current version of the book. Is this something that would be worthwhile to include in the new version? cc: @steveklabnik

@aidanhs
Copy link
Member

aidanhs commented Jul 6, 2016

For anyone who stumbles across this issue and wants to actually know how to cast functions (particularly extern ones), see #20178 for a hint until the docs/error message is updated.

@steveklabnik steveklabnik removed the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Mar 9, 2017
@Mark-Simulacrum Mark-Simulacrum added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Jul 24, 2017
@steveklabnik
Copy link
Member

Triage: the error message is fundamentally the same here, just with the new format:

error[E0606]: casting `&for<'r> fn(&'r usize) -> usize {heap_size}` as `*const for<'r> fn(&'r usize) -> usize` is invalid
 --> src/main.rs:5:45
  |
5 | const f: *const fn(*const usize) -> usize = (&heap_size as *const fn(&usize) -> usize) as *const fn(*const usize) -> usize;
  |                                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

@crlf0710 crlf0710 added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Jun 11, 2020
@Dylan-DPC Dylan-DPC added C-bug Category: This is a bug. and removed C-enhancement Category: An issue proposing an enhancement or a PR with one. labels May 26, 2023
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. 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

9 participants