Skip to content

Commit

Permalink
Avoid name conflict of rust Option with message Option
Browse files Browse the repository at this point in the history
Functions like `map_get` return a rust Option, which
might conflict with a `message Option` defined in the
current proto file. Use `::__std::option::Option` to
disambiguate rust Option and message Option.

PiperOrigin-RevId: 680004267
  • Loading branch information
protobuf-github-bot authored and copybara-github committed Sep 28, 2024
1 parent a746c47 commit 3c9f72b
Show file tree
Hide file tree
Showing 7 changed files with 9 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/google/protobuf/compiler/rust/accessors/map.cc
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ void Map::InExternC(Context& ctx, const FieldDescriptor& field) const {
if (ctx.is_upb()) {
ctx.Emit({}, R"rs(
fn $getter_thunk$(raw_msg: $pbr$::RawMessage)
-> Option<$pbr$::RawMap>;
-> $Option$<$pbr$::RawMap>;
fn $getter_mut_thunk$(raw_msg: $pbr$::RawMessage,
arena: $pbr$::RawArena) -> $pbr$::RawMap;
)rs");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ void RepeatedField::InExternC(Context& ctx,
fn $getter_thunk$(
raw_msg: $pbr$::RawMessage,
size: *const usize,
) -> Option<$pbr$::RawRepeatedField>;
) -> $Option$<$pbr$::RawRepeatedField>;
)rs");
} else {
ctx.Emit(R"rs(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ void SingularMessage::InExternC(Context& ctx,
} else {
// upb kernel may return NULL for a submsg, we can detect this
// in terra rust if the option returned is None
ctx.Emit({}, "Option<$pbr$::RawMessage>;");
ctx.Emit({}, "$Option$<$pbr$::RawMessage>;");
}
}},
},
Expand Down
4 changes: 2 additions & 2 deletions src/google/protobuf/compiler/rust/enum.cc
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ void EnumProxiedInMapValue(Context& ctx, const EnumDescriptor& desc) {
unsafe { $pbr$::$map_insert_thunk$(map.as_raw($pbi$::Private), $to_ffi_key_expr$, value.into_proxied($pbi$::Private).0) }
}
fn map_get<'a>(map: $pb$::MapView<'a, $key_t$, Self>, key: $pb$::View<'_, $key_t$>) -> Option<$pb$::View<'a, Self>> {
fn map_get<'a>(map: $pb$::MapView<'a, $key_t$, Self>, key: $pb$::View<'_, $key_t$>) -> $Option$<$pb$::View<'a, Self>> {
let key = $to_ffi_key_expr$;
let mut value = $std$::mem::MaybeUninit::uninit();
let found = unsafe { $pbr$::$map_get_thunk$(map.as_raw($pbi$::Private), key, value.as_mut_ptr()) };
Expand Down Expand Up @@ -127,7 +127,7 @@ void EnumProxiedInMapValue(Context& ctx, const EnumDescriptor& desc) {
}
}
fn map_iter_next<'a>(iter: &mut $pb$::MapIter<'a, $key_t$, Self>) -> Option<($pb$::View<'a, $key_t$>, $pb$::View<'a, Self>)> {
fn map_iter_next<'a>(iter: &mut $pb$::MapIter<'a, $key_t$, Self>) -> $Option$<($pb$::View<'a, $key_t$>, $pb$::View<'a, Self>)> {
// SAFETY:
// - The `MapIter` API forbids the backing map from being mutated for 'a,
// and guarantees that it's the correct key and value types.
Expand Down
1 change: 1 addition & 0 deletions src/google/protobuf/compiler/rust/generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ bool RustGenerator::Generate(const FileDescriptor* file,
{"NonNull", "::__std::ptr::NonNull"},
{"Phantom", "::__std::marker::PhantomData"},
{"Result", "::__std::result::Result"},
{"Option", "::__std::option::Option"},
});

ctx.Emit({{"kernel", KernelRsName(ctx.opts().kernel)}}, R"rs(
Expand Down
4 changes: 2 additions & 2 deletions src/google/protobuf/compiler/rust/message.cc
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,7 @@ void MessageProxiedInMapValue(Context& ctx, const Descriptor& msg) {
}
}
fn map_get<'a>(map: $pb$::MapView<'a, $key_t$, Self>, key: $pb$::View<'_, $key_t$>) -> Option<$pb$::View<'a, Self>> {
fn map_get<'a>(map: $pb$::MapView<'a, $key_t$, Self>, key: $pb$::View<'_, $key_t$>) -> $Option$<$pb$::View<'a, Self>> {
use $pbr$::MapNodeSizeInfoIndexForType;
let key = $key_expr$;
let mut value = $std$::mem::MaybeUninit::uninit();
Expand Down Expand Up @@ -675,7 +675,7 @@ void MessageProxiedInMapValue(Context& ctx, const Descriptor& msg) {
}
}
fn map_iter_next<'a>(iter: &mut $pb$::MapIter<'a, $key_t$, Self>) -> Option<($pb$::View<'a, $key_t$>, $pb$::View<'a, Self>)> {
fn map_iter_next<'a>(iter: &mut $pb$::MapIter<'a, $key_t$, Self>) -> $Option$<($pb$::View<'a, $key_t$>, $pb$::View<'a, Self>)> {
use $pbr$::MapNodeSizeInfoIndexForType;
// SAFETY:
// - The `MapIter` API forbids the backing map from being mutated for 'a,
Expand Down
2 changes: 1 addition & 1 deletion src/google/protobuf/compiler/rust/oneof.cc
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ void GenerateOneofDefinition(Context& ctx, const OneofDescriptor& oneof) {
//~ This try_from is not a TryFrom impl so that it isn't
//~ committed to as part of our public api.
#[allow(dead_code)]
pub(crate) fn try_from(v: u32) -> Option<$case_enum_name$> {
pub(crate) fn try_from(v: u32) -> $Option$<$case_enum_name$> {
match v {
0 => Some($case_enum_name$::not_set),
$try_from_cases$
Expand Down

0 comments on commit 3c9f72b

Please sign in to comment.