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

Allow mutable return reference on signatures with any lifetime in argument #765

Closed
dtolnay opened this issue Mar 25, 2021 · 0 comments · Fixed by #779
Closed

Allow mutable return reference on signatures with any lifetime in argument #765

dtolnay opened this issue Mar 25, 2021 · 0 comments · Fixed by #779

Comments

@dtolnay
Copy link
Owner

dtolnay commented Mar 25, 2021

As of #608 now that extern types defined in the bridge can be generic over lifetimes, it no longer makes sense to expect specifically a reference in one of the function arguments of a function that returns a mutable reference. The following two signatures would be perfectly valid:

#[cxx::bridge]
mod ffi {
    extern "Rust" {
        type Thing<'a>;
    }

    unsafe extern "C++" {
        fn f<'a>(t: Box<Thing<'a>>) -> &'a mut i32;
        fn g(t: Box<Thing>) -> &mut i32;  // same thing, with lifetime elision
    }
}

pub struct Thing<'a>(&'a mut i32);

Current behavior:

error[cxxbridge]: &mut return type is not allowed unless there is a &mut argument
  ┌─ src/main.rs:8:9

8 │         fn f<'a>(t: Box<Thing<'a>>) -> &'a mut i32;
  │         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ &mut return type is not allowed unless there is a &mut argument

error[cxxbridge]: &mut return type is not allowed unless there is a &mut argument
  ┌─ src/main.rs:9:9

9 │         fn g(t: Box<Thing>) -> &mut i32;
  │         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ &mut return type is not allowed unless there is a &mut argument
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