From 8b4c7a1f33eb6849b57b6457d12adbd001109273 Mon Sep 17 00:00:00 2001 From: Protobuf Team Bot Date: Wed, 31 Jan 2024 11:17:39 -0800 Subject: [PATCH] Fail to build descriptor if ctype is specified for non string/bytes fields for Edition 2023 and beyond. Since ctype option is visible to all languages than just C++, this CL moves the ctype validation to DescriptorBuilder for Edition 2023 and beyond. This may cause failures to proto files that were never built for C++ but still have the issue. Note that this CL does not affect proto2 or proto3. PiperOrigin-RevId: 603106684 --- src/google/protobuf/compiler/cpp/generator.cc | 5 ++++- src/google/protobuf/descriptor.cc | 2 -- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/google/protobuf/compiler/cpp/generator.cc b/src/google/protobuf/compiler/cpp/generator.cc index 841785515f..952ef19c14 100644 --- a/src/google/protobuf/compiler/cpp/generator.cc +++ b/src/google/protobuf/compiler/cpp/generator.cc @@ -352,6 +352,8 @@ static bool IsEnumMapType(const FieldDescriptor& field) { absl::Status CppGenerator::ValidateFeatures(const FileDescriptor* file) const { absl::Status status = absl::OkStatus(); + auto edition = GetEdition(*file); + google::protobuf::internal::VisitDescriptors(*file, [&](const FieldDescriptor& field) { const FeatureSet& resolved_features = GetResolvedSourceFeatures(field); const pb::CppFeatures& unresolved_features = @@ -380,7 +382,8 @@ absl::Status CppGenerator::ValidateFeatures(const FileDescriptor* file) const { } } - if (field.options().has_ctype()) { + // 'ctype' check has moved to DescriptorBuilder for Edition 2023 and above. + if (edition < Edition::EDITION_2023 && field.options().has_ctype()) { if (field.cpp_type() != FieldDescriptor::CPPTYPE_STRING) { status = absl::FailedPreconditionError(absl::StrCat( "Field ", field.full_name(), diff --git a/src/google/protobuf/descriptor.cc b/src/google/protobuf/descriptor.cc index 5138f6024d..0cf7b75772 100644 --- a/src/google/protobuf/descriptor.cc +++ b/src/google/protobuf/descriptor.cc @@ -7664,8 +7664,6 @@ void DescriptorBuilder::ValidateOptions(const FieldDescriptor* field, ValidateFieldFeatures(field, proto); - // The following check is temporarily OSS only till we fix all affected - // google3 TAP tests. if (field->file()->edition() >= Edition::EDITION_2023 && field->options().has_ctype()) { if (field->cpp_type() != FieldDescriptor::CPPTYPE_STRING) {