On Thu, Jun 5, 2025 at 2:11 PM Paolo Bonzini <pbonz...@redhat.com> wrote:

> If the enum includes values such as "Ok", "Err", or "Error", the TryInto
> macro can cause errors.  Be careful and qualify identifiers with the full
> path, or in the case of TryFrom<>::Error do not use the associated type
> at all.
>
> Signed-off-by: Paolo Bonzini <pbonz...@redhat.com>
>

Reviewed-by: Marc-André Lureau <marcandre.lur...@redhat.com>


> ---
>  rust/qemu-api-macros/src/lib.rs | 7 ++++---
>  1 file changed, 4 insertions(+), 3 deletions(-)
>
> diff --git a/rust/qemu-api-macros/src/lib.rs b/rust/qemu-api-macros/src/
> lib.rs
> index 103470785e3..c18bb4e036f 100644
> --- a/rust/qemu-api-macros/src/lib.rs
> +++ b/rust/qemu-api-macros/src/lib.rs
> @@ -203,8 +203,8 @@ fn derive_tryinto_body(
>      Ok(quote! {
>          #(const #discriminants: #repr = #name::#discriminants as #repr;)*;
>          match value {
> -            #(#discriminants => Ok(#name::#discriminants),)*
> -            _ => Err(value),
> +            #(#discriminants =>
> core::result::Result::Ok(#name::#discriminants),)*
> +            _ => core::result::Result::Err(value),
>          }
>      })
>  }
> @@ -236,7 +236,8 @@ pub const fn from_bits(value: #repr) -> Self {
>          impl core::convert::TryFrom<#repr> for #name {
>              type Error = #repr;
>
> -            fn try_from(value: #repr) -> Result<Self, Self::Error> {
> +            #[allow(ambiguous_associated_items)]
> +            fn try_from(value: #repr) -> Result<Self, #repr> {
>                  #body
>              }
>          }
> --
> 2.49.0
>
>

Reply via email to