|
| 1 | +// run-pass |
| 2 | + |
| 3 | +// This does not reflect a stable guarantee (we guarantee very little for equality of pointers |
| 4 | +// around `const`), but it would be good to understand what is happening if these assertions ever |
| 5 | +// fail. |
| 6 | +use std::ptr::NonNull; |
| 7 | +use std::slice::from_raw_parts; |
| 8 | + |
| 9 | +const PTR_U8: *const u8 = NonNull::dangling().as_ptr(); |
| 10 | +const CONST_U8_REF: &[u8] = unsafe { from_raw_parts(PTR_U8, 0) }; |
| 11 | +const CONST_U8_PTR: *const u8 = unsafe { from_raw_parts(PTR_U8, 0).as_ptr() }; |
| 12 | +static STATIC_U8_REF: &[u8] = unsafe { from_raw_parts(PTR_U8, 0) }; |
| 13 | + |
| 14 | +const PTR_U16: *const u16 = NonNull::dangling().as_ptr(); |
| 15 | +const CONST_U16_REF: &[u16] = unsafe { from_raw_parts(PTR_U16, 0) }; |
| 16 | + |
| 17 | +const fn const_u8_fn() -> &'static [u8] { |
| 18 | + unsafe { from_raw_parts(PTR_U8, 0) } |
| 19 | +} |
| 20 | + |
| 21 | +fn main() { |
| 22 | + let ptr_u8 = unsafe { from_raw_parts(PTR_U8, 0) }.as_ptr(); |
| 23 | + let ptr_u16 = unsafe { from_raw_parts(PTR_U16, 0) }.as_ptr(); |
| 24 | + |
| 25 | + assert_eq!(ptr_u8, PTR_U8); |
| 26 | + assert_eq!(ptr_u8, CONST_U8_PTR); |
| 27 | + assert_eq!(ptr_u8, const_u8_fn().as_ptr()); |
| 28 | + assert_eq!(ptr_u8, STATIC_U8_REF.as_ptr()); |
| 29 | + assert_eq!(ptr_u16, CONST_U16_REF.as_ptr()); |
| 30 | + assert_eq!(ptr_u8, CONST_U8_REF.as_ptr()); |
| 31 | +} |
0 commit comments