From 6079136781a560a4e480002f3fbce37193f60937 Mon Sep 17 00:00:00 2001 From: anstylian <11269403+anstylian@users.noreply.github.com> Date: Fri, 15 Sep 2023 21:48:36 +0300 Subject: [PATCH] Fixes 905, fix: Use full path of Result type + unit test + Option unti test (#917) * add cmake and ninja in the flake * unit-test added to replicate issue #905 * fix: Use full path of `Result` type Fixes #905 * add unit-test for Option to ensure that there is no type resolution collision between Rust Option and a user type defined with name Option, in code generation --------- Co-authored-by: Lucio Franco --- flake.nix | 2 +- prost-derive/src/field/map.rs | 4 ++-- prost-derive/src/field/scalar.rs | 6 +++--- tests/src/build.rs | 16 ++++++++++++++++ tests/src/lib.rs | 18 ++++++++++++++++-- tests/src/mod.rs | 3 +++ tests/src/option_enum.proto | 11 +++++++++++ tests/src/option_struct.proto | 8 ++++++++ tests/src/result_enum.proto | 11 +++++++++++ tests/src/result_struct.proto | 8 ++++++++ 10 files changed, 79 insertions(+), 8 deletions(-) create mode 100644 tests/src/mod.rs create mode 100644 tests/src/option_enum.proto create mode 100644 tests/src/option_struct.proto create mode 100644 tests/src/result_enum.proto create mode 100644 tests/src/result_struct.proto diff --git a/flake.nix b/flake.nix index ac943ea47..a200020e3 100644 --- a/flake.nix +++ b/flake.nix @@ -14,7 +14,7 @@ { devShells.default = pkgs.mkShell { packages = with pkgs; [ cargo rustc ]; - buildInputs = with pkgs; [ pkg-config protobuf curl ]; + buildInputs = with pkgs; [ pkg-config protobuf curl cmake ninja ]; }; }); } diff --git a/prost-derive/src/field/map.rs b/prost-derive/src/field/map.rs index aabceb1f1..4855cc5c6 100644 --- a/prost-derive/src/field/map.rs +++ b/prost-derive/src/field/map.rs @@ -276,14 +276,14 @@ impl Field { #[doc=#get_doc] pub fn #get(&self, key: #key_ref_ty) -> ::core::option::Option<#ty> { self.#ident.get(#take_ref key).cloned().and_then(|x| { - let result: Result<#ty, _> = ::core::convert::TryFrom::try_from(x); + let result: ::core::result::Result<#ty, _> = ::core::convert::TryFrom::try_from(x); result.ok() }) } #[doc=#insert_doc] pub fn #insert(&mut self, key: #key_ty, value: #ty) -> ::core::option::Option<#ty> { self.#ident.insert(key, value as i32).and_then(|x| { - let result: Result<#ty, _> = ::core::convert::TryFrom::try_from(x); + let result: ::core::result::Result<#ty, _> = ::core::convert::TryFrom::try_from(x); result.ok() }) } diff --git a/prost-derive/src/field/scalar.rs b/prost-derive/src/field/scalar.rs index fa426b7fd..5a3dfb2ec 100644 --- a/prost-derive/src/field/scalar.rs +++ b/prost-derive/src/field/scalar.rs @@ -219,7 +219,7 @@ impl Field { struct #wrap_name<'a>(&'a i32); impl<'a> ::core::fmt::Debug for #wrap_name<'a> { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - let res: Result<#ty, _> = ::core::convert::TryFrom::try_from(*self.0); + let res: ::core::result::Result<#ty, _> = ::core::convert::TryFrom::try_from(*self.0); match res { Err(_) => ::core::fmt::Debug::fmt(&self.0, f), Ok(en) => ::core::fmt::Debug::fmt(&en, f), @@ -316,7 +316,7 @@ impl Field { #[doc=#get_doc] pub fn #get(&self) -> #ty { self.#ident.and_then(|x| { - let result: Result<#ty, _> = ::core::convert::TryFrom::try_from(x); + let result: ::core::result::Result<#ty, _> = ::core::convert::TryFrom::try_from(x); result.ok() }).unwrap_or(#default) } @@ -341,7 +341,7 @@ impl Field { fn(i32) -> ::core::option::Option<#ty>, > { self.#ident.iter().cloned().filter_map(|x| { - let result: Result<#ty, _> = ::core::convert::TryFrom::try_from(x); + let result: ::core::result::Result<#ty, _> = ::core::convert::TryFrom::try_from(x); result.ok() }) } diff --git a/tests/src/build.rs b/tests/src/build.rs index 2ddd397e5..bfb086a68 100644 --- a/tests/src/build.rs +++ b/tests/src/build.rs @@ -95,6 +95,22 @@ fn main() { .compile_protos(&[src.join("custom_debug.proto")], includes) .unwrap(); + config + .compile_protos(&[src.join("result_enum.proto")], includes) + .unwrap(); + + config + .compile_protos(&[src.join("result_struct.proto")], includes) + .unwrap(); + + config + .compile_protos(&[src.join("option_enum.proto")], includes) + .unwrap(); + + config + .compile_protos(&[src.join("option_struct.proto")], includes) + .unwrap(); + prost_build::Config::new() .protoc_arg("--experimental_allow_proto3_optional") .compile_protos(&[src.join("proto3_presence.proto")], includes) diff --git a/tests/src/lib.rs b/tests/src/lib.rs index 4fcc45634..c03c94c10 100644 --- a/tests/src/lib.rs +++ b/tests/src/lib.rs @@ -45,8 +45,22 @@ mod no_unused_results; #[cfg(test)] #[cfg(feature = "std")] mod skip_debug; -#[cfg(test)] -mod well_known_types; + +mod test_enum_named_option_value { + include!(concat!(env!("OUT_DIR"), "/myenum.optionn.rs")); +} + +mod test_enum_named_result_value { + include!(concat!(env!("OUT_DIR"), "/myenum.result.rs")); +} + +mod test_result_named_option_value { + include!(concat!(env!("OUT_DIR"), "/mystruct.optionn.rs")); +} + +mod test_result_named_result_value { + include!(concat!(env!("OUT_DIR"), "/mystruct.result.rs")); +} pub mod foo { pub mod bar_baz { diff --git a/tests/src/mod.rs b/tests/src/mod.rs new file mode 100644 index 000000000..ec5996abd --- /dev/null +++ b/tests/src/mod.rs @@ -0,0 +1,3 @@ + +include!(concat!(env!("OUT_DIR"), "/myenum.result.rs")); +include!(concat!(env!("OUT_DIR"), "/mymessage.result.rs")); diff --git a/tests/src/option_enum.proto b/tests/src/option_enum.proto new file mode 100644 index 000000000..5ccd6a2f5 --- /dev/null +++ b/tests/src/option_enum.proto @@ -0,0 +1,11 @@ +syntax = "proto3"; +package myenum.optionn; + + +enum Option { + HELLO = 0; +} + +message FailMessage { + Option result = 1; +} diff --git a/tests/src/option_struct.proto b/tests/src/option_struct.proto new file mode 100644 index 000000000..2eedcf252 --- /dev/null +++ b/tests/src/option_struct.proto @@ -0,0 +1,8 @@ +syntax = "proto3"; +package mystruct.optionn; + + +message Option { + string msg = 1; +} + diff --git a/tests/src/result_enum.proto b/tests/src/result_enum.proto new file mode 100644 index 000000000..3ce9c14cc --- /dev/null +++ b/tests/src/result_enum.proto @@ -0,0 +1,11 @@ +syntax = "proto3"; +package myenum.result; + + +enum Result { + HELLO = 0; +} + +message FailMessage { + Result result = 1; +} diff --git a/tests/src/result_struct.proto b/tests/src/result_struct.proto new file mode 100644 index 000000000..941566434 --- /dev/null +++ b/tests/src/result_struct.proto @@ -0,0 +1,8 @@ +syntax = "proto3"; +package mystruct.result; + + +message Result { + string msg = 1; +} +