Allow string_type for Edition 2023.

PiperOrigin-RevId: 621050370
pull/16368/head
Protobuf Team Bot 11 months ago committed by Copybara-Service
parent 132168069d
commit d8251ebc43
  1. 7
      src/google/protobuf/compiler/cpp/generator.cc
  2. 21
      src/google/protobuf/compiler/cpp/generator_unittest.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."));

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

Loading…
Cancel
Save