about summary refs log tree commit diff
diff options
context:
space:
mode:
authorVadim Petrochenkov <vadim.petrochenkov@gmail.com>2016-08-26 19:23:42 +0300
committerVadim Petrochenkov <vadim.petrochenkov@gmail.com>2016-09-03 13:39:35 +0300
commit436cfe56534b405786816d4bbcccd11ed7571981 (patch)
tree21c4e2972397731442e7753a991f69288fb9b3c8
parent93067ca089ea570e4e2bdfc456958c81a4d1e092 (diff)
downloadrust-436cfe56534b405786816d4bbcccd11ed7571981.tar.gz
rust-436cfe56534b405786816d4bbcccd11ed7571981.zip
Fix type encoding/decoding for unions
Fix union debuginfo test on lldb
-rw-r--r--src/librustc_metadata/decoder.rs2
-rw-r--r--src/librustc_metadata/tydecode.rs8
-rw-r--r--src/librustc_metadata/tyencode.rs2
-rw-r--r--src/test/debuginfo/union-smoke.rs6
-rw-r--r--src/test/run-pass/union/auxiliary/union.rs2
-rw-r--r--src/test/run-pass/union/union-basic.rs55
-rw-r--r--src/test/run-pass/union/union-xcrate.rs21
7 files changed, 46 insertions, 50 deletions
diff --git a/src/librustc_metadata/decoder.rs b/src/librustc_metadata/decoder.rs
index 8775f58d0b2..aeb95e5670d 100644
--- a/src/librustc_metadata/decoder.rs
+++ b/src/librustc_metadata/decoder.rs
@@ -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),
diff --git a/src/librustc_metadata/tydecode.rs b/src/librustc_metadata/tydecode.rs
index f51299226fe..55ff4817683 100644
--- a/src/librustc_metadata/tydecode.rs
+++ b/src/librustc_metadata/tydecode.rs
@@ -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();
diff --git a/src/librustc_metadata/tyencode.rs b/src/librustc_metadata/tyencode.rs
index b334a21c07c..bef3cf3a194 100644
--- a/src/librustc_metadata/tyencode.rs
+++ b/src/librustc_metadata/tyencode.rs
@@ -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, "]");
         }
diff --git a/src/test/debuginfo/union-smoke.rs b/src/test/debuginfo/union-smoke.rs
index 17dea300ff9..319927c979b 100644
--- a/src/test/debuginfo/union-smoke.rs
+++ b/src/test/debuginfo/union-smoke.rs
@@ -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)]
diff --git a/src/test/run-pass/union/auxiliary/union.rs b/src/test/run-pass/union/auxiliary/union.rs
index dc0ca7c81c0..0231e38a729 100644
--- a/src/test/run-pass/union/auxiliary/union.rs
+++ b/src/test/run-pass/union/auxiliary/union.rs
@@ -12,5 +12,5 @@
 
 pub union U {
     pub a: u8,
-    b: u16,
+    pub b: u16,
 }
diff --git a/src/test/run-pass/union/union-basic.rs b/src/test/run-pass/union/union-basic.rs
index 1651aa901b9..d23af4b41b7 100644
--- a/src/test/run-pass/union/union-basic.rs
+++ b/src/test/run-pass/union/union-basic.rs
@@ -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);
@@ -57,3 +61,8 @@ fn main() {
         assert_eq!(w.b, 1);
     }
 }
+
+fn main() {
+    local();
+    xcrate();
+}
diff --git a/src/test/run-pass/union/union-xcrate.rs b/src/test/run-pass/union/union-xcrate.rs
deleted file mode 100644
index 2a76c96ef25..00000000000
--- a/src/test/run-pass/union/union-xcrate.rs
+++ /dev/null
@@ -1,21 +0,0 @@
-// 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 <LICENSE-APACHE or
-// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
-// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
-// 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;
-
-type A = union::U;
-
-fn main() {
-    assert_eq!(std::mem::size_of::<A>(), 8);
-}