Skip to content

Commit 7a8c258

Browse files
committed
Support local aliased type name differing from remote name
1 parent de3702b commit 7a8c258

File tree

5 files changed

+23
-11
lines changed

5 files changed

+23
-11
lines changed

gen/src/write.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -936,7 +936,10 @@ fn write_type(out: &mut OutFile, ty: &Type) {
936936

937937
fn write_alias(out: &mut OutFile, alias: &TypeAlias) {
938938
if let Some(namespace) = &alias.namespace {
939-
let path = namespace.path_for_type(&alias.ident);
939+
// Review TODO: Is this unwrap fine? i.e. is it ok to assume that, if
940+
// the TypePath parsed, that it has at least one segment?
941+
let remote_type = &alias.ty.path.segments.last().unwrap().ident;
942+
let path = namespace.path_for_type(remote_type);
940943
writeln!(out, "using {} = {};", alias.ident, path)
941944
}
942945
}

macro/src/expand.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -668,8 +668,11 @@ fn expand_type_alias(alias: &TypeAlias) -> TokenStream {
668668

669669
fn expand_type_alias_verify(namespace: &Namespace, alias: &TypeAlias) -> TokenStream {
670670
let namespace = alias.namespace.as_ref().unwrap_or(namespace);
671+
// Review TODO: Is this unwrap fine? i.e. is it ok to assume that, if the
672+
// TypePath parsed, that it has at least one segment?
673+
let remote_type = &alias.ty.path.segments.last().unwrap().ident;
674+
let type_id = type_id(namespace, remote_type);
671675
let ident = &alias.ident;
672-
let type_id = type_id(namespace, ident);
673676
let begin_span = alias.type_token.span;
674677
let end_span = alias.semi_token.span;
675678
let begin = quote_spanned!(begin_span=> ::cxx::private::verify_extern_type::<);

tests/ffi/alias.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ pub mod ffi {
1111
#[namespace = "tests"]
1212
type C = crate::ffi::C;
1313

14+
#[namespace = "tests"]
15+
type SameC = crate::ffi::C;
16+
1417
fn c_return_unique_ptr() -> UniquePtr<C>;
15-
fn c_take_unique_ptr(c: UniquePtr<C>);
18+
fn c_take_unique_ptr(c: UniquePtr<SameC>);
1619
}
1720
}

tests/ui/wrong_type_id.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1-
#[cxx::bridge(namespace = folly)]
1+
#[cxx::bridge(namespace = correct)]
22
mod here {
33
extern "C" {
44
type StringPiece;
55
}
66
}
77

8+
// Rustfmt mangles the extern type alias.
9+
// https://github.com/rust-lang/rustfmt/issues/4159
10+
#[rustfmt::skip]
811
#[cxx::bridge(namespace = folly)]
912
mod there {
1013
extern "C" {
11-
type ByteRange = crate::here::StringPiece;
14+
type OtherName = crate::here::StringPiece;
1215
}
1316
}
1417

tests/ui/wrong_type_id.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
error[E0271]: type mismatch resolving `<StringPiece as ExternType>::Id == (f, o, l, l, y, (), B, y, t, e, R, a, n, g, e)`
2-
--> $DIR/wrong_type_id.rs:11:9
1+
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)`
2+
--> $DIR/wrong_type_id.rs:11:1
33
|
4-
11 | type ByteRange = crate::here::StringPiece;
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected a tuple with 15 elements, found one with 17 elements
4+
11 | #[cxx::bridge(namespace = folly)]
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected a tuple with 17 elements, found one with 19 elements
66
|
77
::: $WORKSPACE/src/extern_type.rs
88
|
99
| pub fn verify_extern_type<T: ExternType<Id = Id>, Id>() {}
1010
| ------- required by this bound in `verify_extern_type`
1111
|
12-
= note: expected tuple `(f, o, l, l, y, (), B, y, t, e, R, a, n, g, e)`
13-
found tuple `(f, o, l, l, y, (), S, t, r, i, n, g, P, i, e, c, e)`
12+
= note: expected tuple `(f, o, l, l, y, (), S, t, r, i, n, g, P, i, e, c, e)`
13+
found tuple `(c, o, r, r, e, c, t, (), S, t, r, i, n, g, P, i, e, c, e)`

0 commit comments

Comments
 (0)