Skip to content

Commit 9a0edb8

Browse files
author
bors-servo
authored
Auto merge of #1271 - emilio:repr-align, r=xidorn
codegen: Support repr(align) Fixes #917.
2 parents 60c3d33 + 853ae4d commit 9a0edb8

File tree

14 files changed

+130
-27
lines changed

14 files changed

+130
-27
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ name = "bindgen"
1313
readme = "README.md"
1414
repository = "https://github.com/rust-lang-nursery/rust-bindgen"
1515
documentation = "https://docs.rs/bindgen"
16-
version = "0.33.2"
16+
version = "0.34.0"
1717
build = "build.rs"
1818

1919
include = [

src/codegen/impl_partialeq.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ pub fn gen_partialeq_impl(
2020
&self._bindgen_opaque_blob[..] == &other._bindgen_opaque_blob[..]
2121
});
2222
} else if comp_info.kind() == CompKind::Union {
23-
assert!(!ctx.options().rust_features().untagged_union());
23+
assert!(!ctx.options().rust_features().untagged_union);
2424
tokens.push(quote! {
2525
&self.bindgen_union_field[..] == &other.bindgen_union_field[..]
2626
});

src/codegen/mod.rs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1527,13 +1527,16 @@ impl CodeGenerator for CompInfo {
15271527
});
15281528
}
15291529

