Skip to content

Commit

Permalink
Implement proto2/proto3 with editions
Browse files Browse the repository at this point in the history
This change migrates proto2 and proto3 to real editions, without impacting users at all.  While we already had feature defaults set, we weren't really using them outside of editions.  This change introduces InferLegacyProtoFeatures, which infers features from descriptors.  Any language that wants to follow this rollout pattern will need to implement similar transformations.

PiperOrigin-RevId: 572593956
  • Loading branch information
mkruskal-google authored and Copybara-Service committed Oct 11, 2023
1 parent 9ad2268 commit 3813b66
Show file tree
Hide file tree
Showing 8 changed files with 173 additions and 203 deletions.
2 changes: 1 addition & 1 deletion src/google/protobuf/compiler/command_line_interface.cc
Expand Up @@ -1554,7 +1554,7 @@ bool CommandLineInterface::SetupFeatureResolution(DescriptorPool& pool) {
<< " specifies a maximum edition "
<< output.generator->GetMaximumEdition()
<< " which is not the protoc maximum "
<< PROTOBUF_MINIMUM_EDITION << ".";
<< PROTOBUF_MAXIMUM_EDITION << ".";
return false;
}
for (const FieldDescriptor* ext :
Expand Down
12 changes: 6 additions & 6 deletions src/google/protobuf/compiler/command_line_interface_unittest.cc
Expand Up @@ -1489,7 +1489,7 @@ TEST_F(CommandLineInterfaceTest, InvalidMinimumEditionError) {
"--experimental_editions foo.proto");
ExpectErrorSubstring(
"generator --test_out specifies a minimum edition 1_TEST_ONLY which is "
"not the protoc minimum 2023");
"not the protoc minimum PROTO2");
}

TEST_F(CommandLineInterfaceTest, InvalidMaximumEditionError) {
Expand Down Expand Up @@ -1780,7 +1780,7 @@ TEST_F(CommandLineInterfaceTest, EditionDefaults) {
json_format: ALLOW
}
}
minimum_edition: EDITION_2023
minimum_edition: EDITION_PROTO2
maximum_edition: EDITION_2023
)pb"));
}
Expand Down Expand Up @@ -1829,7 +1829,7 @@ TEST_F(CommandLineInterfaceTest, EditionDefaultsWithMaximum) {
json_format: ALLOW
}
}
minimum_edition: EDITION_2023
minimum_edition: EDITION_PROTO2
maximum_edition: EDITION_99997_TEST_ONLY
)pb"));
}
Expand Down Expand Up @@ -1896,7 +1896,7 @@ TEST_F(CommandLineInterfaceTest, EditionDefaultsWithExtension) {
ExpectNoErrors();

FeatureSetDefaults defaults = ReadEditionDefaults("defaults");
EXPECT_EQ(defaults.minimum_edition(), EDITION_2023);
EXPECT_EQ(defaults.minimum_edition(), EDITION_PROTO2);
EXPECT_EQ(defaults.maximum_edition(), EDITION_99999_TEST_ONLY);
ASSERT_EQ(defaults.defaults_size(), 5);
EXPECT_EQ(defaults.defaults(0).edition(), EDITION_PROTO2);
Expand All @@ -1906,10 +1906,10 @@ TEST_F(CommandLineInterfaceTest, EditionDefaultsWithExtension) {
EXPECT_EQ(defaults.defaults(4).edition(), EDITION_99998_TEST_ONLY);
EXPECT_EQ(
defaults.defaults(0).features().GetExtension(pb::test).int_file_feature(),
0);
-2);
EXPECT_EQ(
defaults.defaults(1).features().GetExtension(pb::test).int_file_feature(),
0);
-3);
EXPECT_EQ(
defaults.defaults(2).features().GetExtension(pb::test).int_file_feature(),
1);
Expand Down
4 changes: 3 additions & 1 deletion src/google/protobuf/compiler/java/helpers.h
Expand Up @@ -350,7 +350,9 @@ inline bool ExposePublicParser(const FileDescriptor* descriptor) {
// ints.
inline bool SupportUnknownEnumValue(const FieldDescriptor* field) {
// TODO: Check Java legacy_enum_field_treated_as_closed feature.
return !field->legacy_enum_field_treated_as_closed();
return field->type() != FieldDescriptor::TYPE_ENUM ||
FileDescriptorLegacy(field->file()).syntax() ==
FileDescriptorLegacy::SYNTAX_PROTO3;
}

// Check whether a message has repeated fields.
Expand Down

0 comments on commit 3813b66

Please sign in to comment.