Skip to content

Commit d8251eb

Browse files
Allow string_type for Edition 2023.
PiperOrigin-RevId: 621050370
1 parent 1321680 commit d8251eb

2 files changed

Lines changed: 20 additions & 8 deletions

File tree

src/google/protobuf/compiler/cpp/generator.cc

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,6 @@ static bool IsEnumMapType(const FieldDescriptor& field) {
356356
absl::Status CppGenerator::ValidateFeatures(const FileDescriptor* file) const {
357357
absl::Status status = absl::OkStatus();
358358
auto edition = GetEdition(*file);
359-
absl::string_view filename = file->name();
360359

361360
google::protobuf::internal::VisitDescriptors(*file, [&](const FieldDescriptor& field) {
362361
const FeatureSet& resolved_features = GetResolvedSourceFeatures(field);
@@ -387,11 +386,7 @@ absl::Status CppGenerator::ValidateFeatures(const FileDescriptor* file) const {
387386
}
388387

389388
if (unresolved_features.has_string_type()) {
390-
if (!CanSkipEditionCheck(filename) && edition < Edition::EDITION_2024) {
391-
status = absl::FailedPreconditionError(absl::StrCat(
392-
"Field ", field.full_name(),
393-
" specifies string_type which is not currently allowed."));
394-
} else if (field.cpp_type() != FieldDescriptor::CPPTYPE_STRING) {
389+
if (field.cpp_type() != FieldDescriptor::CPPTYPE_STRING) {
395390
status = absl::FailedPreconditionError(absl::StrCat(
396391
"Field ", field.full_name(),
397392
" specifies string_type, but is not a string nor bytes field."));

src/google/protobuf/compiler/cpp/generator_unittest.cc

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ TEST_F(CppGeneratorTest, LegacyClosedEnumImplicit) {
144144
"Field Foo.bar has a closed enum type with implicit presence.");
145145
}
146146

147-
TEST_F(CppGeneratorTest, NoStringTypeTillEdition2024) {
147+
TEST_F(CppGeneratorTest, AllowStringTypeForEdition2023) {
148148
CreateTempFile("foo.proto", R"schema(
149149
edition = "2023";
150150
import "google/protobuf/cpp_features.proto";
@@ -155,11 +155,28 @@ TEST_F(CppGeneratorTest, NoStringTypeTillEdition2024) {
155155
}
156156
)schema");
157157

158+
RunProtoc(
159+
"protocol_compiler --proto_path=$tmpdir --cpp_out=$tmpdir foo.proto");
160+
161+
ExpectNoErrors();
162+
}
163+
164+
TEST_F(CppGeneratorTest, ErrorsOnBothStringTypeAndCtype) {
165+
CreateTempFile("foo.proto", R"schema(
166+
edition = "2023";
167+
import "google/protobuf/cpp_features.proto";
168+
169+
message Foo {
170+
int32 bar = 1;
171+
bytes baz = 2 [ctype = CORD, features.(pb.cpp).string_type = VIEW];
172+
}
173+
)schema");
174+
158175
RunProtoc(
159176
"protocol_compiler --proto_path=$tmpdir --cpp_out=$tmpdir foo.proto");
160177

161178
ExpectErrorSubstring(
162-
"Field Foo.baz specifies string_type which is not currently allowed.");
179+
"Foo.baz specifies both string_type and ctype which is not supported.");
163180
}
164181

165182
TEST_F(CppGeneratorTest, StringTypeForCord) {

0 commit comments

Comments
 (0)