[PATCH] rust: alloc: fix assert in `Vec::reserve` doc test

Hsiu Che Yu posted 1 patch 1 month, 2 weeks ago
rust/kernel/alloc/kvec.rs | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
[PATCH] rust: alloc: fix assert in `Vec::reserve` doc test
Posted by Hsiu Che Yu 1 month, 2 weeks ago
The assert in the doctest used `>= 10`, which only checks that the
capacity can hold `additional` elements, ignoring the existing length
of `v`. The correct check should ensure there is room for `additional`
*extra* elements on top of what is already in the vector.

Fix the assert to use `>= v.len() + 10` so the example accurately
reflects the actual semantics of the function.

Reported-by: Miguel Ojeda <miguel.ojeda.sandonis@gmail.com>
Closes: https://lore.kernel.org/rust-for-linux/CANiq72nkXWhjK9iFRrhGtkMZGsvNE_zVsu4JnxaFRfxWL7RRdg@mail.gmail.com/
Fixes: 2aac4cd7dae3d ("rust: alloc: implement kernel `Vec` type")
Signed-off-by: Hsiu Che Yu <yu.whisper.personal@gmail.com>
---
 rust/kernel/alloc/kvec.rs | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/rust/kernel/alloc/kvec.rs b/rust/kernel/alloc/kvec.rs
index ac8d6f763ae8..d05f3835cf53 100644
--- a/rust/kernel/alloc/kvec.rs
+++ b/rust/kernel/alloc/kvec.rs
@@ -611,7 +611,7 @@ pub fn clear(&mut self) {
     ///
     /// v.reserve(10, GFP_KERNEL)?;
     /// let cap = v.capacity();
-    /// assert!(cap >= 10);
+    /// assert!(cap >= v.len() + 10);
     ///
     /// v.reserve(10, GFP_KERNEL)?;
     /// let new_cap = v.capacity();

---
base-commit: b4e07588e743c989499ca24d49e752c074924a9a
change-id: 20260427-doctest-kvec-reserve-6a84e832b05e

Best regards,
--  
Hsiu Che Yu <yu.whisper.personal@gmail.com>
Re: [PATCH] rust: alloc: fix assert in `Vec::reserve` doc test
Posted by Danilo Krummrich 1 month ago
On Mon, 27 Apr 2026 22:15:49 +0800, Hsiu Che Yu wrote:
> [PATCH] rust: alloc: fix assert in `Vec::reserve` doc test

Applied, thanks!

  Branch: alloc-next
  Tree:   git@github.com:Rust-for-Linux/linux.git

[1/1] rust: alloc: fix assert in `Vec::reserve` doc test
      commit: 75619f2df7a5

The patch will appear in the next linux-next integration (typically within 24
hours on weekdays).

The patch is queued up for the upcoming merge window for the next major kernel
release.
Re: [PATCH] rust: alloc: fix assert in `Vec::reserve` doc test
Posted by Alice Ryhl 1 month, 2 weeks ago
On Mon, Apr 27, 2026 at 10:15:49PM +0800, Hsiu Che Yu wrote:
> The assert in the doctest used `>= 10`, which only checks that the
> capacity can hold `additional` elements, ignoring the existing length
> of `v`. The correct check should ensure there is room for `additional`
> *extra* elements on top of what is already in the vector.
> 
> Fix the assert to use `>= v.len() + 10` so the example accurately
> reflects the actual semantics of the function.
> 
> Reported-by: Miguel Ojeda <miguel.ojeda.sandonis@gmail.com>
> Closes: https://lore.kernel.org/rust-for-linux/CANiq72nkXWhjK9iFRrhGtkMZGsvNE_zVsu4JnxaFRfxWL7RRdg@mail.gmail.com/
> Fixes: 2aac4cd7dae3d ("rust: alloc: implement kernel `Vec` type")
> Signed-off-by: Hsiu Che Yu <yu.whisper.personal@gmail.com>

Reviewed-by: Alice Ryhl <aliceryhl@google.com>
Re: [PATCH] rust: alloc: fix assert in `Vec::reserve` doc test
Posted by Alexandre Courbot 1 month, 2 weeks ago
On Mon Apr 27, 2026 at 11:15 PM JST, Hsiu Che Yu wrote:
> The assert in the doctest used `>= 10`, which only checks that the
> capacity can hold `additional` elements, ignoring the existing length
> of `v`. The correct check should ensure there is room for `additional`
> *extra* elements on top of what is already in the vector.
>
> Fix the assert to use `>= v.len() + 10` so the example accurately
> reflects the actual semantics of the function.
>
> Reported-by: Miguel Ojeda <miguel.ojeda.sandonis@gmail.com>
> Closes: https://lore.kernel.org/rust-for-linux/CANiq72nkXWhjK9iFRrhGtkMZGsvNE_zVsu4JnxaFRfxWL7RRdg@mail.gmail.com/
> Fixes: 2aac4cd7dae3d ("rust: alloc: implement kernel `Vec` type")
> Signed-off-by: Hsiu Che Yu <yu.whisper.personal@gmail.com>

Reviewed-by: Alexandre Courbot <acourbot@nvidia.com>