-
Notifications
You must be signed in to change notification settings - Fork 13.4k
fn(u64) -> u64
can be cast as i64
, resulting in gibberish
#50796
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
Comments
The number is the address of the function. |
ah. well here is one voice urging to discontinue this shortcut as it is quite a footgun by rust standards. |
You're running into one of the type casts that isn't a coercion. These exist so pointers (and function pointers) can be converted into Regardless, changing this behavior is non backwards-compatible. So the documentation on casts/coercions might need to be updated (it's not in the book's second edition). I also guess restricting what |
So, what we could do is to issue a warning for casts like this. The most verbose way would be EDIT: function names getting directly converted into raw pointers seems the confusing point here. |
There's an open issue in clippy for this issue: rust-lang/rust-clippy#2588 |
I hope you meant |
in the meantime, I saw a post a few weeks ago about custom lints. For those who have some familiarity with this, how difficult would it be (for a knowledgeable rust dev, just no experience writing plugins) to write a plugin that prohibits this specific cast? It never occurred to me this could happen and I'm concerned there could be other places where it did. |
@jonathanstrong this particular lint should be fairly easy, as you essentially just want to prevent casting functions to anything but I would definitely suggest to add it to clippy, because the bar is much lower for new lints. Adding new lints to rustc requires an RFC. (also clippy builds faster than rustc 😉 ) The clippy-issue referenced above has already some small hint as to where to start. If you have more questions just ask on the issue or in the #clippy irc channel. |
This got implemented in clippy (rust-clippy#2814) so it can probably be closed here. |
Uh oh!
There was an error while loading. Please reload this page.
minimal example:
playground
Ordinarily rust is very strict about casts so I was surprised by this behavior when, because of a typo, I was trying to cast a
fn
as ani64
. It compiled fine and resulted in garbage numbers that were difficult to track down. I assume this is a bug and not intended behavior.First encountered the problem on nightly 1.27 2018-05-13 but it is also present in stable 1.26, per the playground example.
The text was updated successfully, but these errors were encountered: