浏览代码

[ObjC] Improve validation on public apis.

Thomas Van Lenten 5 年之前
父节点
当前提交
7b8a241b57
共有 1 个文件被更改,包括 131 次插入77 次删除
  1. 131 77
      objectivec/GPBUtilities.m

+ 131 - 77
objectivec/GPBUtilities.m

@@ -357,6 +357,9 @@ void GPBMaybeClearOneof(GPBMessage *self, GPBOneofDescriptor *oneof,
 //%TYPE GPBGetMessage##NAME##Field(GPBMessage *self,
 //%TYPE GPBGetMessage##NAME##Field(GPBMessage *self,
 //% TYPE$S            NAME$S       GPBFieldDescriptor *field) {
 //% TYPE$S            NAME$S       GPBFieldDescriptor *field) {
 //%#if defined(DEBUG) && DEBUG
 //%#if defined(DEBUG) && DEBUG
+//%  NSCAssert([[self descriptor] fieldWithNumber:field.number] == field,
+//%            @"FieldDescriptor %@ doesn't appear to be for %@ messages.",
+//%            field.name, [self class]);
 //%  NSCAssert(DataTypesEquivalent(GPBGetFieldDataType(field),
 //%  NSCAssert(DataTypesEquivalent(GPBGetFieldDataType(field),
 //%                                GPBDataType##NAME),
 //%                                GPBDataType##NAME),
 //%            @"Attempting to get value of TYPE from field %@ "
 //%            @"Attempting to get value of TYPE from field %@ "
@@ -378,15 +381,10 @@ void GPBMaybeClearOneof(GPBMessage *self, GPBOneofDescriptor *oneof,
 //%                   NAME$S     GPBFieldDescriptor *field,
 //%                   NAME$S     GPBFieldDescriptor *field,
 //%                   NAME$S     TYPE value) {
 //%                   NAME$S     TYPE value) {
 //%  if (self == nil || field == nil) return;
 //%  if (self == nil || field == nil) return;
-//%  GPBFileSyntax syntax = [self descriptor].file.syntax;
-//%  GPBSet##NAME##IvarWithFieldInternal(self, field, value, syntax);
-//%}
-//%
-//%void GPBSet##NAME##IvarWithFieldInternal(GPBMessage *self,
-//%            NAME$S                     GPBFieldDescriptor *field,
-//%            NAME$S                     TYPE value,
-//%            NAME$S                     GPBFileSyntax syntax) {
 //%#if defined(DEBUG) && DEBUG
 //%#if defined(DEBUG) && DEBUG
+//%  NSCAssert([[self descriptor] fieldWithNumber:field.number] == field,
+//%            @"FieldDescriptor %@ doesn't appear to be for %@ messages.",
+//%            field.name, [self class]);
 //%  NSCAssert(DataTypesEquivalent(GPBGetFieldDataType(field),
 //%  NSCAssert(DataTypesEquivalent(GPBGetFieldDataType(field),
 //%                                GPBDataType##NAME),
 //%                                GPBDataType##NAME),
 //%            @"Attempting to set field %@ of %@ which is of type %@ with "
 //%            @"Attempting to set field %@ of %@ which is of type %@ with "
@@ -394,6 +392,14 @@ void GPBMaybeClearOneof(GPBMessage *self, GPBOneofDescriptor *oneof,
 //%            [self class], field.name,
 //%            [self class], field.name,
 //%            TypeToString(GPBGetFieldDataType(field)));
 //%            TypeToString(GPBGetFieldDataType(field)));
 //%#endif
 //%#endif
+//%  GPBFileSyntax syntax = [self descriptor].file.syntax;
+//%  GPBSet##NAME##IvarWithFieldInternal(self, field, value, syntax);
+//%}
+//%
+//%void GPBSet##NAME##IvarWithFieldInternal(GPBMessage *self,
+//%            NAME$S                     GPBFieldDescriptor *field,
+//%            NAME$S                     TYPE value,
+//%            NAME$S                     GPBFileSyntax syntax) {
 //%  GPBOneofDescriptor *oneof = field->containingOneof_;
 //%  GPBOneofDescriptor *oneof = field->containingOneof_;
 //%  GPBMessageFieldDescription *fieldDesc = field->description_;
 //%  GPBMessageFieldDescription *fieldDesc = field->description_;
 //%  if (oneof) {
 //%  if (oneof) {
@@ -679,6 +685,16 @@ id GPBGetObjectIvarWithFieldNoAutocreate(GPBMessage *self,
 
 
 // Only exists for public api, no core code should use this.
 // Only exists for public api, no core code should use this.
 int32_t GPBGetMessageEnumField(GPBMessage *self, GPBFieldDescriptor *field) {
 int32_t GPBGetMessageEnumField(GPBMessage *self, GPBFieldDescriptor *field) {
+  #if defined(DEBUG) && DEBUG
+    NSCAssert([[self descriptor] fieldWithNumber:field.number] == field,
+              @"FieldDescriptor %@ doesn't appear to be for %@ messages.",
+              field.name, [self class]);
+    NSCAssert(GPBGetFieldDataType(field) == GPBDataTypeEnum,
+              @"Attempting to get value of type Enum from field %@ "
+              @"of %@ which is of type %@.",
+              [self class], field.name,
+              TypeToString(GPBGetFieldDataType(field)));
+  #endif
   GPBFileSyntax syntax = [self descriptor].file.syntax;
   GPBFileSyntax syntax = [self descriptor].file.syntax;
   return GPBGetEnumIvarWithFieldInternal(self, field, syntax);
   return GPBGetEnumIvarWithFieldInternal(self, field, syntax);
 }
 }
@@ -686,13 +702,6 @@ int32_t GPBGetMessageEnumField(GPBMessage *self, GPBFieldDescriptor *field) {
 int32_t GPBGetEnumIvarWithFieldInternal(GPBMessage *self,
 int32_t GPBGetEnumIvarWithFieldInternal(GPBMessage *self,
                                         GPBFieldDescriptor *field,
                                         GPBFieldDescriptor *field,
                                         GPBFileSyntax syntax) {
                                         GPBFileSyntax syntax) {
-#if defined(DEBUG) && DEBUG
-  NSCAssert(GPBGetFieldDataType(field) == GPBDataTypeEnum,
-            @"Attempting to get value of type Enum from field %@ "
-            @"of %@ which is of type %@.",
-            [self class], field.name,
-            TypeToString(GPBGetFieldDataType(field)));
-#endif
   int32_t result = GPBGetMessageInt32Field(self, field);
   int32_t result = GPBGetMessageInt32Field(self, field);
   // If this is presevering unknown enums, make sure the value is valid before
   // If this is presevering unknown enums, make sure the value is valid before
   // returning it.
   // returning it.
@@ -706,6 +715,16 @@ int32_t GPBGetEnumIvarWithFieldInternal(GPBMessage *self,
 // Only exists for public api, no core code should use this.
 // Only exists for public api, no core code should use this.
 void GPBSetMessageEnumField(GPBMessage *self, GPBFieldDescriptor *field,
 void GPBSetMessageEnumField(GPBMessage *self, GPBFieldDescriptor *field,
                             int32_t value) {
                             int32_t value) {
+  #if defined(DEBUG) && DEBUG
+    NSCAssert([[self descriptor] fieldWithNumber:field.number] == field,
+              @"FieldDescriptor %@ doesn't appear to be for %@ messages.",
+              field.name, [self class]);
+    NSCAssert(GPBGetFieldDataType(field) == GPBDataTypeEnum,
+              @"Attempting to set field %@ of %@ which is of type %@ with "
+              @"value of type Enum.",
+              [self class], field.name,
+              TypeToString(GPBGetFieldDataType(field)));
+  #endif
   GPBFileSyntax syntax = [self descriptor].file.syntax;
   GPBFileSyntax syntax = [self descriptor].file.syntax;
   GPBSetInt32IvarWithFieldInternal(self, field, value, syntax);
   GPBSetInt32IvarWithFieldInternal(self, field, value, syntax);
 }
 }
@@ -713,13 +732,6 @@ void GPBSetMessageEnumField(GPBMessage *self, GPBFieldDescriptor *field,
 void GPBSetEnumIvarWithFieldInternal(GPBMessage *self,
 void GPBSetEnumIvarWithFieldInternal(GPBMessage *self,
                                      GPBFieldDescriptor *field, int32_t value,
                                      GPBFieldDescriptor *field, int32_t value,
                                      GPBFileSyntax syntax) {
                                      GPBFileSyntax syntax) {
-#if defined(DEBUG) && DEBUG
-  NSCAssert(GPBGetFieldDataType(field) == GPBDataTypeEnum,
-            @"Attempting to set field %@ of %@ which is of type %@ with "
-            @"value of type Enum.",
-            [self class], field.name,
-            TypeToString(GPBGetFieldDataType(field)));
-#endif
   // Don't allow in unknown values.  Proto3 can use the Raw method.
   // Don't allow in unknown values.  Proto3 can use the Raw method.
   if (![field isValidEnumValue:value]) {
   if (![field isValidEnumValue:value]) {
     [NSException raise:NSInvalidArgumentException
     [NSException raise:NSInvalidArgumentException
@@ -746,6 +758,9 @@ void GPBSetMessageRawEnumField(GPBMessage *self, GPBFieldDescriptor *field,
 BOOL GPBGetMessageBoolField(GPBMessage *self,
 BOOL GPBGetMessageBoolField(GPBMessage *self,
                             GPBFieldDescriptor *field) {
                             GPBFieldDescriptor *field) {
 #if defined(DEBUG) && DEBUG
 #if defined(DEBUG) && DEBUG
+  NSCAssert([[self descriptor] fieldWithNumber:field.number] == field,
+            @"FieldDescriptor %@ doesn't appear to be for %@ messages.",
+            field.name, [self class]);
   NSCAssert(DataTypesEquivalent(GPBGetFieldDataType(field), GPBDataTypeBool),
   NSCAssert(DataTypesEquivalent(GPBGetFieldDataType(field), GPBDataTypeBool),
             @"Attempting to get value of type bool from field %@ "
             @"Attempting to get value of type bool from field %@ "
             @"of %@ which is of type %@.",
             @"of %@ which is of type %@.",
@@ -769,6 +784,16 @@ void GPBSetMessageBoolField(GPBMessage *self,
                             GPBFieldDescriptor *field,
                             GPBFieldDescriptor *field,
                             BOOL value) {
                             BOOL value) {
   if (self == nil || field == nil) return;
   if (self == nil || field == nil) return;
+  #if defined(DEBUG) && DEBUG
+    NSCAssert([[self descriptor] fieldWithNumber:field.number] == field,
+              @"FieldDescriptor %@ doesn't appear to be for %@ messages.",
+              field.name, [self class]);
+    NSCAssert(DataTypesEquivalent(GPBGetFieldDataType(field), GPBDataTypeBool),
+              @"Attempting to set field %@ of %@ which is of type %@ with "
+              @"value of type bool.",
+              [self class], field.name,
+              TypeToString(GPBGetFieldDataType(field)));
+  #endif
   GPBFileSyntax syntax = [self descriptor].file.syntax;
   GPBFileSyntax syntax = [self descriptor].file.syntax;
   GPBSetBoolIvarWithFieldInternal(self, field, value, syntax);
   GPBSetBoolIvarWithFieldInternal(self, field, value, syntax);
 }
 }
@@ -777,13 +802,6 @@ void GPBSetBoolIvarWithFieldInternal(GPBMessage *self,
                                      GPBFieldDescriptor *field,
                                      GPBFieldDescriptor *field,
                                      BOOL value,
                                      BOOL value,
                                      GPBFileSyntax syntax) {
                                      GPBFileSyntax syntax) {
-#if defined(DEBUG) && DEBUG
-  NSCAssert(DataTypesEquivalent(GPBGetFieldDataType(field), GPBDataTypeBool),
-            @"Attempting to set field %@ of %@ which is of type %@ with "
-            @"value of type bool.",
-            [self class], field.name,
-            TypeToString(GPBGetFieldDataType(field)));
-#endif
   GPBMessageFieldDescription *fieldDesc = field->description_;
   GPBMessageFieldDescription *fieldDesc = field->description_;
   GPBOneofDescriptor *oneof = field->containingOneof_;
   GPBOneofDescriptor *oneof = field->containingOneof_;
   if (oneof) {
   if (oneof) {
@@ -812,6 +830,9 @@ void GPBSetBoolIvarWithFieldInternal(GPBMessage *self,
 int32_t GPBGetMessageInt32Field(GPBMessage *self,
 int32_t GPBGetMessageInt32Field(GPBMessage *self,
                                 GPBFieldDescriptor *field) {
                                 GPBFieldDescriptor *field) {
 #if defined(DEBUG) && DEBUG
 #if defined(DEBUG) && DEBUG
+  NSCAssert([[self descriptor] fieldWithNumber:field.number] == field,
+            @"FieldDescriptor %@ doesn't appear to be for %@ messages.",
+            field.name, [self class]);
   NSCAssert(DataTypesEquivalent(GPBGetFieldDataType(field),
   NSCAssert(DataTypesEquivalent(GPBGetFieldDataType(field),
                                 GPBDataTypeInt32),
                                 GPBDataTypeInt32),
             @"Attempting to get value of int32_t from field %@ "
             @"Attempting to get value of int32_t from field %@ "
@@ -833,15 +854,10 @@ void GPBSetMessageInt32Field(GPBMessage *self,
                              GPBFieldDescriptor *field,
                              GPBFieldDescriptor *field,
                              int32_t value) {
                              int32_t value) {
   if (self == nil || field == nil) return;
   if (self == nil || field == nil) return;
-  GPBFileSyntax syntax = [self descriptor].file.syntax;
-  GPBSetInt32IvarWithFieldInternal(self, field, value, syntax);
-}
-
-void GPBSetInt32IvarWithFieldInternal(GPBMessage *self,
-                                      GPBFieldDescriptor *field,
-                                      int32_t value,
-                                      GPBFileSyntax syntax) {
 #if defined(DEBUG) && DEBUG
 #if defined(DEBUG) && DEBUG
+  NSCAssert([[self descriptor] fieldWithNumber:field.number] == field,
+            @"FieldDescriptor %@ doesn't appear to be for %@ messages.",
+            field.name, [self class]);
   NSCAssert(DataTypesEquivalent(GPBGetFieldDataType(field),
   NSCAssert(DataTypesEquivalent(GPBGetFieldDataType(field),
                                 GPBDataTypeInt32),
                                 GPBDataTypeInt32),
             @"Attempting to set field %@ of %@ which is of type %@ with "
             @"Attempting to set field %@ of %@ which is of type %@ with "
@@ -849,6 +865,14 @@ void GPBSetInt32IvarWithFieldInternal(GPBMessage *self,
             [self class], field.name,
             [self class], field.name,
             TypeToString(GPBGetFieldDataType(field)));
             TypeToString(GPBGetFieldDataType(field)));
 #endif
 #endif
+  GPBFileSyntax syntax = [self descriptor].file.syntax;
+  GPBSetInt32IvarWithFieldInternal(self, field, value, syntax);
+}
+
+void GPBSetInt32IvarWithFieldInternal(GPBMessage *self,
+                                      GPBFieldDescriptor *field,
+                                      int32_t value,
+                                      GPBFileSyntax syntax) {
   GPBOneofDescriptor *oneof = field->containingOneof_;
   GPBOneofDescriptor *oneof = field->containingOneof_;
   GPBMessageFieldDescription *fieldDesc = field->description_;
   GPBMessageFieldDescription *fieldDesc = field->description_;
   if (oneof) {
   if (oneof) {
@@ -882,6 +906,9 @@ void GPBSetInt32IvarWithFieldInternal(GPBMessage *self,
 uint32_t GPBGetMessageUInt32Field(GPBMessage *self,
 uint32_t GPBGetMessageUInt32Field(GPBMessage *self,
                                   GPBFieldDescriptor *field) {
                                   GPBFieldDescriptor *field) {
 #if defined(DEBUG) && DEBUG
 #if defined(DEBUG) && DEBUG
+  NSCAssert([[self descriptor] fieldWithNumber:field.number] == field,
+            @"FieldDescriptor %@ doesn't appear to be for %@ messages.",
+            field.name, [self class]);
   NSCAssert(DataTypesEquivalent(GPBGetFieldDataType(field),
   NSCAssert(DataTypesEquivalent(GPBGetFieldDataType(field),
                                 GPBDataTypeUInt32),
                                 GPBDataTypeUInt32),
             @"Attempting to get value of uint32_t from field %@ "
             @"Attempting to get value of uint32_t from field %@ "
@@ -903,15 +930,10 @@ void GPBSetMessageUInt32Field(GPBMessage *self,
                               GPBFieldDescriptor *field,
                               GPBFieldDescriptor *field,
                               uint32_t value) {
                               uint32_t value) {
   if (self == nil || field == nil) return;
   if (self == nil || field == nil) return;
-  GPBFileSyntax syntax = [self descriptor].file.syntax;
-  GPBSetUInt32IvarWithFieldInternal(self, field, value, syntax);
-}
-
-void GPBSetUInt32IvarWithFieldInternal(GPBMessage *self,
-                                       GPBFieldDescriptor *field,
-                                       uint32_t value,
-                                       GPBFileSyntax syntax) {
 #if defined(DEBUG) && DEBUG
 #if defined(DEBUG) && DEBUG
+  NSCAssert([[self descriptor] fieldWithNumber:field.number] == field,
+            @"FieldDescriptor %@ doesn't appear to be for %@ messages.",
+            field.name, [self class]);
   NSCAssert(DataTypesEquivalent(GPBGetFieldDataType(field),
   NSCAssert(DataTypesEquivalent(GPBGetFieldDataType(field),
                                 GPBDataTypeUInt32),
                                 GPBDataTypeUInt32),
             @"Attempting to set field %@ of %@ which is of type %@ with "
             @"Attempting to set field %@ of %@ which is of type %@ with "
@@ -919,6 +941,14 @@ void GPBSetUInt32IvarWithFieldInternal(GPBMessage *self,
             [self class], field.name,
             [self class], field.name,
             TypeToString(GPBGetFieldDataType(field)));
             TypeToString(GPBGetFieldDataType(field)));
 #endif
 #endif
+  GPBFileSyntax syntax = [self descriptor].file.syntax;
+  GPBSetUInt32IvarWithFieldInternal(self, field, value, syntax);
+}
+
+void GPBSetUInt32IvarWithFieldInternal(GPBMessage *self,
+                                       GPBFieldDescriptor *field,
+                                       uint32_t value,
+                                       GPBFileSyntax syntax) {
   GPBOneofDescriptor *oneof = field->containingOneof_;
   GPBOneofDescriptor *oneof = field->containingOneof_;
   GPBMessageFieldDescription *fieldDesc = field->description_;
   GPBMessageFieldDescription *fieldDesc = field->description_;
   if (oneof) {
   if (oneof) {
@@ -952,6 +982,9 @@ void GPBSetUInt32IvarWithFieldInternal(GPBMessage *self,
 int64_t GPBGetMessageInt64Field(GPBMessage *self,
 int64_t GPBGetMessageInt64Field(GPBMessage *self,
                                 GPBFieldDescriptor *field) {
                                 GPBFieldDescriptor *field) {
 #if defined(DEBUG) && DEBUG
 #if defined(DEBUG) && DEBUG
+  NSCAssert([[self descriptor] fieldWithNumber:field.number] == field,
+            @"FieldDescriptor %@ doesn't appear to be for %@ messages.",
+            field.name, [self class]);
   NSCAssert(DataTypesEquivalent(GPBGetFieldDataType(field),
   NSCAssert(DataTypesEquivalent(GPBGetFieldDataType(field),
                                 GPBDataTypeInt64),
                                 GPBDataTypeInt64),
             @"Attempting to get value of int64_t from field %@ "
             @"Attempting to get value of int64_t from field %@ "
@@ -973,15 +1006,10 @@ void GPBSetMessageInt64Field(GPBMessage *self,
                              GPBFieldDescriptor *field,
                              GPBFieldDescriptor *field,
                              int64_t value) {
                              int64_t value) {
   if (self == nil || field == nil) return;
   if (self == nil || field == nil) return;
-  GPBFileSyntax syntax = [self descriptor].file.syntax;
-  GPBSetInt64IvarWithFieldInternal(self, field, value, syntax);
-}
-
-void GPBSetInt64IvarWithFieldInternal(GPBMessage *self,
-                                      GPBFieldDescriptor *field,
-                                      int64_t value,
-                                      GPBFileSyntax syntax) {
 #if defined(DEBUG) && DEBUG
 #if defined(DEBUG) && DEBUG
+  NSCAssert([[self descriptor] fieldWithNumber:field.number] == field,
+            @"FieldDescriptor %@ doesn't appear to be for %@ messages.",
+            field.name, [self class]);
   NSCAssert(DataTypesEquivalent(GPBGetFieldDataType(field),
   NSCAssert(DataTypesEquivalent(GPBGetFieldDataType(field),
                                 GPBDataTypeInt64),
                                 GPBDataTypeInt64),
             @"Attempting to set field %@ of %@ which is of type %@ with "
             @"Attempting to set field %@ of %@ which is of type %@ with "
@@ -989,6 +1017,14 @@ void GPBSetInt64IvarWithFieldInternal(GPBMessage *self,
             [self class], field.name,
             [self class], field.name,
             TypeToString(GPBGetFieldDataType(field)));
             TypeToString(GPBGetFieldDataType(field)));
 #endif
 #endif
+  GPBFileSyntax syntax = [self descriptor].file.syntax;
+  GPBSetInt64IvarWithFieldInternal(self, field, value, syntax);
+}
+
+void GPBSetInt64IvarWithFieldInternal(GPBMessage *self,
+                                      GPBFieldDescriptor *field,
+                                      int64_t value,
+                                      GPBFileSyntax syntax) {
   GPBOneofDescriptor *oneof = field->containingOneof_;
   GPBOneofDescriptor *oneof = field->containingOneof_;
   GPBMessageFieldDescription *fieldDesc = field->description_;
   GPBMessageFieldDescription *fieldDesc = field->description_;
   if (oneof) {
   if (oneof) {
@@ -1022,6 +1058,9 @@ void GPBSetInt64IvarWithFieldInternal(GPBMessage *self,
 uint64_t GPBGetMessageUInt64Field(GPBMessage *self,
 uint64_t GPBGetMessageUInt64Field(GPBMessage *self,
                                   GPBFieldDescriptor *field) {
                                   GPBFieldDescriptor *field) {
 #if defined(DEBUG) && DEBUG
 #if defined(DEBUG) && DEBUG
+  NSCAssert([[self descriptor] fieldWithNumber:field.number] == field,
+            @"FieldDescriptor %@ doesn't appear to be for %@ messages.",
+            field.name, [self class]);
   NSCAssert(DataTypesEquivalent(GPBGetFieldDataType(field),
   NSCAssert(DataTypesEquivalent(GPBGetFieldDataType(field),
                                 GPBDataTypeUInt64),
                                 GPBDataTypeUInt64),
             @"Attempting to get value of uint64_t from field %@ "
             @"Attempting to get value of uint64_t from field %@ "
@@ -1043,15 +1082,10 @@ void GPBSetMessageUInt64Field(GPBMessage *self,
                               GPBFieldDescriptor *field,
                               GPBFieldDescriptor *field,
                               uint64_t value) {
                               uint64_t value) {
   if (self == nil || field == nil) return;
   if (self == nil || field == nil) return;
-  GPBFileSyntax syntax = [self descriptor].file.syntax;
-  GPBSetUInt64IvarWithFieldInternal(self, field, value, syntax);
-}
-
-void GPBSetUInt64IvarWithFieldInternal(GPBMessage *self,
-                                       GPBFieldDescriptor *field,
-                                       uint64_t value,
-                                       GPBFileSyntax syntax) {
 #if defined(DEBUG) && DEBUG
 #if defined(DEBUG) && DEBUG
+  NSCAssert([[self descriptor] fieldWithNumber:field.number] == field,
+            @"FieldDescriptor %@ doesn't appear to be for %@ messages.",
+            field.name, [self class]);
   NSCAssert(DataTypesEquivalent(GPBGetFieldDataType(field),
   NSCAssert(DataTypesEquivalent(GPBGetFieldDataType(field),
                                 GPBDataTypeUInt64),
                                 GPBDataTypeUInt64),
             @"Attempting to set field %@ of %@ which is of type %@ with "
             @"Attempting to set field %@ of %@ which is of type %@ with "
@@ -1059,6 +1093,14 @@ void GPBSetUInt64IvarWithFieldInternal(GPBMessage *self,
             [self class], field.name,
             [self class], field.name,
             TypeToString(GPBGetFieldDataType(field)));
             TypeToString(GPBGetFieldDataType(field)));
 #endif
 #endif
+  GPBFileSyntax syntax = [self descriptor].file.syntax;
+  GPBSetUInt64IvarWithFieldInternal(self, field, value, syntax);
+}
+
+void GPBSetUInt64IvarWithFieldInternal(GPBMessage *self,
+                                       GPBFieldDescriptor *field,
+                                       uint64_t value,
+                                       GPBFileSyntax syntax) {
   GPBOneofDescriptor *oneof = field->containingOneof_;
   GPBOneofDescriptor *oneof = field->containingOneof_;
   GPBMessageFieldDescription *fieldDesc = field->description_;
   GPBMessageFieldDescription *fieldDesc = field->description_;
   if (oneof) {
   if (oneof) {
@@ -1092,6 +1134,9 @@ void GPBSetUInt64IvarWithFieldInternal(GPBMessage *self,
 float GPBGetMessageFloatField(GPBMessage *self,
 float GPBGetMessageFloatField(GPBMessage *self,
                               GPBFieldDescriptor *field) {
                               GPBFieldDescriptor *field) {
 #if defined(DEBUG) && DEBUG
 #if defined(DEBUG) && DEBUG
+  NSCAssert([[self descriptor] fieldWithNumber:field.number] == field,
+            @"FieldDescriptor %@ doesn't appear to be for %@ messages.",
+            field.name, [self class]);
   NSCAssert(DataTypesEquivalent(GPBGetFieldDataType(field),
   NSCAssert(DataTypesEquivalent(GPBGetFieldDataType(field),
                                 GPBDataTypeFloat),
                                 GPBDataTypeFloat),
             @"Attempting to get value of float from field %@ "
             @"Attempting to get value of float from field %@ "
@@ -1113,15 +1158,10 @@ void GPBSetMessageFloatField(GPBMessage *self,
                              GPBFieldDescriptor *field,
                              GPBFieldDescriptor *field,
                              float value) {
                              float value) {
   if (self == nil || field == nil) return;
   if (self == nil || field == nil) return;
-  GPBFileSyntax syntax = [self descriptor].file.syntax;
-  GPBSetFloatIvarWithFieldInternal(self, field, value, syntax);
-}
-
-void GPBSetFloatIvarWithFieldInternal(GPBMessage *self,
-                                      GPBFieldDescriptor *field,
-                                      float value,
-                                      GPBFileSyntax syntax) {
 #if defined(DEBUG) && DEBUG
 #if defined(DEBUG) && DEBUG
+  NSCAssert([[self descriptor] fieldWithNumber:field.number] == field,
+            @"FieldDescriptor %@ doesn't appear to be for %@ messages.",
+            field.name, [self class]);
   NSCAssert(DataTypesEquivalent(GPBGetFieldDataType(field),
   NSCAssert(DataTypesEquivalent(GPBGetFieldDataType(field),
                                 GPBDataTypeFloat),
                                 GPBDataTypeFloat),
             @"Attempting to set field %@ of %@ which is of type %@ with "
             @"Attempting to set field %@ of %@ which is of type %@ with "
@@ -1129,6 +1169,14 @@ void GPBSetFloatIvarWithFieldInternal(GPBMessage *self,
             [self class], field.name,
             [self class], field.name,
             TypeToString(GPBGetFieldDataType(field)));
             TypeToString(GPBGetFieldDataType(field)));
 #endif
 #endif
+  GPBFileSyntax syntax = [self descriptor].file.syntax;
+  GPBSetFloatIvarWithFieldInternal(self, field, value, syntax);
+}
+
+void GPBSetFloatIvarWithFieldInternal(GPBMessage *self,
+                                      GPBFieldDescriptor *field,
+                                      float value,
+                                      GPBFileSyntax syntax) {
   GPBOneofDescriptor *oneof = field->containingOneof_;
   GPBOneofDescriptor *oneof = field->containingOneof_;
   GPBMessageFieldDescription *fieldDesc = field->description_;
   GPBMessageFieldDescription *fieldDesc = field->description_;
   if (oneof) {
   if (oneof) {
@@ -1162,6 +1210,9 @@ void GPBSetFloatIvarWithFieldInternal(GPBMessage *self,
 double GPBGetMessageDoubleField(GPBMessage *self,
 double GPBGetMessageDoubleField(GPBMessage *self,
                                 GPBFieldDescriptor *field) {
                                 GPBFieldDescriptor *field) {
 #if defined(DEBUG) && DEBUG
 #if defined(DEBUG) && DEBUG
+  NSCAssert([[self descriptor] fieldWithNumber:field.number] == field,
+            @"FieldDescriptor %@ doesn't appear to be for %@ messages.",
+            field.name, [self class]);
   NSCAssert(DataTypesEquivalent(GPBGetFieldDataType(field),
   NSCAssert(DataTypesEquivalent(GPBGetFieldDataType(field),
                                 GPBDataTypeDouble),
                                 GPBDataTypeDouble),
             @"Attempting to get value of double from field %@ "
             @"Attempting to get value of double from field %@ "
@@ -1183,15 +1234,10 @@ void GPBSetMessageDoubleField(GPBMessage *self,
                               GPBFieldDescriptor *field,
                               GPBFieldDescriptor *field,
                               double value) {
                               double value) {
   if (self == nil || field == nil) return;
   if (self == nil || field == nil) return;
-  GPBFileSyntax syntax = [self descriptor].file.syntax;
-  GPBSetDoubleIvarWithFieldInternal(self, field, value, syntax);
-}
-
-void GPBSetDoubleIvarWithFieldInternal(GPBMessage *self,
-                                       GPBFieldDescriptor *field,
-                                       double value,
-                                       GPBFileSyntax syntax) {
 #if defined(DEBUG) && DEBUG
 #if defined(DEBUG) && DEBUG
+  NSCAssert([[self descriptor] fieldWithNumber:field.number] == field,
+            @"FieldDescriptor %@ doesn't appear to be for %@ messages.",
+            field.name, [self class]);
   NSCAssert(DataTypesEquivalent(GPBGetFieldDataType(field),
   NSCAssert(DataTypesEquivalent(GPBGetFieldDataType(field),
                                 GPBDataTypeDouble),
                                 GPBDataTypeDouble),
             @"Attempting to set field %@ of %@ which is of type %@ with "
             @"Attempting to set field %@ of %@ which is of type %@ with "
@@ -1199,6 +1245,14 @@ void GPBSetDoubleIvarWithFieldInternal(GPBMessage *self,
             [self class], field.name,
             [self class], field.name,
             TypeToString(GPBGetFieldDataType(field)));
             TypeToString(GPBGetFieldDataType(field)));
 #endif
 #endif
+  GPBFileSyntax syntax = [self descriptor].file.syntax;
+  GPBSetDoubleIvarWithFieldInternal(self, field, value, syntax);
+}
+
+void GPBSetDoubleIvarWithFieldInternal(GPBMessage *self,
+                                       GPBFieldDescriptor *field,
+                                       double value,
+                                       GPBFileSyntax syntax) {
   GPBOneofDescriptor *oneof = field->containingOneof_;
   GPBOneofDescriptor *oneof = field->containingOneof_;
   GPBMessageFieldDescription *fieldDesc = field->description_;
   GPBMessageFieldDescription *fieldDesc = field->description_;
   if (oneof) {
   if (oneof) {