2023-09-05 15:29:51 -05:00
|
|
|
use std::mem::ManuallyDrop;
|
|
|
|
use std::fmt::Debug;
|
|
|
|
|
2020-05-16 09:17:07 -05:00
|
|
|
#[repr(packed)]
|
|
|
|
pub struct Good {
|
2020-05-27 13:31:17 -05:00
|
|
|
data: u64,
|
|
|
|
ptr: &'static u64,
|
|
|
|
data2: [u64; 2],
|
2020-05-16 09:17:07 -05:00
|
|
|
aligned: [u8; 32],
|
|
|
|
}
|
|
|
|
|
2021-03-28 05:50:20 -05:00
|
|
|
#[repr(packed(2))]
|
|
|
|
pub struct Packed2 {
|
|
|
|
x: u32,
|
|
|
|
y: u16,
|
|
|
|
z: u8,
|
|
|
|
}
|
|
|
|
|
2023-02-16 11:17:55 -06:00
|
|
|
trait Foo {
|
|
|
|
fn evil(&self);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test for #108122
|
|
|
|
#[automatically_derived]
|
|
|
|
impl Foo for Packed2 {
|
|
|
|
fn evil(&self) {
|
|
|
|
unsafe {
|
|
|
|
&self.x; //~ ERROR reference to packed field
|
|
|
|
}
|
|
|
|
}
|
2023-09-05 15:29:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test for #115396
|
|
|
|
fn packed_dyn() {
|
|
|
|
#[repr(packed)]
|
2023-09-05 16:06:35 -05:00
|
|
|
struct Unaligned<T: ?Sized>(ManuallyDrop<T>);
|
2023-09-05 15:29:51 -05:00
|
|
|
|
|
|
|
let ref local = Unaligned(ManuallyDrop::new([3, 5, 8u64]));
|
|
|
|
let foo: &Unaligned<dyn Debug> = &*local;
|
|
|
|
println!("{:?}", &*foo.0); //~ ERROR reference to packed field
|
2023-09-05 16:06:35 -05:00
|
|
|
let foo: &Unaligned<[u64]> = &*local;
|
|
|
|
println!("{:?}", &*foo.0); //~ ERROR reference to packed field
|
|
|
|
|
|
|
|
// Even if the actual alignment is 1, we cannot know that when looking at `dyn Debug.`
|
|
|
|
let ref local = Unaligned(ManuallyDrop::new([3, 5, 8u8]));
|
|
|
|
let foo: &Unaligned<dyn Debug> = &*local;
|
|
|
|
println!("{:?}", &*foo.0); //~ ERROR reference to packed field
|
|
|
|
// However, we *can* know the alignment when looking at a slice.
|
|
|
|
let foo: &Unaligned<[u8]> = &*local;
|
|
|
|
println!("{:?}", &*foo.0); // no error!
|
2023-02-16 11:17:55 -06:00
|
|
|
}
|
|
|
|
|
2020-05-16 09:17:07 -05:00
|
|
|
fn main() {
|
|
|
|
unsafe {
|
2020-05-27 13:31:17 -05:00
|
|
|
let good = Good { data: 0, ptr: &0, data2: [0, 0], aligned: [0; 32] };
|
2020-05-16 09:17:07 -05:00
|
|
|
|
2020-05-27 13:31:17 -05:00
|
|
|
let _ = &good.ptr; //~ ERROR reference to packed field
|
2020-05-16 09:17:07 -05:00
|
|
|
let _ = &good.data; //~ ERROR reference to packed field
|
2020-05-27 13:31:17 -05:00
|
|
|
// Error even when turned into raw pointer immediately.
|
2020-05-25 04:15:38 -05:00
|
|
|
let _ = &good.data as *const _; //~ ERROR reference to packed field
|
|
|
|
let _: *const _ = &good.data; //~ ERROR reference to packed field
|
2020-05-27 13:31:17 -05:00
|
|
|
// Error on method call.
|
|
|
|
let _ = good.data.clone(); //~ ERROR reference to packed field
|
|
|
|
// Error for nested fields.
|
2020-05-16 09:17:07 -05:00
|
|
|
let _ = &good.data2[0]; //~ ERROR reference to packed field
|
2020-05-27 13:31:17 -05:00
|
|
|
|
|
|
|
let _ = &*good.ptr; // ok, behind a pointer
|
2020-05-16 09:17:07 -05:00
|
|
|
let _ = &good.aligned; // ok, has align 1
|
|
|
|
let _ = &good.aligned[2]; // ok, has align 1
|
|
|
|
}
|
2021-03-28 05:50:20 -05:00
|
|
|
|
|
|
|
unsafe {
|
|
|
|
let packed2 = Packed2 { x: 0, y: 0, z: 0 };
|
|
|
|
let _ = &packed2.x; //~ ERROR reference to packed field
|
|
|
|
let _ = &packed2.y; // ok, has align 2 in packed(2) struct
|
|
|
|
let _ = &packed2.z; // ok, has align 1
|
2023-02-16 11:17:55 -06:00
|
|
|
packed2.evil();
|
2021-03-28 05:50:20 -05:00
|
|
|
}
|
2022-08-02 11:21:38 -05:00
|
|
|
|
|
|
|
unsafe {
|
|
|
|
struct U16(u16);
|
|
|
|
|
|
|
|
impl Drop for U16 {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
println!("{:p}", self);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct HasDrop;
|
|
|
|
|
|
|
|
impl Drop for HasDrop {
|
|
|
|
fn drop(&mut self) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(unused)]
|
|
|
|
struct Wrapper {
|
|
|
|
a: U16,
|
|
|
|
b: HasDrop,
|
|
|
|
}
|
|
|
|
#[allow(unused)]
|
|
|
|
#[repr(packed(2))]
|
|
|
|
struct Wrapper2 {
|
|
|
|
a: U16,
|
|
|
|
b: HasDrop,
|
|
|
|
}
|
|
|
|
|
|
|
|
// An outer struct with more restrictive packing than the inner struct -- make sure we
|
|
|
|
// notice that!
|
|
|
|
#[repr(packed)]
|
|
|
|
struct Misalign<T>(u8, T);
|
|
|
|
|
2023-02-16 11:17:55 -06:00
|
|
|
let m1 = Misalign(0, Wrapper { a: U16(10), b: HasDrop });
|
2022-08-02 11:21:38 -05:00
|
|
|
let _ref = &m1.1.a; //~ ERROR reference to packed field
|
|
|
|
|
2023-02-16 11:17:55 -06:00
|
|
|
let m2 = Misalign(0, Wrapper2 { a: U16(10), b: HasDrop });
|
2022-08-02 11:21:38 -05:00
|
|
|
let _ref = &m2.1.a; //~ ERROR reference to packed field
|
|
|
|
}
|
2020-05-16 09:17:07 -05:00
|
|
|
}
|