-
Notifications
You must be signed in to change notification settings - Fork 13k
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
The Unsafe<T> reference problem #15920
Comments
I'm not sure this issue as described is really a problem. The bug here is that the fn bad_code<'a>() -> &'a str {
let x = "allocated string".to_string();
unsafe { mem::transmute(x.as_slice()) }
} The above returns a |
Your example is different - the drop glue invokes the unsafe function |
@arielb1 Seems to me that's roughly equivalent to your code with Of course, Ultimately, the public Of course, if static items change as suggested in rust-lang/rfcs#177 such that you can then say |
Not really. I agree that the bug is within the unsafe block - however this is exactly in the same manner that the bug in a hypothetical method within std::vec that conjures a bogus vector and pushes something into it (causing UB) is not within I'm quite sure that privacy should not be related to undefined behaviour, except in the trivial manner that it can reduce the amount of code that needs to be checked. Keeping this in mind, you can notice that RefCell ALSO creates an I never said that this was an unsoundness in Rust because it isn't - only an ugly point. Of course the issue would become less hidden if mut statics are removed - but it is still an issue even with them. By the way, I currently think the best way to fix this would be to make accessing the fields of an |
We already had an RFC for The alternative is to somehow mark a field as being usable in a struct literal expression, but private everywhere else. I can't think of a good way to denote that, though, or any use for that outside of |
This is now RFC PR rust-lang/rfcs#182 |
Closing in favor of the RFC thread. |
Currently, in Rust, undefined behaviour can only appear within an unsafe block, with one exception: one can take a reference to the body of an Unsafe block when unsafe code had borrowed the inside of the block, as in the following Rust code:
This was confused with some issues with statics in rust-lang/rfcs#177 and #14862, but, as shown here, has nothing to do with them.
This is not an RFC because I don't have a plan to fix this (and would prefer to first talk about the issue here).
The text was updated successfully, but these errors were encountered: