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

Implement lifetime elision for member function receivers #660

Merged
merged 2 commits into from
Jan 4, 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
50 changes: 39 additions & 11 deletions macro/src/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ use crate::syntax::instantiate::ImplKey;
use crate::syntax::report::Errors;
use crate::syntax::symbol::Symbol;
use crate::syntax::{
self, check, mangle, Api, Doc, Enum, ExternFn, ExternType, Impl, Pair, Signature, Struct,
Trait, Type, TypeAlias, Types,
self, check, mangle, Api, Doc, Enum, ExternFn, ExternType, Impl, Lifetimes, Pair, Signature,
Struct, Trait, Type, TypeAlias, Types,
};
use proc_macro2::{Ident, Span, TokenStream};
use quote::{format_ident, quote, quote_spanned, ToTokens};
use std::mem;
use syn::{parse_quote, Result, Token};
use syn::{parse_quote, punctuated, Lifetime, Result, Token};

pub fn bridge(mut ffi: Module) -> Result<TokenStream> {
let ref mut errors = Errors::new();
Expand Down Expand Up @@ -617,16 +617,44 @@ fn expand_cxx_function_shim(efn: &ExternFn, types: &Types) -> TokenStream {
#trampolines
#dispatch
});
let function_shim = quote! {
#doc
#attrs
#visibility #unsafety #fn_token #ident #generics #arg_list #ret #fn_body
};
match &efn.receiver {
None => function_shim,
None => {
quote! {
#doc
#attrs
#visibility #unsafety #fn_token #ident #generics #arg_list #ret #fn_body
}
}
Some(receiver) => {
let receiver_type = &receiver.ty;
quote!(impl #receiver_type { #function_shim })
let elided_generics;
let receiver_ident = &receiver.ty.rust;
let resolve = types.resolve(&receiver.ty);
let receiver_generics = if receiver.ty.generics.lt_token.is_some() {
&receiver.ty.generics
} else {
elided_generics = Lifetimes {
lt_token: resolve.generics.lt_token,
lifetimes: resolve
.generics
.lifetimes
.pairs()
.map(|pair| {
let lifetime = Lifetime::new("'_", pair.value().apostrophe);
let punct = pair.punct().map(|&&comma| comma);
punctuated::Pair::new(lifetime, punct)
})
.collect(),
gt_token: resolve.generics.gt_token,
};
&elided_generics
};
quote! {
impl #generics #receiver_ident #receiver_generics {
#doc
#attrs
#visibility #unsafety #fn_token #ident #arg_list #ret #fn_body
}
}
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions tests/ffi/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,8 @@ pub mod ffi {

#[rust_name = "c_return_borrow_elided"]
fn c_return_borrow(s: &CxxString) -> UniquePtr<Borrow>;

fn const_member(self: &Borrow);
}

#[repr(u32)]
Expand Down
2 changes: 2 additions & 0 deletions tests/ffi/tests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,8 @@ ::A::B::ABEnum c_return_nested_ns_enum(uint16_t n) {

Borrow::Borrow(const std::string &s) : s(s) {}

void Borrow::const_member() const {}

std::unique_ptr<Borrow> c_return_borrow(const std::string &s) {
return std::unique_ptr<Borrow>(new Borrow(s));
}
Expand Down
1 change: 1 addition & 0 deletions tests/ffi/tests.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ enum COwnedEnum {

struct Borrow {
Borrow(const std::string &s);
void const_member() const;
const std::string &s;
};

Expand Down