[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
pull/17416/head
Thomas Van Lenten 5 months ago committed by Copybara-Service
parent 8e3eca48d0
commit 339261f58c
  1. 14
      objectivec/GPBDescriptor.m
  2. 5
      src/google/protobuf/compiler/objectivec/extension.cc
  3. 8
      src/google/protobuf/compiler/objectivec/message.cc

@ -267,6 +267,10 @@ static NSArray *NewFieldsArrayForHasIndex(int hasIndex, NSArray *allMessageField
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 @@ uint32_t GPBFieldAlternateTag(GPBFieldDescriptor *self) {
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])) {

@ -39,6 +39,11 @@ ExtensionGenerator::ExtensionGenerator(
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 {

@ -133,7 +133,7 @@ struct ExtensionRangeOrdering {
// 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 @@ MessageGenerator::MessageGenerator(const std::string& file_description_name,
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));

Loading…
Cancel
Save