diff --git a/src/google/protobuf/repeated_field_unittest.cc b/src/google/protobuf/repeated_field_unittest.cc index 0443b2d41a..41cb12e04a 100644 --- a/src/google/protobuf/repeated_field_unittest.cc +++ b/src/google/protobuf/repeated_field_unittest.cc @@ -1795,12 +1795,30 @@ TEST(RepeatedPtrField, Erase) { } TEST(RepeatedPtrField, CopyConstruct) { + auto token = internal::InternalVisibilityForTesting{}; RepeatedPtrField source; source.Add()->assign("1"); source.Add()->assign("2"); - RepeatedPtrField destination(source); + RepeatedPtrField destination1(source); + ASSERT_EQ(2, destination1.size()); + EXPECT_EQ("1", destination1.Get(0)); + EXPECT_EQ("2", destination1.Get(1)); + + RepeatedPtrField destination2(token, nullptr, source); + ASSERT_EQ(2, destination2.size()); + EXPECT_EQ("1", destination2.Get(0)); + EXPECT_EQ("2", destination2.Get(1)); +} +TEST(RepeatedPtrField, CopyConstructWithArena) { + auto token = internal::InternalVisibilityForTesting{}; + RepeatedPtrField source; + source.Add()->assign("1"); + source.Add()->assign("2"); + + Arena arena; + RepeatedPtrField destination(token, &arena, source); ASSERT_EQ(2, destination.size()); EXPECT_EQ("1", destination.Get(0)); EXPECT_EQ("2", destination.Get(1)); diff --git a/src/google/protobuf/repeated_ptr_field.h b/src/google/protobuf/repeated_ptr_field.h index 816efec13e..fffe61ed77 100644 --- a/src/google/protobuf/repeated_ptr_field.h +++ b/src/google/protobuf/repeated_ptr_field.h @@ -60,6 +60,7 @@ #include "google/protobuf/port.h" #include "absl/base/attributes.h" #include "absl/log/absl_check.h" +#include "google/protobuf/internal_visibility.h" #include "google/protobuf/message_lite.h" #include "google/protobuf/port.h" @@ -924,9 +925,18 @@ class RepeatedPtrField final : private internal::RepeatedPtrFieldBase { public: constexpr RepeatedPtrField(); - explicit RepeatedPtrField(Arena* arena); + RepeatedPtrField(const RepeatedPtrField& other) + : RepeatedPtrField(nullptr, other) {} + + // Arena enabled constructors: for internal use only. + RepeatedPtrField(internal::InternalVisibility, Arena* arena) + : RepeatedPtrField(arena) {} + RepeatedPtrField(internal::InternalVisibility, Arena* arena, + const RepeatedPtrField& rhs) + : RepeatedPtrField(arena, rhs) {} - RepeatedPtrField(const RepeatedPtrField& other); + // TODO(b/290091828): make constructor private + explicit RepeatedPtrField(Arena* arena); template ::RepeatedPtrField(Arena* arena) } template -inline RepeatedPtrField::RepeatedPtrField( - const RepeatedPtrField& other) - : RepeatedPtrFieldBase() { +inline RepeatedPtrField::RepeatedPtrField(Arena* arena, + const RepeatedPtrField& rhs) + : RepeatedPtrFieldBase(arena) { StaticValidityCheck(); - MergeFrom(other); + MergeFrom(rhs); } template