Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[beta] trans: pad const structs to aligned size #37344

Merged
merged 1 commit into from
Oct 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions src/librustc_trans/adt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -695,12 +695,9 @@ pub fn trans_const<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, t: Ty<'tcx>, discr: D
let lldiscr = C_integral(Type::from_integer(ccx, d), discr.0 as u64, true);
let mut vals_with_discr = vec![lldiscr];
vals_with_discr.extend_from_slice(vals);
let mut contents = build_const_struct(ccx, &variant.offset_after_field[..],
&vals_with_discr[..], variant.packed);
let needed_padding = l.size(dl).bytes() - variant.min_size().bytes();
if needed_padding > 0 {
contents.push(padding(ccx, needed_padding));
}
let aligned_size = l.size(dl).bytes();
let contents = build_const_struct(ccx, &variant.offset_after_field[..],
&vals_with_discr[..], variant.packed, aligned_size);
C_struct(ccx, &contents[..], false)
}
layout::UntaggedUnion { ref variants, .. }=> {
Expand All @@ -710,8 +707,9 @@ pub fn trans_const<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, t: Ty<'tcx>, discr: D
}
layout::Univariant { ref variant, .. } => {
assert_eq!(discr, Disr(0));
let contents = build_const_struct(ccx,
&variant.offset_after_field[..], vals, variant.packed);
let aligned_size = l.size(dl).bytes();
let contents = build_const_struct(ccx, &variant.offset_after_field[..],
vals, variant.packed, aligned_size);
C_struct(ccx, &contents[..], variant.packed)
}
layout::Vector { .. } => {
Expand All @@ -727,10 +725,11 @@ pub fn trans_const<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, t: Ty<'tcx>, discr: D
}
}
layout::StructWrappedNullablePointer { ref nonnull, nndiscr, .. } => {
let aligned_size = l.size(dl).bytes();
if discr.0 == nndiscr {
C_struct(ccx, &build_const_struct(ccx,
&nonnull.offset_after_field[..],
vals, nonnull.packed),
vals, nonnull.packed, aligned_size),
false)
} else {
let fields = compute_fields(ccx, t, nndiscr as usize, false);
Expand All @@ -742,7 +741,8 @@ pub fn trans_const<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, t: Ty<'tcx>, discr: D
C_struct(ccx, &build_const_struct(ccx,
&nonnull.offset_after_field[..],
&vals[..],
false),
false,
aligned_size),
false)
}
}
Expand All @@ -761,7 +761,8 @@ pub fn trans_const<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, t: Ty<'tcx>, discr: D
fn build_const_struct<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
offset_after_field: &[layout::Size],
vals: &[ValueRef],
packed: bool)
packed: bool,
aligned_size: u64)
-> Vec<ValueRef> {
assert_eq!(vals.len(), offset_after_field.len());

Expand All @@ -787,9 +788,8 @@ fn build_const_struct<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
}
}

let size = offset_after_field.last().unwrap();
if offset < size.bytes() {
cfields.push(padding(ccx, size.bytes() - offset));
if offset < aligned_size {
cfields.push(padding(ccx, aligned_size - offset));
}

cfields
Expand Down
25 changes: 25 additions & 0 deletions src/test/run-pass/issue-37222.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#[derive(Debug, PartialEq)]
enum Bar {
A(i64),
B(i32),
C,
}

#[derive(Debug, PartialEq)]
struct Foo(Bar, u8);

static FOO: [Foo; 2] = [Foo(Bar::C, 0), Foo(Bar::C, 0xFF)];

fn main() {
assert_eq!(&FOO[1], &Foo(Bar::C, 0xFF));
}