@@ -7,7 +7,8 @@ use crate::syntax::set::{OrderedSet, UnorderedSet};
7
7
use crate :: syntax:: trivial:: { self , TrivialReason } ;
8
8
use crate :: syntax:: visit:: { self , Visit } ;
9
9
use crate :: syntax:: {
10
- toposort, Api , Atom , Enum , EnumRepr , ExternType , Impl , Lifetimes , Pair , Struct , Type , TypeAlias ,
10
+ toposort, Api , Atom , Enum , EnumRepr , ExternType , Impl , Lang , Lifetimes , Pair , Struct , Type ,
11
+ TypeAlias ,
11
12
} ;
12
13
use proc_macro2:: Ident ;
13
14
use quote:: ToTokens ;
@@ -20,6 +21,8 @@ pub(crate) struct Types<'a> {
20
21
pub rust : UnorderedSet < & ' a Ident > ,
21
22
/// Type aliases defined in the `extern "C++"` section of `#[cxx::bridge]`.
22
23
pub cxx_aliases : UnorderedMap < & ' a Ident , & ' a TypeAlias > ,
24
+ /// Type aliases defined in the `extern "Rust"` section of `#[cxx::bridge]`.
25
+ pub rust_aliases : UnorderedMap < & ' a Ident , & ' a TypeAlias > ,
23
26
pub untrusted : UnorderedMap < & ' a Ident , & ' a ExternType > ,
24
27
pub required_trivial : UnorderedMap < & ' a Ident , Vec < TrivialReason < ' a > > > ,
25
28
pub impls : OrderedMap < ImplKey < ' a > , Option < & ' a Impl > > ,
@@ -36,6 +39,7 @@ impl<'a> Types<'a> {
36
39
let mut cxx = UnorderedSet :: new ( ) ;
37
40
let mut rust = UnorderedSet :: new ( ) ;
38
41
let mut cxx_aliases = UnorderedMap :: new ( ) ;
42
+ let mut rust_aliases = UnorderedMap :: new ( ) ;
39
43
let mut untrusted = UnorderedMap :: new ( ) ;
40
44
let mut impls = OrderedMap :: new ( ) ;
41
45
let mut resolutions = UnorderedMap :: new ( ) ;
@@ -158,8 +162,16 @@ impl<'a> Types<'a> {
158
162
if !type_names. insert ( ident) {
159
163
duplicate_name ( cx, alias, ident) ;
160
164
}
161
- cxx. insert ( ident) ;
162
- cxx_aliases. insert ( ident, alias) ;
165
+ match alias. lang {
166
+ Lang :: Cxx | Lang :: CxxUnwind => {
167
+ cxx. insert ( ident) ;
168
+ cxx_aliases. insert ( ident, alias) ;
169
+ }
170
+ Lang :: Rust => {
171
+ rust. insert ( ident) ;
172
+ rust_aliases. insert ( ident, alias) ;
173
+ }
174
+ }
163
175
add_resolution ( & alias. name , & alias. generics ) ;
164
176
}
165
177
Api :: Impl ( imp) => {
@@ -182,7 +194,9 @@ impl<'a> Types<'a> {
182
194
| ImplKey :: SharedPtr ( ident)
183
195
| ImplKey :: WeakPtr ( ident)
184
196
| ImplKey :: CxxVector ( ident) => {
185
- Atom :: from ( ident. rust ) . is_none ( ) && !cxx_aliases. contains_key ( ident. rust )
197
+ Atom :: from ( ident. rust ) . is_none ( )
198
+ && !cxx_aliases. contains_key ( ident. rust )
199
+ && !rust_aliases. contains_key ( ident. rust )
186
200
}
187
201
} ;
188
202
if implicit_impl && !impls. contains_key ( & impl_key) {
@@ -204,6 +218,7 @@ impl<'a> Types<'a> {
204
218
cxx,
205
219
rust,
206
220
cxx_aliases,
221
+ rust_aliases,
207
222
untrusted,
208
223
required_trivial,
209
224
impls,
@@ -310,4 +325,19 @@ mod test {
310
325
let types = collect_types ( & apis) . unwrap ( ) ;
311
326
assert ! ( types. cxx_aliases. contains_key( & format_ident!( "Alias" ) ) ) ;
312
327
}
328
+
329
+ #[ test]
330
+ fn test_parsing_of_rust_type_aliases ( ) {
331
+ let apis = parse_apis ( quote ! {
332
+ #[ cxx:: bridge]
333
+ mod ffi {
334
+ extern "Rust" {
335
+ type Alias = crate :: another_module:: Foo ;
336
+ }
337
+ }
338
+ } )
339
+ . unwrap ( ) ;
340
+ let types = collect_types ( & apis) . unwrap ( ) ;
341
+ assert ! ( types. rust_aliases. contains_key( & format_ident!( "Alias" ) ) ) ;
342
+ }
313
343
}
0 commit comments