Skip to content

Commit f21cea8

Browse files
committed
Don't generate Vec/Box impls for aliases
1 parent bf2c451 commit f21cea8

File tree

7 files changed

+71
-21
lines changed

7 files changed

+71
-21
lines changed

gen/src/write.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1007,12 +1007,14 @@ fn write_generic_instantiations(out: &mut OutFile, types: &Types) {
10071007
for ty in types {
10081008
if let Type::RustBox(ty) = ty {
10091009
if let Type::Ident(inner) = &ty.inner {
1010-
out.next_section();
1011-
write_rust_box_extern(out, inner);
1010+
if Atom::from(inner).is_none() && !types.aliases.contains_key(inner) {
1011+
out.next_section();
1012+
write_rust_box_extern(out, inner);
1013+
}
10121014
}
10131015
} else if let Type::RustVec(ty) = ty {
10141016
if let Type::Ident(inner) = &ty.inner {
1015-
if Atom::from(inner).is_none() {
1017+
if Atom::from(inner).is_none() && !types.aliases.contains_key(inner) {
10161018
out.next_section();
10171019
write_rust_vec_extern(out, inner);
10181020
}
@@ -1040,11 +1042,13 @@ fn write_generic_instantiations(out: &mut OutFile, types: &Types) {
10401042
for ty in types {
10411043
if let Type::RustBox(ty) = ty {
10421044
if let Type::Ident(inner) = &ty.inner {
1043-
write_rust_box_impl(out, inner);
1045+
if Atom::from(inner).is_none() && !types.aliases.contains_key(inner) {
1046+
write_rust_box_impl(out, inner);
1047+
}
10441048
}
10451049
} else if let Type::RustVec(ty) = ty {
10461050
if let Type::Ident(inner) = &ty.inner {
1047-
if Atom::from(inner).is_none() {
1051+
if Atom::from(inner).is_none() && !types.aliases.contains_key(inner) {
10481052
write_rust_vec_impl(out, inner);
10491053
}
10501054
}

macro/src/expand.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,13 @@ fn expand(ffi: Module, apis: &[Api], types: &Types) -> TokenStream {
6565
for ty in types {
6666
if let Type::RustBox(ty) = ty {
6767
if let Type::Ident(ident) = &ty.inner {
68-
if Atom::from(ident).is_none() {
68+
if Atom::from(ident).is_none() && !types.aliases.contains_key(ident) {
6969
hidden.extend(expand_rust_box(namespace, ident));
7070
}
7171
}
7272
} else if let Type::RustVec(ty) = ty {
7373
if let Type::Ident(ident) = &ty.inner {
74-
if Atom::from(ident).is_none() {
74+
if Atom::from(ident).is_none() && !types.aliases.contains_key(ident) {
7575
hidden.extend(expand_rust_vec(namespace, ident));
7676
}
7777
}

tests/ffi/alias.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,29 +13,28 @@ pub mod ffi {
1313

1414
type C = crate::ffi::C;
1515

16-
// TODO: The alias prefix can be removed once these are in their own namespace.
16+
// TODO(https://github.com/dtolnay/cxx/pull/298): The alias prefix can be removed once these
17+
// are in their own namespace.
1718
fn alias_c_return_shared() -> Shared;
19+
fn alias_c_return_unique_ptr() -> UniquePtr<C>;
1820
fn alias_c_return_ref(shared: &Shared) -> &usize;
1921
fn alias_c_return_mut(shared: &mut Shared) -> &mut usize;
2022
fn alias_c_return_enum(n: u16) -> Enum;
23+
fn alias_c_return_unique_ptr_shared() -> UniquePtr<Shared>;
2124
fn alias_c_return_unique_ptr_vector_shared() -> UniquePtr<CxxVector<Shared>>;
2225

2326
fn alias_c_take_shared(shared: Shared);
24-
// TODO: This don't work yet because both bridges try to emit the rust_vec$tests$Shared
25-
// functions. Need the remote bridge to always emit if this is gonna work. Or can we work
26-
// around ODR by making the rust_vec functions inline?
27-
//fn alias_c_take_rust_vec_shared(v: Vec<Shared>);
28-
// TODO: Box<Shared> probably has the same problem, not currently tested
29-
// TODO: Same for UniquePtr<Shared>, not currently tested
30-
// TODO: Below probably doesn't work if the remote bridge doesn't use CxxVector<Shared>
27+
fn alias_c_take_box_shared(shared: Box<Shared>);
28+
fn alias_c_take_unique_ptr(c: UniquePtr<C>);
29+
fn alias_c_take_unique_ptr_shared(s: UniquePtr<Shared>);
3130
fn alias_c_take_unique_ptr_vector_shared(v: UniquePtr<CxxVector<Shared>>);
31+
fn alias_c_take_rust_vec_shared(v: Vec<Shared>);
3232
fn alias_c_take_enum(e: Enum);
33-
34-
fn c_take_unique_ptr(c: UniquePtr<C>);
3533
}
3634

3735
extern "Rust" {
38-
// TODO: The alias prefix can be removed once these are in their own namespace.
36+
// TODO(https://github.com/dtolnay/cxx/pull/298): The alias prefix can be removed once these
37+
// are in their own namespace.
3938
fn alias_r_return_shared() -> Shared;
4039
fn alias_r_return_ref(shared: &Shared) -> &usize;
4140
fn alias_r_return_mut(shared: &mut Shared) -> &mut usize;

tests/ffi/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ pub mod ffi {
3131
fn c_return_shared() -> Shared;
3232
fn c_return_box() -> Box<R>;
3333
fn c_return_unique_ptr() -> UniquePtr<C>;
34+
fn c_return_unique_ptr_shared() -> UniquePtr<Shared>;
3435
fn c_return_ref(shared: &Shared) -> &usize;
3536
fn c_return_mut(shared: &mut Shared) -> &mut usize;
3637
fn c_return_str(shared: &Shared) -> &str;
@@ -55,6 +56,9 @@ pub mod ffi {
5556
fn c_take_primitive(n: usize);
5657
fn c_take_shared(shared: Shared);
5758
fn c_take_box(r: Box<R>);
59+
fn c_take_box_shared(shared: Box<Shared>);
60+
fn c_take_unique_ptr(c: UniquePtr<C>);
61+
fn c_take_unique_ptr_shared(s: UniquePtr<Shared>);
5862
fn c_take_ref_r(r: &R);
5963
fn c_take_ref_c(c: &C);
6064
fn c_take_str(s: &str);

tests/ffi/tests.cc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ rust::Slice<uint8_t> c_return_sliceu8(const Shared &shared) {
6767

6868
rust::String c_return_rust_string() { return "2020"; }
6969

70+
std::unique_ptr<Shared> c_return_unique_ptr_shared() {
71+
return std::unique_ptr<Shared>(new Shared{2020});
72+
}
73+
7074
std::unique_ptr<std::string> c_return_unique_ptr_string() {
7175
return std::unique_ptr<std::string>(new std::string("2020"));
7276
}
@@ -161,12 +165,24 @@ void c_take_box(rust::Box<R> r) {
161165
}
162166
}
163167

168+
void c_take_box_shared(rust::Box<Shared> shared) {
169+
if (shared->z == 2020) {
170+
cxx_test_suite_set_correct();
171+
}
172+
}
173+
164174
void c_take_unique_ptr(std::unique_ptr<C> c) {
165175
if (c->get() == 2020) {
166176
cxx_test_suite_set_correct();
167177
}
168178
}
169179

180+
void c_take_unique_ptr_shared(std::unique_ptr<Shared> shared) {
181+
if (shared->z == 2020) {
182+
cxx_test_suite_set_correct();
183+
}
184+
}
185+
170186
void c_take_ref_r(const R &r) {
171187
if (cxx_test_suite_r_is_correct(&r)) {
172188
cxx_test_suite_set_correct();

tests/ffi/tests.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ size_t &c_return_mut(Shared &shared);
4040
rust::Str c_return_str(const Shared &shared);
4141
rust::Slice<uint8_t> c_return_sliceu8(const Shared &shared);
4242
rust::String c_return_rust_string();
43+
std::unique_ptr<Shared> c_return_unique_ptr_shared();
4344
std::unique_ptr<std::string> c_return_unique_ptr_string();
4445
std::unique_ptr<std::vector<uint8_t>> c_return_unique_ptr_vector_u8();
4546
std::unique_ptr<std::vector<double>> c_return_unique_ptr_vector_f64();
@@ -59,7 +60,9 @@ Enum c_return_enum(uint16_t n);
5960
void c_take_primitive(size_t n);
6061
void c_take_shared(Shared shared);
6162
void c_take_box(rust::Box<R> r);
63+
void c_take_box_shared(rust::Box<Shared> shared);
6264
void c_take_unique_ptr(std::unique_ptr<C> c);
65+
void c_take_unique_ptr_shared(std::unique_ptr<Shared> shared);
6366
void c_take_ref_r(const R &r);
6467
void c_take_ref_c(const C &c);
6568
void c_take_str(rust::Str s);
@@ -102,14 +105,21 @@ rust::Vec<rust::String> c_try_return_rust_vec_string();
102105
const rust::Vec<uint8_t> &c_try_return_ref_rust_vec(const C &c);
103106

104107
const auto alias_c_return_shared = c_return_shared;
108+
const auto alias_c_return_unique_ptr = c_return_unique_ptr;
105109
const auto alias_c_return_ref = c_return_ref;
106110
const auto alias_c_return_mut = c_return_mut;
107111
const auto alias_c_return_enum = c_return_enum;
112+
const auto alias_c_return_unique_ptr_shared = c_return_unique_ptr_shared;
108113
const auto alias_c_return_unique_ptr_vector_shared =
109114
c_return_unique_ptr_vector_shared;
115+
116+
const auto alias_c_take_shared = c_take_shared;
117+
const auto alias_c_take_box_shared = c_take_box_shared;
118+
const auto alias_c_take_unique_ptr = c_take_unique_ptr;
119+
const auto alias_c_take_unique_ptr_shared = c_take_unique_ptr_shared;
110120
const auto alias_c_take_unique_ptr_vector_shared =
111121
c_take_unique_ptr_vector_shared;
112-
const auto alias_c_take_shared = c_take_shared;
122+
const auto alias_c_take_rust_vec_shared = c_take_rust_vec_shared;
113123
const auto alias_c_take_enum = c_take_enum;
114124

115125
} // namespace tests

tests/test.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,12 @@ fn test_c_take() {
116116
check!(ffi::c_take_primitive(2020));
117117
check!(ffi::c_take_shared(ffi::Shared { z: 2020 }));
118118
check!(ffi::c_take_box(Box::new(2020)));
119+
check!(ffi::c_take_box_shared(Box::new(ffi::Shared { z: 2020 })));
119120
check!(ffi::c_take_ref_c(&unique_ptr));
120-
check!(alias::ffi::c_take_unique_ptr(unique_ptr));
121+
check!(ffi::c_take_unique_ptr(unique_ptr));
122+
check!(ffi::c_take_unique_ptr_shared(
123+
ffi::c_return_unique_ptr_shared()
124+
));
121125
check!(ffi::c_take_str("2020"));
122126
check!(ffi::c_take_sliceu8(b"2020"));
123127
check!(ffi::c_take_rust_string("2020".to_owned()));
@@ -151,11 +155,24 @@ fn test_c_take() {
151155

152156
#[test]
153157
fn test_alias_c_take() {
158+
let unique_ptr = alias::ffi::alias_c_return_unique_ptr();
159+
154160
check!(alias::ffi::alias_c_take_shared(ffi::Shared { z: 2020 }));
155-
check!(alias::ffi::alias_c_take_enum(ffi::Enum::AVal));
161+
check!(alias::ffi::alias_c_take_box_shared(Box::new(ffi::Shared {
162+
z: 2020
163+
})));
164+
check!(alias::ffi::alias_c_take_unique_ptr(unique_ptr));
165+
check!(alias::ffi::alias_c_take_unique_ptr_shared(
166+
alias::ffi::alias_c_return_unique_ptr_shared()
167+
));
156168
check!(alias::ffi::alias_c_take_unique_ptr_vector_shared(
157169
alias::ffi::alias_c_return_unique_ptr_vector_shared()
158170
));
171+
let shared_test_vec = vec![ffi::Shared { z: 1010 }, ffi::Shared { z: 1011 }];
172+
check!(alias::ffi::alias_c_take_rust_vec_shared(
173+
shared_test_vec.clone()
174+
));
175+
check!(alias::ffi::alias_c_take_enum(ffi::Enum::AVal));
159176
}
160177

161178
/*

0 commit comments

Comments
 (0)