Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Type error in Kotlin generated code when using external types in async functions in 0.25.0 #1798

Closed
dani-garcia opened this issue Oct 23, 2023 · 4 comments

Comments

@dani-garcia
Copy link
Contributor

Not sure what information can help with this, or the exact cause of the error, but I have some reproduction project here:
https://github.com/dani-garcia/uniffi_025_repro

The project is structured as such:

  • One inner crate, that exposes a uniffi library using proc-macros and defines a external type conversion, in this case chrono::DateTime
  • One outer crate, that also exposes a uniffi library using proc-macros, imports inner and just reexports it's functions and external types (with ffi_converter_forward).

Note that the only function that these two crates expose is get_time.

With both functions defined as sync, the generated code works fine, but when we change the function in outer to async fn, we get the following error in the generated kotlin code:

Type mismatch: inferred type is com.example.outer.RustBuffer.ByValue but com.example.inner.RustBuffer.ByValue was expected

Code generated for the function without async:

    override fun `getTime`(): DateTime =
        callWithPointer {
            rustCall() { _status ->
                _UniFFILib.INSTANCE.uniffi_outer_fn_method_outerclient_get_time(
                    it,

                    _status
                )
            }
        }.let {
            FfiConverterTypeDateTime.lift(it)
        }

Code generated for the function with async:

    override suspend fun `getTime`() : DateTime {
        return uniffiRustCallAsync(
            callWithPointer { thisPtr ->
                _UniFFILib.INSTANCE.uniffi_outer_fn_method_outerclient_get_time(
                    thisPtr,
                    
                )
            },
            { future, continuation -> _UniFFILib.INSTANCE.ffi_outer_rust_future_poll_rust_buffer(future, continuation) },
            { future, status -> _UniFFILib.INSTANCE.ffi_outer_rust_future_complete_rust_buffer(future, status) },
            { future -> _UniFFILib.INSTANCE.ffi_outer_rust_future_free_rust_buffer(future) },
            // lift function
            { FfiConverterTypeDateTime.lift(it) }, // <------- Here is where it's failing
            // Error FFI converter
            NullCallStatusErrorHandler,
        )
    }

The function in outer just calls the inner function:

use std::sync::Arc;
use chrono::Utc;
uniffi::setup_scaffolding!();

#[derive(uniffi::Object)]
pub struct OuterClient();

#[uniffi::export]
impl OuterClient {
    #[uniffi::constructor]
    pub fn new() -> Arc<Self> {
        Arc::new(Self())
    }
    // If we remove `async` here, it works
    pub async fn get_time(&self) -> chrono::DateTime<Utc> {
        inner::InnerClient::new().get_time()
    }
}

type DateTime = chrono::DateTime<chrono::Utc>;
uniffi::ffi_converter_forward!(DateTime, inner::UniFfiTag, crate::UniFfiTag);

This same code used to work in the past, at least up to commit 53d5ac7, which is the one we were using while the proc-macro changes where unreleased. Now when using 0.25.0 it's showing the error mentioned above.

Let me know if you need any extra details.

@dani-garcia
Copy link
Contributor Author

Doing some bisecting between that working commit and master, it seems to appear after #1767 was merged.

bendk added a commit to bendk/uniffi-rs that referenced this issue Oct 25, 2023
* Kotlin: fixed external types with async functions (mozilla#1798)
* Swift: fixed compile error when multiple crates define async functions
@bendk
Copy link
Contributor

bendk commented Oct 25, 2023

I think the above PR should fix your issues, can you test with that?

@dani-garcia
Copy link
Contributor Author

Yes, that linked PR seems to solve the issue for me, thanks!

bendk added a commit to bendk/uniffi-rs that referenced this issue Oct 26, 2023
* Kotlin: fixed external types with async functions (mozilla#1798)
* Swift: fixed compile error when multiple crates define async functions
bendk added a commit that referenced this issue Oct 26, 2023
* Kotlin: fixed external types with async functions (#1798)
* Swift: fixed compile error when multiple crates define async functions
@bendk
Copy link
Contributor

bendk commented Oct 26, 2023

Great, that code is just merged. We should do a 0.25.1 release soon.

@bendk bendk closed this as completed Oct 27, 2023
bendk added a commit to bendk/uniffi-rs that referenced this issue Oct 27, 2023
* Kotlin: fixed external types with async functions (mozilla#1798)
* Swift: fixed compile error when multiple crates define async functions
bendk added a commit that referenced this issue Oct 27, 2023
* Kotlin: fixed external types with async functions (#1798)
* Swift: fixed compile error when multiple crates define async functions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants