Skip to content

Generalize impl<T> Clone for Box<T> to unsized types - #146381

Open
a1phyr wants to merge 6 commits into
rust-lang:mainfrom
a1phyr:box_clone_more
Open

Generalize impl<T> Clone for Box<T> to unsized types#146381
a1phyr wants to merge 6 commits into
rust-lang:mainfrom
a1phyr:box_clone_more

Conversation

@a1phyr

@a1phyr a1phyr commented Sep 9, 2025

Copy link
Copy Markdown
Contributor

View all comments

Part of #142518

I had to add a non-default method to CloneToUninit for this to work, but I think this is fine. (I did not do the renaming to CloneUnsized as 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.

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Sep 9, 2025
@rustbot

rustbot commented Sep 9, 2025

Copy link
Copy Markdown
Collaborator

r? @workingjubilee

rustbot has assigned @workingjubilee.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rust-log-analyzer

This comment has been minimized.

@workingjubilee

Copy link
Copy Markdown
Member

r? library

@rustbot

rustbot commented Sep 9, 2025

Copy link
Copy Markdown
Collaborator

Failed to set assignee to library: invalid assignee

Note: Only org members with at least the repository "read" role, users with write permissions, or people who have commented on the PR may be assigned.

@workingjubilee

Copy link
Copy Markdown
Member

r? libs

@rustbot rustbot assigned jhpratt and unassigned workingjubilee Sep 9, 2025
@rust-log-analyzer

This comment has been minimized.

Comment thread library/alloc/src/boxed.rs Outdated
Comment thread library/alloc/src/boxed.rs Outdated
Comment thread library/core/src/clone.rs
///
/// # Safety
///
/// `dest` must be a valid pointer to a valid value of the same concrete type than `self`.

@zachs18 zachs18 Sep 10, 2025

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@zachs18 zachs18 Sep 10, 2025

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems similar to the issue with Rc::get_mut_unchecked, where either:

  1. if any other pointers exist to this value, we must only write a value that is valid for all those pointers' types/metadatas.
  2. if there are no other pointers to this value, we can write anything that is valid for this pointer's type/metadata.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread library/alloc/src/boxed/uninit.rs
@jhpratt

jhpratt commented Sep 12, 2025

Copy link
Copy Markdown
Member

As CI is failing, @rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Sep 12, 2025
@rustbot

rustbot commented Sep 12, 2025

Copy link
Copy Markdown
Collaborator

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@rustbot

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rustbot

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

jhpratt added a commit to jhpratt/rust that referenced this pull request Jul 13, 2026
… 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
jhpratt added a commit to jhpratt/rust that referenced this pull request Jul 13, 2026
… 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
rust-timer added a commit that referenced this pull request Jul 13, 2026
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

@mejrs mejrs left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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?

View changes since this review

Comment thread library/alloc/src/boxed.rs
a1phyr added 3 commits July 28, 2026 22:40
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`.
@rustbot

rustbot commented Jul 28, 2026

Copy link
Copy Markdown
Collaborator

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.

@a1phyr

a1phyr commented Jul 28, 2026

Copy link
Copy Markdown
Contributor Author

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jul 28, 2026
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@Mark-Simulacrum Mark-Simulacrum left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

View changes since this review

// SAFETY: guaranteed by caller
let layout = unsafe { Layout::for_value_raw(ptr) };

let ptr = if layout.size() == 0 {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems still unresolved.

Comment thread library/core/src/clone.rs
/// # 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);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems pretty strange to have clone_to_init on CloneToUninit trait. I guess this is more of a trait DynClone at this point...

Comment thread library/core/src/clone.rs
///
/// # Safety
///
/// `dest` must be a valid pointer to a valid value of the same concrete type than `self`.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread library/core/src/clone.rs
#[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`

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is true from the contract above (just leaving a note as this is a concrete example of divergence).

@rustbot rustbot added I-libs-api-nominated [DEPRECATED; DO NOT USE] S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Aug 2, 2026
@Mark-Simulacrum

Copy link
Copy Markdown
Member

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.

@Mark-Simulacrum Mark-Simulacrum removed the I-libs-api-nominated [DEPRECATED; DO NOT USE] label Aug 4, 2026
@clarfonthey clarfonthey added T-libs Relevant to the library team, which will review and decide on the PR/issue. and removed T-libs-api [DEPRECATED; DO NOT USE] labels Aug 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-fcp This change is insta-stable, or significant enough to need a team FCP to proceed. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.