From 7b15ffd0d1774f78244e3a35068f657f09723331 Mon Sep 17 00:00:00 2001 From: Agathangelos Stylianidis Date: Thu, 7 Sep 2023 20:58:37 +0300 Subject: [PATCH 1/4] add cmake and ninja in the flake --- flake.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 ]; }; }); } From 30c800d03f75e376293a49e1271760d8985ff418 Mon Sep 17 00:00:00 2001 From: Agathangelos Stylianidis Date: Thu, 7 Sep 2023 20:59:42 +0300 Subject: [PATCH 2/4] unit-test added to replicate issue #905 --- tests/src/build.rs | 8 ++++++++ tests/src/lib.rs | 10 ++++++++-- tests/src/mod.rs | 3 +++ tests/src/result_enum.proto | 11 +++++++++++ tests/src/result_struct.proto | 8 ++++++++ 5 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 tests/src/mod.rs create mode 100644 tests/src/result_enum.proto create mode 100644 tests/src/result_struct.proto diff --git a/tests/src/build.rs b/tests/src/build.rs index 2ddd397e5..3953f2d2e 100644 --- a/tests/src/build.rs +++ b/tests/src/build.rs @@ -95,6 +95,14 @@ 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(); + 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..e5ad4a014 100644 --- a/tests/src/lib.rs +++ b/tests/src/lib.rs @@ -45,8 +45,14 @@ mod no_unused_results; #[cfg(test)] #[cfg(feature = "std")] mod skip_debug; -#[cfg(test)] -mod well_known_types; + +mod test_enum_named_value { + include!(concat!(env!("OUT_DIR"), "/myenum.result.rs")); +} + +mod test_result_named_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/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; +} + From 16e7e35865a16cfa8dc877c1986d0bffda0944eb Mon Sep 17 00:00:00 2001 From: Agathangelos Stylianidis Date: Mon, 4 Sep 2023 14:09:24 +0300 Subject: [PATCH 3/4] fix: Use full path of `Result` type Fixes #905 --- prost-derive/src/field/map.rs | 4 ++-- prost-derive/src/field/scalar.rs | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) 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() }) } From e2f6b4d611505970d20a22db87c241e349803870 Mon Sep 17 00:00:00 2001 From: Agathangelos Stylianidis Date: Thu, 7 Sep 2023 21:30:04 +0300 Subject: [PATCH 4/4] 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 --- tests/src/build.rs | 8 ++++++++ tests/src/lib.rs | 12 ++++++++++-- tests/src/option_enum.proto | 11 +++++++++++ tests/src/option_struct.proto | 8 ++++++++ 4 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 tests/src/option_enum.proto create mode 100644 tests/src/option_struct.proto diff --git a/tests/src/build.rs b/tests/src/build.rs index 3953f2d2e..bfb086a68 100644 --- a/tests/src/build.rs +++ b/tests/src/build.rs @@ -103,6 +103,14 @@ fn main() { .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 e5ad4a014..c03c94c10 100644 --- a/tests/src/lib.rs +++ b/tests/src/lib.rs @@ -46,11 +46,19 @@ mod no_unused_results; #[cfg(feature = "std")] mod skip_debug; -mod test_enum_named_value { +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_value { +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")); } 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; +} +