Skip to content

Commit 00d838b

Browse files
habermancopybara-github
authored andcommitted
Make it a runtime flag whether we treat closed enums as open.
PiperOrigin-RevId: 850517623
1 parent 8d8e168 commit 00d838b

File tree

10 files changed

+39
-14
lines changed

10 files changed

+39
-14
lines changed

ruby/ext/google/protobuf_c/defs.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,10 @@ static VALUE DescriptorPool_alloc(VALUE klass) {
114114

115115
RB_OBJ_WRITE(ret, &self->def_to_descriptor, rb_hash_new());
116116
self->symtab = upb_DefPool_New();
117+
118+
// Ruby treats all enums as open.
119+
upb_DefPool_DisableClosedEnumChecking(self->symtab);
120+
117121
return ObjectCache_TryAdd(self->symtab, ret);
118122
}
119123

ruby/lib/google/protobuf/ffi/descriptor_pool.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class FFI
1212
attach_function :add_serialized_file, :upb_DefPool_AddFile, [:DefPool, :FileDescriptorProto, Status.by_ref], :FileDef
1313
attach_function :free_descriptor_pool, :upb_DefPool_Free, [:DefPool], :void
1414
attach_function :create_descriptor_pool,:upb_DefPool_New, [], :DefPool
15+
attach_function :disable_closed_enum_checking, :upb_DefPool_DisableClosedEnumChecking, [:DefPool], :void
1516
attach_function :get_extension_registry,:upb_DefPool_ExtensionRegistry, [:DefPool], :ExtensionRegistry
1617
attach_function :lookup_enum, :upb_DefPool_FindEnumByName, [:DefPool, :string], EnumDescriptor
1718
attach_function :lookup_extension, :upb_DefPool_FindExtensionByName,[:DefPool, :string], FieldDescriptor
@@ -30,6 +31,9 @@ def initialize
3031
@descriptor_pool = ::FFI::AutoPointer.new(Google::Protobuf::FFI.create_descriptor_pool, Google::Protobuf::FFI.method(:free_descriptor_pool))
3132
@descriptor_class_by_def = {}
3233

34+
# Ruby treats all enums as open.
35+
Google::Protobuf::FFI.disable_closed_enum_checking(@descriptor_pool)
36+
3337
# Should always be the last expression of the initializer to avoid
3438
# leaking references to this object before construction is complete.
3539
Google::Protobuf::OBJECT_CACHE.try_add @descriptor_pool.address, self

upb/bazel/amalgamate.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,6 @@ def amalgamate(self, h_files, c_files):
5252
self.h_files = set(h_files)
5353
self.output_c.write("/* Amalgamated source file */\n")
5454
self.output_c.write('#include "%s"\n' % (self.h_out))
55-
if self.h_out == "ruby-upb.h":
56-
self.output_h.write("// Ruby is still using proto3 enum semantics for proto2\n")
57-
self.output_h.write("#define UPB_DISABLE_CLOSED_ENUM_CHECKING\n")
58-
5955
self.output_h.write("/* Amalgamated source file */\n")
6056

6157
port_def = self._find_include_file("upb/port/def.inc")

upb/port/def.inc

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -501,14 +501,6 @@ Error, UINTPTR_MAX is undefined
501501

502502
#undef UPB_FASTTABLE_SUPPORTED
503503

504-
/* Disable proto2 arena behavior (TEMPORARY) **********************************/
505-
506-
#ifdef UPB_DISABLE_CLOSED_ENUM_CHECKING
507-
#define UPB_TREAT_CLOSED_ENUMS_LIKE_OPEN 1
508-
#else
509-
#define UPB_TREAT_CLOSED_ENUMS_LIKE_OPEN 0
510-
#endif
511-
512504
#if defined(__cplusplus)
513505
#if defined(__clang__) || UPB_GNUC_MIN(6, 0)
514506
// https://gcc.gnu.org/gcc-6/changes.html

upb/port/undef.inc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@
5959
#undef UPB_MSAN
6060
#undef UPB_MALLOC_ALIGN
6161
#undef UPB_TSAN
62-
#undef UPB_TREAT_CLOSED_ENUMS_LIKE_OPEN
6362
#undef UPB_DEPRECATED
6463
#undef UPB_GNUC_MIN
6564
#undef UPB_DESCRIPTOR_UPB_H_FILENAME

