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.

(The change to assert_unsafe_precondition here doesn't technically need libs-api assent, but I'd still like to hear your thoughts since it's different from the "promise the assert" that IIRC you were also asked to consider elsewhere.)

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.

@Noratrieb
Copy link
Copy Markdown
Member

Yeah I agree, making this an non-unwinding panic seems like an obvious improvement, but disabling the check when UB checks are disabled seems strictly worse. The idea of UB checks being optional is that they can be expensive, but this check is constant and therefore guaranteed to be free, so why not always?

@ds84182
Copy link
Copy Markdown

ds84182 commented Apr 30, 2026

The check isn't constant when the source is unsized.

@scottmcm
Copy link
Copy Markdown
Member Author

Also, the thing with the "well it's a 'free' check" is that that's only true if it's Sized, but the same reasoning as that means that it's also the kind of thing that you only need to run once with ubchecks enabled to see the error.

Plus, it's often not language UB to violate this (at least under TB), since often the reference is into a larger allocation anyway. The interesting and difficult part of the UB remains fundamentally uncheckable.

The assert was useful before because people never noticed that they were doing something always-UB. But the ubcheck is absolutely sufficient to cover that need. If you have code that you never run in debug ever and you're pervasively getting your unsafe code wrong, a release-mode panic here doesn't really improve anything materially.

@scottmcm scottmcm added S-waiting-on-t-libs-api Status: Awaiting decision from T-libs-api and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 1, 2026
@Amanieu
Copy link
Copy Markdown
Member

Amanieu commented May 5, 2026

We discussed this in the @rust-lang/libs-api meeting and agree with all of the arguments that @scottmcm raised.

@rfcbot merge libs-api

@rust-rfcbot
Copy link
Copy Markdown
Collaborator

rust-rfcbot commented May 5, 2026

Team member @Amanieu has proposed to merge this. The next step is review by the rest of the tagged team members:

No concerns currently listed.

Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

See this document for info about what commands tagged team members can give me.

@rust-rfcbot rust-rfcbot added the proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. label May 5, 2026
@Amanieu Amanieu removed the I-libs-api-nominated Nominated for discussion during a libs-api team meeting. label May 5, 2026
@rust-rfcbot rust-rfcbot added the disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. label May 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. needs-fcp This change is insta-stable, or significant enough to need a team FCP to proceed. proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. S-waiting-on-t-libs-api Status: Awaiting decision from T-libs-api 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.

8 participants