Skip to content

Commit b359e50

Browse files
anandoleecopybara-github
authored andcommitted
Lock down ctype=CORD in proto file.
ctype can not be used for none string/bytes fields. ctye=CORD can not be used for extensions. A new macro PROTOBUF_FUTURE_REMOVE_WRONG_CTYPE is added that will be flipped for our next breaking release. PiperOrigin-RevId: 555615362
1 parent e701f4f commit b359e50

File tree

7 files changed

+78
-13
lines changed

7 files changed

+78
-13
lines changed

csharp/protos/unittest.proto

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ extend TestAllExtensions {
258258
optional_import_enum_extension = 23;
259259

260260
optional string optional_string_piece_extension = 24 [ctype = STRING_PIECE];
261-
optional string optional_cord_extension = 25 [ctype = CORD];
261+
optional string optional_cord_extension = 25;
262262

263263
optional protobuf_unittest_import_proto2.PublicImportMessage
264264
optional_public_import_message_extension = 26;
@@ -298,7 +298,7 @@ extend TestAllExtensions {
298298
repeated_import_enum_extension = 53;
299299

300300
repeated string repeated_string_piece_extension = 54 [ctype = STRING_PIECE];
301-
repeated string repeated_cord_extension = 55 [ctype = CORD];
301+
repeated string repeated_cord_extension = 55;
302302

303303
repeated TestAllTypes.NestedMessage repeated_lazy_message_extension = 57
304304
[lazy = true];
@@ -329,7 +329,7 @@ extend TestAllExtensions {
329329

330330
optional string default_string_piece_extension = 84
331331
[ctype = STRING_PIECE, default = "abc"];
332-
optional string default_cord_extension = 85 [ctype = CORD, default = "123"];
332+
optional string default_cord_extension = 85 [default = "123"];
333333

334334
// For oneof test
335335
optional uint32 oneof_uint32_extension = 111;

objectivec/Tests/unittest.proto

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ extend TestAllExtensions {
244244
optional_import_enum_extension = 23;
245245

246246
optional string optional_string_piece_extension = 24 [ctype=STRING_PIECE];
247-
optional string optional_cord_extension = 25 [ctype=CORD];
247+
optional string optional_cord_extension = 25;
248248

249249
optional objc.protobuf.tests.public_import.Message
250250
optional_public_import_message_extension = 26;
@@ -286,7 +286,7 @@ extend TestAllExtensions {
286286
repeated_import_enum_extension = 53;
287287

288288
repeated string repeated_string_piece_extension = 54 [ctype=STRING_PIECE];
289-
repeated string repeated_cord_extension = 55 [ctype=CORD];
289+
repeated string repeated_cord_extension = 55;
290290

291291
repeated TestAllTypes.NestedMessage
292292
repeated_lazy_message_extension = 57 [lazy=true];
@@ -317,7 +317,7 @@ extend TestAllExtensions {
317317

318318
optional string default_string_piece_extension = 84 [ctype=STRING_PIECE,
319319
default="abc"];
320-
optional string default_cord_extension = 85 [ctype=CORD, default="123"];
320+
optional string default_cord_extension = 85 [default="123"];
321321

322322
// For oneof test
323323
optional uint32 oneof_uint32_extension = 111;

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,22 @@ absl::Status CppGenerator::ValidateFeatures(const FileDescriptor* file) const {
379379
absl::StrCat("Field ", field.full_name(),
380380
" has a closed enum type with implicit presence."));
381381
}
382+
#ifdef PROTOBUF_FUTURE_REMOVE_WRONG_CTYPE
383+
if (field.options().has_ctype()) {
384+
if (field.cpp_type() != FieldDescriptor::CPPTYPE_STRING) {
385+
status = absl::FailedPreconditionError(absl::StrCat(
386+
"Field ", field.full_name(),
387+
" specifies ctype, but is not a string nor bytes field."));
388+
}
389+
if (field.options().ctype() == FieldOptions::CORD) {
390+
if (field.is_extension()) {
391+
status = absl::FailedPreconditionError(absl::StrCat(
392+
"Extension ", field.full_name(),
393+
" specifies ctype=CORD which is not supported for extensions."));
394+
}
395+
}
396+
}
397+
#endif // !PROTOBUF_FUTURE_REMOVE_WRONG_CTYPE
382398
});
383399
return status;
384400
}

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,40 @@ TEST_F(CppGeneratorTest, LegacyClosedEnumImplicit) {
172172
"Field Foo.bar has a closed enum type with implicit presence.");
173173
}
174174

175+
#ifdef PROTOBUF_FUTURE_REMOVE_WRONG_CTYPE
176+
TEST_F(CppGeneratorTest, CtypeOnNoneStringFieldTest) {
177+
CreateTempFile("foo.proto",
178+
R"schema(
179+
edition = "2023";
180+
message Foo {
181+
int32 bar = 1 [ctype=STRING];
182+
})schema");
183+
RunProtoc(
184+
"protocol_compiler --proto_path=$tmpdir --cpp_out=$tmpdir "
185+
"--experimental_editions foo.proto");
186+
ExpectErrorSubstring(
187+
"Field Foo.bar specifies ctype, but is not "
188+
"a string nor bytes field.");
189+
}
190+
191+
TEST_F(CppGeneratorTest, CtypeOnExtensionTest) {
192+
CreateTempFile("foo.proto",
193+
R"schema(
194+
edition = "2023";
195+
message Foo {
196+
extensions 1 to max;
197+
}
198+
extend Foo {
199+
bytes bar = 1 [ctype=CORD];
200+
})schema");
201+
RunProtoc(
202+
"protocol_compiler --proto_path=$tmpdir --cpp_out=$tmpdir "
203+
"--experimental_editions foo.proto");
204+
ExpectErrorSubstring(
205+
"Extension bar specifies ctype=CORD which is "
206+
"not supported for extensions.");
207+
}
208+
#endif // !PROTOBUF_FUTURE_REMOVE_WRONG_CTYPE
175209
} // namespace
176210
} // namespace cpp
177211
} // namespace compiler

