forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix alignment for packed unions Add some missing privacy test Get rid of `unimplemented_unions` macro
- Loading branch information
1 parent
2dc2fc5
commit d9b332b
Showing
11 changed files
with
206 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// 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. | ||
|
||
#![feature(untagged_unions)] | ||
|
||
union U { | ||
a: usize, | ||
b: usize, | ||
} | ||
|
||
const C: U = U { a: 10 }; | ||
|
||
fn main() { | ||
unsafe { | ||
let a: [u8; C.a]; // OK | ||
let b: [u8; C.b]; //~ ERROR constant evaluation error | ||
//~^ NOTE nonexistent struct field | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// 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. | ||
|
||
#![feature(untagged_unions)] | ||
|
||
union U { | ||
a: usize, | ||
b: usize, | ||
} | ||
|
||
const C: U = U { a: 10 }; | ||
|
||
fn main() { | ||
match C { | ||
C => {} //~ ERROR cannot use unions in constant patterns | ||
_ => {} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// 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. | ||
|
||
#![feature(untagged_unions)] | ||
|
||
mod m { | ||
pub union U { | ||
a: u8 | ||
} | ||
} | ||
|
||
fn main() { | ||
let u = m::U { a: 0 }; //~ ERROR field `a` of union `m::U` is private | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// 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. | ||
|
||
#![feature(untagged_unions)] | ||
|
||
union U { | ||
a: u64, | ||
b: u64, | ||
} | ||
|
||
const C: U = U { b: 10 }; | ||
|
||
fn main() { | ||
unsafe { | ||
let a = C.a; | ||
let b = C.b; | ||
assert_eq!(a, 10); | ||
assert_eq!(b, 10); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// 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. | ||
|
||
#![feature(untagged_unions)] | ||
|
||
use std::mem::{size_of, size_of_val, align_of, align_of_val}; | ||
|
||
struct S { | ||
a: u16, | ||
b: [u8; 3], | ||
} | ||
|
||
#[repr(packed)] | ||
struct Sp { | ||
a: u16, | ||
b: [u8; 3], | ||
} | ||
|
||
union U { | ||
a: u16, | ||
b: [u8; 3], | ||
} | ||
|
||
#[repr(packed)] | ||
union Up { | ||
a: u16, | ||
b: [u8; 3], | ||
} | ||
|
||
const CS: S = S { a: 0, b: [0, 0, 0] }; | ||
const CSP: Sp = Sp { a: 0, b: [0, 0, 0] }; | ||
const CU: U = U { b: [0, 0, 0] }; | ||
const CUP: Up = Up { b: [0, 0, 0] }; | ||
|
||
fn main() { | ||
let s = S { a: 0, b: [0, 0, 0] }; | ||
assert_eq!(size_of::<S>(), 6); | ||
assert_eq!(size_of_val(&s), 6); | ||
assert_eq!(size_of_val(&CS), 6); | ||
assert_eq!(align_of::<S>(), 2); | ||
assert_eq!(align_of_val(&s), 2); | ||
assert_eq!(align_of_val(&CS), 2); | ||
|
||
let sp = Sp { a: 0, b: [0, 0, 0] }; | ||
assert_eq!(size_of::<Sp>(), 5); | ||
assert_eq!(size_of_val(&sp), 5); | ||
assert_eq!(size_of_val(&CSP), 5); | ||
assert_eq!(align_of::<Sp>(), 1); | ||
assert_eq!(align_of_val(&sp), 1); | ||
assert_eq!(align_of_val(&CSP), 1); | ||
|
||
let u = U { b: [0, 0, 0] }; | ||
assert_eq!(size_of::<U>(), 4); | ||
assert_eq!(size_of_val(&u), 4); | ||
assert_eq!(size_of_val(&CU), 4); | ||
assert_eq!(align_of::<U>(), 2); | ||
assert_eq!(align_of_val(&u), 2); | ||
assert_eq!(align_of_val(&CU), 2); | ||
|
||
let up = Up { b: [0, 0, 0] }; | ||
assert_eq!(size_of::<Up>(), 3); | ||
assert_eq!(size_of_val(&up), 3); | ||
assert_eq!(size_of_val(&CUP), 3); | ||
assert_eq!(align_of::<Up>(), 1); | ||
assert_eq!(align_of_val(&up), 1); | ||
assert_eq!(align_of_val(&CUP), 1); | ||
} |