Skip to content

Commit

Permalink
Allow string_type for Edition 2023.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 620955493
  • Loading branch information
protobuf-github-bot authored and copybara-github committed Apr 2, 2024
1 parent a916d44 commit 1875a8d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
7 changes: 1 addition & 6 deletions src/google/protobuf/compiler/cpp/generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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."));
Expand Down
21 changes: 19 additions & 2 deletions src/google/protobuf/compiler/cpp/generator_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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) {
Expand Down

0 comments on commit 1875a8d

Please sign in to comment.