Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix unsafe unaligned loads in test. #39682

Merged
merged 2 commits into from
Feb 10, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 46 additions & 15 deletions src/test/run-pass/mir_adt_construction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,24 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use std::fmt;

#[repr(C)]
enum CEnum {
Hello = 30,
World = 60
}

fn test1(c: CEnum) -> i32 {
let c2 = CEnum::Hello;
match (c, c2) {
(CEnum::Hello, CEnum::Hello) => 42,
(CEnum::World, CEnum::Hello) => 0,
_ => 1
}
let c2 = CEnum::Hello;
match (c, c2) {
(CEnum::Hello, CEnum::Hello) => 42,
(CEnum::World, CEnum::Hello) => 0,
_ => 1
}
}

#[repr(packed)]
#[derive(PartialEq, Debug)]
struct Pakd {
a: u64,
b: u32,
Expand All @@ -33,6 +34,36 @@ struct Pakd {
e: ()
}

// It is unsafe to use #[derive(Debug)] on a packed struct because the code generated by the derive
// macro takes references to the fields instead of accessing them directly.
impl fmt::Debug for Pakd {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
// It's important that we load the fields into locals by-value here. This will do safe
// unaligned loads into the locals, then pass references to the properly-aligned locals to
// the formatting code.
let Pakd { a, b, c, d, e } = *self;
f.debug_struct("Pakd")
.field("a", &a)
.field("b", &b)
.field("c", &c)
.field("d", &d)
.field("e", &e)
.finish()
}
}

// It is unsafe to use #[derive(PartialEq)] on a packed struct because the code generated by the
// derive macro takes references to the fields instead of accessing them directly.
impl PartialEq for Pakd {
fn eq(&self, other: &Pakd) -> bool {
self.a == other.a &&
self.b == other.b &&
self.c == other.c &&
self.d == other.d &&
self.e == other.e
}
}

impl Drop for Pakd {
fn drop(&mut self) {}
}
Expand All @@ -59,12 +90,12 @@ fn test5(x: fn(u32) -> Option<u32>) -> (Option<u32>, Option<u32>) {
}

fn main() {
assert_eq!(test1(CEnum::Hello), 42);
assert_eq!(test1(CEnum::World), 0);
assert_eq!(test2(), Pakd { a: 42, b: 42, c: 42, d: 42, e: () });
assert_eq!(test3(), TupleLike(42, 42));
let t4 = test4(TupleLike);
assert_eq!(t4.0, t4.1);
let t5 = test5(Some);
assert_eq!(t5.0, t5.1);
assert_eq!(test1(CEnum::Hello), 42);
assert_eq!(test1(CEnum::World), 0);
assert_eq!(test2(), Pakd { a: 42, b: 42, c: 42, d: 42, e: () });
assert_eq!(test3(), TupleLike(42, 42));
let t4 = test4(TupleLike);
assert_eq!(t4.0, t4.1);
let t5 = test5(Some);
assert_eq!(t5.0, t5.1);
}