-
Notifications
You must be signed in to change notification settings - Fork 508
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
Document auto-generated drop glue #873
Comments
Can you clarify more details on what is currently missing? Comparing that issue to https://doc.rust-lang.org/nightly/reference/destructors.html, it seems like the current docs are fairly clear which values have explicit drop code generated. Based on your comment at rust-lang/unsafe-code-guidelines#225 (comment), I also conclude that since a enum discriminant is not listed as needing a destructor, that there aren't any special requirements for "dropping" it. |
I mostly forwarded this issue from rust-lang/unsafe-code-guidelines#225, so @joshlf would be good if you could answer the question above. :) |
Not sure if the reference is the right place for that, but I'd be curious to learn how the drop glue is implemented (ie the exact logic, and also what the generated code looks like) |
In the original issue, I had code like the following: 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);
}
}
}
} The question was whether the let mut b = Box::new(0);
ptr::write(&mut b, Box::new(1)); While this wouldn't be UB, the act of overwriting the bits of The question in the original case is: does the act of overwriting the discriminant of an enum have any side effect? Or, to put it another way, does the act of If we wanted to be more formal, and phrase this in terms that make reference only to well-defined concepts in the Rust reference, we might rephrase this as something like: Let Note: I phrase it in this convoluted manner since it's possible that some other variant of |
@elichai Perhaps ask in the compiler channel on Zulip? |
@joshlf I don't think the discriminant matters much there. Once it's checked in the match and you are inside it, you're no longer accessing it. You are accessing I think the answer to your question is a "yes". |
It is not sound. |
The interaction between NLL, N.b. I arrived here after looking for documentation to reference in a URLO thread. |
NLL doesn't affect the location of drops. Instead drop types still behave as if they have lexical lifetimes if they are not explicitly moved or dropped. |
Yes, but we don't use the reference |
It's invalid for entirely unrelated reasons -- when you do EDIT: Looking at the code again, I don't actually see you doing that... you are writing into |
Enum discriminants certainly do not have any drop glue themselves, they just guide the drop glue by determining which variant to call the fields' drop glue for. However, overwriting with |
In some cases, not executing drop glue can have observable effects. E.g., |
Oh that's what you mean. I'd call that the absence of an effect -- so not executing something can be observable, but can never "have an effect". Hence my confusion. |
Ah, understood. I guess I've been using "effect" and "observable" as interchangeable. |
So, as far as I know, the drop glue for enum E { V1(T1, T2, ...), ... }
fn drop_in_place(ptr: *mut E) {
// Call user-defined drop (if it exists).
Drop::drop(&mut *ptr);
// Call drop glue of the fields.
match *ptr {
E::V1(f1, f2, ...) => {
// FIXME: I am not entirely sure about the order here.
drop_in_place(f1); // T1 drop glue
drop_in_place(f2); // T2 drop glue
...
}
...
}
} Does that answer your question? |
Guess I dropped the ball on responding; that does answer my question, although I do still wonder whether that behavior (or at least some observable aspects of that behavior) should be guaranteed by the reference. It sounds like you're just saying that that's what happens today, not that that's what's guaranteed by the language? FWIW, this is no longer relevant to me personally, although ofc it's still worthwhile to clarify in the reference IMO. |
I would like to vote in favor of some sort of augmentation of the documentation with regard to the term "drop glue". The This specifically came up for me in working through this answer to a question I asked on the Rust Users Forum. The issue I had came down to a closure definition unexpectedly (to me) capturing a lifetime in a way that led to a compiler error. A closure couldn't be dropped because it's "drop glue" required access to the captured lifetime that would otherwise have already gone out of scope. I never would have figured that out without the help of the nice responder on the Rust Users Forum, and even with their help it took some doing to chase through the details until I felt like I actually understood what was happening. It would be nice if:
|
There should be a section in the reference which document how the compiler-generated drop glue behaves. It should answer questions like this one: rust-lang/unsafe-code-guidelines#225.
The text was updated successfully, but these errors were encountered: