Skip to content

Commit 30a8ef5

Browse files
Prepare MessageLite::GetTypeName to be upgraded to return
`absl::string_view`. This is a modernization of the API and performance improvement for all callers. Currently gated behind opt-in macro `PROTOBUF_TEMPORARY_ENABLE_STRING_VIEW_RETURN_TYPE` for users to test and prevent issues when the breaking change happens. PiperOrigin-RevId: 667616632
1 parent 0d9baf3 commit 30a8ef5

File tree

4 files changed

+30
-20
lines changed

4 files changed

+30
-20
lines changed

src/google/protobuf/lite_unittest.cc

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1363,9 +1363,10 @@ TEST(LiteTest, DynamicCastMessage) {
13631363
TEST(LiteTest, DynamicCastMessageInvalidReferenceType) {
13641364
CastType1 test_type_1;
13651365
const MessageLite& test_type_1_pointer_const_ref = test_type_1;
1366-
ASSERT_DEATH(DynamicCastMessage<CastType2>(test_type_1_pointer_const_ref),
1367-
"Cannot downcast " + test_type_1.GetTypeName() + " to " +
1368-
CastType2::default_instance().GetTypeName());
1366+
ASSERT_DEATH(
1367+
DynamicCastMessage<CastType2>(test_type_1_pointer_const_ref),
1368+
absl::StrCat("Cannot downcast ", test_type_1.GetTypeName(), " to ",
1369+
CastType2::default_instance().GetTypeName()));
13691370
}
13701371
#endif // GTEST_HAS_DEATH_TEST
13711372

@@ -1396,19 +1397,21 @@ TEST(LiteTest, DownCastMessageInvalidPointerType) {
13961397

13971398
MessageLite* test_type_1_pointer = &test_type_1;
13981399

1399-
ASSERT_DEBUG_DEATH(DownCastMessage<CastType2>(test_type_1_pointer),
1400-
"Cannot downcast " + test_type_1.GetTypeName() + " to " +
1401-
CastType2::default_instance().GetTypeName());
1400+
ASSERT_DEBUG_DEATH(
1401+
DownCastMessage<CastType2>(test_type_1_pointer),
1402+
absl::StrCat("Cannot downcast ", test_type_1.GetTypeName(), " to ",
1403+
CastType2::default_instance().GetTypeName()));
14021404
}
14031405

14041406
TEST(LiteTest, DownCastMessageInvalidReferenceType) {
14051407
CastType1 test_type_1;
14061408

14071409
MessageLite& test_type_1_pointer = test_type_1;
14081410

1409-
ASSERT_DEBUG_DEATH(DownCastMessage<CastType2>(test_type_1_pointer),
1410-
"Cannot downcast " + test_type_1.GetTypeName() + " to " +
1411-
CastType2::default_instance().GetTypeName());
1411+
ASSERT_DEBUG_DEATH(
1412+
DownCastMessage<CastType2>(test_type_1_pointer),
1413+
absl::StrCat("Cannot downcast ", test_type_1.GetTypeName(), " to ",
1414+
CastType2::default_instance().GetTypeName()));
14121415
}
14131416
#endif // GTEST_HAS_DEATH_TEST
14141417

src/google/protobuf/message_lite.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ const char* MessageLite::_InternalParse(const char* ptr,
7474
return internal::TcParser::ParseLoop(this, ptr, ctx, GetTcParseTable());
7575
}
7676

77-
std::string MessageLite::GetTypeName() const {
78-
return std::string(TypeId::Get(*this).name());
77+
internal::GetTypeNameReturnType MessageLite::GetTypeName() const {
78+
return internal::GetTypeNameReturnType(TypeId::Get(*this).name());
7979
}
8080

8181
absl::string_view TypeId::name() const {

src/google/protobuf/message_lite.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,12 @@ inline int ToIntSize(size_t size) {
190190
return static_cast<int>(size);
191191
}
192192

193+
#if defined(PROTOBUF_FUTURE_STRING_VIEW_RETURN_TYPE)
194+
using GetTypeNameReturnType = absl::string_view;
195+
#else
196+
using GetTypeNameReturnType = std::string;
197+
#endif
198+
193199
// Default empty string object. Don't use this directly. Instead, call
194200
// GetEmptyString() to get the reference. This empty string is aligned with a
195201
// minimum alignment of 8 bytes to match the requirement of ArenaStringPtr.
@@ -240,7 +246,7 @@ class PROTOBUF_EXPORT MessageLite {
240246
// Basic Operations ------------------------------------------------
241247

242248
// Get the name of this message type, e.g. "foo.bar.BazProto".
243-
std::string GetTypeName() const;
249+
internal::GetTypeNameReturnType GetTypeName() const;
244250

245251
// Construct a new instance of the same type. Ownership is passed to the
246252
// caller.

src/google/protobuf/message_unittest.inc

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -792,10 +792,11 @@ TEST(MESSAGE_TEST_NAME, DynamicCastMessage) {
792792
TEST(MESSAGE_TEST_NAME, DynamicCastMessageInvalidReferenceType) {
793793
UNITTEST::TestAllTypes test_all_types;
794794
const MessageLite& test_all_types_pointer_const_ref = test_all_types;
795-
ASSERT_DEATH(DynamicCastMessage<UNITTEST::TestRequired>(
796-
test_all_types_pointer_const_ref),
797-
"Cannot downcast " + test_all_types.GetTypeName() + " to " +
798-
UNITTEST::TestRequired::default_instance().GetTypeName());
795+
ASSERT_DEATH(
796+
DynamicCastMessage<UNITTEST::TestRequired>(
797+
test_all_types_pointer_const_ref),
798+
absl::StrCat("Cannot downcast ", test_all_types.GetTypeName(), " to ",
799+
UNITTEST::TestRequired::default_instance().GetTypeName()));
799800
}
800801

801802
TEST(MESSAGE_TEST_NAME, DownCastMessageValidType) {
@@ -829,8 +830,8 @@ TEST(MESSAGE_TEST_NAME, DownCastMessageInvalidPointerType) {
829830

830831
ASSERT_DEBUG_DEATH(
831832
DownCastMessage<UNITTEST::TestRequired>(test_all_types_pointer),
832-
"Cannot downcast " + test_all_types.GetTypeName() + " to " +
833-
UNITTEST::TestRequired::default_instance().GetTypeName());
833+
absl::StrCat("Cannot downcast ", test_all_types.GetTypeName(), " to ",
834+
UNITTEST::TestRequired::default_instance().GetTypeName()));
834835
}
835836

836837
TEST(MESSAGE_TEST_NAME, DownCastMessageInvalidReferenceType) {
@@ -840,8 +841,8 @@ TEST(MESSAGE_TEST_NAME, DownCastMessageInvalidReferenceType) {
840841

841842
ASSERT_DEBUG_DEATH(
842843
DownCastMessage<UNITTEST::TestRequired>(test_all_types_ref),
843-
"Cannot downcast " + test_all_types.GetTypeName() + " to " +
844-
UNITTEST::TestRequired::default_instance().GetTypeName());
844+
absl::StrCat("Cannot downcast ", test_all_types.GetTypeName(), " to ",
845+
UNITTEST::TestRequired::default_instance().GetTypeName()));
845846
}
846847

847848
TEST(MESSAGE_TEST_NAME, MessageDebugStringMatchesBehindPointerAndLitePointer) {

0 commit comments

Comments
 (0)