Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Using errors as function/callback method parameters #1873

Open
arg0d opened this issue Nov 29, 2023 · 5 comments
Open

Using errors as function/callback method parameters #1873

arg0d opened this issue Nov 29, 2023 · 5 comments

Comments

@arg0d
Copy link
Contributor

arg0d commented Nov 29, 2023

I'm not entirely sure whether this is meant to be supported or not. If this is indeed a valid use case, then a similliar issue in uniffi-bindgen-go will also have to be fixed.

Using an error as function parameter causes compilation errors.

error[E0277]: the trait bound `Enumeration: Lift<UniFfiTag>` is not satisfied
   --> /mounted_workdir/target/debug/build/uniffi-example-rondpoint-7ba6758a67cd664b/out/rondpoint.uniffi.rs:181:1
    |
181 | #[::uniffi::export_for_udl]
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Lift<UniFfiTag>` is not implemented for `Enumeration`
    |
    = help: the following other types implement trait `Lift<UT>`:
              <bool as Lift<UT>>
              <i8 as Lift<UT>>
              <i16 as Lift<UT>>
              <i32 as Lift<UT>>
              <i64 as Lift<UT>>
              <u8 as Lift<UT>>
              <u16 as Lift<UT>>
              <u32 as Lift<UT>>
            and 18 others
    = note: this error originates in the attribute macro `::uniffi::export_for_udl` (in Nightly builds, run with -Z macro-backtrace for more info)

Sample:

namespace rondpoint {
  Enumeration copie_enumeration(Enumeration e);
}

[Error]
enum Enumeration {
    "Un",
    "Deux",
    "Trois",
};

Repro:

iff --git a/examples/rondpoint/src/rondpoint.udl b/examples/rondpoint/src/rondpoint.udl
index 7c8261d74..40e27aee3 100644
--- a/examples/rondpoint/src/rondpoint.udl
+++ b/examples/rondpoint/src/rondpoint.udl
@@ -14,6 +14,7 @@ enum minusculeMAJUSCULEEnum {
   "minusculeMAJUSCULEVariant",
 };
 
+[Error]
 enum Enumeration {
     "Un",
     "Deux",
@mhammond
Copy link
Member

welp, I was hoping this was fixed on main, but there I see something slightly different.

error[E0277]: the trait bound `Enumeration: LowerReturn<UniFfiTag>` is not satisfied
   --> /Users/skip/src/moz/uniffi-rs/target/debug/build/uniffi-example-rondpoint-3da52427f3b90e4f/out/rondpoint.uniffi.rs:181:1
    |
181 | #[::uniffi::export_for_udl]
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `LowerReturn<UniFfiTag>` is not implemented for `Enumeration`
    |

@bendk any ideas?

@arg0d
Copy link
Contributor Author

arg0d commented Nov 29, 2023

Oh, I see. I was testing with v0.25.0. It seems to be working on main.

@arg0d
Copy link
Contributor Author

arg0d commented Nov 29, 2023

No, its not working on main afterall :D I reset my changes after switching branches.

@bendk
Copy link
Contributor

bendk commented Nov 29, 2023

We implement Lift, Lower, and ConvertError for error types:

let lower_impl_spec = tagged_impl_header("Lower", ident, udl_mode);
let lift_impl_spec = tagged_impl_header("Lift", ident, udl_mode);
let derive_ffi_traits = derive_ffi_traits(ident, udl_mode, &["ConvertError"]);

I think this means that using Enumeration as an argument should work, but returning it doesn't. It should be easy to extend this by adding LiftReturn and/or LowerReturn to the derive_ffi_traits list. That seems like a good idea to me, although I haven't really thought though all the consequences of returning an exception type.

@arg0d
Copy link
Contributor Author

arg0d commented Nov 29, 2023

Yes you are right, if Rust enum derives thiserror:Error, then using it as parameter is fine. Returning error as a value is what actually causes these errors.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants