Skip to content

Enable lifetimes on opaque Rust types #653

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

Merged
merged 3 commits into from
Jan 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions macro/src/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -677,19 +677,20 @@ fn expand_rust_type_import(ety: &ExternType) -> TokenStream {

fn expand_rust_type_impl(ety: &ExternType) -> TokenStream {
let ident = &ety.name.rust;
let generics = &ety.generics;
let span = ident.span();
let unsafe_impl = quote_spanned!(ety.type_token.span=> unsafe impl);

let mut impls = quote_spanned! {span=>
#unsafe_impl ::cxx::private::RustType for #ident {}
#unsafe_impl #generics ::cxx::private::RustType for #ident #generics {}
};

for derive in &ety.derives {
if derive.what == Trait::ExternType {
let type_id = type_id(&ety.name);
let span = derive.span;
impls.extend(quote_spanned! {span=>
unsafe impl ::cxx::ExternType for #ident {
unsafe impl #generics ::cxx::ExternType for #ident #generics {
type Id = #type_id;
type Kind = ::cxx::kind::Opaque;
}
Expand Down
7 changes: 0 additions & 7 deletions syntax/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,13 +335,6 @@ fn check_api_type(cx: &mut Check, ety: &ExternType) {
cx.error(derive, msg);
}

if let Some(lifetime) = ety.generics.lifetimes.first() {
if ety.lang == Lang::Rust {
let msg = "extern Rust type with lifetimes is not supported yet";
cx.error(lifetime, msg);
}
}

if !ety.bounds.is_empty() {
let bounds = &ety.bounds;
let span = quote!(#(#bounds)*);
Expand Down
7 changes: 7 additions & 0 deletions tests/ffi/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,11 @@ pub mod ffi {
type Job = crate::module::ffi::Job;
}

extern "Rust" {
#[derive(ExternType)]
type Reference<'a>;
}

unsafe extern "C++" {
type Borrow<'a>;

Expand Down Expand Up @@ -377,6 +382,8 @@ impl R {
}
}

pub struct Reference<'a>(&'a String);

impl ffi::Shared {
fn r_method_on_shared(&self) -> String {
"2020".to_owned()
Expand Down