Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add MaybeUninit array transpose impls #102023

Merged
merged 1 commit into from
Oct 16, 2022

Conversation

SUPERCILEX
Copy link
Contributor

@SUPERCILEX SUPERCILEX commented Sep 19, 2022

See discussion in #101179 and #96097. I believe this solution offers the simplest implementation with minimal future API regret. Closes rust-lang/libs-team#110.

@RalfJung mind doing a correctness review?

@rustbot rustbot added the T-libs Relevant to the library team, which will review and decide on the PR/issue. label Sep 19, 2022
@rust-highfive
Copy link
Collaborator

r? @scottmcm

(rust-highfive has picked a reviewer for you, use r? to override)

@rustbot
Copy link
Collaborator

rustbot commented Sep 19, 2022

Hey! It looks like you've submitted a new PR for the library teams!

If this PR contains changes to any rust-lang/rust public library APIs then please comment with @rustbot label +T-libs-api -T-libs to tag it appropriately. If this PR contains changes to any unstable APIs please edit the PR description to add a link to the relevant API Change Proposal or create one if you haven't already. If you're unsure where your change falls no worries, just leave it as is and the reviewer will take a look and make a decision to forward on if necessary.

Examples of T-libs-api changes:

  • Stabilizing library features
  • Introducing insta-stable changes such as new implementations of existing stable traits on existing stable types
  • Introducing new or changing existing unstable library APIs (excluding permanently unstable features / features without a tracking issue)
  • Changing public documentation in ways that create new stability guarantees
  • Changing observable runtime behavior of library APIs

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Sep 19, 2022
@SUPERCILEX
Copy link
Contributor Author

@rustbot label +T-libs-api -T-libs

@rustbot rustbot added T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. and removed T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Sep 19, 2022
@thomcc
Copy link
Member

thomcc commented Sep 19, 2022

I'm definitely in favor of adding this, but I think it needs an ACP.

@SUPERCILEX
Copy link
Contributor Author

@thomcc Sounds good, done: rust-lang/libs-team#110

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@SUPERCILEX
Copy link
Contributor Author

Hmmm, no idea how those failures are related. Will ignore for now.

@RalfJung
Copy link
Member

Yeah that's a strange error, can you rebase and force push?

Copy link
Member

@RalfJung RalfJung left a comment

Choose a reason for hiding this comment

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

The transmutation itself is sound. This is insta-stable though so definitely needs careful T-libs review.

src/test/ui/suggestions/issue-71394-no-from-impl.stderr Outdated Show resolved Hide resolved
library/core/src/mem/maybe_uninit.rs Outdated Show resolved Hide resolved
library/core/src/mem/maybe_uninit.rs Outdated Show resolved Hide resolved
@SUPERCILEX SUPERCILEX force-pushed the maybeuninit-transpose branch 2 times, most recently from 4c3f901 to 11037a3 Compare September 22, 2022 10:50
@RalfJung
Copy link
Member

Looks good, apart from some nits.

But I can't judge if Into instances are the right way to expose such APIs; I leave that to T-libs.

@SUPERCILEX
Copy link
Contributor Author

Sweet, thanks for the review! :)

@scottmcm
Copy link
Member

impls are insta-stable, so this needs to go to libs-api.

@rustbot label +needs-fcp
r? rust-lang/libs-api

@rustbot rustbot added the needs-fcp This change is insta-stable, so needs a completed FCP to proceed. label Sep 24, 2022
@scottmcm

This comment was marked as outdated.

@SUPERCILEX
Copy link
Contributor Author

r? @scottmcm I think this can go back to you now that these are going through the standard nightly process.

@rust-highfive rust-highfive assigned scottmcm and unassigned m-ou-se Oct 15, 2022
@scottmcm scottmcm removed the needs-fcp This change is insta-stable, so needs a completed FCP to proceed. label Oct 15, 2022
// SAFETY: T and MaybeUninit<T> have the same layout
let result = unsafe { super::transmute_copy(&self) };
super::forget(self);
result
Copy link
Member

Choose a reason for hiding this comment

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

suggestion: while this works fine, since none of it can panic, we try in the docs to follow the "pre-pit your peaches" order of things, rather than "forget it afterwards", and consider forget soft-deprecated.

So please update this to something like

    pub fn transpose(self) -> [MaybeUninit<T>; N] {
        // SAFETY: T, ManuallyDrop<T>, and MaybeUninit<T> have the same layout
        unsafe { super::transmute_copy(&ManuallyDrop::new(self)) }
    }

