Skip to content

Commit f497c18

Browse files
Add missing enum case in TypeKind calculation.
Add more tests for it. PiperOrigin-RevId: 698875053
1 parent d425214 commit f497c18

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

src/google/protobuf/map.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,9 @@ class PROTOBUF_EXPORT UntypedMapBase {
412412
if constexpr (std::is_same_v<T, bool>) {
413413
return TypeKind::kBool;
414414
} else if constexpr (std::is_same_v<T, int32_t> ||
415-
std::is_same_v<T, uint32_t>) {
415+
std::is_same_v<T, uint32_t> || std::is_enum_v<T>) {
416+
static_assert(sizeof(T) == 4,
417+
"Only enums with the right underlying type are supported.");
416418
return TypeKind::kU32;
417419
} else if constexpr (std::is_same_v<T, int64_t> ||
418420
std::is_same_v<T, uint64_t>) {

src/google/protobuf/map_test.cc

+21-3
Original file line numberDiff line numberDiff line change
@@ -298,15 +298,33 @@ void TestGetTypeInfoDynamic(void (*)(Key...), void (*)(Value...)) {
298298
}
299299

300300
TEST(MapTest, StaticTypeInfoMatchesDynamicOne) {
301+
enum SomeEnum : int {};
302+
301303
TestGetTypeInfoDynamic(
304+
static_cast<void (*)(bool, int32_t, uint32_t, int64_t, uint64_t,
305+
std::string)>(nullptr),
302306
static_cast<void (*)(bool, int32_t, uint32_t, int64_t, uint64_t, float,
303-
double, std::string)>(nullptr),
304-
static_cast<void (*)(bool, int32_t, uint32_t, int64_t, uint64_t, float,
305-
double, std::string,
307+
double, std::string, SomeEnum,
306308
protobuf_unittest::TestEmptyMessage,
307309
protobuf_unittest::TestAllTypes)>(nullptr));
308310
}
309311

312+
TEST(MapTest, StaticTypeKindWorks) {
313+
enum SomeEnum : int {};
314+
315+
using UMB = UntypedMapBase;
316+
EXPECT_EQ(UMB::TypeKind::kBool, UMB::StaticTypeKind<bool>());
317+
EXPECT_EQ(UMB::TypeKind::kU32, UMB::StaticTypeKind<int32_t>());
318+
EXPECT_EQ(UMB::TypeKind::kU32, UMB::StaticTypeKind<uint32_t>());
319+
EXPECT_EQ(UMB::TypeKind::kU32, UMB::StaticTypeKind<SomeEnum>());
320+
EXPECT_EQ(UMB::TypeKind::kU64, UMB::StaticTypeKind<int64_t>());
321+
EXPECT_EQ(UMB::TypeKind::kU64, UMB::StaticTypeKind<uint64_t>());
322+
EXPECT_EQ(UMB::TypeKind::kString, UMB::StaticTypeKind<std::string>());
323+
EXPECT_EQ(UMB::TypeKind::kMessage,
324+
UMB::StaticTypeKind<protobuf_unittest::TestAllTypes>());
325+
EXPECT_EQ(UMB::TypeKind::kUnknown, UMB::StaticTypeKind<void**>());
326+
}
327+
310328
TEST(MapTest, IteratorNodeFieldIsNullPtrAtEnd) {
311329
Map<int, int> map;
312330
EXPECT_EQ(internal::UntypedMapIterator::FromTyped(map.cbegin()).node_,

0 commit comments

Comments
 (0)