From 390e37b44776bfe8b5323c5d2487c48d83abab0d Mon Sep 17 00:00:00 2001 From: Protobuf Team Bot Date: Sun, 29 Sep 2024 15:49:21 -0700 Subject: [PATCH] Avoid name conflict of rust Option with message Option 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: 680333636 --- src/google/protobuf/compiler/rust/accessors/map.cc | 2 +- src/google/protobuf/compiler/rust/accessors/repeated_field.cc | 2 +- .../protobuf/compiler/rust/accessors/singular_message.cc | 2 +- src/google/protobuf/compiler/rust/enum.cc | 4 ++-- src/google/protobuf/compiler/rust/generator.cc | 1 + src/google/protobuf/compiler/rust/message.cc | 4 ++-- src/google/protobuf/compiler/rust/oneof.cc | 2 +- 7 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/google/protobuf/compiler/rust/accessors/map.cc b/src/google/protobuf/compiler/rust/accessors/map.cc index a485a5dbd1..2952df9ec7 100644 --- a/src/google/protobuf/compiler/rust/accessors/map.cc +++ b/src/google/protobuf/compiler/rust/accessors/map.cc @@ -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"); diff --git a/src/google/protobuf/compiler/rust/accessors/repeated_field.cc b/src/google/protobuf/compiler/rust/accessors/repeated_field.cc index 5477e77025..e47678ad3a 100644 --- a/src/google/protobuf/compiler/rust/accessors/repeated_field.cc +++ b/src/google/protobuf/compiler/rust/accessors/repeated_field.cc @@ -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( diff --git a/src/google/protobuf/compiler/rust/accessors/singular_message.cc b/src/google/protobuf/compiler/rust/accessors/singular_message.cc index 8fc11874f8..b3df063b46 100644 --- a/src/google/protobuf/compiler/rust/accessors/singular_message.cc +++ b/src/google/protobuf/compiler/rust/accessors/singular_message.cc @@ -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>;"); } }}, }, diff --git a/src/google/protobuf/compiler/rust/enum.cc b/src/google/protobuf/compiler/rust/enum.cc index f8b654301f..7e35ff2e05 100644 --- a/src/google/protobuf/compiler/rust/enum.cc +++ b/src/google/protobuf/compiler/rust/enum.cc @@ -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()) }; @@ -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. diff --git a/src/google/protobuf/compiler/rust/generator.cc b/src/google/protobuf/compiler/rust/generator.cc index 58acef2799..ecadf48fb0 100644 --- a/src/google/protobuf/compiler/rust/generator.cc +++ b/src/google/protobuf/compiler/rust/generator.cc @@ -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( diff --git a/src/google/protobuf/compiler/rust/message.cc b/src/google/protobuf/compiler/rust/message.cc index 7f0dec216d..18efb64c04 100644 --- a/src/google/protobuf/compiler/rust/message.cc +++ b/src/google/protobuf/compiler/rust/message.cc @@ -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(); @@ -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, diff --git a/src/google/protobuf/compiler/rust/oneof.cc b/src/google/protobuf/compiler/rust/oneof.cc index 83881b3513..a7e40c219f 100644 --- a/src/google/protobuf/compiler/rust/oneof.cc +++ b/src/google/protobuf/compiler/rust/oneof.cc @@ -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$