-
Notifications
You must be signed in to change notification settings - Fork 60
Dropping enum discriminants? #225
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
Comments
This line doesn't really make sense unless
It's difficult to tell exactly what you're asking, but writing an invalid value to an enum is always UB even if it's immediately dropped. If you're not doing that it's probably OK? |
(Reminder: if |
The full context is here for the curious. Let me make this more precise, though. Here's a slightly filled out version of the method in question: fn do_thing<T, E>(res: &mut Result<T, E>) {
match res {
Ok(...) => ...,
Err(x) => {
unsafe {
let y = ptr::read(x);
let new = // compute on y
ptr::write(res, new);
}
}
}
} Since |
This isn't true. Citation: https://doc.rust-lang.org/core/ptr/fn.read.html#ownership-of-the-returned-value |
I'd actually be curious about how we model the behavior of |
Yes, "live" or "not live" is just "in the compiler's head", so to speak.
This is one part of what makes |
It reads the storage pointed to by its argument at the given type, and returns that. Nothing more. Re: the example code, it seems fine to em assuming you are sure that the "compute on y" code will never panic. Because if it does, you'll double-free the contents of As far as you original question goes, I think it is fair to assume that the enum discriminant doesn't care about dropping. @Lokathor, you are right but note that the code here doesn't do |
Yeah, we certainly need to ensure that we don't unwind (in the code that I'm actually writing, I do this using
Sounds good. What do you think is the right way to articulate that, and should we put that in the spec somewhere? I imagine articulating it something like:
|
The thing is, drop code is associated with types, not with data. And there is no type for discriminants. So there's no plausible way, for me, how they could have any drop glue. Given that you raised the question, this is probably worth documenting, but your formulation sounds like data has drop glue, and that gives the wrong impression. |
I guess the remaining action item here is documenting the behavior of drop glue. But I'd say that's reference material, not UCG, so I will open an issue over there. |
Closing in favor of rust-lang/reference#873. |
Are we guaranteed that enum discriminants have trivial drop impls? In other words:
The context is that I have the following code:
Am I guaranteed that
ptr::write(self, new)
is fine even though it clobbers the enum discriminants, which are technically still initialized?The text was updated successfully, but these errors were encountered: