-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
Remove impossible panic note from Vec::append
#95214
Conversation
(rust-highfive has picked a reviewer for you, use r? to override) |
Neither the number of elements in a vector can overflow a `usize`, nor can the amount of elements in two vectors.
What about ZST? |
All comments about ZST apply to |
I'm not sure I understand how ZST types are relevant here; I'm not seeing code that will panic on ZST types related to isize::MAX. This change seems OK to me though, since it matches the reserve(...) panic note, and AFAICT that's the only source of panics for this method as well. |
I don't see anything to do for me. Did you add "S-waiting-on-author" by accident? @Mark-Simulacrum |
@@ -1767,7 +1767,7 @@ impl<T, A: Allocator> Vec<T, A> { | |||
/// | |||
/// # Panics | |||
/// | |||
/// Panics if the number of elements in the vector overflows a `usize`. | |||
/// Panics if the new capacity exceeds `isize::MAX` bytes. |
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.
I think the wording here (and in Vec::push
) is confusing because it uses the term capacity
which in this case is not related to the Vec::capacity
method since for a ZST, Vec::capacity
can be bigger than isize::MAX
but no actual memory will be allocated:
fn main() {
let mut vec = vec![(); usize::MAX];
assert_eq!(vec.len(), usize::MAX);
assert_eq!(vec.capacity(), usize::MAX);
assert!(vec.capacity() > usize::try_from(isize::MAX).unwrap());
vec.append(&mut vec![]);
assert!(vec.capacity() > usize::try_from(isize::MAX).unwrap());
}
I would rather change both panic notes to use a different terminology like allocated memory
with something for ZST that says no memory will be allocated.
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.
Note: It's also not directly related to the Vec::capacity
method for other types, it's just for 1-byte large types where it corresponds 1-to-1. E.g. a 2-byte type can at most have isize::MAX / 2
capacity.
What replacement would you suggest?
Panics if the memory to be allocated would exceed
isize::MAX
bytes.
It seems to me that this would introduce another terminology and would thus make the note harder to understand.
@JohnCSimon On what action of the author is the PR waiting on? |
@tbu- triage just bumps the PR by adding + removing the tag so it shows that it was updated. You can message the reviewer for clarification or talk on zulip. FYI: when a PR is ready for review, send a message containing |
@rustbot ready |
@bors r+ rollup There may be further improvements to the language here but I don't think anything is blocking this PR at this point, so I'm going to go ahead and approve. |
📌 Commit 4123d33 has been approved by |
…askrgr Rollup of 6 pull requests Successful merges: - rust-lang#95214 (Remove impossible panic note from `Vec::append`) - rust-lang#97411 (Print stderr consistently) - rust-lang#97453 (rename `TyKind` to `RegionKind` in comment in rustc_middle) - rust-lang#97457 (Add regression test for rust-lang#81899) - rust-lang#97458 (Modify `derive(Debug)` to use `Self` in struct literal to avoid redundant error) - rust-lang#97462 (Add more eslint rules) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
Neither the number of elements in a vector can overflow a
usize
, norcan the amount of elements in two vectors.