-
-
Notifications
You must be signed in to change notification settings - Fork 15.4k
add rustc_no_writable to mem::forget and structs it uses #159181
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| // Regression test. This failed before applying `#[rustc_no_writable]` to `mem::forget`, `ManuallyDrop::new`, and `MaybeDangling::new`. | ||
| //@compile-flags: -Zmiri-tree-borrows -Zmiri-tree-borrows-implicit-writes | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This currently doesn't test Also seems like
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What exactly is meant with it doesn't test
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could remove
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, when I remove it from the
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah I should maybe have mentioned that 😅. That's why I added the attribute.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, makes sense, thanks :) |
||
|
|
||
| use std::mem; | ||
|
|
||
| // This function is taken from the crate `derive_more`, from the file `into.rs` | ||
| unsafe fn transmute<From, To>(from: From) -> To { | ||
| let to = unsafe { mem::transmute_copy(&from) }; | ||
| mem::forget(from); | ||
| to | ||
| } | ||
|
Comment on lines
+7
to
+11
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a reason that we want to bless this pattern with unsafe fn transmute<From, To>(from: From) -> To {
let from = ManuallyDrop::new(from); // effectively already forgotten, but happens first, so no reborrow after the copy
unsafe { mem::transmute_copy(&from) }
}
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Adding a test case is not the same as blessing a code pattern. We should still tell people that it is a bad idea to write code like this.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Specifically adding the attribute to
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The only thing that should be taken as "this is fine actually" is documentation.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The attribute is part of an experiment by @RalfJung and me and @quiode to see if we can unlock more optimizations w/o breaking too much real-world code and that includes figuring out which places need this attribute (and if that is "too many"). Since this is all highly experimental, we have not yet thought about how much "intentionality" the attribute ought to convey. Currently we're using it for both, at some later point we will need to decide where the attribute is "intended" and where not (if we decide we want to keep it at all). So I hope we do not need to decide that now :)
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, this is part of trying to minimize the amount of new UB introduced to existing crates, so the optimization could become useful. This function seemed like a good place because apparently it causes trouble in some code out there, independent if it's actually good code or not. Currently, under Tree Borrows at least, that pattern would be allowed so I took that as my point of reference. |
||
|
|
||
| fn main() { | ||
| let mut val = 10u32; | ||
| let r: &mut u32 = &mut val; | ||
|
|
||
| let to: &mut i32 = unsafe { transmute(r) }; | ||
|
|
||
| assert_eq!(*to, 10); | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.