Skip to content

Support type aliases across bridges using different C++ namespaces #298

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

Closed
wants to merge 11 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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: 4 additions & 1 deletion gen/src/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -936,7 +936,10 @@ fn write_type(out: &mut OutFile, ty: &Type) {

fn write_alias(out: &mut OutFile, alias: &TypeAlias) {
if let Some(namespace) = &alias.namespace {
let path = namespace.path_for_type(&alias.ident);
// Review TODO: Is this unwrap fine? i.e. is it ok to assume that, if
// the TypePath parsed, that it has at least one segment?
let remote_type = &alias.ty.path.segments.last().unwrap().ident;
let path = namespace.path_for_type(remote_type);
writeln!(out, "using {} = {};", alias.ident, path)
}
}
Expand Down
5 changes: 4 additions & 1 deletion macro/src/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -668,8 +668,11 @@ fn expand_type_alias(alias: &TypeAlias) -> TokenStream {

fn expand_type_alias_verify(namespace: &Namespace, alias: &TypeAlias) -> TokenStream {
let namespace = alias.namespace.as_ref().unwrap_or(namespace);
// Review TODO: Is this unwrap fine? i.e. is it ok to assume that, if the
// TypePath parsed, that it has at least one segment?
let remote_type = &alias.ty.path.segments.last().unwrap().ident;
let type_id = type_id(namespace, remote_type);
let ident = &alias.ident;
let type_id = type_id(namespace, ident);
let begin_span = alias.type_token.span;
let end_span = alias.semi_token.span;
let begin = quote_spanned!(begin_span=> ::cxx::private::verify_extern_type::<);
Expand Down
5 changes: 4 additions & 1 deletion tests/ffi/alias.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ pub mod ffi {
#[namespace = "tests"]
type C = crate::ffi::C;

#[namespace = "tests"]
type SameC = crate::ffi::C;

fn c_return_unique_ptr() -> UniquePtr<C>;
fn c_take_unique_ptr(c: UniquePtr<C>);
fn c_take_unique_ptr(c: UniquePtr<SameC>);
}
}
7 changes: 5 additions & 2 deletions tests/ui/wrong_type_id.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
#[cxx::bridge(namespace = folly)]
#[cxx::bridge(namespace = correct)]
mod here {
extern "C" {
type StringPiece;
}
}

// Rustfmt mangles the extern type alias.
// https://github.com/rust-lang/rustfmt/issues/4159
#[rustfmt::skip]
#[cxx::bridge(namespace = folly)]
mod there {
extern "C" {
type ByteRange = crate::here::StringPiece;
type OtherName = crate::here::StringPiece;
}
}

Expand Down
12 changes: 6 additions & 6 deletions tests/ui/wrong_type_id.stderr
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
error[E0271]: type mismatch resolving `<StringPiece as ExternType>::Id == (f, o, l, l, y, (), B, y, t, e, R, a, n, g, e)`
--> $DIR/wrong_type_id.rs:11:9
error[E0271]: type mismatch resolving `<StringPiece as ExternType>::Id == (f, o, l, l, y, (), S, t, r, i, n, g, P, i, e, c, e)`
--> $DIR/wrong_type_id.rs:11:1
|
11 | type ByteRange = crate::here::StringPiece;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected a tuple with 15 elements, found one with 17 elements
11 | #[cxx::bridge(namespace = folly)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected a tuple with 17 elements, found one with 19 elements
|
::: $WORKSPACE/src/extern_type.rs
|
| pub fn verify_extern_type<T: ExternType<Id = Id>, Id>() {}
| ------- required by this bound in `verify_extern_type`
|
= note: expected tuple `(f, o, l, l, y, (), B, y, t, e, R, a, n, g, e)`
found tuple `(f, o, l, l, y, (), S, t, r, i, n, g, P, i, e, c, e)`
= note: expected tuple `(f, o, l, l, y, (), S, t, r, i, n, g, P, i, e, c, e)`
found tuple `(c, o, r, r, e, c, t, (), S, t, r, i, n, g, P, i, e, c, e)`