Skip to content

Commit

Permalink
Fix type encoding/decoding for unions
Browse files Browse the repository at this point in the history
Fix union debuginfo test on lldb
  • Loading branch information
petrochenkov committed Sep 3, 2016
1 parent 93067ca commit 436cfe5
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 50 deletions.
2 changes: 1 addition & 1 deletion src/librustc_metadata/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ fn maybe_item_name(item: rbml::Doc) -> Option<ast::Name> {

fn family_to_variant_kind<'tcx>(family: Family) -> Option<ty::VariantKind> {
match family {
Struct(VariantKind::Struct) | Variant(VariantKind::Struct) =>
Struct(VariantKind::Struct) | Variant(VariantKind::Struct) | Union =>
Some(ty::VariantKind::Struct),
Struct(VariantKind::Tuple) | Variant(VariantKind::Tuple) =>
Some(ty::VariantKind::Tuple),
Expand Down
8 changes: 8 additions & 0 deletions src/librustc_metadata/tydecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,14 @@ impl<'a,'tcx> TyDecoder<'a,'tcx> {
let def = self.tcx.lookup_adt_def(did);
return self.tcx.mk_struct(def, substs);
}
'U' => {
assert_eq!(self.next(), '[');
let did = self.parse_def();
let substs = self.parse_substs();
assert_eq!(self.next(), ']');
let def = self.tcx.lookup_adt_def(did);
return self.tcx.mk_union(def, substs);
}
'k' => {
assert_eq!(self.next(), '[');
let did = self.parse_def();
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_metadata/tyencode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ pub fn enc_ty<'a, 'tcx>(w: &mut Cursor<Vec<u8>>, cx: &ctxt<'a, 'tcx>, t: Ty<'tcx
write!(w, "]");
}
ty::TyUnion(def, substs) => {
write!(w, "u[{}|", (cx.ds)(cx.tcx, def.did));
write!(w, "U[{}|", (cx.ds)(cx.tcx, def.did));
enc_substs(w, cx, substs);
write!(w, "]");
}
Expand Down
6 changes: 3 additions & 3 deletions src/test/debuginfo/union-smoke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
// === LLDB TESTS ==================================================================================

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

#![allow(unused)]
#![feature(omit_gdb_pretty_printer_section)]
Expand Down
2 changes: 1 addition & 1 deletion src/test/run-pass/union/auxiliary/union.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@

pub union U {
pub a: u8,
b: u16,
pub b: u16,
}
55 changes: 32 additions & 23 deletions src/test/run-pass/union/union-basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,47 +8,51 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// aux-build:union.rs

#![feature(untagged_unions)]

extern crate union;
use std::mem::{size_of, align_of, zeroed};

union U {
a: u8,
b: u16
}

union U64 {
a: u64,
}
fn local() {
assert_eq!(size_of::<U>(), 2);
assert_eq!(align_of::<U>(), 2);

union W {
a: u8,
b: u64,
}
let u = U { a: 10 };
unsafe {
assert_eq!(u.a, 10);
let U { a } = u;
assert_eq!(a, 10);
}

#[repr(C)]
union Y {
f1: u16,
f2: [u8; 4],
let mut w = U { b: 0 };
unsafe {
assert_eq!(w.a, 0);
assert_eq!(w.b, 0);
w.a = 1;
assert_eq!(w.a, 1);
assert_eq!(w.b, 1);
}
}

fn main() {
assert_eq!(size_of::<U>(), 1);
assert_eq!(size_of::<U64>(), 8);
assert_eq!(size_of::<W>(), 8);
assert_eq!(align_of::<U>(), 1);
assert_eq!(align_of::<U64>(), align_of::<u64>());
assert_eq!(align_of::<W>(), align_of::<u64>());
assert_eq!(size_of::<Y>(), 4);
assert_eq!(align_of::<Y>(), 2);
fn xcrate() {
assert_eq!(size_of::<union::U>(), 2);
assert_eq!(align_of::<union::U>(), 2);

let u = U { a: 10 };
let u = union::U { a: 10 };
unsafe {
assert_eq!(u.a, 10);
let U { a } = u;
let union::U { a } = u;
assert_eq!(a, 10);
}

let mut w = W { b: 0 };
let mut w = union::U { b: 0 };
unsafe {
assert_eq!(w.a, 0);
assert_eq!(w.b, 0);
Expand All @@ -57,3 +61,8 @@ fn main() {
assert_eq!(w.b, 1);
}
}

fn main() {
local();
xcrate();
}
21 changes: 0 additions & 21 deletions src/test/run-pass/union/union-xcrate.rs

This file was deleted.

0 comments on commit 436cfe5

Please sign in to comment.