Skip to content

Commit

Permalink
Fix some non-breaking issues with rust gencode.
Browse files Browse the repository at this point in the history
- Stop emitting a clear thunk for without-presence string/bytes fields (was unused, but would link-error it was used since there's no such operation).
- Remove unused clearer_thunk variable in message.cc
- Remove unsafe{} block surrounding non-unsafe BytesMutVTable::new
- Add #[allow(non_snake_case)] on module names (the new mangling added in cr/593048297 puts double-underscore in names which rustc recognizes as non-snake).

PiperOrigin-RevId: 594236747
  • Loading branch information
protobuf-github-bot authored and copybara-github committed Dec 28, 2023
1 parent 0b23719 commit 87f0627
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ void SingularScalar::InExternC(Context& ctx,
{"getter_thunk", Thunk(ctx, field, "get")},
{"setter_thunk", Thunk(ctx, field, "set")},
{"clearer_thunk", Thunk(ctx, field, "clear")},
{"hazzer_and_clearer",
{"thunks_for_with_presence_fields",
[&] {
if (field.has_presence()) {
ctx.Emit(
Expand All @@ -133,7 +133,7 @@ void SingularScalar::InExternC(Context& ctx,
}
}}},
R"rs(
$hazzer_and_clearer$
$thunks_for_with_presence_fields$
fn $getter_thunk$(raw_msg: $pbi$::RawMessage) -> $Scalar$;
fn $setter_thunk$(raw_msg: $pbi$::RawMessage, val: $Scalar$);
)rs");
Expand All @@ -148,7 +148,7 @@ void SingularScalar::InThunkCc(Context& ctx,
{"getter_thunk", Thunk(ctx, field, "get")},
{"setter_thunk", Thunk(ctx, field, "set")},
{"clearer_thunk", Thunk(ctx, field, "clear")},
{"hazzer_and_clearer",
{"thunks_for_with_presence_fields",
[&] {
if (field.has_presence()) {
ctx.Emit(R"cc(
Expand All @@ -160,7 +160,7 @@ void SingularScalar::InThunkCc(Context& ctx,
}
}}},
R"cc(
$hazzer_and_clearer$;
$thunks_for_with_presence_fields$;
$Scalar$ $getter_thunk$($QualifiedMsg$* msg) { return msg->$field$(); }
void $setter_thunk$($QualifiedMsg$* msg, $Scalar$ val) {
msg->set_$field$(val);
Expand Down
15 changes: 7 additions & 8 deletions src/google/protobuf/compiler/rust/accessors/singular_string.cc
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,12 @@ void SingularString::InMsgImpl(Context& ctx,
{"setter_thunk", setter_thunk}},
R"rs(
pub fn $field$_mut(&mut self) -> $pb$::Mut<'_, $proxied_type$> {
static VTABLE: $pbi$::BytesMutVTable = unsafe {
static VTABLE: $pbi$::BytesMutVTable =
$pbi$::BytesMutVTable::new(
$pbi$::Private,
$getter_thunk$,
$setter_thunk$,
)
};
);
unsafe {
<$pb$::Mut<$proxied_type$>>::from_inner(
$pbi$::Private,
Expand Down Expand Up @@ -158,19 +157,19 @@ void SingularString::InExternC(Context& ctx,
{"getter_thunk", Thunk(ctx, field, "get")},
{"setter_thunk", Thunk(ctx, field, "set")},
{"clearer_thunk", Thunk(ctx, field, "clear")},
{"hazzer",
{"thunks_for_with_presence_fields",
[&] {
if (field.has_presence()) {
ctx.Emit(R"rs(
fn $hazzer_thunk$(raw_msg: $pbi$::RawMessage) -> bool;
fn $clearer_thunk$(raw_msg: $pbi$::RawMessage);
)rs");
}
}}},
R"rs(
$hazzer$
$thunks_for_with_presence_fields$
fn $getter_thunk$(raw_msg: $pbi$::RawMessage) -> $pbi$::PtrAndLen;
fn $setter_thunk$(raw_msg: $pbi$::RawMessage, val: $pbi$::PtrAndLen);
fn $clearer_thunk$(raw_msg: $pbi$::RawMessage);
)rs");
}

Expand All @@ -182,7 +181,7 @@ void SingularString::InThunkCc(Context& ctx,
{"getter_thunk", Thunk(ctx, field, "get")},
{"setter_thunk", Thunk(ctx, field, "set")},
{"clearer_thunk", Thunk(ctx, field, "clear")},
{"hazzer",
{"thunks_for_with_presence_fields",
[&] {
if (field.has_presence()) {
ctx.Emit(R"cc(
Expand All @@ -194,7 +193,7 @@ void SingularString::InThunkCc(Context& ctx,
}
}}},
R"cc(
$hazzer$;
$thunks_for_with_presence_fields$;
::google::protobuf::rust_internal::PtrAndLen $getter_thunk$($QualifiedMsg$* msg) {
absl::string_view val = msg->$field$();
return ::google::protobuf::rust_internal::PtrAndLen(val.data(), val.size());
Expand Down
2 changes: 2 additions & 0 deletions src/google/protobuf/compiler/rust/generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ void EmitOpeningOfPackageModules(Context& ctx, absl::string_view pkg) {
for (absl::string_view segment : absl::StrSplit(pkg, '.')) {
ctx.Emit({{"segment", segment}},
R"rs(
#[allow(non_snake_case)]
pub mod $segment$ {
)rs");
}
Expand Down Expand Up @@ -157,6 +158,7 @@ void DeclareSubmodulesForNonPrimarySrcs(
{"mod_name", RustInternalModuleName(ctx, *non_primary_src)}},
R"rs(
#[path="$file_path$"]
#[allow(non_snake_case)]
pub mod $mod_name$;
)rs");
}
Expand Down
2 changes: 0 additions & 2 deletions src/google/protobuf/compiler/rust/message.cc
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,6 @@ void GetterForViewOrMut(Context& ctx, const FieldDescriptor& field,
auto fieldType = field.type();
auto getter_thunk = Thunk(ctx, field, "get");
auto setter_thunk = Thunk(ctx, field, "set");
auto clearer_thunk = Thunk(ctx, field, "clear");
// If we're dealing with a Mut, the getter must be supplied
// self.inner.msg() whereas a View has to be supplied self.msg
auto self = is_mut ? "self.inner.msg()" : "self.msg";
Expand Down Expand Up @@ -240,7 +239,6 @@ void GetterForViewOrMut(Context& ctx, const FieldDescriptor& field,
ctx.Emit({{"field", fieldName},
{"getter_thunk", getter_thunk},
{"setter_thunk", setter_thunk},
{"clearer_thunk", clearer_thunk},
{"self", self},
{"RsType", rsType},
{"as_ref", asRef},
Expand Down

0 comments on commit 87f0627

Please sign in to comment.