[llvm] [ADT] Use static_assert in PackedVector (PR #164142)

Kazu Hirata via llvm-commits llvm-commits at lists.llvm.org
Sat Oct 18 19:39:26 PDT 2025


https://github.com/kazutakahirata created https://github.com/llvm/llvm-project/pull/164142

This patch replaces an intentionally undefined template
specialization:

  template <typename T> class PackedVector<T, 0>;

with:

  static_assert(BitNum > 0, "BitNum must be > 0");

This way, the compiler diagnostic on a use of PackedVector<T, 0>
improves from:

  error: implicit instantiation of undefined template
  'llvm::PackedVector<unsigned int, 0>'

to:

  error: static assertion failed due to requirement '0U > 0': BitNum
  must be > 0


>From fe7ecaf39135fabe60e823ac127a9a5782b3e686 Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Thu, 16 Oct 2025 11:01:13 -0700
Subject: [PATCH] [ADT] Use static_assert in PackedVector

This patch replaces an intentionally undefined template
specialization:

  template <typename T> class PackedVector<T, 0>;

with:

  static_assert(BitNum > 0, "BitNum must be > 0");

This way, the compiler diagnostic on a use of PackedVector<T, 0>
improves from:

  error: implicit instantiation of undefined template
  'llvm::PackedVector<unsigned int, 0>'

to:

  error: static assertion failed due to requirement '0U > 0': BitNum
  must be > 0
---
 llvm/include/llvm/ADT/PackedVector.h | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/llvm/include/llvm/ADT/PackedVector.h b/llvm/include/llvm/ADT/PackedVector.h
index 09c20e39d1552..57e41979b4ce2 100644
--- a/llvm/include/llvm/ADT/PackedVector.h
+++ b/llvm/include/llvm/ADT/PackedVector.h
@@ -29,6 +29,8 @@ namespace llvm {
 /// an assertion.
 template <typename T, unsigned BitNum, typename BitVectorTy = BitVector>
 class PackedVector {
+  static_assert(BitNum > 0, "BitNum must be > 0");
+
   BitVectorTy Bits;
   // Keep track of the number of elements on our own.
   // We always maintain Bits.size() == NumElements * BitNum.
@@ -133,9 +135,6 @@ class PackedVector {
   BitVectorTy &raw_bits() { return Bits; }
 };
 
-// Leave BitNum=0 undefined.
-template <typename T> class PackedVector<T, 0>;
-
 } // end namespace llvm
 
 #endif // LLVM_ADT_PACKEDVECTOR_H



More information about the llvm-commits mailing list