Skip to content

Commit

Permalink
Allow legacy_closed_enum feature on enum map fields.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 590998931
  • Loading branch information
mkruskal-google authored and copybara-github committed Dec 14, 2023
1 parent c8f6195 commit 9afd752
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/google/protobuf/compiler/cpp/generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,16 @@ bool CppGenerator::Generate(const FileDescriptor* file,
return true;
}

static bool IsEnumMapType(const FieldDescriptor& field) {
if (!field.is_map()) return false;
for (int i = 0; i < field.message_type()->field_count(); ++i) {
if (field.message_type()->field(i)->type() == FieldDescriptor::TYPE_ENUM) {
return true;
}
}
return false;
}

absl::Status CppGenerator::ValidateFeatures(const FileDescriptor* file) const {
absl::Status status = absl::OkStatus();
google::protobuf::internal::VisitDescriptors(*file, [&](const FieldDescriptor& field) {
Expand All @@ -357,7 +367,8 @@ absl::Status CppGenerator::ValidateFeatures(const FileDescriptor* file) const {
// a lot of these conditions. Note: we do still validate the
// user-specified map field.
if (unresolved_features.has_legacy_closed_enum() &&
field.cpp_type() != FieldDescriptor::CPPTYPE_ENUM) {
field.cpp_type() != FieldDescriptor::CPPTYPE_ENUM &&
!IsEnumMapType(field)) {
status = absl::FailedPreconditionError(
absl::StrCat("Field ", field.full_name(),
" specifies the legacy_closed_enum feature but has "
Expand Down

0 comments on commit 9afd752

Please sign in to comment.