diff options
author | bors-servo <lbergstrom+bors@mozilla.com> | 2017-01-22 16:30:02 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-01-22 16:30:02 -0800 |
commit | 7373a4258f652c23e5fe00ad14740393caa40082 (patch) | |
tree | ec517f994c43a1e938821a47b6fd6711f8064465 /libbindgen/src/codegen/helpers.rs | |
parent | 0a770cc487422b4e337e106099a3eb8ca1f74d5d (diff) | |
parent | 30a4722622883459c87d5de7a5ffae37849805d1 (diff) |
Auto merge of #413 - emilio:debug-array-unaligned, r=fitzgen
codegen: Avoid generating invalid Rust code when a struct is not properly aligned.
r? @fitzgen
cc #412
Diffstat (limited to 'libbindgen/src/codegen/helpers.rs')
-rw-r--r-- | libbindgen/src/codegen/helpers.rs | 24 |
1 files changed, 13 insertions, 11 deletions
diff --git a/libbindgen/src/codegen/helpers.rs b/libbindgen/src/codegen/helpers.rs index b4cc75f5..06dadab0 100644 --- a/libbindgen/src/codegen/helpers.rs +++ b/libbindgen/src/codegen/helpers.rs @@ -49,20 +49,22 @@ impl BlobTyBuilder { } pub fn build(self) -> P<ast::Ty> { - use std::cmp; + let opaque = self.layout.opaque(); - let ty_name = match self.layout.align { - 8 => "u64", - 4 => "u32", - 2 => "u16", - 1 | _ => "u8", - }; - let data_len = if ty_name == "u8" { - self.layout.size - } else { - self.layout.size / cmp::max(self.layout.align, 1) + // FIXME(emilio, #412): We fall back to byte alignment, but there are + // some things that legitimately are more than 8-byte aligned. + // + // Eventually we should be able to `unwrap` here, but... + let ty_name = match opaque.known_rust_type_for_array() { + Some(ty) => ty, + None => { + warn!("Found unknown alignment on code generation!"); + "u8" + } }; + let data_len = opaque.array_size().unwrap_or(self.layout.size); + let inner_ty = aster::AstBuilder::new().ty().path().id(ty_name).build(); if data_len == 1 { inner_ty |