From d1b328ace390310cedef3840929c16d628ba60bc Mon Sep 17 00:00:00 2001 From: Alyssa Haroldsen Date: Fri, 5 Jan 2024 13:07:40 -0800 Subject: [PATCH] Fix nested enum generation when enums are the sole nested types PiperOrigin-RevId: 596066421 --- rust/test/shared/enum_test.rs | 7 +++++++ src/google/protobuf/compiler/rust/message.cc | 4 ++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/rust/test/shared/enum_test.rs b/rust/test/shared/enum_test.rs index ced4c6c4eb..5bf69af040 100644 --- a/rust/test/shared/enum_test.rs +++ b/rust/test/shared/enum_test.rs @@ -19,6 +19,13 @@ fn test_nested_enum_values() { assert_that!(i32::from(proto2_unittest::TestAllTypes_::NestedEnum::Neg), eq(-1)); } +#[test] +fn test_isolated_nested_enum() { + // Ensure that the enum is generated even when it's the only nested type for the + // message. + assert_that!(i32::from(proto2_unittest::TestRequiredEnumNoMask_::NestedEnum::Foo), eq(2)); +} + #[test] fn test_enum_value_name_same_as_enum() { assert_that!(i32::from(enums::TestEnumValueNameSameAsEnum::TestEnumValueNameSameAsEnum), eq(1)); diff --git a/src/google/protobuf/compiler/rust/message.cc b/src/google/protobuf/compiler/rust/message.cc index 4230da200a..6b5bfa877a 100644 --- a/src/google/protobuf/compiler/rust/message.cc +++ b/src/google/protobuf/compiler/rust/message.cc @@ -390,9 +390,9 @@ void GenerateRs(Context& ctx, const Descriptor& msg) { }}, {"nested_in_msg", [&] { - // If we have no nested types or oneofs, bail out without + // If we have no nested types, enums, or oneofs, bail out without // emitting an empty mod SomeMsg_. - if (msg.nested_type_count() == 0 && + if (msg.nested_type_count() == 0 && msg.enum_type_count() == 0 && msg.real_oneof_decl_count() == 0) { return; }