Skip to content

Commit

Permalink
fix: Enum variant named Error causes ambiguous item (#1098)
Browse files Browse the repository at this point in the history
The generated code uses `Self::Error` to refer to `::prost::UnknownEnumValue`, but the term `Error` is not unique when an enum variant is called `Error`. Use the full type name to resolve the ambiguity.

The compiler reported:
```
error: ambiguous associated item
   --> /home/ircdev/src/prost/target/debug/build/tests-no-std-e27db7197924752f/out/enum_keyword_variant.rs:2:68
    |
2   | #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, ::prost::Enumeration)]
    |                                                                    ^^^^^^^^^^^^^^^^^^^^
    |
    = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
    = note: for more information, see issue #57644 <rust-lang/rust#57644>
note: `Error` could refer to the variant defined here
   --> /home/ircdev/src/prost/target/debug/build/tests-no-std-e27db7197924752f/out/enum_keyword_variant.rs:10:5
    |
10  |     Error = 4,
    |     ^^^^^
note: `Error` could also refer to the associated type defined here
   --> /home/ircdev/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/convert/mod.rs:682:5
    |
682 |     type Error;
    |     ^^^^^^^^^^
    = note: `#[deny(ambiguous_associated_items)]` on by default
    = note: this error originates in the derive macro `::prost::Enumeration` (in Nightly builds, run with -Z macro-backtrace for more info)
```
  • Loading branch information
caspermeijn committed Jul 9, 2024
1 parent 23f7174 commit 26463f4
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 1 deletion.
2 changes: 1 addition & 1 deletion prost-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ fn try_enumeration(input: TokenStream) -> Result<TokenStream, Error> {
impl #impl_generics ::core::convert::TryFrom::<i32> for #ident #ty_generics #where_clause {
type Error = ::prost::UnknownEnumValue;

fn try_from(value: i32) -> ::core::result::Result<#ident, Self::Error> {
fn try_from(value: i32) -> ::core::result::Result<#ident, ::prost::UnknownEnumValue> {
match value {
#(#try_from,)*
_ => ::core::result::Result::Err(::prost::UnknownEnumValue(value)),
Expand Down
1 change: 1 addition & 0 deletions tests/src/enum_keyword_variant.proto
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ enum Feeding {
// Careful: code generation resulted in "Self". Now it is "Self_".
FEEDING_SELF = 2;
FEEDING_ELSE = 3;
FEEDING_ERROR = 4;
}

enum Grooming {
Expand Down

0 comments on commit 26463f4

Please sign in to comment.