[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:20:52 PST 2024


================
@@ -99,8 +99,9 @@ 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;
+    auto n = static_cast<Size_T>(N);
+    assert(n <= capacity());
----------------
browneee wrote:

> I think it makes sense to keep the assert(static_cast<size_t>(Size) == N); at the end as well.

This assertion is correct, but not necessary because it is implied by `assert(N <= capacity());`.

I would avoid an unnecessary assertion, because this is performance sensitive code.

If you want to keep it for clarity then I'd suggest only doing it in debug:
```
LLVM_DEBUG(assert(static_cast<size_t>(Size) == N));
```

My vote would be to omit it, because it is covered by the earlier assertion.

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


More information about the llvm-commits mailing list