(And ditto for the one below)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh yeah, that's so much better. Is there a reason forget isn't deprecated? It seems like you could always replace it with let _ = ManuallyDrop::new(foo); (or drop(ManuallyDrop::new(foo));) worse case scenario.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, you can -- in fact that's literally what forget does

pub const fn forget<T>(t: T) {
let _ = ManuallyDrop::new(t);
}

I think it's not deprecated because, unlike mem::uninitialized, there's lots of code that used forget that's still perfectly correct because it only does things (like copy_nonoverlapping) that can't panic.

But maybe it's time to start thinking about it, now that ManuallyDrop has been around for long enough. Or at least a deprecated-in-rust-2.0 for now so it gets picked up by

declare_lint! {
/// The `deprecated_in_future` lint is internal to rustc and should not be
/// used by user code.
///
/// This lint is only enabled in the standard library. It works with the
/// use of `#[deprecated]` with a `since` field of a version in the future.
/// This allows something to be marked as deprecated in a future version,
/// and then this lint will ensure that the item is no longer used in the
/// standard library. See the [stability documentation] for more details.
///
/// [stability documentation]: https://rustc-dev-guide.rust-lang.org/stability.html#deprecated
pub DEPRECATED_IN_FUTURE,
Allow,
"detects use of items that will be deprecated in a future version",
report_in_external_macro
}

Copy link
Member

Choose a reason for hiding this comment

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

Hmm, maybe not. It looks like mem::forget(guard); is still very common in the library for panic guards, where there's no need to consume them like in the example, and thus the forget phrasing is kinda nice.

But maybe that also just means that the PanicGuard types should have a defuse method or something as part of its API instead of telling people to forget it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmmm, yeah I see the appeal for panic guards, but poking around the stdlib I think it's actually pretty hard to understand what's going on unless you know what the guards are for, so maybe encouraging people to write a defuse method or add a comment explaining the weirdness is a good thing. Created #103101.

@scottmcm scottmcm 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 Oct 15, 2022
Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com>
@scottmcm
Copy link
Member

Thanks! These look good for nightly to me.

@bors r+

@bors
Copy link
Contributor

bors commented Oct 15, 2022

📌 Commit 393434c has been approved by scottmcm

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Oct 15, 2022
bors added a commit to rust-lang-ci/rust that referenced this pull request Oct 16, 2022
Rollup of 6 pull requests

Successful merges:

 - rust-lang#101717 (Add documentation about the memory layout of `UnsafeCell<T>`)
 - rust-lang#102023 (Add MaybeUninit array transpose From impls)
 - rust-lang#103033 (Update pkg-config)
 - rust-lang#103080 (pretty: fix to print some lifetimes on HIR pretty-print)
 - rust-lang#103082 (Surround type with backticks)
 - rust-lang#103088 (Fix settings page)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors
Copy link
Contributor

bors commented Oct 16, 2022

⌛ Testing commit 393434c with merge e928a46...

@bors bors merged commit 166f664 into rust-lang:master Oct 16, 2022
@rustbot rustbot added this to the 1.66.0 milestone Oct 16, 2022
@SUPERCILEX SUPERCILEX deleted the maybeuninit-transpose branch October 16, 2022 19:05
@RalfJung
Copy link
Member

The PR title is outdated now, right?

@SUPERCILEX SUPERCILEX changed the title Add MaybeUninit array transpose From impls Add MaybeUninit array transpose impls Oct 19, 2022
@SUPERCILEX
Copy link
Contributor Author

Yup, fixed.

@SimonSapin
Copy link
Contributor

Is transmute_copy(&ManuallyDrop::new(self)) different from transmute(self)?

@scottmcm
Copy link
Member

scottmcm commented Oct 20, 2022

@SimonSapin Yes, because transmute doesn't know that MaybeUninit<[T; N]> and [MaybeUninit<T>; N] are the same size, and thus fails to compile.

EDIT: Fixed! Thanks @thomcc!

@thomcc
Copy link
Member

thomcc commented Oct 20, 2022

Yes, because transpose...

(I think you mean transmute, and only mention it because it might be pretty confusing with the mistake)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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.

Add array transpose From implementations: [MaybeUninit<T>; N] <-> MaybeUninit<[T; N]>