From d8251ebc43cb21d3239a38554a2f0a618f368a78 Mon Sep 17 00:00:00 2001 From: Protobuf Team Bot Date: Mon, 1 Apr 2024 21:54:22 -0700 Subject: [PATCH] Allow string_type for Edition 2023. PiperOrigin-RevId: 621050370 --- src/google/protobuf/compiler/cpp/generator.cc | 7 +------ .../compiler/cpp/generator_unittest.cc | 21 +++++++++++++++++-- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/src/google/protobuf/compiler/cpp/generator.cc b/src/google/protobuf/compiler/cpp/generator.cc index 0bced323eef82..6b166e74fb5e9 100644 --- a/src/google/protobuf/compiler/cpp/generator.cc +++ b/src/google/protobuf/compiler/cpp/generator.cc @@ -356,7 +356,6 @@ static bool IsEnumMapType(const FieldDescriptor& field) { absl::Status CppGenerator::ValidateFeatures(const FileDescriptor* file) const { absl::Status status = absl::OkStatus(); auto edition = GetEdition(*file); - absl::string_view filename = file->name(); google::protobuf::internal::VisitDescriptors(*file, [&](const FieldDescriptor& field) { const FeatureSet& resolved_features = GetResolvedSourceFeatures(field); @@ -387,11 +386,7 @@ absl::Status CppGenerator::ValidateFeatures(const FileDescriptor* file) const { } if (unresolved_features.has_string_type()) { - if (!CanSkipEditionCheck(filename) && edition < Edition::EDITION_2024) { - status = absl::FailedPreconditionError(absl::StrCat( - "Field ", field.full_name(), - " specifies string_type which is not currently allowed.")); - } else if (field.cpp_type() != FieldDescriptor::CPPTYPE_STRING) { + if (field.cpp_type() != FieldDescriptor::CPPTYPE_STRING) { status = absl::FailedPreconditionError(absl::StrCat( "Field ", field.full_name(), " specifies string_type, but is not a string nor bytes field.")); diff --git a/src/google/protobuf/compiler/cpp/generator_unittest.cc b/src/google/protobuf/compiler/cpp/generator_unittest.cc index a9cc448a95d8a..201805a81c537 100644 --- a/src/google/protobuf/compiler/cpp/generator_unittest.cc +++ b/src/google/protobuf/compiler/cpp/generator_unittest.cc @@ -144,7 +144,7 @@ TEST_F(CppGeneratorTest, LegacyClosedEnumImplicit) { "Field Foo.bar has a closed enum type with implicit presence."); } -TEST_F(CppGeneratorTest, NoStringTypeTillEdition2024) { +TEST_F(CppGeneratorTest, AllowStringTypeForEdition2023) { CreateTempFile("foo.proto", R"schema( edition = "2023"; import "google/protobuf/cpp_features.proto"; @@ -155,11 +155,28 @@ TEST_F(CppGeneratorTest, NoStringTypeTillEdition2024) { } )schema"); + RunProtoc( + "protocol_compiler --proto_path=$tmpdir --cpp_out=$tmpdir foo.proto"); + + ExpectNoErrors(); +} + +TEST_F(CppGeneratorTest, ErrorsOnBothStringTypeAndCtype) { + CreateTempFile("foo.proto", R"schema( + edition = "2023"; + import "google/protobuf/cpp_features.proto"; + + message Foo { + int32 bar = 1; + bytes baz = 2 [ctype = CORD, features.(pb.cpp).string_type = VIEW]; + } + )schema"); + RunProtoc( "protocol_compiler --proto_path=$tmpdir --cpp_out=$tmpdir foo.proto"); ExpectErrorSubstring( - "Field Foo.baz specifies string_type which is not currently allowed."); + "Foo.baz specifies both string_type and ctype which is not supported."); } TEST_F(CppGeneratorTest, StringTypeForCord) {