Skip to content

Vec::reserve() (etc) panic conditions don't apply to zero-sized or empty types #119149

Closed

Description

@bsilver8192

Location

https://doc.rust-lang.org/std/vec/struct.Vec.html#method.reserve (and several other methods)

Summary

Vec::<()>::new().capacity() is usize::MAX, which is greater than isize::MAX. Calling reserve(1) on this vector does not panic, and the capacity afterwards is still greater than isize::MAX. The docs say this should unconditionally panic.

I would find it reasonable to rely on this documentation as part of ensuring soundness of some unsafe code interacting with Vec, without duplicating the assertion in my code.

This same comment is repeated on several other Vec methods, and the same thing applies.

For reference, #117437 and #99790 are another issue and a PR touching on this same sentence of the documentation.

Activity

  1. added
    A-docsArea: Documentation for any part of the project, including the compiler, standard library, and tools
    on Dec 20, 2023
  2. added
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Dec 20, 2023
  3. Jules-Bertholet commented on Dec 20, 2023

    @Jules-Bertholet
    Contributor

    The docs say (emphasis mine):

    Panics

    Panics if the new capacity exceeds isize::MAX bytes.

    A Vec::<()> will never have more than 0 bytes of capacity, and therefore reserve will never panic. The documentation is correct.

  4. removed
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Dec 20, 2023
  5. bsilver8192 commented on Dec 20, 2023

    @bsilver8192
    Author

    Oh you're right, I read that many times and never processed the last word. That's the only place I see which talks about "capacity" as a number of bytes instead of a number of elements, which is easy to miss IMHO.

  6. Noratrieb commented on Dec 20, 2023

    @Noratrieb
    Member

    Might make sense to make thebytes bold.

  7. gurry commented on Dec 28, 2023

    @gurry
    Contributor

    Might make sense to make thebytes bold.

    I have raised PR #119374 doing just that (except italicising it instead making it bold)

  8. added a commit that references this issue on Dec 29, 2023
    ef55e75
  9. added a commit that references this issue on Dec 30, 2023
    f0b25d5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-docsArea: Documentation for any part of the project, including the compiler, standard library, and tools

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions