Skip to content

Update transmute_copy to ub_checks and ?Sized#155989

Open
scottmcm wants to merge 1 commit intorust-lang:mainfrom
scottmcm:unsized-transmute-copy
Open

Update transmute_copy to ub_checks and ?Sized#155989
scottmcm wants to merge 1 commit intorust-lang:mainfrom
scottmcm:unsized-transmute-copy

Conversation

@scottmcm
Copy link
Copy Markdown
Member

@scottmcm scottmcm commented Apr 30, 2026

The assert! in transmute_copy is from #98839. However, it was controversial at the time because it's possible to write transmute_copys that were UB by the description but didn't pass the documented conditions. It's also sub-optimal to have possible panics in unsafe code where the panic can actually make things worse by dropping things in the middle of what the programmer expected to be panic-free. Now that we have the UbChecks possibility, though, we can do this better: this PR makes it a non-unwinding panic when not meeting the documented condition when UB checks are enabled, but which very clearly emphasizes that said check isn't a documented behaviour of the function.

Also, I was staring at the signature and couldn't figure out why it needs Src: Sized. Since it always takes a reference, there seems to be no downside to accepting Src: ?Sized. That would also give it more of a reason to exist even if https://doc.rust-lang.org/nightly/std/mem/fn.transmute_prefix.html exists (as transmute_prefix(x) existing will no longer need the transmute_copy(&ManuallyDrop::new(x)) polyfill, cc rust-lang/rfcs#3844). That definitely needs an FCP, though.

A partial replacement for #154665

@scottmcm scottmcm added T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. needs-fcp This change is insta-stable, or significant enough to need a team FCP to proceed. I-libs-api-nominated Nominated for discussion during a libs-api team meeting. labels Apr 30, 2026
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Apr 30, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 30, 2026

r? @Mark-Simulacrum

rustbot has assigned @Mark-Simulacrum.
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

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: @scottmcm, libs
  • @scottmcm, libs expanded to 8 candidates
  • Random selection from Mark-Simulacrum, jhpratt, nia-e

@theemathas
Copy link
Copy Markdown
Contributor

Currently, this UB is detected at run time when transmute_copy is done from a small type to a large type, regardless of whether the program is compiled in debug or release mode. This PR presumably causes this UB to silently occur if the program is compiled in release mode, which seems undesirable to me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

I-libs-api-nominated Nominated for discussion during a libs-api team meeting. needs-fcp This change is insta-stable, or significant enough to need a team FCP to proceed. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants