Skip to content

Commit 7f6d473

Browse files
committed
Correct initial field alignment for repr(C)/repr(int)
1 parent 0d8321b commit 7f6d473

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

src/librustc/ty/layout.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -940,11 +940,15 @@ impl<'a, 'tcx> LayoutCx<'tcx, TyCtxt<'a, 'tcx, 'tcx>> {
940940
// We increase the size of the discriminant to avoid LLVM copying
941941
// padding when it doesn't need to. This normally causes unaligned
942942
// load/stores and excessive memcpy/memset operations. By using a
943-
// bigger integer size, LLVM can be sure about it's contents and
943+
// bigger integer size, LLVM can be sure about its contents and
944944
// won't be so conservative.
945945

946946
// Use the initial field alignment
947-
let mut ity = Integer::for_abi_align(dl, start_align).unwrap_or(min_ity);
947+
let mut ity = if def.repr.c() || def.repr.int.is_some() {
948+
min_ity
949+
} else {
950+
Integer::for_abi_align(dl, start_align).unwrap_or(min_ity)
951+
};
948952

949953
// If the alignment is not larger than the chosen discriminant size,
950954
// don't use the alignment as the final size.

src/test/run-pass/repr_c_int_align.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// compile-flags: -O
12+
13+
#![allow(dead_code)]
14+
15+
#[repr(C, u8)]
16+
enum ReprCu8 {
17+
A(u16),
18+
B,
19+
}
20+
21+
#[repr(C)]
22+
struct ReprC {
23+
tag: u8,
24+
padding: u8,
25+
payload: u16,
26+
}
27+
28+
fn main() {
29+
let r1 = ReprC { tag: 0, padding: 0, payload: 0 };
30+
let r2 = ReprC { tag: 0, padding: 1, payload: 1 };
31+
32+
let t1: &ReprCu8 = unsafe { std::mem::transmute(&r1) };
33+
let t2: &ReprCu8 = unsafe { std::mem::transmute(&r2) };
34+
35+
match (t1, t2) {
36+
(ReprCu8::A(_), ReprCu8::A(_)) => (),
37+
_ => assert!(false)
38+
};
39+
}

0 commit comments

Comments
 (0)