[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
Mon Jan 15 10:04:55 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:

I think this assertion is not useful, and may be misleading.

An assertion that would be clear and useful here would be:
```
assert(M <= SizeTypeMax());
```
...before assigning the elements and capacity.

You also wouldn't need `assert(capacity() == M);` as this assertion implies it will be successful. So remove that.

https://github.com/llvm/llvm-project/pull/77939


More information about the llvm-commits mailing list