Change message-nesting namespacing to be some_msg:: instead of SomeMsg_::

PiperOrigin-RevId: 617153657
pull/16213/head
Protobuf Team Bot 11 months ago committed by Copybara-Service
parent d428610254
commit 8198fe9a5f
  1. 8
      rust/test/reserved.proto
  2. 16
      rust/test/shared/accessors_proto3_test.rs
  3. 4
      rust/test/shared/accessors_repeated_test.rs
  4. 22
      rust/test/shared/accessors_test.rs
  5. 12
      rust/test/shared/enum_test.rs
  6. 2
      rust/test/shared/fields_with_imported_types_test.rs
  7. 10
      rust/test/shared/nested_types_test.rs
  8. 6
      rust/test/shared/reserved_test.rs
  9. 34
      rust/test/shared/simple_nested_test.rs
  10. 2
      src/google/protobuf/compiler/rust/enum.cc
  11. 9
      src/google/protobuf/compiler/rust/message.cc
  12. 84
      src/google/protobuf/compiler/rust/naming.cc
  13. 13
      src/google/protobuf/compiler/rust/naming.h
  14. 20
      src/google/protobuf/compiler/rust/oneof.cc

@ -23,14 +23,16 @@ message Self {
optional string false = 4;
}
message pub {
message Pub {
enum Self {
enum = 0;
}
}
message enum {
message enum {}
message Ref {
oneof self {
.type.type.pub.Self const = 3;
.type.type.Pub.Self const = 3;
}
}

@ -9,8 +9,8 @@
/// on proto3.
use googletest::prelude::*;
use protobuf::Optional;
use unittest_proto3::{TestAllTypes, TestAllTypes_};
use unittest_proto3_optional::{TestProto3Optional, TestProto3Optional_};
use unittest_proto3::{test_all_types, TestAllTypes};
use unittest_proto3_optional::{test_proto3_optional, TestProto3Optional};
#[test]
fn test_fixed32_accessors() {
@ -125,7 +125,7 @@ fn test_optional_string_accessors() {
#[test]
fn test_nested_enum_accessors() {
use TestAllTypes_::NestedEnum;
use test_all_types::NestedEnum;
let mut msg = TestAllTypes::new();
assert_that!(msg.optional_nested_enum(), eq(NestedEnum::Zero));
@ -139,7 +139,7 @@ fn test_nested_enum_accessors() {
#[test]
fn test_optional_nested_enum_accessors() {
use TestProto3Optional_::NestedEnum;
use test_proto3_optional::NestedEnum;
let mut msg = TestProto3Optional::new();
assert_that!(msg.optional_nested_enum(), eq(NestedEnum::Unspecified));
@ -170,7 +170,7 @@ fn test_foreign_enum_accessors() {
#[test]
fn test_oneof_accessors() {
use TestAllTypes_::OneofField::*;
use test_all_types::OneofField::*;
let mut msg = TestAllTypes::new();
assert_that!(msg.oneof_field(), matches_pattern!(not_set(_)));
@ -199,7 +199,7 @@ fn test_oneof_accessors() {
#[test]
fn test_oneof_accessors_view_long_lifetime() {
use TestAllTypes_::OneofField::*;
use test_all_types::OneofField::*;
let mut msg = TestAllTypes::new();
msg.set_oneof_uint32(7);
@ -216,8 +216,8 @@ fn test_oneof_accessors_view_long_lifetime() {
#[test]
fn test_oneof_enum_accessors() {
use unittest_proto3::{
test_oneof2::{Foo, FooCase, NestedEnum},
TestOneof2,
TestOneof2_::{Foo, FooCase, NestedEnum},
};
let mut msg = TestOneof2::new();
@ -233,7 +233,7 @@ fn test_oneof_enum_accessors() {
#[test]
fn test_submsg_setter() {
use TestAllTypes_::*;
use test_all_types::*;
let mut nested = NestedMessage::new();
nested.set_bb(7);

@ -8,7 +8,7 @@
use googletest::prelude::*;
use paste::paste;
use protobuf::ViewProxy;
use unittest_proto::{TestAllTypes, TestAllTypes_, TestAllTypes_::NestedMessage};
use unittest_proto::{test_all_types, test_all_types::NestedMessage, TestAllTypes};
macro_rules! generate_repeated_numeric_test {
($(($t: ty, $field: ident)),*) => {
@ -138,7 +138,7 @@ fn test_repeated_bool_accessors() {
#[test]
fn test_repeated_enum_accessors() {
use TestAllTypes_::NestedEnum;
use test_all_types::NestedEnum;
let mut msg = TestAllTypes::new();
assert_that!(msg.repeated_nested_enum(), empty());

@ -9,7 +9,7 @@
use googletest::prelude::*;
use protobuf::{MutProxy, Optional};
use unittest_proto::{TestAllTypes, TestAllTypes_};
use unittest_proto::{test_all_types, TestAllTypes};
#[test]
fn test_default_accessors() {
@ -485,7 +485,7 @@ fn test_nonempty_default_string_accessors() {
#[test]
fn test_singular_msg_field() {
use TestAllTypes_::*;
use test_all_types::*;
let mut msg = TestAllTypes::new();
let msg_view: NestedMessageView = msg.optional_nested_message();
@ -507,8 +507,8 @@ fn test_singular_msg_field() {
fn test_message_opt() {
let msg = TestAllTypes::new();
let opt: Optional<
unittest_proto::TestAllTypes_::NestedMessageView<'_>,
unittest_proto::TestAllTypes_::NestedMessageView<'_>,
unittest_proto::test_all_types::NestedMessageView<'_>,
unittest_proto::test_all_types::NestedMessageView<'_>,
> = msg.optional_nested_message_opt();
assert_that!(opt.is_set(), eq(false));
assert_that!(opt.into_inner().bb(), eq(0));
@ -517,7 +517,7 @@ fn test_message_opt() {
#[test]
fn test_message_opt_set() {
let mut msg = TestAllTypes::new();
let submsg = TestAllTypes_::NestedMessage::new();
let submsg = test_all_types::NestedMessage::new();
assert_that!(msg.optional_nested_message_opt().is_set(), eq(false));
msg.set_optional_nested_message(submsg);
@ -530,7 +530,7 @@ fn test_message_opt_set() {
#[test]
fn test_setting_submsg() {
let mut msg = TestAllTypes::new();
let submsg = TestAllTypes_::NestedMessage::new();
let submsg = test_all_types::NestedMessage::new();
assert_that!(msg.optional_nested_message_opt().is_set(), eq(false));
@ -557,7 +557,7 @@ fn test_msg_mut_initializes() {
#[test]
fn test_optional_nested_enum_accessors() {
use TestAllTypes_::NestedEnum;
use test_all_types::NestedEnum;
let mut msg = TestAllTypes::new();
assert_that!(msg.optional_nested_enum_opt(), eq(Optional::Unset(NestedEnum::Foo)));
@ -574,7 +574,7 @@ fn test_optional_nested_enum_accessors() {
#[test]
fn test_default_nested_enum_accessors() {
use TestAllTypes_::NestedEnum;
use test_all_types::NestedEnum;
let mut msg = TestAllTypes::new();
assert_that!(msg.default_nested_enum(), eq(NestedEnum::Bar));
@ -659,8 +659,8 @@ fn test_default_import_enum_accessors() {
#[test]
fn test_oneof_accessors() {
use unittest_proto::test_oneof2::{Foo::*, FooCase, NestedEnum};
use unittest_proto::TestOneof2;
use unittest_proto::TestOneof2_::{Foo::*, FooCase, NestedEnum};
let mut msg = TestOneof2::new();
assert_that!(msg.foo(), matches_pattern!(not_set(_)));
@ -707,7 +707,7 @@ fn test_oneof_accessors() {
#[test]
fn test_msg_oneof_default_accessors() {
use unittest_proto::TestOneof2_::{Bar::*, BarCase, NestedEnum};
use unittest_proto::test_oneof2::{Bar::*, BarCase, NestedEnum};
let mut msg = unittest_proto::TestOneof2::new();
assert_that!(msg.bar(), matches_pattern!(not_set(_)));
@ -764,7 +764,7 @@ fn test_group() {
#[test]
fn test_submsg_setter() {
use TestAllTypes_::*;
use test_all_types::*;
let mut nested = NestedMessage::new();
nested.set_bb(7);

@ -13,17 +13,17 @@ use unittest_proto::*;
#[test]
fn test_nested_enum_values() {
assert_that!(i32::from(TestAllTypes_::NestedEnum::Foo), eq(1));
assert_that!(i32::from(TestAllTypes_::NestedEnum::Bar), eq(2));
assert_that!(i32::from(TestAllTypes_::NestedEnum::Baz), eq(3));
assert_that!(i32::from(TestAllTypes_::NestedEnum::Neg), eq(-1));
assert_that!(i32::from(test_all_types::NestedEnum::Foo), eq(1));
assert_that!(i32::from(test_all_types::NestedEnum::Bar), eq(2));
assert_that!(i32::from(test_all_types::NestedEnum::Baz), eq(3));
assert_that!(i32::from(test_all_types::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(TestRequiredEnumNoMask_::NestedEnum::Foo), eq(2));
assert_that!(i32::from(test_required_enum_no_mask::NestedEnum::Foo), eq(2));
}
#[test]
@ -40,7 +40,7 @@ fn test_enum_defaults() {
TestEnumWithDuplicateStrippedPrefixNames::default(),
eq(TestEnumWithDuplicateStrippedPrefixNames::Unknown)
);
assert_that!(TestAllTypes_::NestedEnum::default(), eq(TestAllTypes_::NestedEnum::Foo));
assert_that!(test_all_types::NestedEnum::default(), eq(test_all_types::NestedEnum::Foo));
}
#[test]

@ -30,8 +30,8 @@ fn test_enum_field_generated() {
#[test]
fn test_oneof_message_field_generated() {
use fields_with_imported_types_proto::msg_with_fields_with_imported_types::ImportedTypesOneof::not_set;
use fields_with_imported_types_proto::MsgWithFieldsWithImportedTypes;
use fields_with_imported_types_proto::MsgWithFieldsWithImportedTypes_::ImportedTypesOneof::not_set;
use imported_types_proto::ImportedEnum;
use imported_types_proto::ImportedMessageView;

@ -10,14 +10,14 @@
#[test]
fn test_nested_messages_accessible() {
let _parent: unittest_proto::TestAllTypes;
let _child: unittest_proto::TestAllTypes_::NestedMessage;
unittest_proto::TestChildExtensionData_::
NestedTestAllExtensionsData_::NestedDynamicExtensions::new();
let _child: unittest_proto::test_all_types::NestedMessage;
unittest_proto::test_child_extension_data::
nested_test_all_extensions_data::NestedDynamicExtensions::new();
}
#[test]
fn test_nested_enums_accessible() {
let _parent: unittest_proto::TestAllTypes;
let _child: unittest_proto::TestAllTypes_::NestedEnum;
unittest_proto::TestDynamicExtensions_::DynamicEnumType::default();
let _child: unittest_proto::test_all_types::NestedEnum;
unittest_proto::test_dynamic_extensions::DynamicEnumType::default();
}

@ -7,8 +7,8 @@
/// Test covering proto compilation with reserved words.
use googletest::prelude::*;
use reserved_proto::r#enum;
use reserved_proto::Self__mangled_because_ident_isnt_a_legal_raw_identifier;
use reserved_proto::{r#enum, Ref};
#[test]
fn test_reserved_keyword_in_accessors() {
@ -19,6 +19,6 @@ fn test_reserved_keyword_in_accessors() {
#[test]
fn test_reserved_keyword_in_messages() {
let msg = r#enum::new();
let _ = msg.r#const();
let _ = r#enum::new();
let _ = Ref::new().r#const();
}

@ -6,34 +6,34 @@
// https://developers.google.com/open-source/licenses/bsd
use googletest::prelude::*;
use nested_proto::Outer_::InnerView;
use nested_proto::Outer_::Inner_::InnerEnum;
use nested_proto::outer::inner::InnerEnum;
use nested_proto::outer::InnerView;
use nested_proto::*;
#[test]
fn test_deeply_nested_message() {
let deep =
Outer_::Inner_::SuperInner_::DuperInner_::EvenMoreInner_::CantBelieveItsSoInner::new();
outer::inner::super_inner::duper_inner::even_more_inner::CantBelieveItsSoInner::new();
assert_that!(deep.num(), eq(0));
let outer_msg = Outer::new();
assert_that!(outer_msg.deep().num(), eq(0));
let outermsg = Outer::new();
assert_that!(outermsg.deep().num(), eq(0));
}
#[test]
fn test_deeply_nested_enum() {
use Outer_::Inner_::SuperInner_::DuperInner_::EvenMoreInner_::JustWayTooInner;
use outer::inner::super_inner::duper_inner::even_more_inner::JustWayTooInner;
let deep = JustWayTooInner::default();
assert_that!(i32::from(deep), eq(0));
let outer_msg = Outer::new();
assert_that!(outer_msg.deep_enum(), eq(JustWayTooInner::Unspecified));
let outermsg = Outer::new();
assert_that!(outermsg.deep_enum(), eq(JustWayTooInner::Unspecified));
}
#[test]
fn test_nested_views() {
let outer_msg = Outer::new();
let inner_msg: InnerView<'_> = outer_msg.inner();
let outermsg = Outer::new();
let inner_msg: InnerView<'_> = outermsg.inner();
assert_that!(inner_msg.double(), eq(0.0));
assert_that!(inner_msg.float(), eq(0.0));
assert_that!(inner_msg.int32(), eq(0));
@ -58,24 +58,24 @@ fn test_nested_view_lifetimes() {
// Ensure that views have the lifetime of the first layer of borrow, and don't
// create intermediate borrows through nested accessors.
let outer_msg = Outer::new();
let outermsg = Outer::new();
let string = outer_msg.inner().string();
let string = outermsg.inner().string();
assert_that!(string, eq(""));
let bytes = outer_msg.inner().bytes();
let bytes = outermsg.inner().bytes();
assert_that!(bytes, eq(b""));
let inner_submsg = outer_msg.inner().inner_submsg();
let inner_submsg = outermsg.inner().inner_submsg();
assert_that!(inner_submsg.flag(), eq(false));
let repeated_int32 = outer_msg.inner().repeated_int32();
let repeated_int32 = outermsg.inner().repeated_int32();
assert_that!(repeated_int32, empty());
let repeated_inner_submsg = outer_msg.inner().repeated_inner_submsg();
let repeated_inner_submsg = outermsg.inner().repeated_inner_submsg();
assert_that!(repeated_inner_submsg, empty());
let string_map = outer_msg.inner().string_map();
let string_map = outermsg.inner().string_map();
assert_that!(string_map.len(), eq(0));
}

@ -497,7 +497,7 @@ void GenerateEnumThunksCc(Context& ctx, const EnumDescriptor& desc) {
ctx.Emit(
{
{"cpp_t", cpp::QualifiedClassName(&desc)},
{"rs_t", GetUnderscoreDelimitedFullName(ctx, desc)},
{"rs_t", UnderscoreDelimitFullName(ctx, desc.full_name())},
{"abi", "\"C\""}, // Workaround for syntax highlight bug in VSCode.
},
R"cc(

@ -712,12 +712,12 @@ void GenerateRs(Context& ctx, const Descriptor& msg) {
{"nested_in_msg",
[&] {
// If we have no nested types, enums, or oneofs, bail out without
// emitting an empty mod SomeMsg_.
// emitting an empty mod some_msg.
if (msg.nested_type_count() == 0 && msg.enum_type_count() == 0 &&
msg.real_oneof_decl_count() == 0) {
return;
}
ctx.Emit({{"Msg", RsSafeName(msg.name())},
ctx.Emit({{"mod_name", RsSafeName(CamelToSnakeCase(msg.name()))},
{"nested_msgs",
[&] {
for (int i = 0; i < msg.nested_type_count(); ++i) {
@ -737,13 +737,12 @@ void GenerateRs(Context& ctx, const Descriptor& msg) {
}
}}},
R"rs(
#[allow(non_snake_case)]
pub mod $Msg$_ {
pub mod $mod_name$ {
$nested_msgs$
$nested_enums$
$oneofs$
} // mod $Msg$_
} // mod $mod_name$
)rs");
}},
{"raw_arena_getter_for_message",

@ -131,28 +131,6 @@ std::string ThunkMapOrRepeated(Context& ctx, const FieldDescriptor& field,
return thunkName;
}
std::string RustModule(Context& ctx, const Descriptor* containing_type) {
std::vector<std::string> modules;
// Innermost to outermost order.
const Descriptor* parent = containing_type;
while (parent != nullptr) {
modules.push_back(absl::StrCat(parent->name(), "_"));
parent = parent->containing_type();
}
// Reverse the vector to get submodules in outer-to-inner order).
std::reverse(modules.begin(), modules.end());
// If there are any modules at all, push an empty string on the end so that
// we get the trailing ::
if (!modules.empty()) {
modules.push_back("");
}
return absl::StrJoin(modules, "::");
}
} // namespace
std::string ThunkName(Context& ctx, const FieldDescriptor& field,
@ -179,32 +157,23 @@ std::string VTableName(const FieldDescriptor& field) {
return absl::StrCat("__", absl::AsciiStrToUpper(field.name()), "_VTABLE");
}
std::string GetFullyQualifiedPath(Context& ctx, const Descriptor& msg) {
auto rel_path = GetCrateRelativeQualifiedPath(ctx, msg);
if (IsInCurrentlyGeneratingCrate(ctx, msg)) {
template <typename Desc>
std::string GetFullyQualifiedPath(Context& ctx, const Desc& desc) {
auto rel_path = GetCrateRelativeQualifiedPath(ctx, desc);
if (IsInCurrentlyGeneratingCrate(ctx, desc)) {
return absl::StrCat("crate::", rel_path);
}
return absl::StrCat(GetCrateName(ctx, *msg.file()), "::", rel_path);
return absl::StrCat(GetCrateName(ctx, *desc.file()), "::", rel_path);
}
std::string GetFullyQualifiedPath(Context& ctx, const EnumDescriptor& enum_) {
auto rel_path = GetCrateRelativeQualifiedPath(ctx, enum_);
if (IsInCurrentlyGeneratingCrate(ctx, enum_)) {
return absl::StrCat("crate::", rel_path);
}
return absl::StrCat(GetCrateName(ctx, *enum_.file()), "::", rel_path);
template <typename Desc>
std::string GetUnderscoreDelimitedFullName(Context& ctx, const Desc& desc) {
return UnderscoreDelimitFullName(ctx, desc.full_name());
}
std::string GetUnderscoreDelimitedFullName(Context& ctx,
const Descriptor& msg) {
std::string result = msg.full_name();
absl::StrReplaceAll({{".", "_"}}, &result);
return result;
}
std::string GetUnderscoreDelimitedFullName(Context& ctx,
const EnumDescriptor& enum_) {
std::string result = enum_.full_name();
std::string UnderscoreDelimitFullName(Context& ctx,
absl::string_view full_name) {
std::string result = std::string(full_name);
absl::StrReplaceAll({{".", "_"}}, &result);
return result;
}
@ -237,12 +206,39 @@ std::string RsTypePath(Context& ctx, const FieldDescriptor& field) {
ABSL_LOG(FATAL) << "Unsupported field type: " << field.type_name();
}
std::string RustModuleForContainingType(Context& ctx,
const Descriptor* containing_type) {
std::vector<std::string> modules;
// Innermost to outermost order.
const Descriptor* parent = containing_type;
while (parent != nullptr) {
modules.push_back(RsSafeName(CamelToSnakeCase(parent->name())));
parent = parent->containing_type();
}
// Reverse the vector to get submodules in outer-to-inner order).
std::reverse(modules.begin(), modules.end());
// If there are any modules at all, push an empty string on the end so that
// we get the trailing ::
if (!modules.empty()) {
modules.push_back("");
}
return absl::StrJoin(modules, "::");
}
std::string RustModule(Context& ctx, const Descriptor& msg) {
return RustModule(ctx, msg.containing_type());
return RustModuleForContainingType(ctx, msg.containing_type());
}
std::string RustModule(Context& ctx, const EnumDescriptor& enum_) {
return RustModule(ctx, enum_.containing_type());
return RustModuleForContainingType(ctx, enum_.containing_type());
}
std::string RustModule(Context& ctx, const OneofDescriptor& oneof) {
return RustModuleForContainingType(ctx, oneof.containing_type());
}
std::string RustInternalModuleName(Context& ctx, const FileDescriptor& file) {

@ -68,17 +68,24 @@ std::string RsSafeName(absl::string_view name);
//
// If the message has no package and no containing messages then this returns
// empty string.
std::string RustModuleForContainingType(Context& ctx,
const Descriptor* containing_type);
std::string RustModule(Context& ctx, const Descriptor& msg);
std::string RustModule(Context& ctx, const EnumDescriptor& enum_);
std::string RustModule(Context& ctx, const OneofDescriptor& oneof);
std::string RustInternalModuleName(Context& ctx, const FileDescriptor& file);
std::string GetCrateRelativeQualifiedPath(Context& ctx, const Descriptor& msg);
std::string GetCrateRelativeQualifiedPath(Context& ctx,
const EnumDescriptor& enum_);
std::string GetCrateRelativeQualifiedPath(Context& ctx,
const OneofDescriptor& oneof);
std::string GetUnderscoreDelimitedFullName(Context& ctx, const Descriptor& msg);
std::string GetUnderscoreDelimitedFullName(Context& ctx,
const EnumDescriptor& enum_);
template <typename Desc>
std::string GetUnderscoreDelimitedFullName(Context& ctx, const Desc& desc);
std::string UnderscoreDelimitFullName(Context& ctx,
absl::string_view full_name);
// TODO: Unify these with other case-conversion functions.

@ -41,7 +41,7 @@ namespace rust {
// }
//
// This will emit as the exposed API:
// pub mod SomeMsg_ {
// pub mod some_msg {
// pub enum SomeOneof<'msg> {
// FieldA(i32) = 7,
// FieldB(View<'msg, SomeMsg>) = 9,
@ -180,6 +180,9 @@ void GenerateOneofAccessors(Context& ctx, const OneofDescriptor& oneof,
{"oneof_name", RsSafeName(oneof.name())},
{"view_lifetime", ViewLifetime(accessor_case)},
{"self", ViewReceiver(accessor_case)},
{"oneof_enum_module",
absl::StrCat("crate::", RustModuleForContainingType(
ctx, oneof.containing_type()))},
{"view_enum_name", OneofViewEnumRsName(oneof)},
{"case_enum_name", OneofCaseEnumRsName(oneof)},
{"view_cases",
@ -197,22 +200,22 @@ void GenerateOneofAccessors(Context& ctx, const OneofDescriptor& oneof,
{"type", rs_type},
},
R"rs(
$Msg$_::$case_enum_name$::$case$ =>
$Msg$_::$view_enum_name$::$case$(self.$rs_getter$()),
$oneof_enum_module$$case_enum_name$::$case$ =>
$oneof_enum_module$$view_enum_name$::$case$(self.$rs_getter$()),
)rs");
}
}},
{"case_thunk", ThunkName(ctx, oneof, "case")},
},
R"rs(
pub fn $oneof_name$($self$) -> $Msg$_::$view_enum_name$<$view_lifetime$> {
pub fn $oneof_name$($self$) -> $oneof_enum_module$$view_enum_name$<$view_lifetime$> {
match $self$.$oneof_name$_case() {
$view_cases$
_ => $Msg$_::$view_enum_name$::not_set(std::marker::PhantomData)
_ => $oneof_enum_module$$view_enum_name$::not_set(std::marker::PhantomData)
}
}
pub fn $oneof_name$_case($self$) -> $Msg$_::$case_enum_name$ {
pub fn $oneof_name$_case($self$) -> $oneof_enum_module$$case_enum_name$ {
unsafe { $case_thunk$(self.raw_msg()) }
}
)rs");
@ -221,11 +224,14 @@ void GenerateOneofAccessors(Context& ctx, const OneofDescriptor& oneof,
void GenerateOneofExternC(Context& ctx, const OneofDescriptor& oneof) {
ctx.Emit(
{
{"oneof_enum_module",
absl::StrCat("crate::", RustModuleForContainingType(
ctx, oneof.containing_type()))},
{"case_enum_rs_name", OneofCaseEnumRsName(oneof)},
{"case_thunk", ThunkName(ctx, oneof, "case")},
},
R"rs(
fn $case_thunk$(raw_msg: $pbi$::RawMessage) -> $Msg$_::$case_enum_rs_name$;
fn $case_thunk$(raw_msg: $pbi$::RawMessage) -> $oneof_enum_module$$case_enum_rs_name$;
)rs");
}

Loading…
Cancel
Save