rust/src/test/run-pass/packed-struct-borrow-element.rs
Ariel Ben-Yehuda bdb72edb5c make accesses to fields of packed structs unsafe
To handle packed structs with destructors (which you'll think are a rare
case, but the `#[repr(packed)] struct Packed<T>(T);` pattern is
ever-popular, which requires handling packed structs with destructors to
avoid monomorphization-time errors), drops of subfields of packed
structs should drop a local move of the field instead of the original
one.

cc #27060 - this should deal with that issue after codegen of drop glue
is updated.

The new errors need to be changed to future-compatibility warnings, but
I'll rather do a crater run first with them as errors to assess the
impact.
2017-11-26 16:12:42 +02:00

24 lines
645 B
Rust

// Copyright 2013 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.
#[repr(packed)]
struct Foo {
bar: u8,
baz: usize
}
pub fn main() {
let foo = Foo { bar: 1, baz: 2 };
let brw = unsafe { &foo.baz };
assert_eq!(*brw, 2);
}