1530+
let mut explicit_align = None;
15301531
if is_opaque {
15311532
// Opaque item should not have generated methods, fields.
15321533
debug_assert!(fields.is_empty());
15331534
debug_assert!(methods.is_empty());
15341535

15351536
match layout {
15361537
Some(l) => {
1538+
explicit_align = Some(l.align);
1539+
15371540
let ty = helpers::blob(l);
15381541
fields.push(quote! {
15391542
pub _bindgen_opaque_blob: #ty ,
@@ -1555,6 +1558,7 @@ impl CodeGenerator for CompInfo {
15551558
if layout.align == 1 {
15561559
packed = true;
15571560
} else {
1561+
explicit_align = Some(layout.align);
15581562
let ty = helpers::blob(Layout::new(0, layout.align));
15591563
fields.push(quote! {
15601564
pub __bindgen_align: #ty ,
@@ -1637,6 +1641,18 @@ impl CodeGenerator for CompInfo {
16371641
attributes.push(attributes::repr("C"));
16381642
}
16391643

1644+
if ctx.options().rust_features().repr_align {
1645+
if let Some(explicit) = explicit_align {
1646+
// Ensure that the struct has the correct alignment even in
1647+
// presence of alignas.
1648+
let explicit = helpers::ast_ty::int_expr(explicit as i64);
1649+
attributes.push(quote! {
1650+
#[repr(align(#explicit))]
1651+
});
1652+
}
1653+
}
1654+
1655+
16401656
let mut derives = vec![];
16411657
if item.can_derive_debug(ctx) {
16421658
derives.push("Debug");
@@ -1655,7 +1671,7 @@ impl CodeGenerator for CompInfo {
16551671
if item.can_derive_copy(ctx) && !item.annotations().disallow_copy() {
16561672
derives.push("Copy");
16571673

1658-
if ctx.options().rust_features().builtin_clone_impls() ||
1674+
if ctx.options().rust_features().builtin_clone_impls ||
16591675
used_template_params.is_some()
16601676
{
16611677
// FIXME: This requires extra logic if you have a big array in a
@@ -1996,7 +2012,7 @@ impl MethodCodegen for Method {
19962012
_ => panic!("How in the world?"),
19972013
};
19982014

1999-
if let (Abi::ThisCall, false) = (signature.abi(), ctx.options().rust_features().thiscall_abi()) {
2015+
if let (Abi::ThisCall, false) = (signature.abi(), ctx.options().rust_features().thiscall_abi) {
20002016
return;
20012017
}
20022018

@@ -3167,7 +3183,7 @@ impl TryToRustTy for FunctionSig {
31673183
let abi = self.abi();
31683184

31693185
match abi {
3170-
Abi::ThisCall if !ctx.options().rust_features().thiscall_abi() => {
3186+
Abi::ThisCall if !ctx.options().rust_features().thiscall_abi => {
31713187
warn!("Skipping function with thiscall ABI that isn't supported by the configured Rust target");
31723188
Ok(quote::Tokens::new())
31733189
}
@@ -3264,7 +3280,7 @@ impl CodeGenerator for Function {
32643280
}
32653281

32663282
let abi = match signature.abi() {
3267-
Abi::ThisCall if !ctx.options().rust_features().thiscall_abi() => {
3283+
Abi::ThisCall if !ctx.options().rust_features().thiscall_abi => {
32683284
warn!("Skipping function with thiscall ABI that isn't supported by the configured Rust target");
32693285
return;
32703286
}

src/features.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ macro_rules! rust_target_base {
9292
=> Stable_1_19 => 1.19;
9393
/// Rust stable 1.21
9494
=> Stable_1_21 => 1.21;
95+
/// Rust stable 1.25
96+
=> Stable_1_25 => 1.25;
9597
/// Nightly rust
9698
=> Nightly => nightly;
9799
);
@@ -111,7 +113,10 @@ macro_rules! rust_feature_def {
111113
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
112114
pub struct RustFeatures {
113115
$(
114-
$feature: bool,
116+
$(
117+
#[$attr]
118+
)*
119+
pub $feature: bool,
115120
)*
116121
}
117122

@@ -124,15 +129,6 @@ macro_rules! rust_feature_def {
124129
)*
125130
}
126131
}
127-
128-
$(
129-
$(
130-
#[$attr]
131-
)*
132-
pub fn $feature(&self) -> bool {
133-
self.$feature
134-
}
135-
)*
136132
}
137133
}
138134
}
@@ -144,6 +140,8 @@ rust_feature_def!(
144140
=> thiscall_abi;
145141
/// builtin impls for `Clone` ([PR](https://github.com/rust-lang/rust/pull/43690))
146142
=> builtin_clone_impls;
143+
/// repr(align) https://github.com/rust-lang/rust/pull/47006
144+
=> repr_align;
147145
);
148146

149147
impl From<RustTarget> for RustFeatures {
@@ -158,6 +156,10 @@ impl From<RustTarget> for RustFeatures {
158156
features.builtin_clone_impls = true;
159157
}
160158

159+
if rust_target >= RustTarget::Stable_1_25 {
160+
features.repr_align = true;
161+
}
162+
161163
if rust_target >= RustTarget::Nightly {
162164
features.thiscall_abi = true;
163165
}
@@ -189,6 +191,7 @@ mod test {
189191
test_target("1.0", RustTarget::Stable_1_0);
190192
test_target("1.19", RustTarget::Stable_1_19);
191193
test_target("1.21", RustTarget::Stable_1_21);
194+
test_target("1.25", RustTarget::Stable_1_25);
192195
test_target("nightly", RustTarget::Nightly);
193196
}
194197
}

src/ir/analysis/derive_copy.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ impl<'ctx> MonotoneFramework for CannotDeriveCopy<'ctx> {
234234
}
235235

236236
if info.kind() == CompKind::Union {
237-
if !self.ctx.options().rust_features().untagged_union() {
237+
if !self.ctx.options().rust_features().untagged_union {
238238
// NOTE: If there's no template parameters we can derive
239239
// copy unconditionally, since arrays are magical for
240240
// rustc, and __BindgenUnionField always implements

src/ir/analysis/derive_debug.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ impl<'ctx> MonotoneFramework for CannotDeriveDebug<'ctx> {
150150
});
151151
return if layout_can_derive &&
152152
!(ty.is_union() &&
153-
self.ctx.options().rust_features().untagged_union()) {
153+
self.ctx.options().rust_features().untagged_union) {
154154
trace!(" we can trivially derive Debug for the layout");
155155
ConstrainResult::Same
156156
} else {
@@ -235,7 +235,7 @@ impl<'ctx> MonotoneFramework for CannotDeriveDebug<'ctx> {
235235
);
236236

237237
if info.kind() == CompKind::Union {
238-
if self.ctx.options().rust_features().untagged_union() {
238+
if self.ctx.options().rust_features().untagged_union {
239239
trace!(" cannot derive Debug for Rust unions");
240240
return self.insert(id);
241241
}

src/ir/analysis/derive_default.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ impl<'ctx> MonotoneFramework for CannotDeriveDefault<'ctx> {
177177
});
178178
return if layout_can_derive &&
179179
!(ty.is_union() &&
180-
self.ctx.options().rust_features().untagged_union()) {
180+
self.ctx.options().rust_features().untagged_union) {
181181
trace!(" we can trivially derive Default for the layout");
182182
ConstrainResult::Same
183183
} else {
@@ -271,7 +271,7 @@ impl<'ctx> MonotoneFramework for CannotDeriveDefault<'ctx> {
271271
}
272272

273273
if info.kind() == CompKind::Union {
274-
if self.ctx.options().rust_features().untagged_union() {
274+
if self.ctx.options().rust_features().untagged_union {
275275
trace!(" cannot derive Default for Rust unions");
276276
return self.insert(id);
277277
}

src/ir/analysis/derive_hash.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ impl<'ctx> MonotoneFramework for CannotDeriveHash<'ctx> {
137137
});
138138
return if layout_can_derive &&
139139
!(ty.is_union() &&
140-
self.ctx.options().rust_features().untagged_union()) {
140+
self.ctx.options().rust_features().untagged_union) {
141141
trace!(" we can trivially derive Hash for the layout");
142142
ConstrainResult::Same
143143
} else {
@@ -257,7 +257,7 @@ impl<'ctx> MonotoneFramework for CannotDeriveHash<'ctx> {
257257
}
258258

259259
if info.kind() == CompKind::Union {
260-
if self.ctx.options().rust_features().untagged_union() {
260+
if self.ctx.options().rust_features().untagged_union {
261261
trace!(" cannot derive Hash for Rust unions");
262262
return self.insert(id);
263263
}

src/ir/analysis/derive_partialeq_or_partialord.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ impl<'ctx> CannotDerivePartialEqOrPartialOrd<'ctx> {
119119
trace!("ty: {:?}", ty);
120120
if item.is_opaque(self.ctx, &()) {
121121
if ty.is_union()
122-
&& self.ctx.options().rust_features().untagged_union()
122+
&& self.ctx.options().rust_features().untagged_union
123123
{
124124
trace!(
125125
" cannot derive `PartialEq`/`PartialOrd` for Rust unions"
@@ -242,7 +242,7 @@ impl<'ctx> CannotDerivePartialEqOrPartialOrd<'ctx> {
242242
}
243243

244244
if info.kind() == CompKind::Union {
245-
if self.ctx.options().rust_features().untagged_union() {
245+
if self.ctx.options().rust_features().untagged_union {
246246
trace!(
247247
" cannot derive `PartialEq`/`PartialOrd` for Rust unions"
248248
);

0 commit comments

Comments
 (0)