Fix bug where enums only got traits if they are used in a field.

Now they always get traits, even if unused.

PiperOrigin-RevId: 691046942
pull/18969/head
Protobuf Team Bot 4 months ago committed by Copybara-Service
parent 116edcefe3
commit 083bbd4fcd
  1. 1
      src/google/protobuf/BUILD.bazel
  2. 8
      src/google/protobuf/compiler/cpp/file.cc
  3. 15
      src/google/protobuf/lite_unittest.cc
  4. 15
      src/google/protobuf/only_one_enum_test.proto

@ -815,6 +815,7 @@ filegroup(
name = "lite_test_proto_srcs",
srcs = [
"map_lite_unittest.proto",
"only_one_enum_test.proto",
"unittest_import_lite.proto",
"unittest_import_public_lite.proto",
"unittest_lite.proto",

@ -1546,12 +1546,12 @@ void FileGenerator::GenerateForwardDeclarations(io::Printer* p) {
}
ListAllTypesForServices(file_, &classes);
} else {
// List all enums in this file, to declare the traits.
google::protobuf::internal::VisitDescriptors(
*file_, [&](const EnumDescriptor& e) { enums.push_back(&e); });
}
// List all enums in this file, to declare the traits.
google::protobuf::internal::VisitDescriptors(
*file_, [&](const EnumDescriptor& e) { enums.push_back(&e); });
// Calculate the set of files whose definitions we get through include.
// No need to forward declare types that are defined in these.
absl::flat_hash_set<const FileDescriptor*> public_set;

@ -23,6 +23,7 @@
#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"
#include "google/protobuf/arena_test_util.h"
#include "google/protobuf/generated_enum_util.h"
#include "google/protobuf/io/coded_stream.h"
#include "google/protobuf/io/zero_copy_stream.h"
#include "google/protobuf/io/zero_copy_stream_impl.h"
@ -30,6 +31,7 @@
#include "google/protobuf/map_lite_test_util.h"
#include "google/protobuf/map_lite_unittest.pb.h"
#include "google/protobuf/message_lite.h"
#include "google/protobuf/only_one_enum_test.pb.h"
#include "google/protobuf/parse_context.h"
#include "google/protobuf/test_util_lite.h"
#include "google/protobuf/unittest_lite.pb.h"
@ -1458,6 +1460,19 @@ TEST(LiteTest, DownCastMessageInvalidReferenceType) {
}
#endif // GTEST_HAS_DEATH_TEST
TEST(LiteTest, FileWithOnlyAnEnumGeneratesProperValidationHooks) {
EXPECT_TRUE(protobuf_unittest::OnlyOneEnum_IsValid(0));
EXPECT_TRUE(protobuf_unittest::OnlyOneEnum_IsValid(10));
EXPECT_FALSE(protobuf_unittest::OnlyOneEnum_IsValid(6));
// Traits also work
constexpr auto* data =
internal::EnumTraits<protobuf_unittest::OnlyOneEnum>::validation_data();
EXPECT_TRUE(internal::ValidateEnum(0, data));
EXPECT_TRUE(internal::ValidateEnum(10, data));
EXPECT_FALSE(internal::ValidateEnum(6, data));
}
} // namespace
} // namespace protobuf
} // namespace google

@ -0,0 +1,15 @@
edition = "2023";
package protobuf_unittest;
option optimize_for = LITE_RUNTIME;
// A file with only a single enum, without any use of it.
// This is useful for testing enum specific codegen that is not directly related
// to field codegen.
enum OnlyOneEnum {
ONLY_ONE_ENUM_DEFAULT = 0;
ONLY_ONE_ENUM_VALID = 10;
}
Loading…
Cancel
Save