Skip to content

Enable lifetimes on opaque C++ types #652

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 2 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
14 changes: 4 additions & 10 deletions syntax/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,6 @@ fn check_type_ident(cx: &mut Check, name: &NamedType) {
{
let msg = format!("unsupported type: {}", ident);
cx.error(ident, &msg);
return;
}

if !name.generics.lifetimes.is_empty() {
cx.error(name, "type with lifetime parameter is not supported yet");
}
}

Expand Down Expand Up @@ -341,7 +336,10 @@ fn check_api_type(cx: &mut Check, ety: &ExternType) {
}

if let Some(lifetime) = ety.generics.lifetimes.first() {
cx.error(lifetime, "extern type with lifetimes is not supported yet");
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() {
Expand Down Expand Up @@ -447,10 +445,6 @@ fn check_api_type_alias(cx: &mut Check, alias: &TypeAlias) {
let msg = format!("derive({}) on extern type alias is not supported", derive);
cx.error(derive, msg);
}

if let Some(lifetime) = alias.generics.lifetimes.first() {
cx.error(lifetime, "extern type with lifetimes is not supported yet");
}
}

fn check_api_impl(cx: &mut Check, imp: &Impl) {
Expand Down
9 changes: 9 additions & 0 deletions tests/ffi/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,15 @@ pub mod ffi {
type Job = crate::module::ffi::Job;
}

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

fn c_return_borrow<'a>(s: &'a CxxString) -> UniquePtr<Borrow<'a>>;

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

#[repr(u32)]
#[derive(Hash)]
enum COwnedEnum {
Expand Down
6 changes: 6 additions & 0 deletions tests/ffi/tests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,12 @@ ::A::B::ABEnum c_return_nested_ns_enum(uint16_t n) {
}
}

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

std::unique_ptr<Borrow> c_return_borrow(const std::string &s) {
return std::unique_ptr<Borrow>(new Borrow(s));
}

void c_take_primitive(size_t n) {
if (n == 2020) {
cxx_test_suite_set_correct();
Expand Down
6 changes: 6 additions & 0 deletions tests/ffi/tests.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ enum COwnedEnum {
CVAL2,
};

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

size_t c_return_primitive();
Shared c_return_shared();
::A::AShared c_return_ns_shared();
Expand Down Expand Up @@ -110,6 +115,7 @@ size_t c_return_sum(size_t n1, size_t n2);
Enum c_return_enum(uint16_t n);
::A::AEnum c_return_ns_enum(uint16_t n);
::A::B::ABEnum c_return_nested_ns_enum(uint16_t n);
std::unique_ptr<Borrow> c_return_borrow(const std::string &s);

void c_take_primitive(size_t n);
void c_take_shared(Shared shared);
Expand Down
8 changes: 0 additions & 8 deletions tests/ui/extern_type_lifetime.rs

This file was deleted.

5 changes: 0 additions & 5 deletions tests/ui/extern_type_lifetime.stderr

This file was deleted.