-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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 a Lint for Pointer to Integer Transmutes in Consts #130540
base: master
Are you sure you want to change the base?
Conversation
Some changes occurred to MIR optimizations cc @rust-lang/wg-mir-opt |
/// | ||
/// [std::mem::transmute]: https://doc.rust-lang.org/std/mem/fn.transmute.html | ||
pub PTR_TO_INTEGER_TRANSMUTE_IN_CONSTS, | ||
Deny, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if this should be Deny
or Warn
by default.
The job Click to see the possible cause of the failure (guessed by this bot)
|
What's the motivation for this lint? https://doc.rust-lang.org/std/primitive.pointer.html#method.addr and https://doc.rust-lang.org/std/ptr/fn.without_provenance.html are transmutes, and I'd thought that this was considered basically fine now, albeit possibly not what you wanted if you need to preserve provenance. |
Transmuting a pointer to integer in const context is undefined behavior1. Usually, the evaluator will abort and emit an error when it comes across such undefined transmutes. But const functions and associated consts are evaluated only when referenced. This can result in undefined behavior in a library going unnoticed until the function or constant is actually used. Therefore, this lint specifically targets pointer to integer transmutes in const functions and associated consts. From what I can tell, this lint is not related to the functions you have linked because:
Footnotes |
Oh, of course; thank you. Brain fart on my part. Seem hard to lint on when it's not always UB, because things like I'd be tempted to run it on optimized mir where we can have const-folded a bunch of those cases away already, but then it'd be super inconsistent which we probably don't want either :/ |
Fixes #87525
This PR adds a MirLint for pointer to integer transmutes in const functions and associated consts. The implementation closely follows this comment: #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 #85769 (comment):
References: