[llvm] [LLVM][ADT] Explicitly convert size_t values to SmallVector's size type (PR #77939)
Andrew Browne via llvm-commits
llvm-commits at lists.llvm.org
Tue Jan 16 13:12:29 PST 2024
================
@@ -100,7 +100,18 @@ template <class Size_T> class SmallVectorBase {
/// This does not construct or destroy any elements in the vector.
void set_size(size_t N) {
assert(N <= capacity());
- Size = N;
+ Size = static_cast<Size_T>(N);
+ assert(size() == N);
+ }
+
+ /// Set the array capacity to \p M, which the current array size must
+ /// not be greater than.
+ ///
+ /// This does not construct or destroy any elements in the vector.
+ void set_capacity(size_t M) {
+ assert(size() <= M);
----------------
browneee wrote:
No, if the existing `assert(N <= capacity());` passes then I think `assert(M <= SizeTypeMax());` must be true.
I think:
* `set_size` should only check `assert(N <= capacity());`
* `set_capacity` should only check `assert(M <= SizeTypeMax());`
Using set_capacity to shrink (regardless of the size) doesn't make sense, because `set_capacity` would not actually handle shrinking (only a small piece of shrinking - switching to a smaller allocation).
https://github.com/llvm/llvm-project/pull/77939
More information about the llvm-commits
mailing list