Skip to content

Commit

Permalink
Mark proto2::Arena::GetArena as deprecated.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 514489808
  • Loading branch information
protobuf-github-bot authored and Copybara-Service committed Mar 6, 2023
1 parent b21c6b2 commit 9f95958
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 29 deletions.
4 changes: 4 additions & 0 deletions src/google/protobuf/arena.h
Expand Up @@ -389,7 +389,11 @@ class PROTOBUF_EXPORT PROTOBUF_ALIGNAS(8) Arena final {
// message, or nullptr otherwise. If possible, the call resolves at compile
// time. Note that we can often devirtualize calls to `value->GetArena()` so
// usually calling this method is unnecessary.
// TODO(b/271599886): remove this function.
template <typename T>
ABSL_DEPRECATED(
"This will be removed in a future release. Call value->GetArena() "
"instead.")
PROTOBUF_ALWAYS_INLINE static Arena* GetArena(T* value) {
return GetArenaInternal(value);
}
Expand Down
35 changes: 6 additions & 29 deletions src/google/protobuf/arena_unittest.cc
Expand Up @@ -1472,44 +1472,21 @@ TEST(ArenaTest, GetArenaShouldReturnTheArenaForArenaAllocatedMessages) {
Arena arena;
ArenaMessage* message = Arena::CreateMessage<ArenaMessage>(&arena);
const ArenaMessage* const_pointer_to_message = message;
EXPECT_EQ(&arena, Arena::GetArena(message));
EXPECT_EQ(&arena, Arena::GetArena(const_pointer_to_message));
EXPECT_EQ(&arena, message->GetArena());
EXPECT_EQ(&arena, const_pointer_to_message->GetArena());

// Test that the Message* / MessageLite* specialization SFINAE works.
const Message* const_pointer_to_message_type = message;
EXPECT_EQ(&arena, Arena::GetArena(const_pointer_to_message_type));
EXPECT_EQ(&arena, const_pointer_to_message_type->GetArena());
const MessageLite* const_pointer_to_message_lite_type = message;
EXPECT_EQ(&arena, Arena::GetArena(const_pointer_to_message_lite_type));
EXPECT_EQ(&arena, const_pointer_to_message_lite_type->GetArena());
}

TEST(ArenaTest, GetArenaShouldReturnNullForNonArenaAllocatedMessages) {
ArenaMessage message;
const ArenaMessage* const_pointer_to_message = &message;
EXPECT_EQ(nullptr, Arena::GetArena(&message));
EXPECT_EQ(nullptr, Arena::GetArena(const_pointer_to_message));
}

TEST(ArenaTest, GetArenaShouldReturnNullForNonArenaCompatibleTypes) {
// Test that GetArena returns nullptr for types that have a GetArena method
// that doesn't return Arena*.
struct {
int GetArena() const { return 0; }
} has_get_arena_method_wrong_return_type;
EXPECT_EQ(nullptr, Arena::GetArena(&has_get_arena_method_wrong_return_type));

// Test that GetArena returns nullptr for types that have a GetArena alias.
struct {
using GetArena = Arena*;
GetArena unused;
} has_get_arena_alias;
EXPECT_EQ(nullptr, Arena::GetArena(&has_get_arena_alias));

// Test that GetArena returns nullptr for types that have a GetArena data
// member.
struct {
Arena GetArena;
} has_get_arena_data_member;
EXPECT_EQ(nullptr, Arena::GetArena(&has_get_arena_data_member));
EXPECT_EQ(nullptr, message.GetArena());
EXPECT_EQ(nullptr, const_pointer_to_message->GetArena());
}

TEST(ArenaTest, AddCleanup) {
Expand Down

0 comments on commit 9f95958

Please sign in to comment.