-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Add lint against ptr-to-int transmutes in consts #87525
Comments
Cc @rust-lang/wg-const-eval @jhpratt do you want to work on this, or leave this issue open for anyone to pick up? |
Someone else can pick this up, as I've got other priorities for the near future. As I mentioned feel free to use the code linked. It's worth noting that a lint would ideally also catch transmutes that use a union, though that would be quite a bit more difficult I imagine. |
That'll require a lot of complexity, for a case that I assume will be exceedingly rare. So I don't think that is effort, lines of code, and compile time well spent. |
Agreed. Just throwing it out there. |
More context on the lint can be found in the discussion starting at #85769 (comment) |
@rustbot label +A-const-eval |
@rustbot claim |
Add a Lint for Pointer to Integer Transmutes in Consts Fixes rust-lang#87525 This PR adds a MirLint for pointer to integer transmutes in const functions and associated consts. The implementation closely follows this comment: rust-lang#85769 (comment). More details about the implementation can be found in the comments. Note: This could break some sound code as mentioned by RalfJung in rust-lang#85769 (comment): > ... technically const-code could transmute/cast an int to a ptr and then transmute it back and that would be correct -- so the lint will deny some sound code. Does not seem terribly likely though. References: 1. https://doc.rust-lang.org/std/mem/fn.transmute.html 2. https://doc.rust-lang.org/reference/items/associated-items.html#associated-constants
We do not allow ptr-to-int casts in
const
context, so people might be tempted to do a transmute instead. This will not work, and if they ever actually test their code during CTFE they will get an error, but it might still be good to add an extra layer of defense here (since this is a rather odd corner of the language) and have a lint for this.@jhpratt started working on this, here's a link.
The text was updated successfully, but these errors were encountered: