Skip to content

Conversation

@a1phyr
Copy link
Contributor

@a1phyr a1phyr commented Sep 9, 2025

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
Copy link
Collaborator

rustbot commented Sep 9, 2025

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
Member

r? library

@rustbot
Copy link
Collaborator

rustbot commented Sep 9, 2025

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
Member

r? libs

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

This comment has been minimized.

///
/// # Safety
///
/// `dest` must be a valid pointer to a valid value of the same concrete type than `self`.
Copy link
Contributor

@zachs18 zachs18 Sep 10, 2025

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.

Copy link
Contributor

@zachs18 zachs18 Sep 10, 2025

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

@jhpratt
Copy link
Member

jhpratt commented Sep 12, 2025

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
Copy link
Collaborator

rustbot commented Sep 12, 2025

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
Copy link
Collaborator

rustbot commented Sep 25, 2025

This PR was rebased onto a different master 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.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

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`.
@a1phyr
Copy link
Contributor Author

a1phyr commented Sep 26, 2025

@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 Sep 26, 2025
@jhpratt
Copy link
Member

jhpratt commented Dec 17, 2025

I forgot I was assigned to this and apparently missed that it was ready for review. I'm busy at the moment, so passing off.

r? libs

@rustbot rustbot assigned Mark-Simulacrum and unassigned jhpratt Dec 17, 2025
#[cfg(not(no_global_oom_handling))]
pub(crate) unsafe fn new_for_metadata_in(meta: T::Metadata, alloc: A) -> Self {
let ptr = ptr::from_raw_parts_mut::<T>(ptr::null_mut::<()>(), meta);
let layout = unsafe { Layout::for_value_raw(ptr) };
Copy link
Member

Choose a reason for hiding this comment

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

What is the safety justification here? AFAICT, this can hit the last case ("otherwise, it is conservatively not allowed to call this function." in https://doc.rust-lang.org/nightly/std/alloc/struct.Layout.html#method.for_value_raw).

We should have safety comments on the unsafe blocks and new_for_metadata_in itself should have a doc comment with requirements to be called.


let ptr = if layout.size() == 0 {
let dangling = unsafe { core::num::NonZero::new_unchecked(layout.align()) };
NonNull::without_provenance(dangling)
Copy link
Member

Choose a reason for hiding this comment

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

}

#[stable(feature = "more_box_slice_clone", since = "1.29.0")]
impl Clone for Box<CStr> {
Copy link
Member

Choose a reason for hiding this comment

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

Do we have a unit test or compile test verifying this still exists? Want to avoid accidentally losing it if the unstable feature is removed without noticing that we've shrunk the API surface area.

// 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
Member

Choose a reason for hiding this comment

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

Hm, I'm not very happy with using specialization for this. At minimum I'd like to get T-types to weigh in before we merge. It seems like if we replaced this with an unconditional 'false', we'd only regress performance for callers of clone_from with slices?

Maybe the right thing is to add MaybeComparable or some such to a bound of Pointee::Metadata and force impls for all things that might be metadata? AFAICT, that would avoid specialization, right?

|
LL | struct R {
| -------- doesn't satisfy `R: Clone`
| -------- doesn't satisfy `R: CloneToUninit`
Copy link
Member

Choose a reason for hiding this comment

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

This seems like an unfortunate regression to probably relatively common diagnostics... especially the lack of the derive suggestion seems not-great. Is there some do_not_recommend attribute or tweak to the compiler we can land that would avoid this regression? CloneToUninit doesn't seem like it should be surfaced to users, at least until it is stabilized, and even then only if you're dealing with something ?Sized I suspect.

@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 Dec 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

8 participants