Skip to content

Commit d53d154

Browse files
committed
Fix unions
1 parent 31775da commit d53d154

File tree

2 files changed

+88
-3
lines changed

2 files changed

+88
-3
lines changed

src/lvalue.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,6 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
182182
field_ty: Ty<'tcx>,
183183
) -> EvalResult<'tcx, Lvalue<'tcx>> {
184184
let base_layout = self.type_layout(base_ty)?;
185-
186185
use rustc::ty::layout::Layout::*;
187186
let (offset, packed) = match *base_layout {
188187
Univariant { ref variant, .. } => {
@@ -255,8 +254,13 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
255254
}
256255
},
257256
Value::ByVal(_) => {
258-
assert_eq!(field_index, 0, "ByVal can only have 1 non zst field with offset 0");
259-
return Ok(base);
257+
if self.get_field_count(base_ty)? == 1 {
258+
assert_eq!(field_index, 0, "ByVal can only have 1 non zst field with offset 0");
259+
return Ok(base);
260+
}
261+
// this branch is taken when a union creates a large ByVal which is then
262+
// accessed as a struct with multiple small fields
263+
(PrimVal::Ptr(self.force_allocation(base)?.to_ptr()?), LvalueExtra::None)
260264
},
261265
Value::ByValPair(_, _) => {
262266
let field_count = self.get_field_count(base_ty)?;

tests/run-pass/union-overwrite.rs

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// Copyright 2016 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+
#![feature(untagged_unions)]
12+
#![allow(unions_with_drop_fields)]
13+
14+
#[repr(C)]
15+
struct Pair<T, U>(T, U);
16+
#[repr(C)]
17+
struct Triple<T>(T, T, T);
18+
19+
#[repr(C)]
20+
union U<A, B> {
21+
a: Pair<A, A>,
22+
b: B,
23+
}
24+
25+
#[repr(C)]
26+
union W<A, B> {
27+
a: A,
28+
b: B,
29+
}
30+
31+
#[cfg(target_endian = "little")]
32+
unsafe fn check() {
33+
let mut u = U::<u8, u16> { b: 0xDE_DE };
34+
u.a.0 = 0xBE;
35+
assert_eq!(u.b, 0xDE_BE);
36+
37+
let mut u = U::<u16, u32> { b: 0xDEAD_DEAD };
38+
u.a.0 = 0xBEEF;
39+
assert_eq!(u.b, 0xDEAD_BEEF);
40+
41+
let mut u = U::<u32, u64> { b: 0xDEADBEEF_DEADBEEF };
42+
u.a.0 = 0xBAADF00D;
43+
assert_eq!(u.b, 0xDEADBEEF_BAADF00D);
44+
45+
let mut w = W::<Pair<Triple<u8>, u8>, u32> { b: 0xDEAD_DEAD };
46+
w.a.0 = Triple(0, 0, 0);
47+
assert_eq!(w.b, 0xDE00_0000);
48+
49+
let mut w = W::<Pair<u8, Triple<u8>>, u32> { b: 0xDEAD_DEAD };
50+
w.a.1 = Triple(0, 0, 0);
51+
assert_eq!(w.b, 0x0000_00AD);
52+
}
53+
54+
#[cfg(target_endian = "big")]
55+
unsafe fn check() {
56+
let mut u = U::<u8, u16> { b: 0xDE_DE };
57+
u.a.0 = 0xBE;
58+
assert_eq!(u.b, 0xBE_DE);
59+
60+
let mut u = U::<u16, u32> { b: 0xDEAD_DEAD };
61+
u.a.0 = 0xBEEF;
62+
assert_eq!(u.b, 0xBEEF_DEAD);
63+
64+
let mut u = U::<u32, u64> { b: 0xDEADBEEF_DEADBEEF };
65+
u.a.0 = 0xBAADF00D;
66+
assert_eq!(u.b, 0xBAADF00D_DEADBEEF);
67+
68+
let mut w = W::<Pair<Triple<u8>, u8>, u32> { b: 0xDEAD_DEAD };
69+
w.a.0 = Triple(0, 0, 0);
70+
assert_eq!(w.b, 0x0000_00AD);
71+
72+
let mut w = W::<Pair<u8, Triple<u8>>, u32> { b: 0xDEAD_DEAD };
73+
w.a.1 = Triple(0, 0, 0);
74+
assert_eq!(w.b, 0xDE00_0000);
75+
}
76+
77+
fn main() {
78+
unsafe {
79+
check();
80+
}
81+
}

0 commit comments

Comments
 (0)