Skip to content

Commit 7a75898

Browse files
ClaytonKnittelcopybara-github
authored andcommitted
Breaking change: Add a debug check that the target of CopyFrom is not a descendant of the source.
Previously, you were allowed to do the following: ```cc MyMessage msg; *msg.mutable_submessage() = msg; ``` However, this could leave the message in an inconsistent state in some contrived examples, and the order of operations is unclear and potentially compiler-dependent. Instead, if you would like to do this, you should be explicit about the order of operations: ```cc MyMessage msg; MyMessage msg_copy = msg; *msg.mutable_submessage() = std::move(msg_copy); ``` PiperOrigin-RevId: 820462156
1 parent af3430c commit 7a75898

File tree

4 files changed

+0
-9
lines changed

4 files changed

+0
-9
lines changed

src/google/protobuf/compiler/cpp/message.cc

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4702,11 +4702,9 @@ void MessageGenerator::GenerateCopyFrom(io::Printer* p) {
47024702
$DCHK$(!::_pbi::IsDescendant(*this, from))
47034703
<< "Source of CopyFrom cannot be a descendant of the "
47044704
"target.";
4705-
#ifdef PROTOBUF_FUTURE_NO_RECURSIVE_MESSAGE_COPY
47064705
$DCHK$(!::_pbi::IsDescendant(from, *this))
47074706
<< "Target of CopyFrom cannot be a descendant of the "
47084707
"source.";
4709-
#endif // PROTOBUF_FUTURE_NO_RECURSIVE_MESSAGE_COPY
47104708
Clear();)cc");
47114709
} else {
47124710
p->Emit(R"cc(

src/google/protobuf/message.cc

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,8 @@ void Message::CopyFrom(const Message& from) {
108108
// Fail if "from" is a descendant of "to" as such copy is not allowed.
109109
ABSL_DCHECK(!internal::IsDescendant(*this, from))
110110
<< "Source of CopyFrom cannot be a descendant of the target.";
111-
#ifdef PROTOBUF_FUTURE_NO_RECURSIVE_MESSAGE_COPY
112111
ABSL_DCHECK(!internal::IsDescendant(from, *this))
113112
<< "Target of CopyFrom cannot be a descendant of the source.";
114-
#endif // PROTOBUF_FUTURE_NO_RECURSIVE_MESSAGE_COPY
115113
Clear();
116114
class_to->full().merge_to_from(*this, from);
117115
} else {

src/google/protobuf/port_def.inc

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,10 +157,6 @@ static_assert(PROTOBUF_ABSL_MIN(20230125, 3),
157157
// Owner: ckennelly@, mkruskal@
158158
#define PROTOBUF_FUTURE_ADD_NODISCARD [[nodiscard]]
159159

160-
// Removes the ability to copy messages into themselves.
161-
// Owner: cknittel@, mkruskal@
162-
#define PROTOBUF_FUTURE_NO_RECURSIVE_MESSAGE_COPY 1
163-
164160
#else
165161

166162
#define PROTOBUF_FUTURE_ADD_NODISCARD

src/google/protobuf/port_undef.inc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@
7676
#undef PROTOBUF_FUTURE_DESCRIPTOR_EXTENSION_DECL
7777
#undef PROTOBUF_FUTURE_REMOVE_CREATEMESSAGE
7878
#undef PROTOBUF_FUTURE_ADD_NODISCARD
79-
#undef PROTOBUF_FUTURE_NO_RECURSIVE_MESSAGE_COPY
8079
#endif
8180

8281
#include "google/protobuf/os_macros_restore.inc"

0 commit comments

Comments
 (0)