Skip to content

Commit

Permalink
Support cast expressions in array lengths
Browse files Browse the repository at this point in the history
Happens typically to cast to a usize, let's just make it work.

Closes #15
  • Loading branch information
alexcrichton committed Jan 6, 2017
1 parent b703b23 commit 80c0e5d
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -873,7 +873,6 @@ impl<'a> Generator<'a> {
}

let cfield = self.rust2cfield(ty, &name);
let rust_fieldty = self.ty2name(&field.node.ty, true);

t!(writeln!(self.c, r#"
uint64_t __test_offset_{ty}_{rust_field}(void) {{
Expand Down Expand Up @@ -915,15 +914,15 @@ impl<'a> Generator<'a> {
t!(writeln!(self.rust, r#"
extern {{
fn __test_field_type_{ty}_{field}(a: *mut {ty})
-> *mut {field_ty};
-> *mut u8;
}}
unsafe {{
let foo = 0 as *mut {ty};
same(&(*foo).{field} as *const _ as *mut _,
__test_field_type_{ty}_{field}(foo),
"field type {field} of {ty}");
}}
"#, ty = ty, field = name, field_ty = rust_fieldty));
"#, ty = ty, field = name));
}
t!(writeln!(self.rust, r#"
}}
Expand Down Expand Up @@ -1194,6 +1193,7 @@ impl<'a> Generator<'a> {
ast::ExprPath(_, ref path) => {
path.segments.last().unwrap().identifier.to_string()
}
ast::ExprCast(ref e, _) => self.expr2str(e),
_ => panic!("unknown expr: {:?}", e),
}
}
Expand Down
4 changes: 4 additions & 0 deletions testcrate/src/t1.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

typedef int32_t T1Foo;

#define T1N 5

struct T1Bar {
int32_t a;
uint32_t b;
T1Foo c;
uint8_t d;
int64_t e[T1N];
};

struct T1Baz {
Expand All @@ -22,3 +25,4 @@ void T1e(unsigned, const struct T1Bar*);
void T1f(void);

#define T1C 4

3 changes: 3 additions & 0 deletions testcrate/src/t1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ use libc::*;

pub type T1Foo = i32;

pub const T1N: i32 = 5;

#[repr(C)]
pub struct T1Bar {
pub a: i32,
pub b: u32,
pub c: T1Foo,
pub d: u8,
pub e: [i64; T1N as usize],
}

#[repr(C)]
Expand Down

0 comments on commit 80c0e5d

Please sign in to comment.