-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Deprecate uninit_array #101179
Deprecate uninit_array #101179
Conversation
(rust-highfive has picked a reviewer for you, use r? to override) |
r? @RalfJung |
This comment has been minimized.
This comment has been minimized.
96822e6
to
ccca5dd
Compare
No objections from my side, but this should go through t-libs. |
I don't feel that strongly here, but I'm not sure I'm a fan of this removal. I've used Even if it doesn't have a performance benefit over |
Actually, now that I look carefully the |
|
My gut feeling is also against this, although I feel like the MaybeUninit methods for arrays could use an upgrade. What if instead of using a method like this, we just directly implemented |
How would this work when you're trying to read initialized memory? Furthermore, I don't see your suggestion as incompatible with this PR: we still want to nuke this method since the language is now capable of expressing |
In terms of deleting this method, keep in mind that it's not yet doable entirely with safe code, since |
That's actually not true, see how to do it in this docs update: 54014b7#diff-5e72af2fe825c2180f437d54adbdbb19bb40a9cc82bc6c53c2d993d72b08ce3fR149
Forgot that those returned references. This was already attempted (#88837), but I can commit to giving it another spin and then we can nuke the last method in this tracking issue. |
I would be in favour of doing that, especially if we can make arrays and slices work as expected without any dedicated methods. In terms of deletion, even though this is unstable, I do think that we should deprecate it for a few weeks before outright removing it, since I and a lot of people have been using this method. This has been done before and feels pretty reasonable in this case. |
Seems pretty reasonable, done. |
This technique isn't possible with a generic. fn foo<T>() -> [MaybeUninit<T>; 4] {
const UNINIT_T: MaybeUninit<T> = MaybeUninit::uninit(); // E0401
[UNINIT_T; 4]
} |
Dang, that's a bummer. Haven't had a chance to dig further, but assuming there's no way around this I still think deprecation + removal should move forward since you need to be on nightly to use this feature anyway. To address an earlier comment, I'm not seeing the "but you need to switch to another unstable feature" as a valid counterpoint without further evidence. You could say that other feature might not be stabilized and so we'll need to bring this back, but:
|
https://github.com/RalfJung/inline-const works even when there are generics. But anyway, the main point above was:
|
This is why I proposed adding This still doesn't provide a dedicated operation to "transpose" the uninit and array parts of the type, but it provides the same functionality without having to sacrifice readability. |
I think a transpose is more valuable than index methods since you can convert back and forth as needed. So is the conclusion that we should do that first before this PR? |
I don't think that transposing is a good idea because it makes it unclear which version of the transpose is preferred, and simply giving the option to make the top-level version usable seems preferable. Otherwise, we end up having several different ways of doing things without a clear reason to use one or another, which feels clunky. |
@@ -879,7 +879,7 @@ where | |||
} | |||
} | |||
|
|||
let mut array = MaybeUninit::uninit_array::<N>(); | |||
let mut array = MaybeUninit::uninit().transpose(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it worth adding a type annotation?
let mut array = MaybeUninit::uninit().transpose(); | |
let mut array = MaybeUninit::<[???; N]>::uninit().transpose(); |
There's a lot of inference going on here making the code quite hard to follow.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmmm, yeah I was wondering about this too. My reasoning for this being ok was that if the compiler manages to infer the types then I don't need to think about them. I'm guessing this is flawed because if you have no constraints at all, then the compiler will happily give you an i32. An interesting question arises: are integers and floats the only two places where the compiler will ever pull a type out of its 🍑 if there are no constraints? If so, my gut feeling is that we should get rid of that behavior (separate discussion, I know) since not having 🍑 types would let you do what I'm doing now where I say, "well, the compiler did some inferences so whatever the types are they must be correct". Or maybe I'm thinking too hard about this and type annotations would make human consumption easier (though I'm still curious about the above questions).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In safe code I tend to follow the same approach -- if the compiler found a type that makes it all work, then that seems right (but I will still sometimes add type annotations to make the code more readable).
In unsafe code though, I don't like trusting the compiler like that. Types are much less meaningful in unsafe code (that's why the code is unsafe to begin with), so IMO type inference should only be relied upon for absolutely obvious cases -- which this one is not.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, will do.
I don't think we should try to maintain any sort of stability for nightly features. The point of nightly features is that they're experimental and that we can change or remove them at any time.
Agreed
This RFC seems relevant: rust-lang/rfcs#3318 |
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
We briefly discussed this in the libs-api meeting. We prefer removing the function over deprecating it, since it's unstable. |
The sentiment in the libs-api meeting was that the array version is actually easier to understand than the |
I still would like to express my discomfort with transposes as… exposing something more generic for the sake of it, instead of promoting a good API. Right now we have the major hurdle that there have to be different methods for working with arrays, slices, and values. This doesn't actually solve this problem; in order to work with an array, you're still expected to transpose after the initial IMHO, we can remove this entirely by simply letting people slice directly into a This is the main reason why I promoted deprecating the old method as a compromise: if we're going to offer transposes, it seems worthwhile to see if users will actually prefer it. By removing the alternative, everyone's forced to move back to it instead, which just results in more frustration, IMHO. The one downside of this, which I will admit is rather large, is the fact that the metadata of the unsized value "escapes" from the |
Absolutely agree that this sucks, that's what I'm trying to improve.
Not sure how that's possible since Also, I still think you'd need transpose APIs because you'll be passed in an |
hello, visiting and checking progress. By skimming the comment history I'm unclear on the status. Perhaps some design work from the author (cc @SUPERCILEX) ? Can anyone provide an update? :) thanks! |
I still think this will go through, but we're now potentially waiting on inline const stabilization: rust-lang/libs-team#122 (comment) |
Add MaybeUninit array transpose From impls See discussion in rust-lang/rust#101179 and rust-lang/rust#96097. I believe this solution offers the simplest implementation with minimal future API regret. `@RalfJung` mind doing a correctness review?
Closing as this made progress elsewhere: #96097 (comment) |
Now that #98304 has gone through,
uninit_array
should be removed since creating a const array is the One True Way of doing things.Tracking issue: #96097
Closes #66845
Blocked on: rust-lang/libs-team#122 (comment)