[ObjC] Validate MessageSet expectations.
- Make the generator error if things are violated.
- Have the runtime assert in debug about the things also.
PiperOrigin-RevId: 651810474
diff --git a/objectivec/GPBDescriptor.m b/objectivec/GPBDescriptor.m
index 6f2fa9b..5f769d4 100644
--- a/objectivec/GPBDescriptor.m
+++ b/objectivec/GPBDescriptor.m
@@ -267,6 +267,10 @@
fields:(NSArray *)fields
storageSize:(uint32_t)storageSize
wireFormat:(BOOL)wireFormat {
+#if defined(DEBUG) && DEBUG && !defined(NS_BLOCK_ASSERTIONS)
+ // This is also checked by the generator.
+ NSAssert(!wireFormat || fields.count == 0, @"Internal error: MessageSets should not have fields");
+#endif
if ((self = [super init])) {
messageClass_ = messageClass;
messageName_ = [messageName copy];
@@ -1139,8 +1143,16 @@
GPBRuntimeMatchFailure();
}
-#if defined(DEBUG) && DEBUG
+#if defined(DEBUG) && DEBUG && !defined(NS_BLOCK_ASSERTIONS)
NSAssert(usesClassRefs, @"Internal error: all extensions should have class refs");
+
+ // This is also checked by the generator.
+ // If the extension is a MessageSet extension, then it must be a message field.
+ NSAssert(
+ ((desc->options & GPBExtensionSetWireFormat) == 0) || desc->dataType == GPBDataTypeMessage,
+ @"Internal error: If a MessageSet extension is set, the data type must be a message.");
+ // NOTE: Could also check that the exteneded class is a MessageSet, but that would force the ObjC
+ // runtime to start up that class and that isn't desirable here.
#endif
if ((self = [super init])) {
diff --git a/src/google/protobuf/compiler/objectivec/extension.cc b/src/google/protobuf/compiler/objectivec/extension.cc
index 44a7505..528efa1 100644
--- a/src/google/protobuf/compiler/objectivec/extension.cc
+++ b/src/google/protobuf/compiler/objectivec/extension.cc
@@ -39,6 +39,11 @@
ABSL_CHECK(!descriptor->is_map())
<< "error: Extension is a map<>!"
<< " That used to be blocked by the compiler.";
+ ABSL_CHECK(
+ !descriptor->containing_type()->options().message_set_wire_format() ||
+ descriptor->type() == FieldDescriptor::TYPE_MESSAGE)
+ << "error: Extension to a message_set_wire_format message and the type "
+ "wasn't a message!";
}
void ExtensionGenerator::GenerateMembersHeader(io::Printer* printer) const {
diff --git a/src/google/protobuf/compiler/objectivec/message.cc b/src/google/protobuf/compiler/objectivec/message.cc
index cde92f6..984376b 100644
--- a/src/google/protobuf/compiler/objectivec/message.cc
+++ b/src/google/protobuf/compiler/objectivec/message.cc
@@ -133,7 +133,7 @@
// This is a reduced case of Descriptor::ExtensionRange with just start and end.
struct SimpleExtensionRange {
- SimpleExtensionRange(int start, int end) : start(start), end(end){};
+ SimpleExtensionRange(int start, int end) : start(start), end(end) {};
int start; // inclusive
int end; // exclusive
@@ -202,8 +202,12 @@
class_name_(ClassName(descriptor_)),
deprecated_attribute_(
GetOptionalDeprecatedAttribute(descriptor, descriptor->file())) {
- ABSL_DCHECK(!descriptor->options().map_entry())
+ ABSL_CHECK(!descriptor->options().map_entry())
<< "error: MessageGenerator create of a map<>!";
+ ABSL_CHECK(!descriptor->options().message_set_wire_format() ||
+ descriptor->field_count() == 0)
+ << "error: MessageGenerator message_set_wire_format should never have "
+ "fields!";
for (int i = 0; i < descriptor_->real_oneof_decl_count(); i++) {
oneof_generators_.push_back(std::make_unique<OneofGenerator>(
descriptor_->real_oneof_decl(i), generation_options));