upb/reflection/def_pool.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ struct upb_DefPool {
5050
void* scratch_data;
5151
size_t scratch_size;
5252
size_t bytes_loaded;
53+
bool disable_closed_enum_checking;
5354
};
5455

5556
void upb_DefPool_Free(upb_DefPool* s) {
@@ -67,6 +68,7 @@ upb_DefPool* upb_DefPool_New(void) {
6768

6869
s->arena = upb_Arena_New();
6970
s->bytes_loaded = 0;
71+
s->disable_closed_enum_checking = false;
7072

7173
s->scratch_size = 240;
7274
s->scratch_data = upb_gmalloc(s->scratch_size);
@@ -99,6 +101,15 @@ upb_DefPool* upb_DefPool_New(void) {
99101
return NULL;
100102
}
101103

104+
void upb_DefPool_DisableClosedEnumChecking(upb_DefPool* s) {
105+
UPB_ASSERT(upb_strtable_count(&s->files) == 0);
106+
s->disable_closed_enum_checking = true;
107+
}
108+
109+
bool upb_DefPool_ClosedEnumCheckingDisabled(const upb_DefPool* s) {
110+
return s->disable_closed_enum_checking;
111+
}
112+
102113
const UPB_DESC(FeatureSetDefaults) *
103114
upb_DefPool_FeatureSetDefaults(const upb_DefPool* s) {
104115
return s->feature_set_defaults;

upb/reflection/def_pool.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,20 @@ const upb_FieldDef** upb_DefPool_GetAllExtensions(const upb_DefPool* s,
8686
const upb_MessageDef* m,
8787
size_t* count);
8888

89+
// If called, closed enums will be treated as open enums. This is non-standard
90+
// behavior and will cause conformance tests to fail, but it is more useful
91+
// behavior overall and can be used in situations where where the
92+
// non-conformance is acceptable.
93+
//
94+
// This function may only be called immediately after upb_DefPool_New().
95+
// It is an error to call it on an existing def pool or after defs have
96+
// already been added to the pool.
97+
//
98+
// Note: we still require that implicit presence fields have zero as their
99+
// default value.
100+
UPB_API void upb_DefPool_DisableClosedEnumChecking(upb_DefPool* s);
101+
bool upb_DefPool_ClosedEnumCheckingDisabled(const upb_DefPool* s);
102+
89103
#ifdef __cplusplus
90104
} /* extern "C" */
91105
#endif

upb/reflection/enum_def.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ static bool upb_EnumDef_IsSpecifiedAsClosed(const upb_EnumDef* e) {
167167
}
168168

169169
bool upb_EnumDef_IsClosed(const upb_EnumDef* e) {
170-
if (UPB_TREAT_CLOSED_ENUMS_LIKE_OPEN) return false;
170+
if (_upb_FileDef_ClosedEnumCheckingDisabled(e->file)) return false;
171171
return upb_EnumDef_IsSpecifiedAsClosed(e);
172172
}
173173

upb/reflection/file_def.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,10 @@ const int32_t* _upb_FileDef_WeakDependencyIndexes(const upb_FileDef* f) {
115115
return f->weak_deps;
116116
}
117117

118+
bool _upb_FileDef_ClosedEnumCheckingDisabled(const upb_FileDef* f) {
119+
return upb_DefPool_ClosedEnumCheckingDisabled(f->symtab);
120+
}
121+
118122
int upb_FileDef_TopLevelEnumCount(const upb_FileDef* f) {
119123
return f->top_lvl_enum_count;
120124
}

upb/reflection/internal/file_def.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const upb_MiniTableExtension* _upb_FileDef_ExtensionMiniTable(
2121
const upb_FileDef* f, int i);
2222
const int32_t* _upb_FileDef_PublicDependencyIndexes(const upb_FileDef* f);
2323
const int32_t* _upb_FileDef_WeakDependencyIndexes(const upb_FileDef* f);
24+
bool _upb_FileDef_ClosedEnumCheckingDisabled(const upb_FileDef* f);
2425

2526
// upb_FileDef_Package() returns "" if f->package is NULL, this does not.
2627
const char* _upb_FileDef_RawPackage(const upb_FileDef* f);

0 commit comments

Comments
 (0)