Remove upb C accessor thunk name logic.

Now 'thunk' is only for cpp kernel we don't need lots of special casing to replicate the upb accessor names (we basically have free choice of name instead).

Since the ThunkName() function should only be used from cpp kernel, it now check-fails if its called from upb kernel, which requires pushing down the thunkname() calls to the cpp-kernel specific paths in some cases.

PiperOrigin-RevId: 668958849
pull/18025/head
Protobuf Team Bot 7 months ago committed by Copybara-Service
parent fa15d09055
commit d697a86b1b
  1. 15
      src/google/protobuf/compiler/rust/accessors/repeated_field.cc
  2. 148
      src/google/protobuf/compiler/rust/accessors/singular_cord.cc
  3. 19
      src/google/protobuf/compiler/rust/accessors/singular_message.cc
  4. 7
      src/google/protobuf/compiler/rust/accessors/singular_scalar.cc
  5. 11
      src/google/protobuf/compiler/rust/accessors/singular_string.cc
  6. 78
      src/google/protobuf/compiler/rust/naming.cc

@ -32,13 +32,11 @@ void RepeatedField::InMsgImpl(Context& ctx, const FieldDescriptor& field,
{"RsType", RsTypePath(ctx, field)},
{"view_lifetime", ViewLifetime(accessor_case)},
{"view_self", ViewReceiver(accessor_case)},
{"getter_thunk", ThunkName(ctx, field, "get")},
{"getter_mut_thunk", ThunkName(ctx, field, "get_mut")},
{"upb_mt_field_index", UpbMiniTableFieldIndex(field)},
{"getter",
[&] {
if (ctx.is_upb()) {
ctx.Emit({}, R"rs(
ctx.Emit(R"rs(
pub fn $field$($view_self$) -> $pb$::RepeatedView<$view_lifetime$, $RsType$> {
unsafe {
let f = $pbr$::upb_MiniTable_GetFieldByIndex(
@ -55,7 +53,7 @@ void RepeatedField::InMsgImpl(Context& ctx, const FieldDescriptor& field,
}
)rs");
} else {
ctx.Emit({}, R"rs(
ctx.Emit({{"getter_thunk", ThunkName(ctx, field, "get")}}, R"rs(
pub fn $field$($view_self$) -> $pb$::RepeatedView<$view_lifetime$, $RsType$> {
unsafe {
$pb$::RepeatedView::from_raw(
@ -94,7 +92,9 @@ void RepeatedField::InMsgImpl(Context& ctx, const FieldDescriptor& field,
}
)rs");
} else {
ctx.Emit({}, R"rs(
ctx.Emit(
{{"getter_mut_thunk", ThunkName(ctx, field, "get_mut")}},
R"rs(
pub fn $field$_mut(&mut self) -> $pb$::RepeatedMut<'_, $RsType$> {
unsafe {
$pb$::RepeatedMut::from_inner(
@ -108,7 +108,6 @@ void RepeatedField::InMsgImpl(Context& ctx, const FieldDescriptor& field,
)rs");
}
}},
{"move_setter_thunk", ThunkName(ctx, field, "move_set")},
{"setter",
[&] {
if (accessor_case == AccessorCase::VIEW) {
@ -137,7 +136,9 @@ void RepeatedField::InMsgImpl(Context& ctx, const FieldDescriptor& field,
}
)rs");
} else {
ctx.Emit({}, R"rs(
ctx.Emit(
{{"move_setter_thunk", ThunkName(ctx, field, "move_set")}},
R"rs(
pub fn set_$raw_field_name$(&mut self, src: impl $pb$::IntoProxied<$pb$::Repeated<$RsType$>>) {
// Prevent the memory from being deallocated. The setter
// transfers ownership of the memory to the parent message.

@ -32,72 +32,72 @@ void SingularCord::InMsgImpl(Context& ctx, const FieldDescriptor& field,
std::string field_name = FieldNameWithCollisionAvoidance(field);
bool is_string_type = field.type() == FieldDescriptor::TYPE_STRING;
ctx.Emit(
{{"field", RsSafeName(field_name)},
{"raw_field_name", field_name},
{"borrowed_getter_thunk", ThunkName(ctx, field, "get_cord_borrowed")},
{"owned_getter_thunk", ThunkName(ctx, field, "get_cord_owned")},
{"is_flat_thunk", ThunkName(ctx, field, "cord_is_flat")},
{"setter_thunk", ThunkName(ctx, field, "set")},
{"getter_thunk", ThunkName(ctx, field, "get")},
{"proxied_type", RsTypePath(ctx, field)},
{"default_value", DefaultValue(ctx, field)},
{"upb_mt_field_index", UpbMiniTableFieldIndex(field)},
{"borrowed_type",
[&] {
if (is_string_type) {
ctx.Emit("$pb$::ProtoStr");
} else {
ctx.Emit("[u8]");
}
}},
{"transform_borrowed",
[&] {
if (is_string_type) {
ctx.Emit(R"rs(
ctx.Emit({{"field", RsSafeName(field_name)},
{"raw_field_name", field_name},
{"proxied_type", RsTypePath(ctx, field)},
{"default_value", DefaultValue(ctx, field)},
{"upb_mt_field_index", UpbMiniTableFieldIndex(field)},
{"borrowed_type",
[&] {
if (is_string_type) {
ctx.Emit("$pb$::ProtoStr");
} else {
ctx.Emit("[u8]");
}
}},
{"transform_borrowed",
[&] {
if (is_string_type) {
ctx.Emit(R"rs(
$pb$::ProtoStringCow::Borrowed(
// SAFETY: The runtime doesn't require ProtoStr to be UTF-8.
unsafe { $pb$::ProtoStr::from_utf8_unchecked(view.as_ref()) }
)
)rs");
} else {
ctx.Emit(R"rs(
} else {
ctx.Emit(R"rs(
$pb$::ProtoBytesCow::Borrowed(
unsafe { view.as_ref() }
)
)rs");
}
}},
{"transform_owned",
[&] {
if (is_string_type) {
ctx.Emit(R"rs(
}
}},
{"transform_owned",
[&] {
if (is_string_type) {
ctx.Emit(R"rs(
$pb$::ProtoStringCow::Owned(
$pb$::ProtoString::from_inner($pbi$::Private, inner)
)
)rs");
} else {
ctx.Emit(R"rs(
} else {
ctx.Emit(R"rs(
$pb$::ProtoBytesCow::Owned(
$pb$::ProtoBytes::from_inner($pbi$::Private, inner)
)
)rs");
}
}},
{"view_lifetime", ViewLifetime(accessor_case)},
{"view_type",
[&] {
if (is_string_type) {
ctx.Emit("$pb$::ProtoStringCow<$view_lifetime$>");
} else {
ctx.Emit("$pb$::ProtoBytesCow<$view_lifetime$>");
}
}},
{"view_self", ViewReceiver(accessor_case)},
{"getter_impl",
[&] {
if (ctx.is_cpp()) {
ctx.Emit(R"rs(
}
}},
{"view_lifetime", ViewLifetime(accessor_case)},
{"view_type",
[&] {
if (is_string_type) {
ctx.Emit("$pb$::ProtoStringCow<$view_lifetime$>");
} else {
ctx.Emit("$pb$::ProtoBytesCow<$view_lifetime$>");
}
}},
{"view_self", ViewReceiver(accessor_case)},
{"getter_impl",
[&] {
if (ctx.is_cpp()) {
ctx.Emit(
{{"is_flat_thunk", ThunkName(ctx, field, "cord_is_flat")},
{"borrowed_getter_thunk",
ThunkName(ctx, field, "get_cord_borrowed")},
{"owned_getter_thunk",
ThunkName(ctx, field, "get_cord_owned")}},
R"rs(
let cord_is_flat = unsafe { $is_flat_thunk$(self.raw_msg()) };
if cord_is_flat {
let view = unsafe { $borrowed_getter_thunk$(self.raw_msg()) };
@ -109,8 +109,8 @@ void SingularCord::InMsgImpl(Context& ctx, const FieldDescriptor& field,
$transform_owned$
)rs");
} else {
ctx.Emit(R"rs(
} else {
ctx.Emit(R"rs(
let view = unsafe {
let f = $pbr$::upb_MiniTable_GetFieldByIndex(
<Self as $pbr$::AssociatedMiniTable>::mini_table(),
@ -120,21 +120,21 @@ void SingularCord::InMsgImpl(Context& ctx, const FieldDescriptor& field,
};
$transform_borrowed$
)rs");
}
}},
{"getter",
[&] {
ctx.Emit(R"rs(
}
}},
{"getter",
[&] {
ctx.Emit(R"rs(
pub fn $field$($view_self$) -> $view_type$ {
$getter_impl$
}
)rs");
}},
{"setter_impl",
[&] {
if (ctx.is_cpp()) {
ctx.Emit({},
R"rs(
}},
{"setter_impl",
[&] {
if (ctx.is_cpp()) {
ctx.Emit({{"setter_thunk", ThunkName(ctx, field, "set")}},
R"rs(
let s = val.into_proxied($pbi$::Private);
unsafe {
$setter_thunk$(
@ -143,8 +143,8 @@ void SingularCord::InMsgImpl(Context& ctx, const FieldDescriptor& field,
);
}
)rs");
} else {
ctx.Emit(R"rs(
} else {
ctx.Emit(R"rs(
let s = val.into_proxied($pbi$::Private);
let (view, arena) =
s.into_inner($pbi$::Private).into_raw_parts();
@ -165,19 +165,19 @@ void SingularCord::InMsgImpl(Context& ctx, const FieldDescriptor& field,
view);
}
)rs");
}
}},
{"setter",
[&] {
if (accessor_case == AccessorCase::VIEW) return;
ctx.Emit({},
R"rs(
}
}},
{"setter",
[&] {
if (accessor_case == AccessorCase::VIEW) return;
ctx.Emit({},
R"rs(
pub fn set_$raw_field_name$(&mut self, val: impl $pb$::IntoProxied<$proxied_type$>) {
$setter_impl$
}
)rs");
}}},
R"rs(
}}},
R"rs(
$getter$
$setter$
)rs");
@ -217,7 +217,7 @@ void SingularCord::InExternC(Context& ctx, const FieldDescriptor& field) const {
fn $owned_getter_thunk$(raw_msg: $pbr$::RawMessage) -> $pbr$::CppStdString;
)rs");
} else {
ctx.Emit(R"rs(
ctx.Emit({{"getter_thunk", ThunkName(ctx, field, "get")}}, R"rs(
fn $getter_thunk$(raw_msg: $pbr$::RawMessage) -> $pbr$::PtrAndLen;
)rs");
}

@ -39,15 +39,12 @@ void SingularMessage::InMsgImpl(Context& ctx, const FieldDescriptor& field,
{"raw_field_name", field_name},
{"view_lifetime", ViewLifetime(accessor_case)},
{"view_self", ViewReceiver(accessor_case)},
{"getter_thunk", ThunkName(ctx, field, "get")},
{"getter_mut_thunk", ThunkName(ctx, field, "get_mut")},
{"set_allocated_thunk", ThunkName(ctx, field, "set")},
{"upb_mt_field_index", UpbMiniTableFieldIndex(field)},
{
"getter_body",
[&] {
if (ctx.is_upb()) {
ctx.Emit({}, R"rs(
ctx.Emit(R"rs(
let submsg = unsafe {
let f = $pbr$::upb_MiniTable_GetFieldByIndex(
<Self as $pbr$::AssociatedMiniTable>::mini_table(),
@ -64,7 +61,8 @@ void SingularMessage::InMsgImpl(Context& ctx, const FieldDescriptor& field,
}
)rs");
} else {
ctx.Emit({}, R"rs(
ctx.Emit({{"getter_thunk", ThunkName(ctx, field, "get")}},
R"rs(
//~ For C++ kernel, getters automatically return the
//~ default_instance if the field is unset.
let submsg = unsafe { $getter_thunk$(self.raw_msg()) };
@ -75,7 +73,7 @@ void SingularMessage::InMsgImpl(Context& ctx, const FieldDescriptor& field,
},
{"getter",
[&] {
ctx.Emit({}, R"rs(
ctx.Emit(R"rs(
pub fn $field$($view_self$) -> $msg_type$View<$view_lifetime$> {
$getter_body$
}
@ -84,7 +82,9 @@ void SingularMessage::InMsgImpl(Context& ctx, const FieldDescriptor& field,
{"getter_mut_body",
[&] {
if (ctx.is_cpp()) {
ctx.Emit({}, R"rs(
ctx.Emit(
{{"getter_mut_thunk", ThunkName(ctx, field, "get_mut")}},
R"rs(
let raw_msg = unsafe { $getter_mut_thunk$(self.raw_msg()) };
$msg_type$Mut::from_parent($pbi$::Private,
self.as_mutator_message_ref($pbi$::Private), raw_msg)
@ -117,7 +117,7 @@ void SingularMessage::InMsgImpl(Context& ctx, const FieldDescriptor& field,
[&] {
if (accessor_case == AccessorCase::VIEW) return;
if (ctx.is_upb()) {
ctx.Emit({}, R"rs(
ctx.Emit(R"rs(
// The message and arena are dropped after the setter. The
// memory remains allocated as we fuse the arena with the
// parent message's arena.
@ -137,7 +137,8 @@ void SingularMessage::InMsgImpl(Context& ctx, const FieldDescriptor& field,
}
)rs");
} else {
ctx.Emit({}, R"rs(
ctx.Emit({{"set_allocated_thunk", ThunkName(ctx, field, "set")}},
R"rs(
// Prevent the memory from being deallocated. The setter
// transfers ownership of the memory to the parent message.
let mut msg = std::mem::ManuallyDrop::new(val.into_proxied($pbi$::Private));

@ -71,14 +71,13 @@ void SingularScalar::InMsgImpl(Context& ctx, const FieldDescriptor& field,
{"raw_field_name", field_name}, // Never r# prefixed
{"view_self", ViewReceiver(accessor_case)},
{"Scalar", RsTypePath(ctx, field)},
{"hazzer_thunk", ThunkName(ctx, field, "has")},
{"default_value", DefaultValue(ctx, field)},
{"upb_mt_field_index", UpbMiniTableFieldIndex(field)},
{"upb_fn_type_name", UpbCTypeNameForFunctions(field)},
{"getter",
[&] {
if (ctx.is_cpp()) {
ctx.Emit(R"rs(
ctx.Emit({{"getter_thunk", ThunkName(ctx, field, "get")}}, R"rs(
pub fn $field$($view_self$) -> $Scalar$ {
unsafe { $getter_thunk$(self.raw_msg()) }
}
@ -109,7 +108,7 @@ void SingularScalar::InMsgImpl(Context& ctx, const FieldDescriptor& field,
[&] {
if (accessor_case == AccessorCase::VIEW) return;
if (ctx.is_cpp()) {
ctx.Emit(R"rs(
ctx.Emit({{"setter_thunk", ThunkName(ctx, field, "set")}}, R"rs(
pub fn set_$raw_field_name$(&mut self, val: $Scalar$) {
unsafe { $setter_thunk$(self.raw_msg(), val) }
}
@ -132,8 +131,6 @@ void SingularScalar::InMsgImpl(Context& ctx, const FieldDescriptor& field,
)rs");
}
}},
{"getter_thunk", ThunkName(ctx, field, "get")},
{"setter_thunk", ThunkName(ctx, field, "set")},
},
R"rs(
$getter$

@ -35,8 +35,6 @@ void SingularString::InMsgImpl(Context& ctx, const FieldDescriptor& field,
{
{"field", RsSafeName(field_name)},
{"raw_field_name", field_name},
{"getter_thunk", ThunkName(ctx, field, "get")},
{"setter_thunk", ThunkName(ctx, field, "set")},
{"default_value", DefaultValue(ctx, field)},
{"upb_mt_field_index", UpbMiniTableFieldIndex(field)},
{"proxied_type", RsTypePath(ctx, field)},
@ -57,7 +55,8 @@ void SingularString::InMsgImpl(Context& ctx, const FieldDescriptor& field,
{"getter",
[&] {
if (ctx.is_cpp()) {
ctx.Emit(R"rs(
ctx.Emit({{"getter_thunk", ThunkName(ctx, field, "get")}},
R"rs(
pub fn $field$($view_self$) -> $pb$::View<$view_lifetime$, $proxied_type$> {
let str_view = unsafe { $getter_thunk$(self.raw_msg()) };
$transform_view$
@ -79,7 +78,8 @@ void SingularString::InMsgImpl(Context& ctx, const FieldDescriptor& field,
{"setter_impl",
[&] {
if (ctx.is_cpp()) {
ctx.Emit(R"rs(
ctx.Emit({{"setter_thunk", ThunkName(ctx, field, "set")}},
R"rs(
let s = val.into_proxied($pbi$::Private);
unsafe {
$setter_thunk$(
@ -115,8 +115,7 @@ void SingularString::InMsgImpl(Context& ctx, const FieldDescriptor& field,
{"setter",
[&] {
if (accessor_case == AccessorCase::VIEW) return;
ctx.Emit({},
R"rs(
ctx.Emit(R"rs(
pub fn set_$raw_field_name$(&mut self, val: impl $pb$::IntoProxied<$proxied_type$>) {
$setter_impl$
}

@ -77,86 +77,20 @@ std::string RawMapThunk(Context& ctx, const EnumDescriptor& desc,
return absl::StrCat("proto2_rust_thunk_Map_", key_t, "_i32_", op);
}
namespace {
template <typename T>
std::string ThunkNameUpb(Context& ctx, const T& field, absl::string_view op) {
ABSL_CHECK(ctx.is_upb());
absl::string_view format;
// NOTE: this function's outputs must match the symbols
// that the upbc plugin generates exactly. Failure to do so correctly
// will result in a link-time failure.
if (op == "get") {
// upb getter is simply the field name (no "get" in the name).
format = "_$1";
} else if (op == "get_mut") {
// same as above, with with `mutable` prefix
format = "_mutable_$1";
} else if (op == "case") {
// some upb functions are in the order x_op compared to has/set/clear
// which are in the other order e.g. op_x.
format = "_$1_$0";
} else {
format = "_$0_$1";
}
return absl::StrCat(
GetUnderscoreDelimitedFullName(ctx, *field.containing_type()),
absl::Substitute(format, op, field.name()));
}
template <typename T>
std::string ThunkNameCpp(Context& ctx, const T& field, absl::string_view op) {
std::string ThunkName(Context& ctx, const FieldDescriptor& field,
absl::string_view op) {
ABSL_CHECK(ctx.is_cpp());
return absl::StrCat("proto2_rust_thunk_",
UnderscoreDelimitFullName(ctx, field.full_name()), "_",
op);
}
template <typename T>
std::string ThunkName(Context& ctx, const T& field, absl::string_view op) {
if (ctx.is_upb()) {
return ThunkNameUpb(ctx, field, op);
} else {
return ThunkNameCpp(ctx, field, op);
}
}
std::string ThunkMapOrRepeated(Context& ctx, const FieldDescriptor& field,
absl::string_view op) {
if (!ctx.is_upb()) {
return ThunkName<FieldDescriptor>(ctx, field, op);
}
absl::string_view format;
if (op == "get") {
format = field.is_map() ? "_$1_upb_map" : "_$1_upb_array";
} else if (op == "get_mut") {
format = field.is_map() ? "_$1_mutable_upb_map" : "_$1_mutable_upb_array";
} else {
return ThunkName<FieldDescriptor>(ctx, field, op);
}
std::string thunkName = absl::StrCat(
"_", GetUnderscoreDelimitedFullName(ctx, *field.containing_type()));
absl::SubstituteAndAppend(&thunkName, format, op, field.name());
return thunkName;
}
} // namespace
std::string ThunkName(Context& ctx, const FieldDescriptor& field,
absl::string_view op) {
if (field.is_map() || field.is_repeated()) {
return ThunkMapOrRepeated(ctx, field, op);
}
return ThunkName<FieldDescriptor>(ctx, field, op);
}
std::string ThunkName(Context& ctx, const OneofDescriptor& field,
absl::string_view op) {
return ThunkName<OneofDescriptor>(ctx, field, op);
ABSL_CHECK(ctx.is_cpp());
return absl::StrCat("proto2_rust_thunk_",
UnderscoreDelimitFullName(ctx, field.full_name()), "_",
op);
}
std::string ThunkName(Context& ctx, const Descriptor& msg,

Loading…
Cancel
Save