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

Reference return type from fallible Rust function does not compile #782

Closed
dtolnay opened this issue Mar 26, 2021 · 0 comments · Fixed by #784
Closed

Reference return type from fallible Rust function does not compile #782

dtolnay opened this issue Mar 26, 2021 · 0 comments · Fixed by #784

Comments

@dtolnay
Copy link
Owner

dtolnay commented Mar 26, 2021

use anyhow::Result;

#[cxx::bridge]
mod ffi {
    extern "Rust" {
        fn test() -> Result<&'static i32>;
    }
}

fn test() -> Result<&'static i32> {
    Ok(&0)
}

The return type is being implemented by a pointer out-parameter, and it's not possible for that pointer to point to a reference in C++.

$ cargo check
target/debug/build/example-283878198746b170/out/cxxbridge/sources/example/src/main.rs.cc:80:60: error: 'return$' declared as a pointer to a reference of type 'const ::std::int32_t &' (aka 'const int &')
::rust::repr::PtrLen cxxbridge1$test(const ::std::int32_t &*return$) noexcept;
                                                           ^
target/debug/build/example-283878198746b170/out/cxxbridge/sources/example/src/main.rs.cc:51:5: error: union member 'value' has reference type 'const int &'
  T value;
    ^
target/debug/build/example-283878198746b170/out/cxxbridge/sources/example/src/main.rs.cc:84:47: note: in instantiation of template class 'rust::cxxbridge1::MaybeUninit<const int &>' requested here
  ::rust::MaybeUninit<const ::std::int32_t &> return$;
                                              ^
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

Successfully merging a pull request may close this issue.

1 participant