2016-06-17 23:52:24 -05:00
|
|
|
#[derive(PartialEq, Debug)]
|
2016-06-08 06:43:34 -05:00
|
|
|
struct A;
|
|
|
|
|
|
|
|
fn zst_ret() -> A {
|
|
|
|
A
|
|
|
|
}
|
|
|
|
|
|
|
|
fn use_zst() -> A {
|
|
|
|
let a = A;
|
|
|
|
a
|
|
|
|
}
|
|
|
|
|
2016-06-17 23:52:24 -05:00
|
|
|
fn main() {
|
2018-09-15 09:35:37 -05:00
|
|
|
// Not using the () type here, as writes of that type do not even have MIR generated.
|
|
|
|
// Also not assigning directly as that's array initialization, not assignment.
|
|
|
|
let zst_val = [1u8; 0];
|
|
|
|
|
2016-06-17 23:52:24 -05:00
|
|
|
assert_eq!(zst_ret(), A);
|
|
|
|
assert_eq!(use_zst(), A);
|
2018-09-15 09:35:37 -05:00
|
|
|
let x = 42 as *mut [u8; 0];
|
|
|
|
// reading and writing is okay
|
|
|
|
unsafe { *x = zst_val; }
|
|
|
|
unsafe { let _y = *x; }
|
|
|
|
|
|
|
|
// We should even be able to use "true" pointers for ZST when the allocation has been
|
|
|
|
// removed already. The box is for a non-ZST to make sure there actually is an allocation.
|
|
|
|
let mut x_box = Box::new(((), 1u8));
|
|
|
|
let x = &mut x_box.0 as *mut _ as *mut [u8; 0];
|
|
|
|
drop(x_box);
|
|
|
|
// reading and writing is okay
|
|
|
|
unsafe { *x = zst_val; }
|
|
|
|
unsafe { let _y = *x; }
|
2016-06-17 23:52:24 -05:00
|
|
|
}
|