Skip to content

Commit

Permalink
Fail to build descriptor if ctype is specified for non string/bytes f…
Browse files Browse the repository at this point in the history
…ields

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
  • Loading branch information
protobuf-github-bot authored and copybara-github committed Jan 31, 2024
1 parent 21ab745 commit 8b4c7a1
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
5 changes: 4 additions & 1 deletion src/google/protobuf/compiler/cpp/generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down Expand Up @@ -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(),
Expand Down
2 changes: 0 additions & 2 deletions src/google/protobuf/descriptor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 8b4c7a1

Please sign in to comment.