Generalize impl<T> Clone for Box<T> to unsized types - #146381
Conversation
|
rustbot has assigned @workingjubilee. Use |
This comment has been minimized.
This comment has been minimized.
|
r? library |
|
Failed to set assignee to
|
|
r? libs |
This comment has been minimized.
This comment has been minimized.
| /// | ||
| /// # Safety | ||
| /// | ||
| /// `dest` must be a valid pointer to a valid value of the same concrete type than `self`. |
There was a problem hiding this comment.
Given that trait vtable pointers can be deduplicated, I'm not sure if "the same concrete type" is what we want to require here. cc #146381 (comment) . I.e. it's not possible in general to verify for certain that two dyn Trait pointers with the same metadata actually point to the same erased/concrete type.
There was a problem hiding this comment.
This seems similar to the issue with Rc::get_mut_unchecked, where either:
- if any other pointers exist to this value, we must only write a value that is valid for all those pointers' types/metadatas.
- if there are no other pointers to this value, we can write anything that is valid for this pointer's type/metadata.
There was a problem hiding this comment.
I think this is the right thing. It be not be possible in general, but it is in some cases (eg with TypeId). Another formulation would be that it is safe to create a mutable reference from dest.with_metadata_of(self).
There was a problem hiding this comment.
Hm, I guess unlike clone_to_uninit we are not placing a precondition that the destination is big enough here? ("Concrete type" is not enough for that, [T] can be different lengths while being the same type).
I think it's worth elaborating in the doc comment on how this differs from clone_to_uninit and when one or the other is safe to use. Naively, I would expect that this could be implemented as:
ptr::drop_in_place(dest.cast::<Self>().with_metadata_of(self));
self.clone_to_uninit(dest);
But based on the implementations and the safety comment here, it doesn't seem like that's true. That should be clearly highlighted IMO.
|
As CI is failing, @rustbot author |
|
Reminder, once the PR becomes ready for a review, use |
f32cd66 to
bdc70ea
Compare
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
bdc70ea to
32deea5
Compare
This comment has been minimized.
This comment has been minimized.
32deea5 to
6f53c47
Compare
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
6f53c47 to
9cc7f3f
Compare
This comment has been minimized.
This comment has been minimized.
9cc7f3f to
84c014a
Compare
… r=khyperia enable `do_not_recommend` attr for method call errors in current solver This should help with rust-lang#146381 (comment) The logic is mostly copied from the `apply_do_not_recommend` function. r? @khyperia :3
… r=khyperia enable `do_not_recommend` attr for method call errors in current solver This should help with rust-lang#146381 (comment) The logic is mostly copied from the `apply_do_not_recommend` function. r? @khyperia :3
Rollup merge of #158882 - mejrs:do_not_recommend_old_solver, r=khyperia enable `do_not_recommend` attr for method call errors in current solver This should help with #146381 (comment) The logic is mostly copied from the `apply_do_not_recommend` function. r? @khyperia :3
There was a problem hiding this comment.
@a1phyr #158882 has merged so if you rebase and apply do_not_recommend you can fix the regression pointed out at #146381 (comment)
Can you also update the unique-object-noncopyable and unique-pinned-nocopy tests with next solver revisions similar to tests/ui/diagnostic_namespace/do_not_recommend/method_call.rs?
This new module contains a type that is like an uninitialized `Box`, but works with any type, not just `Sized` ones. Additionally, this will be useful for future work on in-place initialization (see rust-lang#142518).
This is necessary to properly support `Clone::clone_from`.
|
This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
|
@rustbot ready |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this comment.
I think it would be worth engaging libs-api to get their read on whether we want this in general before we continue implementing work, as the internals are a bit hairy and I'm not sure this comes up often enough to justify it.
@rustbot label +I-libs-api-nominated
| // SAFETY: guaranteed by caller | ||
| let layout = unsafe { Layout::for_value_raw(ptr) }; | ||
|
|
||
| let ptr = if layout.size() == 0 { |
There was a problem hiding this comment.
We allow zero-sized allocations in Allocator, so I think this can be removed?
| // if the concrete types are different, so we never allow them to | ||
| // go through `clone_from`. | ||
| #[inline] | ||
| default fn meta_eq(self, _: Self) -> bool { |
There was a problem hiding this comment.
This seems still unresolved.
| /// # Safety | ||
| /// | ||
| /// `dest` must be a valid pointer to a valid value of the same concrete type than `self`. | ||
| unsafe fn clone_to_init(&self, dest: *mut u8); |
There was a problem hiding this comment.
It seems pretty strange to have clone_to_init on CloneToUninit trait. I guess this is more of a trait DynClone at this point...
| /// | ||
| /// # Safety | ||
| /// | ||
| /// `dest` must be a valid pointer to a valid value of the same concrete type than `self`. |
There was a problem hiding this comment.
Hm, I guess unlike clone_to_uninit we are not placing a precondition that the destination is big enough here? ("Concrete type" is not enough for that, [T] can be different lengths while being the same type).
I think it's worth elaborating in the doc comment on how this differs from clone_to_uninit and when one or the other is safe to use. Naively, I would expect that this could be implemented as:
ptr::drop_in_place(dest.cast::<Self>().with_metadata_of(self));
self.clone_to_uninit(dest);
But based on the implementations and the safety comment here, it doesn't seem like that's true. That should be clearly highlighted IMO.
| #[cfg_attr(debug_assertions, track_caller)] | ||
| unsafe fn clone_to_init(&self, dest: *mut u8) { | ||
| // SAFETY: by contract, `dest` is a valid pointer to a `[T]` with the | ||
| // same length as `self` |
There was a problem hiding this comment.
I don't think this is true from the contract above (just leaving a note as this is a concrete example of divergence).
|
libs-api discussed and agreed this is generally the right direction to go in, so removing the nomination. Still some review comments left to iterate through of course, and we'll re-nominate if this ends up having any stable-facing implications. |
View all comments
Part of #142518
I had to add a non-default method to
CloneToUninitfor this to work, but I think this is fine. (I did not do the renaming toCloneUnsizedas suggested in #126799 (comment), as this is quite orthogonal)This also adds some infrastructure that will be helpful for in-place initialization.
Commits can be reviewed independently.