-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Finalize the error type for try_reserve
#61780
Conversation
r? @Kimundi (rust_highfive has picked a reviewer for you, use r? to override) |
I thought @davidtwco implemented this? |
https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=2b1b4edc0b7658a9e9f9a5e872226b08 compiles without a warning. |
At the libs team triage meeting, Alex pointed out that if we extend the Therefore, rather than try to anticipate future usage, the conservative choice is to pick a name corresponding to the only methods that currently use this type. I’ll amend this PR to use |
24d7017
to
5939a45
Compare
@SimonSapin I’m not sure there should be a warning for that example. |
Couldn't/wouldn't Vec::try_push be: fn try_push(&mut self, val: T) -> Result<(), (T, CollectionAllocErr)>` |
@davidtwco What does “downstream” mean exactly? Moving the enum definition didn’t change anything. Is it only in other crates? Does |
Sorry, I should have been more clear. I meant other crates. Within the defining crate, the privacy of the item (and any constructors) is unchanged.
Yes,
I don’t know if there’s a great solution to this. So long as the external interface of |
While I disagree with the title of the PR :) it is forwards-compatable with what I want, and I do like adding the I also agree with @gankro that we can probably make this fit the needs of other methods, but that can be worked out later. |
@gankro Maybe. Or maybe some other named type that can more easily have The |
Oh another issue is whether |
☔ The latest upstream changes (presumably #61771) made this pull request unmergeable. Please resolve the merge conflicts. |
5939a45
to
f0d68e4
Compare
☔ The latest upstream changes (presumably #62100) made this pull request unmergeable. Please resolve the merge conflicts. |
f0d68e4
to
b843004
Compare
IIRC we talked about this awhile back in a libs triage meeting and I have a feeling I said the same thing I'm going to say now, but we may have also decided to not do this so if it's the case please let me know! I feel though that this may be slightly more conservative without losing much ergonomics by making |
☔ The latest upstream changes (presumably #62733) made this pull request unmergeable. Please resolve the merge conflicts. |
b843004
to
35cc20f
Compare
I tried it and the ergonomics were not great. We can make an opaque struct if you think that’s important, but I feel that it’s only being maximally conservative out of principle for dubious benefits. The semantics of |
Sorry I'm running shorter nowadays on time than I previously though. @sfackler would you be willing to do the final review for this? |
☔ The latest upstream changes (presumably #60340) made this pull request unmergeable. Please resolve the merge conflicts. |
35cc20f
to
94051db
Compare
☔ The latest upstream changes (presumably #61393) made this pull request unmergeable. Please resolve the merge conflicts. |
94051db
to
abc4f25
Compare
Ping from triage, @sfackler would you be willing to review this? |
Ping from triage, requesting a review from @rust-lang/libs |
@bors r+ |
📌 Commit abc4f253865ff14e5e0e573fce5d76f571bbe3e2 has been approved by |
🔒 Merge conflict This pull request and the master branch diverged in a way that cannot be automatically merged. Please rebase on top of the latest master branch, and let the reviewer approve again. How do I rebase?Assuming
You may also read Git Rebasing to Resolve Conflicts by Drew Blessing for a short tutorial. Please avoid the "Resolve conflicts" button on GitHub. It uses Sometimes step 4 will complete without asking for resolution. This is usually due to difference between how Error message
|
… and add a separately-unstable field to force non-exhaustive matching (`#[non_exhaustive]` is no implemented yet on enum variants) so that we have the option to later expose the allocator’s error value. CC rust-lang/wg-allocators#23
abc4f25
to
59a3409
Compare
@bors r=Amanieu |
📌 Commit 59a3409 has been approved by |
Finalize the error type for `try_reserve` See tracking issue comments from rust-lang#48043 (comment). It is now: ```rust /// The error type for `try_reserve` methods. #[derive(Clone, PartialEq, Eq, Debug)] #[unstable(feature = "try_reserve", reason = "new API", issue="48043")] pub enum TryReserveError { /// Error due to the computed capacity exceeding the collection's maximum /// (usually `isize::MAX` bytes). CapacityOverflow, /// The memory allocator returned an error AllocError { /// The layout of allocation request that failed layout: Layout, #[doc(hidden)] #[unstable(feature = "container_error_extra", issue = "0", reason = "\ Enable exposing the allocator’s custom error value \ if an associated type is added in the future: \ rust-lang/wg-allocators#23")] non_exhaustive: (), }, } #[unstable(feature = "try_reserve", reason = "new API", issue="48043")] impl From<LayoutErr> for TryReserveError { #[inline] fn from(_: LayoutErr) -> Self { TryReserveError::CapacityOverflow } } ``` Changes: * A `Layout` is included. Firefox wants to log the size of failed allocations. If this were not part of the return value of e.g. `HashMap::try_reserve`, users would only be able to estimate based on `HashMap::capacity` and assumptions about the allocation strategy of `HashMap`. * There’s a dummy field that can stay unstable when `try_reserve` and the rest of this enum are stabilized. This forces non-exhaustive matching ~(rust-lang#44109 is not implemented yet for variants)~ and allows adding another field in the future if we want to expose custom error values from the allocator. See rust-lang/wg-allocators#23. - If the `Alloc` trait is stabilized without an associated error type and with a zero-size `AllocErr` type, we can simply remove this dummy field. - If an associated type is added, we can add a default type parameter to `ContainerError` and a generic field to the `AllocError` variant. * ~Moved from the `collections` module to the `alloc` module, and replaced `Collection` in the enum name with `Container`. The wold collection implies a multiplicity of items which is not relevant to this type. For example we may want to use this error type in a future `Box::try_new` method.~ - Renamed to `TryReserveError`, after the methods that involve this type: rust-lang#61780 (comment) * Replaced `Err` with `Error` in the enum and variant names. There is more precedent for this in https://doc.rust-lang.org/std/error/trait.Error.html#implementors, `AllocErr` and `LayoutErr` are the odd ones. * ~Dropped `Alloc` in the enum name. `ContainerAllocError` with a mouthful, and being in the `alloc` module already provides the same indication.~
Rollup of 10 pull requests Successful merges: - #60492 (Add custom nth_back for Chain) - #61780 (Finalize the error type for `try_reserve`) - #63495 ( Remove redundant `ty` fields from `mir::Constant` and `hair::pattern::PatternRange`.) - #63525 (Make sure that all file loading happens via SourceMap) - #63595 (add sparc64-unknown-openbsd target) - #63604 (Some update for vxWorks) - #63613 (Hygienize use of built-in macros in the standard library) - #63632 (A couple of comment fixes.) - #63634 (ci: properly set the job name in CPU stats) - #63636 (ci: move linkcheck from mingw-2 to mingw-1) Failed merges: r? @ghost
rust-lang/rust#61780 changed CollectionAllocErr to TryReserveError.
See tracking issue comments from #48043 (comment).
It is now:
Changes:
A
Layout
is included. Firefox wants to log the size of failed allocations. If this were not part of the return value of e.g.HashMap::try_reserve
, users would only be able to estimate based onHashMap::capacity
and assumptions about the allocation strategy ofHashMap
.There’s a dummy field that can stay unstable when
try_reserve
and the rest of this enum are stabilized. This forces non-exhaustive matching(Tracking issue for RFC 2008: Future-proofing enums/structs with #[non_exhaustive] attribute #44109 is not implemented yet for variants)and allows adding another field in the future if we want to expose custom error values from the allocator. See Associated Err type for the Alloc trait wg-allocators#23.Alloc
trait is stabilized without an associated error type and with a zero-sizeAllocErr
type, we can simply remove this dummy field.ContainerError
and a generic field to theAllocError
variant.Moved from thecollections
module to thealloc
module, and replacedCollection
in the enum name withContainer
. The wold collection implies a multiplicity of items which is not relevant to this type. For example we may want to use this error type in a futureBox::try_new
method.TryReserveError
, after the methods that involve this type: Finalize the error type fortry_reserve
#61780 (comment)Replaced
Err
withError
in the enum and variant names. There is more precedent for this in https://doc.rust-lang.org/std/error/trait.Error.html#implementors,AllocErr
andLayoutErr
are the odd ones.DroppedAlloc
in the enum name.ContainerAllocError
with a mouthful, and being in thealloc
module already provides the same indication.