From a51f76a561f1687e7d5fe7d7cba5169b44ae6613 Mon Sep 17 00:00:00 2001 From: Tim Neumann Date: Sat, 22 Oct 2016 12:57:24 +0200 Subject: [PATCH] [beta] trans: pad const structs to aligned size --- src/librustc_trans/adt.rs | 28 ++++++++++++++-------------- src/test/run-pass/issue-37222.rs | 25 +++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 14 deletions(-) create mode 100644 src/test/run-pass/issue-37222.rs diff --git a/src/librustc_trans/adt.rs b/src/librustc_trans/adt.rs index 8c6074fdaf9aa..19337ba0218c4 100644 --- a/src/librustc_trans/adt.rs +++ b/src/librustc_trans/adt.rs @@ -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, .. }=> { @@ -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 { .. } => { @@ -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); @@ -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) } } @@ -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 { assert_eq!(vals.len(), offset_after_field.len()); @@ -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 diff --git a/src/test/run-pass/issue-37222.rs b/src/test/run-pass/issue-37222.rs new file mode 100644 index 0000000000000..381a5799cc555 --- /dev/null +++ b/src/test/run-pass/issue-37222.rs @@ -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 or the MIT license +// , 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)); +}