Skip to content

Commit 339261f

Browse files
thomasvlcopybara-github
authored andcommitted
[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
1 parent 8e3eca4 commit 339261f

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

objectivec/GPBDescriptor.m

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,10 @@ - (instancetype)initWithClass:(Class)messageClass
267267
fields:(NSArray *)fields
268268
storageSize:(uint32_t)storageSize
269269
wireFormat:(BOOL)wireFormat {
270+
#if defined(DEBUG) && DEBUG && !defined(NS_BLOCK_ASSERTIONS)
271+
// This is also checked by the generator.
272+
NSAssert(!wireFormat || fields.count == 0, @"Internal error: MessageSets should not have fields");
273+
#endif
270274
if ((self = [super init])) {
271275
messageClass_ = messageClass;
272276
messageName_ = [messageName copy];
@@ -1139,8 +1143,16 @@ - (instancetype)initWithExtensionDescription:(GPBExtensionDescription *)desc
11391143
GPBRuntimeMatchFailure();
11401144
}
11411145

1142-
#if defined(DEBUG) && DEBUG
1146+
#if defined(DEBUG) && DEBUG && !defined(NS_BLOCK_ASSERTIONS)
11431147
NSAssert(usesClassRefs, @"Internal error: all extensions should have class refs");
1148+
1149+
// This is also checked by the generator.
1150+
// If the extension is a MessageSet extension, then it must be a message field.
1151+
NSAssert(
1152+
((desc->options & GPBExtensionSetWireFormat) == 0) || desc->dataType == GPBDataTypeMessage,
1153+
@"Internal error: If a MessageSet extension is set, the data type must be a message.");
1154+
// NOTE: Could also check that the exteneded class is a MessageSet, but that would force the ObjC
1155+
// runtime to start up that class and that isn't desirable here.
11441156
#endif
11451157

11461158
if ((self = [super init])) {

src/google/protobuf/compiler/objectivec/extension.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ ExtensionGenerator::ExtensionGenerator(
3939
ABSL_CHECK(!descriptor->is_map())
4040
<< "error: Extension is a map<>!"
4141
<< " That used to be blocked by the compiler.";
42+
ABSL_CHECK(
43+
!descriptor->containing_type()->options().message_set_wire_format() ||
44+
descriptor->type() == FieldDescriptor::TYPE_MESSAGE)
45+
<< "error: Extension to a message_set_wire_format message and the type "
46+
"wasn't a message!";
4247
}
4348

4449
void ExtensionGenerator::GenerateMembersHeader(io::Printer* printer) const {

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ struct ExtensionRangeOrdering {
133133

134134
// This is a reduced case of Descriptor::ExtensionRange with just start and end.
135135
struct SimpleExtensionRange {
136-
SimpleExtensionRange(int start, int end) : start(start), end(end){};
136+
SimpleExtensionRange(int start, int end) : start(start), end(end) {};
137137
int start; // inclusive
138138
int end; // exclusive
139139

@@ -202,8 +202,12 @@ MessageGenerator::MessageGenerator(const std::string& file_description_name,
202202
class_name_(ClassName(descriptor_)),
203203
deprecated_attribute_(
204204
GetOptionalDeprecatedAttribute(descriptor, descriptor->file())) {
205-
ABSL_DCHECK(!descriptor->options().map_entry())
205+
ABSL_CHECK(!descriptor->options().map_entry())
206206
<< "error: MessageGenerator create of a map<>!";
207+
ABSL_CHECK(!descriptor->options().message_set_wire_format() ||
208+
descriptor->field_count() == 0)
209+
<< "error: MessageGenerator message_set_wire_format should never have "
210+
"fields!";
207211
for (int i = 0; i < descriptor_->real_oneof_decl_count(); i++) {
208212
oneof_generators_.push_back(std::make_unique<OneofGenerator>(
209213
descriptor_->real_oneof_decl(i), generation_options));

0 commit comments

Comments
 (0)