Skip to content

Commit 436cfe5

Browse files
committed
Fix type encoding/decoding for unions
Fix union debuginfo test on lldb
1 parent 93067ca commit 436cfe5

File tree

7 files changed

+46
-50
lines changed

7 files changed

+46
-50
lines changed

src/librustc_metadata/decoder.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ fn maybe_item_name(item: rbml::Doc) -> Option<ast::Name> {
291291

292292
fn family_to_variant_kind<'tcx>(family: Family) -> Option<ty::VariantKind> {
293293
match family {
294-
Struct(VariantKind::Struct) | Variant(VariantKind::Struct) =>
294+
Struct(VariantKind::Struct) | Variant(VariantKind::Struct) | Union =>
295295
Some(ty::VariantKind::Struct),
296296
Struct(VariantKind::Tuple) | Variant(VariantKind::Tuple) =>
297297
Some(ty::VariantKind::Tuple),

src/librustc_metadata/tydecode.rs

+8
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,14 @@ impl<'a,'tcx> TyDecoder<'a,'tcx> {
472472
let def = self.tcx.lookup_adt_def(did);
473473
return self.tcx.mk_struct(def, substs);
474474
}
475+
'U' => {
476+
assert_eq!(self.next(), '[');
477+
let did = self.parse_def();
478+
let substs = self.parse_substs();
479+
assert_eq!(self.next(), ']');
480+
let def = self.tcx.lookup_adt_def(did);
481+
return self.tcx.mk_union(def, substs);
482+
}
475483
'k' => {
476484
assert_eq!(self.next(), '[');
477485
let did = self.parse_def();

src/librustc_metadata/tyencode.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ pub fn enc_ty<'a, 'tcx>(w: &mut Cursor<Vec<u8>>, cx: &ctxt<'a, 'tcx>, t: Ty<'tcx
171171
write!(w, "]");
172172
}
173173
ty::TyUnion(def, substs) => {
174-
write!(w, "u[{}|", (cx.ds)(cx.tcx, def.did));
174+
write!(w, "U[{}|", (cx.ds)(cx.tcx, def.did));
175175
enc_substs(w, cx, substs);
176176
write!(w, "]");
177177
}

src/test/debuginfo/union-smoke.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@
2323
// === LLDB TESTS ==================================================================================
2424

2525
// lldb-command:run
26-
// lldb-command:print a
27-
// lldb-check:[...]$0 = {a = {__0 = 2 '\002', __1 = 2 '\002'}, b = 514}
26+
// lldb-command:print u
27+
// lldb-check:[...]$0 = { a = ('\x02', '\x02') b = 514 }
2828
// lldb-command:print union_smoke::SU
29-
// lldb-check:[...]$1 = {a = {__0 = 1 '\001', __1 = 1 '\001'}, b = 257}
29+
// lldb-check:[...]$1 = 257
3030

3131
#![allow(unused)]
3232
#![feature(omit_gdb_pretty_printer_section)]

src/test/run-pass/union/auxiliary/union.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@
1212

1313
pub union U {
1414
pub a: u8,
15-
b: u16,
15+
pub b: u16,
1616
}

src/test/run-pass/union/union-basic.rs

+32-23
Original file line numberDiff line numberDiff line change
@@ -8,47 +8,51 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
// aux-build:union.rs
12+
1113
#![feature(untagged_unions)]
1214

15+
extern crate union;
1316
use std::mem::{size_of, align_of, zeroed};
1417

1518
union U {
1619
a: u8,
20+
b: u16
1721
}
1822

19-
union U64 {
20-
a: u64,
21-
}
23+
fn local() {
24+
assert_eq!(size_of::<U>(), 2);
25+
assert_eq!(align_of::<U>(), 2);
2226

23-
union W {
24-
a: u8,
25-
b: u64,
26-
}
27+
let u = U { a: 10 };
28+
unsafe {
29+
assert_eq!(u.a, 10);
30+
let U { a } = u;
31+
assert_eq!(a, 10);
32+
}
2733

28-
#[repr(C)]
29-
union Y {
30-
f1: u16,
31-
f2: [u8; 4],
34+
let mut w = U { b: 0 };
35+
unsafe {
36+
assert_eq!(w.a, 0);
37+
assert_eq!(w.b, 0);
38+
w.a = 1;
39+
assert_eq!(w.a, 1);
40+
assert_eq!(w.b, 1);
41+
}
3242
}
3343

34-
fn main() {
35-
assert_eq!(size_of::<U>(), 1);
36-
assert_eq!(size_of::<U64>(), 8);
37-
assert_eq!(size_of::<W>(), 8);
38-
assert_eq!(align_of::<U>(), 1);
39-
assert_eq!(align_of::<U64>(), align_of::<u64>());
40-
assert_eq!(align_of::<W>(), align_of::<u64>());
41-
assert_eq!(size_of::<Y>(), 4);
42-
assert_eq!(align_of::<Y>(), 2);
44+
fn xcrate() {
45+
assert_eq!(size_of::<union::U>(), 2);
46+
assert_eq!(align_of::<union::U>(), 2);
4347

44-
let u = U { a: 10 };
48+
let u = union::U { a: 10 };
4549
unsafe {
4650
assert_eq!(u.a, 10);
47-
let U { a } = u;
51+
let union::U { a } = u;
4852
assert_eq!(a, 10);
4953
}
5054

51-
let mut w = W { b: 0 };
55+
let mut w = union::U { b: 0 };
5256
unsafe {
5357
assert_eq!(w.a, 0);
5458
assert_eq!(w.b, 0);
@@ -57,3 +61,8 @@ fn main() {
5761
assert_eq!(w.b, 1);
5862
}
5963
}
64+
65+
fn main() {
66+
local();
67+
xcrate();
68+
}

src/test/run-pass/union/union-xcrate.rs

-21
This file was deleted.

0 commit comments

Comments
 (0)