[PATCH v2 0/2] rust: add unsafe precondition assertions to Vec length helpers
From: JX
Date: Tue Sep 08 2026 - 05:40:20 EST
The safety requirements of `Vec::inc_len` and `Vec::dec_len` are
currently checked with `debug_assert!`. Convert them to
`unsafe_precondition_assert!` so violations are reported consistently
with other unsafe precondition failures.
The first patch makes the no-message form of
`unsafe_precondition_assert!` usable from const functions. This is
needed by `Vec::inc_len`, which is const, and avoids runtime formatting.
The second patch converts both Vec length helpers.
The series has been checked with:
make rustfmtcheck
make CLIPPY=1 rust/kernel.o
make rusttest
make rustdoc
Changes in v2:
- Use a fixed diagnostic literal instead of concatenating the stringified
condition into a format string, since conditions may contain braces.
- Add a const-context documentation example with a block expression.
Link: https://lore.kernel.org/r/20260908080604.34070-1-1239989762@xxxxxx
JX (2):
rust: make unsafe_precondition_assert! const compatible
rust: alloc: use unsafe_precondition_assert! in Vec length helpers
rust/kernel/alloc/kvec.rs | 7 ++++---
rust/kernel/safety.rs | 11 ++++++++++-
2 files changed, 14 insertions(+), 4 deletions(-)
base-commit: cee9395acd8043be0644b25c34bfa86623f2b935
--
2.54.0