Skip to content

Commit dfb25b3

Browse files
committed
codegen: Prefer use instead of type aliases.
1 parent aaa7280 commit dfb25b3

File tree

9 files changed

+56
-19
lines changed

9 files changed

+56
-19
lines changed

libbindgen/src/codegen/mod.rs

+32-14
Original file line numberDiff line numberDiff line change
@@ -523,22 +523,40 @@ impl CodeGenerator for Type {
523523
typedef = typedef.attr().doc(comment);
524524
}
525525

526-
let mut generics = typedef.type_(rust_name).generics();
527-
for template_arg in applicable_template_args.iter() {
528-
let template_arg = ctx.resolve_type(*template_arg);
529-
if template_arg.is_named() {
530-
let name = template_arg.name().unwrap();
531-
if name.contains("typename ") {
532-
warn!("Item contained `typename`'d template \
533-
parameter: {:?}", item);
534-
return;
526+
// We prefer using `pub use` over `pub type` because of:
527+
// https://github.com/rust-lang/rust/issues/26264
528+
let simple_enum_path = match inner_rust_type.node {
529+
ast::TyKind::Path(None, ref p) => {
530+
if applicable_template_args.is_empty() &&
531+
!inner_item.expect_type().canonical_type(ctx).is_builtin_or_named() &&
532+
p.segments.iter().all(|p| p.parameters.is_none()) {
533+
Some(p.clone())
534+
} else {
535+
None
535536
}
536-
generics =
537-
generics.ty_param_id(template_arg.name().unwrap());
538-
}
539-
}
537+
},
538+
_ => None,
539+
};
540540

541-
let typedef = generics.build().build_ty(inner_rust_type);
541+
let typedef = if let Some(p) = simple_enum_path {
542+
typedef.use_().build(p).as_(rust_name)
543+
} else {
544+
let mut generics = typedef.type_(rust_name).generics();
545+
for template_arg in applicable_template_args.iter() {
546+
let template_arg = ctx.resolve_type(*template_arg);
547+
if template_arg.is_named() {
548+
let name = template_arg.name().unwrap();
549+
if name.contains("typename ") {
550+
warn!("Item contained `typename`'d template \
551+
parameter: {:?}", item);
552+
return;
553+
}
554+
generics =
555+
generics.ty_param_id(template_arg.name().unwrap());
556+
}
557+
}
558+
generics.build().build_ty(inner_rust_type)
559+
};
542560
result.push(typedef)
543561
}
544562
TypeKind::Enum(ref ei) => {

libbindgen/src/ir/ty.rs

+8
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,14 @@ impl Type {
123123
}
124124
}
125125

126+
/// Is this an enum type?
127+
pub fn is_enum(&self) -> bool {
128+
match self.kind {
129+
TypeKind::Enum(..) => true,
130+
_ => false,
131+
}
132+
}
133+
126134
/// Is this either a builtin or named type?
127135
pub fn is_builtin_or_named(&self) -> bool {
128136
match self.kind {

libbindgen/tests/expectations/tests/anon_enum.rs

+6
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,9 @@ fn bindgen_test_layout_Test() {
2222
impl Clone for Test {
2323
fn clone(&self) -> Self { *self }
2424
}
25+
pub const Foo: _bindgen_ty_1 = _bindgen_ty_1::Foo;
26+
pub const Bar: _bindgen_ty_1 = _bindgen_ty_1::Bar;
27+
#[repr(u32)]
28+
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
29+
pub enum _bindgen_ty_1 { Foo = 0, Bar = 1, }
30+
pub use _bindgen_ty_1 as Baz;

libbindgen/tests/expectations/tests/bitfield_method_mangling.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,4 @@ impl _bindgen_ty_1 {
4646
((val as u32 as u32) << 24u32) & (4278190080usize as u32);
4747
}
4848
}
49-
pub type mach_msg_type_descriptor_t = _bindgen_ty_1;
49+
pub use _bindgen_ty_1 as mach_msg_type_descriptor_t;

libbindgen/tests/expectations/tests/inherit_typedef.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ fn bindgen_test_layout_Foo() {
1717
impl Clone for Foo {
1818
fn clone(&self) -> Self { *self }
1919
}
20-
pub type TypedefedFoo = Foo;
20+
pub use Foo as TypedefedFoo;
2121
#[repr(C)]
2222
#[derive(Debug, Copy)]
2323
pub struct Bar {

libbindgen/tests/expectations/tests/reparented_replacement.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,5 @@ pub mod root {
2525
fn clone(&self) -> Self { *self }
2626
}
2727
}
28-
pub type ReferencesBar = root::foo::Bar;
28+
pub use root::foo::Bar as ReferencesBar;
2929
}

libbindgen/tests/expectations/tests/union_fields.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,4 @@ fn bindgen_test_layout__bindgen_ty_1() {
4444
impl Clone for _bindgen_ty_1 {
4545
fn clone(&self) -> Self { *self }
4646
}
47-
pub type nsStyleUnion = _bindgen_ty_1;
47+
pub use _bindgen_ty_1 as nsStyleUnion;

libbindgen/tests/expectations/tests/unknown_attr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ pub struct _bindgen_ty_1 {
1313
impl Clone for _bindgen_ty_1 {
1414
fn clone(&self) -> Self { *self }
1515
}
16-
pub type max_align_t = _bindgen_ty_1;
16+
pub use _bindgen_ty_1 as max_align_t;

libbindgen/tests/headers/anon_enum.hpp

+5
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,8 @@ struct Test {
33
float bar;
44
enum { T_NONE };
55
};
6+
7+
typedef enum {
8+
Foo,
9+
Bar,
10+
} Baz;

0 commit comments

Comments
 (0)