Skip to content

Conversation

kazutakahirata
Copy link
Contributor

This patch replaces an intentionally undefined template
specialization:

template 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

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
@llvmbot
Copy link
Member

llvmbot commented Oct 19, 2025

@llvm/pr-subscribers-llvm-adt

Author: Kazu Hirata (kazutakahirata)

Changes

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


Full diff: https://github.com/llvm/llvm-project/pull/164142.diff

1 Files Affected:

  • (modified) llvm/include/llvm/ADT/PackedVector.h (+2-3)
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 
@kazutakahirata kazutakahirata merged commit 5805e4d into llvm:main Oct 19, 2025
12 checks passed
@kazutakahirata kazutakahirata deleted the cleanup_20251018_ADT_PackedVector_static_assert branch October 19, 2025 15:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

4 participants