-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Change lint message to be stronger for &T -> &mut T transmute #92704
Conversation
(rust-highfive has picked a reviewer for you, use r? to override) |
I also wonder if this can't be promoted to a forbid-by-default, though perhaps that should be a separate PR that gets a crater run. |
This comment has been minimized.
This comment has been minimized.
d92fefb
to
36a1141
Compare
Under Stacked Borrows this transmute is indeed insta-UB, but future models might be able to fix rust-lang/unsafe-code-guidelines#133 and then the |
📌 Commit 36a1141 has been approved by |
Hm, not sure I agree with that decision -- so far we have avoided making any official statements based on Stacked Borrows. The reality is that the rules of what exactly is and is not allowed here simply have not been decided yet. So this transmute is better avoided but the lint is a bit too definite in its statement IMO. |
…askrgr Rollup of 13 pull requests Successful merges: - rust-lang#89747 (Add MaybeUninit::(slice_)as_bytes(_mut)) - rust-lang#89764 (Fix variant index / discriminant confusion in uninhabited enum branching) - rust-lang#91606 (Stabilize `-Z print-link-args` as `--print link-args`) - rust-lang#91694 (rustdoc: decouple stability and const-stability) - rust-lang#92183 (Point at correct argument when async fn output type lifetime disagrees with signature) - rust-lang#92582 (improve `_` constants in item signature handling) - rust-lang#92680 (intra-doc: Use the impl's assoc item where possible) - rust-lang#92704 (Change lint message to be stronger for &T -> &mut T transmute) - rust-lang#92861 (Rustdoc mobile: put out-of-band info on its own line) - rust-lang#92992 (Help optimize out backtraces when disabled) - rust-lang#93038 (Fix star handling in block doc comments) - rust-lang#93108 (:arrow_up: rust-analyzer) - rust-lang#93112 (Fix CVE-2022-21658) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
The old message implied that it's only UB if you use the reference to mutate, which (as far as I know) is not true. As in, the following program has UB, and a &T -> &mut T transmute is effectively an
unreachable_unchecked
.In the future, it might be a good idea to use the edition system to make this a hard error, since I don't think it is ever defined behaviour? Unless we rule that
&UnsafeCell<i32> -> &mut i32
is fine. (That, and you always could just use.get()
, so you're not losing anything)