From fa67ce8d4d99c9c70f37bee93de039d4aa8c4464 Mon Sep 17 00:00:00 2001 From: Alyssa Haroldsen Date: Mon, 8 Jan 2024 19:07:49 -0800 Subject: [PATCH] Expand PrimitiveRsTypeName to work with non-primitives, rename, refactor PiperOrigin-RevId: 596765288 --- src/google/protobuf/compiler/rust/accessors/map.cc | 4 ++-- .../compiler/rust/accessors/repeated_scalar.cc | 4 ++-- .../compiler/rust/accessors/singular_message.cc | 5 +++-- .../compiler/rust/accessors/singular_scalar.cc | 4 ++-- .../compiler/rust/accessors/singular_string.cc | 2 +- src/google/protobuf/compiler/rust/message.cc | 4 ++-- src/google/protobuf/compiler/rust/naming.cc | 10 +++++++++- src/google/protobuf/compiler/rust/naming.h | 5 ++++- src/google/protobuf/compiler/rust/oneof.cc | 12 +++--------- 9 files changed, 28 insertions(+), 22 deletions(-) diff --git a/src/google/protobuf/compiler/rust/accessors/map.cc b/src/google/protobuf/compiler/rust/accessors/map.cc index 4061d731ca..5c103a195d 100644 --- a/src/google/protobuf/compiler/rust/accessors/map.cc +++ b/src/google/protobuf/compiler/rust/accessors/map.cc @@ -22,8 +22,8 @@ void Map::InMsgImpl(Context& ctx, const FieldDescriptor& field) const { auto& value_type = *field.message_type()->map_value(); ctx.Emit({{"field", field.name()}, - {"Key", PrimitiveRsTypeName(key_type)}, - {"Value", PrimitiveRsTypeName(value_type)}, + {"Key", RsTypePath(ctx, key_type)}, + {"Value", RsTypePath(ctx, value_type)}, {"getter_thunk", ThunkName(ctx, field, "get")}, {"getter_mut_thunk", ThunkName(ctx, field, "get_mut")}, {"getter", diff --git a/src/google/protobuf/compiler/rust/accessors/repeated_scalar.cc b/src/google/protobuf/compiler/rust/accessors/repeated_scalar.cc index b88fa9fe37..e831c70cd1 100644 --- a/src/google/protobuf/compiler/rust/accessors/repeated_scalar.cc +++ b/src/google/protobuf/compiler/rust/accessors/repeated_scalar.cc @@ -20,7 +20,7 @@ namespace rust { void RepeatedScalar::InMsgImpl(Context& ctx, const FieldDescriptor& field) const { ctx.Emit({{"field", field.name()}, - {"Scalar", PrimitiveRsTypeName(field)}, + {"Scalar", RsTypePath(ctx, field)}, {"getter_thunk", ThunkName(ctx, field, "get")}, {"getter_mut_thunk", ThunkName(ctx, field, "get_mut")}, {"getter", @@ -100,7 +100,7 @@ void RepeatedScalar::InMsgImpl(Context& ctx, void RepeatedScalar::InExternC(Context& ctx, const FieldDescriptor& field) const { - ctx.Emit({{"Scalar", PrimitiveRsTypeName(field)}, + ctx.Emit({{"Scalar", RsTypePath(ctx, field)}, {"getter_thunk", ThunkName(ctx, field, "get")}, {"getter_mut_thunk", ThunkName(ctx, field, "get_mut")}, {"getter", diff --git a/src/google/protobuf/compiler/rust/accessors/singular_message.cc b/src/google/protobuf/compiler/rust/accessors/singular_message.cc index b94473cb6d..81dbc4efe6 100644 --- a/src/google/protobuf/compiler/rust/accessors/singular_message.cc +++ b/src/google/protobuf/compiler/rust/accessors/singular_message.cc @@ -5,6 +5,8 @@ // license that can be found in the LICENSE file or at // https://developers.google.com/open-source/licenses/bsd +#include + #include "absl/strings/string_view.h" #include "google/protobuf/compiler/cpp/helpers.h" #include "google/protobuf/compiler/rust/accessors/accessor_generator.h" @@ -19,8 +21,7 @@ namespace rust { void SingularMessage::InMsgImpl(Context& ctx, const FieldDescriptor& field) const { - auto& msg = *field.message_type(); - auto prefix = "crate::" + GetCrateRelativeQualifiedPath(ctx, msg); + std::string prefix = RsTypePath(ctx, field); ctx.Emit( { diff --git a/src/google/protobuf/compiler/rust/accessors/singular_scalar.cc b/src/google/protobuf/compiler/rust/accessors/singular_scalar.cc index a49cf6cbc2..1f90cd62b7 100644 --- a/src/google/protobuf/compiler/rust/accessors/singular_scalar.cc +++ b/src/google/protobuf/compiler/rust/accessors/singular_scalar.cc @@ -23,7 +23,7 @@ void SingularScalar::InMsgImpl(Context& ctx, ctx.Emit( { {"field", field.name()}, - {"Scalar", PrimitiveRsTypeName(field)}, + {"Scalar", RsTypePath(ctx, field)}, {"hazzer_thunk", ThunkName(ctx, field, "has")}, {"default_value", DefaultValue(field)}, {"getter", @@ -117,7 +117,7 @@ void SingularScalar::InMsgImpl(Context& ctx, void SingularScalar::InExternC(Context& ctx, const FieldDescriptor& field) const { - ctx.Emit({{"Scalar", PrimitiveRsTypeName(field)}, + ctx.Emit({{"Scalar", RsTypePath(ctx, field)}, {"hazzer_thunk", ThunkName(ctx, field, "has")}, {"getter_thunk", ThunkName(ctx, field, "get")}, {"setter_thunk", ThunkName(ctx, field, "set")}, diff --git a/src/google/protobuf/compiler/rust/accessors/singular_string.cc b/src/google/protobuf/compiler/rust/accessors/singular_string.cc index 227c11b34b..6992d09704 100644 --- a/src/google/protobuf/compiler/rust/accessors/singular_string.cc +++ b/src/google/protobuf/compiler/rust/accessors/singular_string.cc @@ -25,7 +25,7 @@ void SingularString::InMsgImpl(Context& ctx, std::string hazzer_thunk = ThunkName(ctx, field, "has"); std::string getter_thunk = ThunkName(ctx, field, "get"); std::string setter_thunk = ThunkName(ctx, field, "set"); - std::string proxied_type = PrimitiveRsTypeName(field); + std::string proxied_type = RsTypePath(ctx, field); auto transform_view = [&] { if (field.type() == FieldDescriptor::TYPE_STRING) { ctx.Emit(R"rs( diff --git a/src/google/protobuf/compiler/rust/message.cc b/src/google/protobuf/compiler/rust/message.cc index 6b5bfa877a..c1d3ba899e 100644 --- a/src/google/protobuf/compiler/rust/message.cc +++ b/src/google/protobuf/compiler/rust/message.cc @@ -233,7 +233,7 @@ void GetterForViewOrMut(Context& ctx, const FieldDescriptor& field, if (!IsInCurrentlyGeneratingCrate(ctx, msg)) { return; } - auto prefix = "crate::" + GetCrateRelativeQualifiedPath(ctx, msg); + auto prefix = RsTypePath(ctx, field); ctx.Emit( { {"prefix", prefix}, @@ -270,7 +270,7 @@ void GetterForViewOrMut(Context& ctx, const FieldDescriptor& field, return; } - auto rsType = PrimitiveRsTypeName(field); + auto rsType = RsTypePath(ctx, field); auto asRef = IsStringOrBytes(fieldType) ? ".as_ref()" : ""; auto vtable = IsStringOrBytes(fieldType) ? "BytesMutVTable" : "PrimitiveVTable"; diff --git a/src/google/protobuf/compiler/rust/naming.cc b/src/google/protobuf/compiler/rust/naming.cc index ea084e6b09..aef71565c4 100644 --- a/src/google/protobuf/compiler/rust/naming.cc +++ b/src/google/protobuf/compiler/rust/naming.cc @@ -175,7 +175,7 @@ std::string ThunkName(Context& ctx, const Descriptor& msg, op); } -std::string PrimitiveRsTypeName(const FieldDescriptor& field) { +std::string RsTypePath(Context& ctx, const FieldDescriptor& field) { switch (field.type()) { case FieldDescriptor::TYPE_BOOL: return "bool"; @@ -201,6 +201,14 @@ std::string PrimitiveRsTypeName(const FieldDescriptor& field) { return "[u8]"; case FieldDescriptor::TYPE_STRING: return "::__pb::ProtoStr"; + case FieldDescriptor::TYPE_MESSAGE: + // TODO: Fix depending on types from other proto_libraries. + return absl::StrCat( + "crate::", GetCrateRelativeQualifiedPath(ctx, *field.message_type())); + case FieldDescriptor::TYPE_ENUM: + // TODO: Fix depending on types from other proto_libraries. + return absl::StrCat( + "crate::", GetCrateRelativeQualifiedPath(ctx, *field.enum_type())); default: break; } diff --git a/src/google/protobuf/compiler/rust/naming.h b/src/google/protobuf/compiler/rust/naming.h index 46b2e951e3..532863ce31 100644 --- a/src/google/protobuf/compiler/rust/naming.h +++ b/src/google/protobuf/compiler/rust/naming.h @@ -33,7 +33,10 @@ std::string ThunkName(Context& ctx, const OneofDescriptor& field, std::string ThunkName(Context& ctx, const Descriptor& msg, absl::string_view op); -std::string PrimitiveRsTypeName(const FieldDescriptor& field); +// Returns an absolute path to the Proxied Rust type of the given field. +// The absolute path is guaranteed to work in the crate that defines the field. +// It may be crate-relative, or directly reference the owning crate of the type. +std::string RsTypePath(Context& ctx, const FieldDescriptor& field); std::string EnumRsName(const EnumDescriptor& desc); diff --git a/src/google/protobuf/compiler/rust/oneof.cc b/src/google/protobuf/compiler/rust/oneof.cc index 1408bfcd94..7f5d90f2ae 100644 --- a/src/google/protobuf/compiler/rust/oneof.cc +++ b/src/google/protobuf/compiler/rust/oneof.cc @@ -92,15 +92,13 @@ std::string RsTypeNameView(Context& ctx, const FieldDescriptor& field) { case FieldDescriptor::TYPE_FLOAT: case FieldDescriptor::TYPE_DOUBLE: case FieldDescriptor::TYPE_BOOL: - return PrimitiveRsTypeName(field); + return RsTypePath(ctx, field); case FieldDescriptor::TYPE_BYTES: return "&'msg [u8]"; case FieldDescriptor::TYPE_STRING: return "&'msg ::__pb::ProtoStr"; case FieldDescriptor::TYPE_MESSAGE: - return absl::StrCat( - "::__pb::View<'msg, crate::", - GetCrateRelativeQualifiedPath(ctx, *field.message_type()), ">"); + return absl::StrCat("::__pb::View<'msg, ", RsTypePath(ctx, field), ">"); case FieldDescriptor::TYPE_ENUM: // TODO: b/300257770 - Support enums. case FieldDescriptor::TYPE_GROUP: // Not supported yet. return ""; @@ -130,12 +128,8 @@ std::string RsTypeNameMut(Context& ctx, const FieldDescriptor& field) { case FieldDescriptor::TYPE_BOOL: case FieldDescriptor::TYPE_BYTES: case FieldDescriptor::TYPE_STRING: - return absl::StrCat("::__pb::Mut<'msg, ", PrimitiveRsTypeName(field), - ">"); case FieldDescriptor::TYPE_MESSAGE: - return absl::StrCat( - "::__pb::Mut<'msg, crate::", - GetCrateRelativeQualifiedPath(ctx, *field.message_type()), ">"); + return absl::StrCat("::__pb::Mut<'msg, ", RsTypePath(ctx, field), ">"); case FieldDescriptor::TYPE_ENUM: // TODO: b/300257770 - Support enums. case FieldDescriptor::TYPE_GROUP: // Not supported yet. return "";