src/google/protobuf/port_def.inc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,10 @@ static_assert(PROTOBUF_ABSL_MIN(20230125, 3),
246246
// Owner: mkruskal@
247247
#define PROTOBUF_FUTURE_EXTENSION_RANGE_CLASS 1
248248

249+
// Used to lock down wrong ctype usages in proto file.
250+
// Owner: jieluo@
251+
#define PROTOBUF_FUTURE_REMOVE_WRONG_CTYPE 1
252+
249253
#endif
250254

251255
#ifdef PROTOBUF_VERSION

src/google/protobuf/unittest.proto

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,9 @@ extend TestAllExtensions {
267267
optional_import_enum_extension = 23;
268268

269269
optional string optional_string_piece_extension = 24 [ctype=STRING_PIECE];
270-
optional string optional_cord_extension = 25 [ctype=CORD];
270+
// TODO(b/294946958): ctype=CORD is not supported for extension. Add
271+
// ctype=CORD option back after it is supported.
272+
optional string optional_cord_extension = 25;
271273

272274
optional protobuf_unittest_import.PublicImportMessage
273275
optional_public_import_message_extension = 26;
@@ -309,7 +311,9 @@ extend TestAllExtensions {
309311
repeated_import_enum_extension = 53;
310312

311313
repeated string repeated_string_piece_extension = 54 [ctype=STRING_PIECE];
312-
repeated string repeated_cord_extension = 55 [ctype=CORD];
314+
// TODO(b/294946958): ctype=CORD is not supported for extension. Add
315+
// ctype=CORD option back after it is supported.
316+
repeated string repeated_cord_extension = 55;
313317

314318
repeated TestAllTypes.NestedMessage
315319
repeated_lazy_message_extension = 57 [lazy=true];
@@ -340,7 +344,9 @@ extend TestAllExtensions {
340344

341345
optional string default_string_piece_extension = 84 [ctype=STRING_PIECE,
342346
default="abc"];
343-
optional string default_cord_extension = 85 [ctype=CORD, default="123"];
347+
// TODO(b/294946958): ctype=CORD is not supported for extension. Add
348+
// ctype=CORD option back after it is supported.
349+
optional string default_cord_extension = 85 [default="123"];
344350

345351
// For oneof test
346352
optional uint32 oneof_uint32_extension = 111;

src/google/protobuf/unittest_lite.proto

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,9 @@ extend TestAllExtensionsLite {
243243

244244
optional string optional_string_piece_extension_lite = 24
245245
[ctype = STRING_PIECE];
246-
optional string optional_cord_extension_lite = 25 [ctype = CORD];
246+
// TODO(b/294946958): ctype=CORD is not supported for extension. Add
247+
// ctype=CORD option back after it is supported.
248+
optional string optional_cord_extension_lite = 25;
247249

248250
optional protobuf_unittest_import.PublicImportMessageLite
249251
optional_public_import_message_extension_lite = 26;
@@ -288,7 +290,9 @@ extend TestAllExtensionsLite {
288290

289291
repeated string repeated_string_piece_extension_lite = 54
290292
[ctype = STRING_PIECE];
291-
repeated string repeated_cord_extension_lite = 55 [ctype = CORD];
293+
// TODO(b/294946958): ctype=CORD is not supported for extension. Add
294+
// ctype=CORD option back after it is supported.
295+
repeated string repeated_cord_extension_lite = 55;
292296

293297
repeated TestAllTypesLite.NestedMessage repeated_lazy_message_extension_lite =
294298
57 [lazy = true];
@@ -319,8 +323,9 @@ extend TestAllExtensionsLite {
319323

320324
optional string default_string_piece_extension_lite = 84
321325
[ctype = STRING_PIECE, default = "abc"];
322-
optional string default_cord_extension_lite = 85
323-
[ctype = CORD, default = "123"];
326+
// TODO(b/294946958): ctype=CORD is not supported for extension. Add
327+
// ctype=CORD option back after it is supported.
328+
optional string default_cord_extension_lite = 85 [default = "123"];
324329

325330
// For oneof test
326331
optional uint32 oneof_uint32_extension_lite = 111;

0 commit comments

Comments
 (0)