Skip to content

Commit db1d3f8

Browse files
mkruskal-googledeannagarcia
authored andcommitted
Replace previous breaking changes with deprecation warnings.
These changes hid public APIs that are deprecated in advance of protobuf editions. They will be fully removed in a future release. Any uses of them should be migrated to the future-facing feature helpers we provide on descriptor classes. PiperOrigin-RevId: 527422059
1 parent 2230d0e commit db1d3f8

File tree

4 files changed

+92
-27
lines changed

4 files changed

+92
-27
lines changed

java/core/src/main/java/com/google/protobuf/Descriptors.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,8 @@ public List<FileDescriptor> getPublicDependencies() {
162162
}
163163

164164
/** The syntax of the .proto file. */
165+
@Deprecated
166+
public
165167
enum Syntax {
166168
UNKNOWN("unknown"),
167169
PROTO2("proto2"),
@@ -175,6 +177,8 @@ enum Syntax {
175177
}
176178

177179
/** Get the syntax of the .proto file. */
180+
@Deprecated
181+
public
178182
Syntax getSyntax() {
179183
if (Syntax.PROTO3.name.equals(proto.getSyntax())) {
180184
return Syntax.PROTO3;

src/google/protobuf/descriptor.cc

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2612,10 +2612,12 @@ void FileDescriptor::CopyHeadingTo(FileDescriptorProto* proto) const {
26122612
}
26132613

26142614
// TODO(liujisi): Also populate when syntax="proto2".
2615-
if (syntax() == SYNTAX_PROTO3
2615+
FileDescriptorLegacy::Syntax syntax = FileDescriptorLegacy(this).syntax();
2616+
if (syntax == FileDescriptorLegacy::Syntax::SYNTAX_PROTO3
26162617
) {
2617-
proto->set_syntax(SyntaxName(syntax()));
2618+
proto->set_syntax(FileDescriptorLegacy::SyntaxName(syntax));
26182619
}
2620+
26192621
if (&options() != &FileOptions::default_instance()) {
26202622
*proto->mutable_options() = options();
26212623
}
@@ -3010,8 +3012,9 @@ std::string FileDescriptor::DebugStringWithOptions(
30103012
SourceLocationCommentPrinter syntax_comment(this, path, "",
30113013
debug_string_options);
30123014
syntax_comment.AddPreComment(&contents);
3013-
absl::SubstituteAndAppend(&contents, "syntax = \"$0\";\n\n",
3014-
SyntaxName(syntax()));
3015+
absl::SubstituteAndAppend(
3016+
&contents, "syntax = \"$0\";\n\n",
3017+
FileDescriptorLegacy::SyntaxName(FileDescriptorLegacy(this).syntax()));
30153018
syntax_comment.AddPostComment(&contents);
30163019
}
30173020

@@ -3282,7 +3285,7 @@ void FieldDescriptor::DebugString(
32823285

32833286
// Label is omitted for maps, oneof, and plain proto3 fields.
32843287
if (is_map() || real_containing_oneof() ||
3285-
(is_optional() && !has_optional_keyword())) {
3288+
(is_optional() && !FieldDescriptorLegacy(this).has_optional_keyword())) {
32863289
label.clear();
32873290
}
32883291

@@ -3573,7 +3576,8 @@ bool FileDescriptor::GetSourceLocation(SourceLocation* out_location) const {
35733576

35743577
bool FieldDescriptor::is_packed() const {
35753578
if (!is_packable()) return false;
3576-
if (file_->syntax() == FileDescriptor::SYNTAX_PROTO2) {
3579+
if (FileDescriptorLegacy(file_).syntax() ==
3580+
FileDescriptorLegacy::Syntax::SYNTAX_PROTO2) {
35773581
return (options_ != nullptr) && options_->packed();
35783582
} else {
35793583
return options_ == nullptr || !options_->has_packed() || options_->packed();
@@ -3582,7 +3586,8 @@ bool FieldDescriptor::is_packed() const {
35823586

35833587
bool FieldDescriptor::requires_utf8_validation() const {
35843588
return type() == TYPE_STRING &&
3585-
file()->syntax() == FileDescriptor::SYNTAX_PROTO3;
3589+
FileDescriptorLegacy(file_).syntax() ==
3590+
FileDescriptorLegacy::Syntax::SYNTAX_PROTO3;
35863591
}
35873592

35883593
bool Descriptor::GetSourceLocation(SourceLocation* out_location) const {
@@ -3989,7 +3994,7 @@ class DescriptorBuilder {
39893994
const Descriptor* result);
39903995
void CheckFieldJsonNameUniqueness(const std::string& message_name,
39913996
const DescriptorProto& message,
3992-
FileDescriptor::Syntax syntax,
3997+
FileDescriptorLegacy::Syntax syntax,
39933998
bool use_custom_names);
39943999
void CheckEnumValueUniqueness(const EnumDescriptorProto& proto,
39954000
const EnumDescriptor* result);
@@ -4711,7 +4716,7 @@ FileDescriptor* DescriptorPool::NewPlaceholderFileWithMutexHeld(
47114716
placeholder->tables_ = &FileDescriptorTables::GetEmptyInstance();
47124717
placeholder->source_code_info_ = &SourceCodeInfo::default_instance();
47134718
placeholder->is_placeholder_ = true;
4714-
placeholder->syntax_ = FileDescriptor::SYNTAX_UNKNOWN;
4719+
placeholder->syntax_ = FileDescriptorLegacy::SYNTAX_UNKNOWN;
47154720
placeholder->finished_building_ = true;
47164721
// All other fields are zero or nullptr.
47174722

@@ -5226,11 +5231,11 @@ FileDescriptor* DescriptorBuilder::BuildFileImpl(
52265231
// TODO(liujisi): Report error when the syntax is empty after all the protos
52275232
// have added the syntax statement.
52285233
if (proto.syntax().empty() || proto.syntax() == "proto2") {
5229-
file_->syntax_ = FileDescriptor::SYNTAX_PROTO2;
5234+
file_->syntax_ = FileDescriptorLegacy::SYNTAX_PROTO2;
52305235
} else if (proto.syntax() == "proto3") {
5231-
file_->syntax_ = FileDescriptor::SYNTAX_PROTO3;
5236+
file_->syntax_ = FileDescriptorLegacy::SYNTAX_PROTO3;
52325237
} else {
5233-
file_->syntax_ = FileDescriptor::SYNTAX_UNKNOWN;
5238+
file_->syntax_ = FileDescriptorLegacy::SYNTAX_UNKNOWN;
52345239
AddError(proto.name(), proto, DescriptorPool::ErrorCollector::OTHER, [&] {
52355240
return absl::StrCat("Unrecognized syntax: ", proto.syntax());
52365241
});
@@ -5681,11 +5686,12 @@ void DescriptorBuilder::BuildMessage(const DescriptorProto& proto,
56815686

56825687
void DescriptorBuilder::CheckFieldJsonNameUniqueness(
56835688
const DescriptorProto& proto, const Descriptor* result) {
5684-
FileDescriptor::Syntax syntax = result->file()->syntax();
5689+
FileDescriptorLegacy::Syntax syntax =
5690+
FileDescriptorLegacy(result->file()).syntax();
56855691
std::string message_name = result->full_name();
56865692
if (pool_->deprecated_legacy_json_field_conflicts_ ||
56875693
IsLegacyJsonFieldConflictEnabled(result->options())) {
5688-
if (syntax == FileDescriptor::SYNTAX_PROTO3) {
5694+
if (syntax == FileDescriptorLegacy::Syntax::SYNTAX_PROTO3) {
56895695
// Only check default JSON names for conflicts in proto3. This is legacy
56905696
// behavior that will be removed in a later version.
56915697
CheckFieldJsonNameUniqueness(message_name, proto, syntax, false);
@@ -5725,7 +5731,7 @@ bool JsonNameLooksLikeExtension(std::string name) {
57255731

57265732
void DescriptorBuilder::CheckFieldJsonNameUniqueness(
57275733
const std::string& message_name, const DescriptorProto& message,
5728-
FileDescriptor::Syntax syntax, bool use_custom_names) {
5734+
FileDescriptorLegacy::Syntax syntax, bool use_custom_names) {
57295735
absl::flat_hash_map<std::string, JsonNameDetails> name_to_field;
57305736
for (const FieldDescriptorProto& field : message.field()) {
57315737
JsonNameDetails details = GetJsonNameDetails(&field, use_custom_names);
@@ -5769,7 +5775,7 @@ void DescriptorBuilder::CheckFieldJsonNameUniqueness(
57695775
};
57705776

57715777
bool involves_default = !details.is_custom || !match.is_custom;
5772-
if (syntax == FileDescriptor::SYNTAX_PROTO2 && involves_default) {
5778+
if (syntax == FileDescriptorLegacy::SYNTAX_PROTO2 && involves_default) {
57735779
// TODO(b/261750676) Upgrade this to an error once downstream protos have
57745780
// been fixed.
57755781
AddWarning(message_name, field, DescriptorPool::ErrorCollector::NAME,
@@ -5808,7 +5814,8 @@ void DescriptorBuilder::BuildFieldOrExtension(const FieldDescriptorProto& proto,
58085814
result->proto3_optional_ = proto.proto3_optional();
58095815

58105816
if (proto.proto3_optional() &&
5811-
file_->syntax() != FileDescriptor::SYNTAX_PROTO3) {
5817+
FileDescriptorLegacy(file_).syntax() !=
5818+
FileDescriptorLegacy::Syntax::SYNTAX_PROTO3) {
58125819
AddError(result->full_name(), proto, DescriptorPool::ErrorCollector::TYPE,
58135820
[&] {
58145821
return absl::StrCat(
@@ -6221,7 +6228,8 @@ void DescriptorBuilder::CheckEnumValueUniqueness(
62216228
// compatibility we issue only a warning for proto2.
62226229
if ((pool_->deprecated_legacy_json_field_conflicts_ ||
62236230
IsLegacyJsonFieldConflictEnabled(result->options())) &&
6224-
result->file()->syntax() == FileDescriptor::SYNTAX_PROTO2) {
6231+
FileDescriptorLegacy(result->file()).syntax() ==
6232+
FileDescriptorLegacy::Syntax::SYNTAX_PROTO2) {
62256233
AddWarning(value->full_name(), proto.value(i),
62266234
DescriptorPool::ErrorCollector::NAME, make_error);
62276235
continue;
@@ -6585,7 +6593,7 @@ void DescriptorBuilder::CrossLinkMessage(Descriptor* message,
65856593
const FieldDescriptor* field = message->field(i);
65866594
if (field->proto3_optional_) {
65876595
if (!field->containing_oneof() ||
6588-
!field->containing_oneof()->is_synthetic()) {
6596+
!OneofDescriptorLegacy(field->containing_oneof()).is_synthetic()) {
65896597
AddError(message->full_name(), proto.field(i),
65906598
DescriptorPool::ErrorCollector::OTHER,
65916599
"Fields with proto3_optional set must be "
@@ -6598,7 +6606,7 @@ void DescriptorBuilder::CrossLinkMessage(Descriptor* message,
65986606
int first_synthetic = -1;
65996607
for (int i = 0; i < message->oneof_decl_count(); i++) {
66006608
const OneofDescriptor* oneof = message->oneof_decl(i);
6601-
if (oneof->is_synthetic()) {
6609+
if (OneofDescriptorLegacy(oneof).is_synthetic()) {
66026610
if (first_synthetic == -1) {
66036611
first_synthetic = i;
66046612
}
@@ -7140,7 +7148,8 @@ void DescriptorBuilder::ValidateFileOptions(FileDescriptor* file,
71407148
}
71417149
}
71427150
}
7143-
if (file->syntax() == FileDescriptor::SYNTAX_PROTO3) {
7151+
if (FileDescriptorLegacy(file).syntax() ==
7152+
FileDescriptorLegacy::Syntax::SYNTAX_PROTO3) {
71447153
ValidateProto3(file, proto);
71457154
}
71467155
}
@@ -7203,8 +7212,10 @@ void DescriptorBuilder::ValidateProto3Field(FieldDescriptor* field,
72037212
}
72047213
if (field->cpp_type() == FieldDescriptor::CPPTYPE_ENUM &&
72057214
field->enum_type() &&
7206-
field->enum_type()->file()->syntax() != FileDescriptor::SYNTAX_PROTO3 &&
7207-
field->enum_type()->file()->syntax() != FileDescriptor::SYNTAX_UNKNOWN) {
7215+
FileDescriptorLegacy(field->enum_type()->file()).syntax() !=
7216+
FileDescriptorLegacy::Syntax::SYNTAX_PROTO3 &&
7217+
FileDescriptorLegacy(field->enum_type()->file()).syntax() !=
7218+
FileDescriptorLegacy::Syntax::SYNTAX_UNKNOWN) {
72087219
// Proto3 messages can only use Proto3 enum types; otherwise we can't
72097220
// guarantee that the default value is zero.
72107221
AddError(

src/google/protobuf/descriptor.h

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@
7979

8080
#ifdef SWIG
8181
#define PROTOBUF_EXPORT
82+
#define PROTOBUF_IGNORE_DEPRECATION_START
83+
#define PROTOBUF_IGNORE_DEPRECATION_STOP
8284
#endif
8385

8486

@@ -922,10 +924,17 @@ class PROTOBUF_EXPORT FieldDescriptor : private internal::SymbolBase {
922924
friend class Reflection;
923925
friend class FieldDescriptorLegacy;
924926

927+
928+
public:
929+
ABSL_DEPRECATED(
930+
"Syntax is deprecated in favor of editions, please use "
931+
"FieldDescriptor::has_presence instead.")
925932
// Returns true if this field was syntactically written with "optional" in the
926933
// .proto file. Excludes singular proto3 fields that do not have a label.
927934
bool has_optional_keyword() const;
928935

936+
private:
937+
929938
// Fill the json_name field of FieldDescriptorProto.
930939
void CopyJsonNameTo(FieldDescriptorProto* proto) const;
931940

@@ -1083,10 +1092,17 @@ class PROTOBUF_EXPORT OneofDescriptor : private internal::SymbolBase {
10831092
friend class compiler::cpp::Formatter;
10841093
friend class OneofDescriptorLegacy;
10851094

1095+
1096+
public:
1097+
ABSL_DEPRECATED(
1098+
"Syntax is deprecated in favor of editions, please use "
1099+
"real_oneof_decl_count for now instead of is_synthetic.")
10861100
// Returns whether this oneof was inserted by the compiler to wrap a proto3
10871101
// optional field. If this returns true, code generators should *not* emit it.
10881102
bool is_synthetic() const;
10891103

1104+
private:
1105+
10901106
// See Descriptor::DebugString().
10911107
void DebugString(int depth, std::string* contents,
10921108
const DebugStringOptions& options) const;
@@ -1657,12 +1673,18 @@ class PROTOBUF_EXPORT FileDescriptor : private internal::SymbolBase {
16571673
// descriptor.proto, and any available extensions of that message.
16581674
const FileOptions& options() const;
16591675

1676+
16601677
private:
16611678
// With the upcoming release of editions, syntax should not be used for
16621679
// business logic. Instead, the various feature helpers defined in this file
16631680
// should be used to query more targeted behaviors. For example:
16641681
// has_presence, is_closed, requires_utf8_validation.
1665-
enum Syntax
1682+
enum
1683+
ABSL_DEPRECATED(
1684+
"Syntax is deprecated in favor of editions. Please use targeted "
1685+
"feature helpers instead (e.g. has_presence, is_packed, "
1686+
"requires_utf8_validation, etc).")
1687+
Syntax
16661688
#ifndef SWIG
16671689
: int
16681690
#endif // !SWIG
@@ -1671,13 +1693,22 @@ class PROTOBUF_EXPORT FileDescriptor : private internal::SymbolBase {
16711693
SYNTAX_PROTO2 = 2,
16721694
SYNTAX_PROTO3 = 3,
16731695
};
1696+
PROTOBUF_IGNORE_DEPRECATION_START
1697+
ABSL_DEPRECATED(
1698+
"Syntax is deprecated in favor of editions. Please use targeted "
1699+
"feature helpers instead (e.g. has_presence, is_packed, "
1700+
"requires_utf8_validation, etc).")
16741701
Syntax syntax() const;
1702+
PROTOBUF_IGNORE_DEPRECATION_STOP
16751703

16761704
// Define a visibility-restricted wrapper for internal use until the migration
16771705
// is complete.
16781706
friend class FileDescriptorLegacy;
16791707

1708+
PROTOBUF_IGNORE_DEPRECATION_START
1709+
ABSL_DEPRECATED("Syntax is deprecated in favor of editions")
16801710
static const char* SyntaxName(Syntax syntax);
1711+
PROTOBUF_IGNORE_DEPRECATION_STOP
16811712

16821713
public:
16831714

@@ -2314,7 +2345,9 @@ PROTOBUF_DEFINE_ARRAY_ACCESSOR(EnumDescriptor, reserved_range,
23142345
PROTOBUF_DEFINE_ACCESSOR(EnumDescriptor, reserved_name_count, int)
23152346

23162347
inline bool EnumDescriptor::is_closed() const {
2348+
PROTOBUF_IGNORE_DEPRECATION_START
23172349
return file()->syntax() != FileDescriptor::SYNTAX_PROTO3;
2350+
PROTOBUF_IGNORE_DEPRECATION_STOP
23182351
}
23192352

23202353
PROTOBUF_DEFINE_NAME_ACCESSOR(EnumValueDescriptor)
@@ -2465,25 +2498,33 @@ inline bool FieldDescriptor::is_map() const {
24652498
}
24662499

24672500
inline bool FieldDescriptor::has_optional_keyword() const {
2501+
PROTOBUF_IGNORE_DEPRECATION_START
24682502
return proto3_optional_ ||
24692503
(file()->syntax() == FileDescriptor::SYNTAX_PROTO2 && is_optional() &&
24702504
!containing_oneof());
2505+
PROTOBUF_IGNORE_DEPRECATION_STOP
24712506
}
24722507

24732508
inline const OneofDescriptor* FieldDescriptor::real_containing_oneof() const {
2509+
PROTOBUF_IGNORE_DEPRECATION_START
24742510
auto* oneof = containing_oneof();
24752511
return oneof && !oneof->is_synthetic() ? oneof : nullptr;
2512+
PROTOBUF_IGNORE_DEPRECATION_STOP
24762513
}
24772514

24782515
inline bool FieldDescriptor::has_presence() const {
2516+
PROTOBUF_IGNORE_DEPRECATION_START
24792517
if (is_repeated()) return false;
24802518
return cpp_type() == CPPTYPE_MESSAGE || containing_oneof() ||
24812519
file()->syntax() == FileDescriptor::SYNTAX_PROTO2;
2520+
PROTOBUF_IGNORE_DEPRECATION_STOP
24822521
}
24832522

24842523
inline bool FieldDescriptor::legacy_enum_field_treated_as_closed() const {
2524+
PROTOBUF_IGNORE_DEPRECATION_START
24852525
return type() == TYPE_ENUM &&
24862526
file()->syntax() == FileDescriptor::SYNTAX_PROTO2;
2527+
PROTOBUF_IGNORE_DEPRECATION_STOP
24872528
}
24882529

24892530
// To save space, index() is computed by looking at the descriptor's position
@@ -2586,9 +2627,11 @@ inline const FileDescriptor* FileDescriptor::weak_dependency(int index) const {
25862627
return dependency(weak_dependencies_[index]);
25872628
}
25882629

2630+
PROTOBUF_IGNORE_DEPRECATION_START
25892631
inline FileDescriptor::Syntax FileDescriptor::syntax() const {
25902632
return static_cast<Syntax>(syntax_);
25912633
}
2634+
PROTOBUF_IGNORE_DEPRECATION_STOP
25922635

25932636
namespace internal {
25942637

src/google/protobuf/descriptor_legacy.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666

6767
namespace google {
6868
namespace protobuf {
69+
PROTOBUF_IGNORE_DEPRECATION_START
6970

7071
// Wraps FileDescriptor.
7172
class PROTOBUF_EXPORT FileDescriptorLegacy {
@@ -74,10 +75,15 @@ class PROTOBUF_EXPORT FileDescriptorLegacy {
7475

7576
// Any dependencies on file-level syntax keyword should be replaced by
7677
// feature-level switches to support go/protobuf-editions.
77-
using Syntax = FileDescriptor::Syntax;
78-
Syntax syntax() const { return desc_->syntax(); }
78+
enum Syntax {
79+
SYNTAX_UNKNOWN = FileDescriptor::SYNTAX_UNKNOWN,
80+
SYNTAX_PROTO2 = FileDescriptor::SYNTAX_PROTO2,
81+
SYNTAX_PROTO3 = FileDescriptor::SYNTAX_PROTO3,
82+
};
83+
Syntax syntax() const { return static_cast<Syntax>(desc_->syntax()); }
7984
static absl::string_view SyntaxName(Syntax syntax) {
80-
return FileDescriptor::SyntaxName(syntax);
85+
return FileDescriptor::SyntaxName(
86+
static_cast<FileDescriptor::Syntax>(syntax));
8187
}
8288

8389
private:
@@ -104,6 +110,7 @@ class PROTOBUF_EXPORT OneofDescriptorLegacy {
104110
const OneofDescriptor* desc_;
105111
};
106112

113+
PROTOBUF_IGNORE_DEPRECATION_STOP
107114
} // namespace protobuf
108115
} // namespace google
109116

0 commit comments

Comments
 (0)