[POC] create an MVP for using Destruct for custom dtors#156090
[POC] create an MVP for using Destruct for custom dtors#156090JayanAXHF wants to merge 4 commits intorust-lang:mainfrom
Destruct for custom dtors#156090Conversation
This comment has been minimized.
This comment has been minimized.
13224fe to
e48ef91
Compare
This comment has been minimized.
This comment has been minimized.
e48ef91 to
30d34f8
Compare
This comment has been minimized.
This comment has been minimized.
30d34f8 to
1a1e29a
Compare
This comment has been minimized.
This comment has been minimized.
1a1e29a to
74b13aa
Compare
This comment has been minimized.
This comment has been minimized.
|
welp i have no idea what this is. Is this due to smth with cg_clift? do i need to make changes there |
|
fixed it i think, it was related to the minicores not being updated |
This comment has been minimized.
This comment has been minimized.
|
It was another mini_core smh |
| /// | ||
| /// Generated by default if not implemented manually. | ||
| #[lang = "destruct_drop_in_place"] | ||
| unsafe fn drop_in_place(_to_drop: *mut Self); |
There was a problem hiding this comment.
To limit the amount of trait-related magic, I would rather do this:
| unsafe fn drop_in_place(_to_drop: *mut Self); | |
| /// Entrypoint for drop. Called when a value is still live at end of scope | |
| /// if none of its fields have been moved out of. | |
| /// | |
| /// The default implementation calls `Drop::drop` if implemented, | |
| /// then recursively calls `drop_in_place` on each field in order. | |
| #[lang = "destruct_drop_in_place"] | |
| unsafe fn drop_in_place(to_drop: *mut Self) { | |
| core::intrinsics::drop_in_place_shim(to_drop); | |
| } |
and then have the intrinsic be the only thing that's auto-generated.
There was a problem hiding this comment.
IMO there isn't much trait magic right now; the resolver simply checks if there are any user impls, and only assembles builtin ones if there aren't any. IMO this indirection would be rather confusing and also might clash with Specialization
There was a problem hiding this comment.
I have the opposite intuition: having a method that magically has a body generated for it is not something that exists in rust; only intrinsics do that. And I don't see why this would interact with specialization in any way. Moreover this has the benefit that if someone wants to they can call the auto-generated drop glue.
There was a problem hiding this comment.
The person can still invoke Destruct::drop_in_place manually, although it causes smth like a double drop issue.
Hey i was dropped
test
Hey i was dropped
I'll work on this. If you're talking about having both a custom dtor and the default one, i see very little usecase for that. Maybe that's just my intuition, but i prefer some less indirection, as this approach makes it more obvious how drop is being called (its just a function call, rather than what Drop::drop did, which called ptr::drop_in_place)
There was a problem hiding this comment.
I mean if I want to override Destruct::drop_in_place in a way that reuses the default thing, e.g. because you want to call it conditionally. I agree it's probably rare.
its just a function call, rather than what Drop::drop did, which called ptr::drop_in_place
I'm confused: Drop::drop never called ptr::drop_in_place.
| if ty.needs_drop(tcx, typing_env) { | ||
| debug!(" => nontrivial drop glue"); | ||
| match *ty.kind() { | ||
| ty::Coroutine(coroutine_def_id, ..) => { | ||
| // FIXME: sync drop of coroutine with async drop (generate both versions?) | ||
| // Currently just ignored | ||
| if tcx.optimized_mir(coroutine_def_id).coroutine_drop_async().is_some() { | ||
| ty::InstanceKind::DropGlue(def_id, None) | ||
| } else { | ||
| ty::InstanceKind::DropGlue(def_id, Some(ty)) | ||
| } | ||
| } | ||
| ty::Closure(..) | ||
| | ty::CoroutineClosure(..) | ||
| | ty::Tuple(..) | ||
| | ty::Adt(..) | ||
| | ty::Dynamic(..) | ||
| | ty::Array(..) | ||
| | ty::Slice(..) | ||
| | ty::UnsafeBinder(..) => ty::InstanceKind::DropGlue(def_id, Some(ty)), | ||
| // Drop shims can only be built from ADTs. | ||
| _ => return Ok(None), | ||
| } | ||
| } else { | ||
| debug!(" => trivial drop glue"); | ||
| ty::InstanceKind::DropGlue(def_id, None) | ||
| } |
There was a problem hiding this comment.
For perf reasons I expect we should keep some of this shortcutting. Also whatever's happening with async drop feels important, why is it ok to remove it?
There was a problem hiding this comment.
I'll have a look soon, thanks for pointing this out
There was a problem hiding this comment.
There was a problem hiding this comment.
Sure, but why is that ok and why is that useful? Doesn't this return the wrong instance kind in some cases now?
This comment has been minimized.
This comment has been minimized.
fix: bless tests
| if ty.needs_drop(tcx, typing_env) { | ||
| debug!(" => nontrivial drop glue"); | ||
| match *ty.kind() { | ||
| ty::Coroutine(coroutine_def_id, ..) => { | ||
| // FIXME: sync drop of coroutine with async drop (generate both versions?) | ||
| // Currently just ignored | ||
| if tcx.optimized_mir(coroutine_def_id).coroutine_drop_async().is_some() { | ||
| ty::InstanceKind::DropGlue(def_id, None) | ||
| } else { | ||
| ty::InstanceKind::DropGlue(def_id, Some(ty)) | ||
| } | ||
| } | ||
| ty::Closure(..) | ||
| | ty::CoroutineClosure(..) | ||
| | ty::Tuple(..) | ||
| | ty::Adt(..) | ||
| | ty::Dynamic(..) | ||
| | ty::Array(..) | ||
| | ty::Slice(..) | ||
| | ty::UnsafeBinder(..) => ty::InstanceKind::DropGlue(def_id, Some(ty)), | ||
| // Drop shims can only be built from ADTs. | ||
| _ => return Ok(None), | ||
| } | ||
| } else { | ||
| debug!(" => trivial drop glue"); | ||
| ty::InstanceKind::DropGlue(def_id, None) | ||
| } |
There was a problem hiding this comment.
Sure, but why is that ok and why is that useful? Doesn't this return the wrong instance kind in some cases now?
|
The job Click to see the possible cause of the failure (guessed by this bot) |
|
☔ The latest upstream changes (presumably #155443) made this pull request unmergeable. Please resolve the merge conflicts. |
| if tcx.is_lang_item(trait_ref.def_id, LangItem::Destruct) { | ||
| if !tcx.is_lang_item(trait_item_id, LangItem::DestructDropInPlace) { | ||
| bug!( | ||
| "unexpected associated item for built-in `{trait_ref}`: {}", | ||
| tcx.item_name(trait_item_id) | ||
| ); | ||
| } | ||
|
|
||
| debug!("Got user Destruct impl"); | ||
| return Ok(Some(Instance { | ||
| def: ty::InstanceKind::Item(leaf_def.item.def_id), | ||
| args: rcvr_args, | ||
| })); | ||
| } |
There was a problem hiding this comment.
Why is this early return useful/necessary? Please add a comment explaning its purpose
Destruct::drop_in_placeinstead ofmem::ptr::drop_in_place.DestructDropInPlacecandidate_assembly, and only checks for builtin impl when it doesnt find oneassemble_builtin_impl_candidatescc: @Nadrieril and @oli-obk (you were interested in reviewing this from the PG)
Note that this is a POC and i've not ironed out the comments and im not 100% confident that this is the best way to do this.
r? ghost