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
pull/15603/head
Protobuf Team Bot 1 year ago committed by Copybara-Service
parent 21ab7459ee
commit 8b4c7a1f33
  1. 5
      src/google/protobuf/compiler/cpp/generator.cc
  2. 2
      src/google/protobuf/descriptor.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(),

@ -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) {

Loading…
Cancel
Save