From 1f21b164b1d3b013637fa370b9add63de62d804d Mon Sep 17 00:00:00 2001 From: Thomas Van Lenten Date: Fri, 21 Feb 2025 09:58:13 -0800 Subject: [PATCH] [ObjC] Retire GPBRuntimeMatchFailure() Make it an assert at runtime instead since it is just a sanity check. PiperOrigin-RevId: 729565587 --- objectivec/GPBDescriptor.m | 32 +++++++++--------------- objectivec/GPBUtilities.m | 7 ------ objectivec/GPBUtilities_PackagePrivate.h | 6 ----- 3 files changed, 12 insertions(+), 33 deletions(-) diff --git a/objectivec/GPBDescriptor.m b/objectivec/GPBDescriptor.m index 5c860b7fea..cb4060985d 100644 --- a/objectivec/GPBDescriptor.m +++ b/objectivec/GPBDescriptor.m @@ -104,16 +104,14 @@ static NSArray *NewFieldsArrayForHasIndex(int hasIndex, NSArray *allMessageField format:@"Proto generation source appears to have been from a version newer than " @"this runtime."]; } -#if defined(DEBUG) && DEBUG +#if defined(DEBUG) && DEBUG && !defined(NS_BLOCK_ASSERTIONS) // Compute the unknown options by this version of the runtime and then check the passed in // descriptor's options (from the generated code). If this does fire either something was // added incorrectly to the runtime or some sorta corruption has happened. GPBDescriptorInitializationFlags unknownFlags = (GPBDescriptorInitializationFlags)(~(GPBDescriptorInitializationFlag_FieldsWithDefault | GPBDescriptorInitializationFlag_WireFormat)); - if ((flags & unknownFlags) != 0) { - GPBRuntimeMatchFailure(); - } + NSAssert((flags & unknownFlags) == 0, @"Internal error: unknown descriptor flags set"); GPBFieldFlags mergedFieldFlags = GPBFieldNone; #endif // defined(DEBUG) && DEBUG @@ -126,13 +124,13 @@ static NSArray *NewFieldsArrayForHasIndex(int hasIndex, NSArray *allMessageField // Need correctly typed pointer for array indexing below to work. if (fieldsIncludeDefault) { desc = &(((GPBMessageFieldDescriptionWithDefault *)fieldDescriptions)[i]); -#if defined(DEBUG) && DEBUG +#if defined(DEBUG) && DEBUG && !defined(NS_BLOCK_ASSERTIONS) mergedFieldFlags |= (((GPBMessageFieldDescriptionWithDefault *)fieldDescriptions)[i]).core.flags; #endif } else { desc = &(((GPBMessageFieldDescription *)fieldDescriptions)[i]); -#if defined(DEBUG) && DEBUG +#if defined(DEBUG) && DEBUG && !defined(NS_BLOCK_ASSERTIONS) mergedFieldFlags |= (((GPBMessageFieldDescription *)fieldDescriptions)[i]).flags; #endif } @@ -141,16 +139,14 @@ static NSArray *NewFieldsArrayForHasIndex(int hasIndex, NSArray *allMessageField [fields addObject:fieldDescriptor]; [fieldDescriptor release]; } -#if defined(DEBUG) && DEBUG +#if defined(DEBUG) && DEBUG && !defined(NS_BLOCK_ASSERTIONS) // No real value in checking all the fields individually, just check the combined flags at the // end. GPBFieldFlags unknownFieldFlags = (GPBFieldFlags)(~(GPBFieldRequired | GPBFieldRepeated | GPBFieldPacked | GPBFieldOptional | GPBFieldHasDefaultValue | GPBFieldClearHasIvarOnZero | GPBFieldTextFormatNameCustom | GPBFieldMapKeyMask)); - if ((mergedFieldFlags & unknownFieldFlags) != 0) { - GPBRuntimeMatchFailure(); - } + NSAssert((mergedFieldFlags & unknownFieldFlags) == 0, @"Internal error: unknown field flags set"); #endif // defined(DEBUG) && DEBUG BOOL wireFormat = (flags & GPBDescriptorInitializationFlag_WireFormat) != 0; @@ -787,16 +783,14 @@ uint32_t GPBFieldAlternateTag(GPBFieldDescriptor *self) { format:@"Proto generation source appears to have been from a version newer than " @"this runtime."]; } -#if defined(DEBUG) && DEBUG +#if defined(DEBUG) && DEBUG && !defined(NS_BLOCK_ASSERTIONS) // Compute the unknown options by this version of the runtime and then check the passed in // descriptor's options (from the generated code). If this does fire either something was // added incorrectly to the runtime or some sorta corruption has happened. GPBEnumDescriptorInitializationFlags unknownFlags = (GPBEnumDescriptorInitializationFlags)(~(GPBEnumDescriptorInitializationFlag_IsClosed)); - if ((flags & unknownFlags) != 0) { - GPBRuntimeMatchFailure(); - } -#endif // defined(DEBUG) && DEBUG + NSAssert((flags & unknownFlags) == 0, @"Internal error: unknown enum flags set"); +#endif // defined(DEBUG) && DEBUG && !defined(NS_BLOCK_ASSERTIONS) GPBEnumDescriptor *descriptor = [[self alloc] initWithName:name valueNames:valueNames values:values @@ -1054,16 +1048,14 @@ uint32_t GPBFieldAlternateTag(GPBFieldDescriptor *self) { format:@"Proto generation source appears to have been from a version newer than " @"this runtime."]; } -#if defined(DEBUG) && DEBUG +#if defined(DEBUG) && DEBUG && !defined(NS_BLOCK_ASSERTIONS) // Compute the unknown options by this version of the runtime and then check the passed in // descriptor's options (from the generated code). If this does fire either something was // added incorrectly to the runtime or some sorta corruption has happened. GPBExtensionOptions unknownOptions = (GPBExtensionOptions)(~(GPBExtensionRepeated | GPBExtensionPacked)); - if ((desc->options & unknownOptions) != 0) { - GPBRuntimeMatchFailure(); - } -#endif // defined(DEBUG) && DEBUG + NSAssert((desc->options & unknownOptions) == 0, @"Internal error: unknown extension flags set"); +#endif // defined(DEBUG) && DEBUG && !defined(NS_BLOCK_ASSERTIONS) if ((self = [super init])) { description_ = desc; diff --git a/objectivec/GPBUtilities.m b/objectivec/GPBUtilities.m index 2874d0b118..b8389cda6e 100644 --- a/objectivec/GPBUtilities.m +++ b/objectivec/GPBUtilities.m @@ -223,13 +223,6 @@ void GPBCheckRuntimeVersionSupport(int32_t objcRuntimeVersion) { #endif // GOOGLE_PROTOBUF_OBJC_MIN_SUPPORTED_VERSION > 30007 -void GPBRuntimeMatchFailure(void) { - [NSException raise:NSInternalInconsistencyException - format:@"Proto generation source appears to have been from a" - @" version newer that this runtime (%d).", - GOOGLE_PROTOBUF_OBJC_VERSION]; -} - BOOL GPBMessageHasFieldNumberSet(GPBMessage *self, uint32_t fieldNumber) { GPBDescriptor *descriptor = [self descriptor]; GPBFieldDescriptor *field = [descriptor fieldWithNumber:fieldNumber]; diff --git a/objectivec/GPBUtilities_PackagePrivate.h b/objectivec/GPBUtilities_PackagePrivate.h index 858fd00ea1..576a913cc1 100644 --- a/objectivec/GPBUtilities_PackagePrivate.h +++ b/objectivec/GPBUtilities_PackagePrivate.h @@ -49,12 +49,6 @@ GPB_INLINE void GPB_DEBUG_CHECK_RUNTIME_VERSIONS(void) { #endif } -// Helper called within the library when the runtime detects something that -// indicates a older runtime is being used with newer generated code. Normally -// GPB_DEBUG_CHECK_RUNTIME_VERSIONS() gates this with a better message; this -// is just a final safety net to prevent otherwise hard to diagnose errors. -void GPBRuntimeMatchFailure(void); - // Conversion functions for de/serializing floating point types. GPB_INLINE int64_t GPBConvertDoubleToInt64(double v) {