Skip to content

Commit

Permalink
Prepare MessageLite::GetTypeName to be upgraded to return
Browse files Browse the repository at this point in the history
`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
  • Loading branch information
protobuf-github-bot authored and copybara-github committed Aug 26, 2024
1 parent 0d9baf3 commit 30a8ef5
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 20 deletions.
21 changes: 12 additions & 9 deletions src/google/protobuf/lite_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1363,9 +1363,10 @@ TEST(LiteTest, DynamicCastMessage) {
TEST(LiteTest, DynamicCastMessageInvalidReferenceType) {
CastType1 test_type_1;
const MessageLite& test_type_1_pointer_const_ref = test_type_1;
ASSERT_DEATH(DynamicCastMessage<CastType2>(test_type_1_pointer_const_ref),
"Cannot downcast " + test_type_1.GetTypeName() + " to " +
CastType2::default_instance().GetTypeName());
ASSERT_DEATH(
DynamicCastMessage<CastType2>(test_type_1_pointer_const_ref),
absl::StrCat("Cannot downcast ", test_type_1.GetTypeName(), " to ",
CastType2::default_instance().GetTypeName()));
}
#endif // GTEST_HAS_DEATH_TEST

Expand Down Expand Up @@ -1396,19 +1397,21 @@ TEST(LiteTest, DownCastMessageInvalidPointerType) {

MessageLite* test_type_1_pointer = &test_type_1;

ASSERT_DEBUG_DEATH(DownCastMessage<CastType2>(test_type_1_pointer),
"Cannot downcast " + test_type_1.GetTypeName() + " to " +
CastType2::default_instance().GetTypeName());
ASSERT_DEBUG_DEATH(
DownCastMessage<CastType2>(test_type_1_pointer),
absl::StrCat("Cannot downcast ", test_type_1.GetTypeName(), " to ",
CastType2::default_instance().GetTypeName()));
}

TEST(LiteTest, DownCastMessageInvalidReferenceType) {
CastType1 test_type_1;

MessageLite& test_type_1_pointer = test_type_1;

ASSERT_DEBUG_DEATH(DownCastMessage<CastType2>(test_type_1_pointer),
"Cannot downcast " + test_type_1.GetTypeName() + " to " +
CastType2::default_instance().GetTypeName());
ASSERT_DEBUG_DEATH(
DownCastMessage<CastType2>(test_type_1_pointer),
absl::StrCat("Cannot downcast ", test_type_1.GetTypeName(), " to ",
CastType2::default_instance().GetTypeName()));
}
#endif // GTEST_HAS_DEATH_TEST

Expand Down
4 changes: 2 additions & 2 deletions src/google/protobuf/message_lite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ const char* MessageLite::_InternalParse(const char* ptr,
return internal::TcParser::ParseLoop(this, ptr, ctx, GetTcParseTable());
}

std::string MessageLite::GetTypeName() const {
return std::string(TypeId::Get(*this).name());
internal::GetTypeNameReturnType MessageLite::GetTypeName() const {
return internal::GetTypeNameReturnType(TypeId::Get(*this).name());
}

absl::string_view TypeId::name() const {
Expand Down
8 changes: 7 additions & 1 deletion src/google/protobuf/message_lite.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,12 @@ inline int ToIntSize(size_t size) {
return static_cast<int>(size);
}

#if defined(PROTOBUF_FUTURE_STRING_VIEW_RETURN_TYPE)
using GetTypeNameReturnType = absl::string_view;
#else
using GetTypeNameReturnType = std::string;
#endif

// Default empty string object. Don't use this directly. Instead, call
// GetEmptyString() to get the reference. This empty string is aligned with a
// minimum alignment of 8 bytes to match the requirement of ArenaStringPtr.
Expand Down Expand Up @@ -240,7 +246,7 @@ class PROTOBUF_EXPORT MessageLite {
// Basic Operations ------------------------------------------------

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

// Construct a new instance of the same type. Ownership is passed to the
// caller.
Expand Down
17 changes: 9 additions & 8 deletions src/google/protobuf/message_unittest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -792,10 +792,11 @@ TEST(MESSAGE_TEST_NAME, DynamicCastMessage) {
TEST(MESSAGE_TEST_NAME, DynamicCastMessageInvalidReferenceType) {
UNITTEST::TestAllTypes test_all_types;
const MessageLite& test_all_types_pointer_const_ref = test_all_types;
ASSERT_DEATH(DynamicCastMessage<UNITTEST::TestRequired>(
test_all_types_pointer_const_ref),
"Cannot downcast " + test_all_types.GetTypeName() + " to " +
UNITTEST::TestRequired::default_instance().GetTypeName());
ASSERT_DEATH(
DynamicCastMessage<UNITTEST::TestRequired>(
test_all_types_pointer_const_ref),
absl::StrCat("Cannot downcast ", test_all_types.GetTypeName(), " to ",
UNITTEST::TestRequired::default_instance().GetTypeName()));
}

TEST(MESSAGE_TEST_NAME, DownCastMessageValidType) {
Expand Down Expand Up @@ -829,8 +830,8 @@ TEST(MESSAGE_TEST_NAME, DownCastMessageInvalidPointerType) {

ASSERT_DEBUG_DEATH(
DownCastMessage<UNITTEST::TestRequired>(test_all_types_pointer),
"Cannot downcast " + test_all_types.GetTypeName() + " to " +
UNITTEST::TestRequired::default_instance().GetTypeName());
absl::StrCat("Cannot downcast ", test_all_types.GetTypeName(), " to ",
UNITTEST::TestRequired::default_instance().GetTypeName()));
}

TEST(MESSAGE_TEST_NAME, DownCastMessageInvalidReferenceType) {
Expand All @@ -840,8 +841,8 @@ TEST(MESSAGE_TEST_NAME, DownCastMessageInvalidReferenceType) {

ASSERT_DEBUG_DEATH(
DownCastMessage<UNITTEST::TestRequired>(test_all_types_ref),
"Cannot downcast " + test_all_types.GetTypeName() + " to " +
UNITTEST::TestRequired::default_instance().GetTypeName());
absl::StrCat("Cannot downcast ", test_all_types.GetTypeName(), " to ",
UNITTEST::TestRequired::default_instance().GetTypeName()));
}

TEST(MESSAGE_TEST_NAME, MessageDebugStringMatchesBehindPointerAndLitePointer) {
Expand Down

0 comments on commit 30a8ef5

Please sign in to comment.