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

Add lint against ptr-to-int transmutes in consts #87525

Closed
RalfJung opened this issue Jul 27, 2021 · 7 comments · Fixed by #130540
Closed

Add lint against ptr-to-int transmutes in consts #87525

RalfJung opened this issue Jul 27, 2021 · 7 comments · Fixed by #130540
Assignees
Labels
A-const-eval Area: Constant evaluation (MIR interpretation)

Comments

@RalfJung
Copy link
Member

RalfJung commented Jul 27, 2021

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.

@RalfJung
Copy link
Member Author

Cc @rust-lang/wg-const-eval

@jhpratt do you want to work on this, or leave this issue open for anyone to pick up?

@jhpratt
Copy link
Member

jhpratt commented Jul 27, 2021

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.

@RalfJung
Copy link
Member Author

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.

@jhpratt
Copy link
Member

jhpratt commented Jul 27, 2021

Agreed. Just throwing it out there.

@oli-obk
Copy link
Contributor

oli-obk commented Jul 28, 2021

More context on the lint can be found in the discussion starting at #85769 (comment)

@clubby789
Copy link
Contributor

@rustbot label +A-const-eval

@rustbot rustbot added the A-const-eval Area: Constant evaluation (MIR interpretation) label Mar 30, 2023
@veera-sivarajan
Copy link
Contributor

@rustbot claim

@bors bors closed this as completed in daebce4 Oct 6, 2024
flip1995 pushed a commit to flip1995/rust that referenced this issue Oct 18, 2024
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-const-eval Area: Constant evaluation (MIR interpretation)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants