Skip to content

Commit 42883ac

Browse files
authored
Merge pull request #652 from dtolnay/lifetimes
Enable lifetimes on opaque C++ types
2 parents 30d4673 + e099157 commit 42883ac

File tree

6 files changed

+25
-23
lines changed

6 files changed

+25
-23
lines changed

syntax/check.rs

+4-10
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,6 @@ fn check_type_ident(cx: &mut Check, name: &NamedType) {
7171
{
7272
let msg = format!("unsupported type: {}", ident);
7373
cx.error(ident, &msg);
74-
return;
75-
}
76-
77-
if !name.generics.lifetimes.is_empty() {
78-
cx.error(name, "type with lifetime parameter is not supported yet");
7974
}
8075
}
8176

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

343338
if let Some(lifetime) = ety.generics.lifetimes.first() {
344-
cx.error(lifetime, "extern type with lifetimes is not supported yet");
339+
if ety.lang == Lang::Rust {
340+
let msg = "extern Rust type with lifetimes is not supported yet";
341+
cx.error(lifetime, msg);
342+
}
345343
}
346344

347345
if !ety.bounds.is_empty() {
@@ -447,10 +445,6 @@ fn check_api_type_alias(cx: &mut Check, alias: &TypeAlias) {
447445
let msg = format!("derive({}) on extern type alias is not supported", derive);
448446
cx.error(derive, msg);
449447
}
450-
451-
if let Some(lifetime) = alias.generics.lifetimes.first() {
452-
cx.error(lifetime, "extern type with lifetimes is not supported yet");
453-
}
454448
}
455449

456450
fn check_api_impl(cx: &mut Check, imp: &Impl) {

tests/ffi/lib.rs

+9
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,15 @@ pub mod ffi {
210210
type Job = crate::module::ffi::Job;
211211
}
212212

213+
unsafe extern "C++" {
214+
type Borrow<'a>;
215+
216+
fn c_return_borrow<'a>(s: &'a CxxString) -> UniquePtr<Borrow<'a>>;
217+
218+
#[rust_name = "c_return_borrow_elided"]
219+
fn c_return_borrow(s: &CxxString) -> UniquePtr<Borrow>;
220+
}
221+
213222
#[repr(u32)]
214223
#[derive(Hash)]
215224
enum COwnedEnum {

tests/ffi/tests.cc

+6
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,12 @@ ::A::B::ABEnum c_return_nested_ns_enum(uint16_t n) {
207207
}
208208
}
209209

210+
Borrow::Borrow(const std::string &s) : s(s) {}
211+
212+
std::unique_ptr<Borrow> c_return_borrow(const std::string &s) {
213+
return std::unique_ptr<Borrow>(new Borrow(s));
214+
}
215+
210216
void c_take_primitive(size_t n) {
211217
if (n == 2020) {
212218
cxx_test_suite_set_correct();

tests/ffi/tests.h

+6
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ enum COwnedEnum {
7777
CVAL2,
7878
};
7979

80+
struct Borrow {
81+
Borrow(const std::string &s);
82+
const std::string &s;
83+
};
84+
8085
size_t c_return_primitive();
8186
Shared c_return_shared();
8287
::A::AShared c_return_ns_shared();
@@ -110,6 +115,7 @@ size_t c_return_sum(size_t n1, size_t n2);
110115
Enum c_return_enum(uint16_t n);
111116
::A::AEnum c_return_ns_enum(uint16_t n);
112117
::A::B::ABEnum c_return_nested_ns_enum(uint16_t n);
118+
std::unique_ptr<Borrow> c_return_borrow(const std::string &s);
113119

114120
void c_take_primitive(size_t n);
115121
void c_take_shared(Shared shared);

tests/ui/extern_type_lifetime.rs

-8
This file was deleted.

tests/ui/extern_type_lifetime.stderr

-5
This file was deleted.

0 commit comments

Comments
 (0)