forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of rust-lang#79684 - usbalbin:const_copy, r=oli-obk
Make copy[_nonoverlapping] const Constifies * `intrinsics::copy` and `intrinsics::copy_nonoverlapping` * `ptr::read` and `ptr::read_unaligned` * `*const T::read` and `*const T::read_unaligned` * `*mut T::read` and `*mut T::read_unaligned` * `MaybeUninit::assume_init_read`
- Loading branch information
Showing
12 changed files
with
185 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Aligned to two bytes | ||
const DATA: [u16; 2] = [u16::from_ne_bytes([0x01, 0x23]), u16::from_ne_bytes([0x45, 0x67])]; | ||
|
||
const fn unaligned_ptr() -> *const u16 { | ||
// Since DATA.as_ptr() is aligned to two bytes, adding 1 byte to that produces an unaligned *const u16 | ||
unsafe { (DATA.as_ptr() as *const u8).add(1) as *const u16 } | ||
} | ||
|
||
#[test] | ||
fn read() { | ||
use core::ptr; | ||
|
||
const FOO: i32 = unsafe { ptr::read(&42 as *const i32) }; | ||
assert_eq!(FOO, 42); | ||
|
||
const ALIGNED: i32 = unsafe { ptr::read_unaligned(&42 as *const i32) }; | ||
assert_eq!(ALIGNED, 42); | ||
|
||
const UNALIGNED_PTR: *const u16 = unaligned_ptr(); | ||
|
||
const UNALIGNED: u16 = unsafe { ptr::read_unaligned(UNALIGNED_PTR) }; | ||
assert_eq!(UNALIGNED, u16::from_ne_bytes([0x23, 0x45])); | ||
} | ||
|
||
#[test] | ||
fn const_ptr_read() { | ||
const FOO: i32 = unsafe { (&42 as *const i32).read() }; | ||
assert_eq!(FOO, 42); | ||
|
||
const ALIGNED: i32 = unsafe { (&42 as *const i32).read_unaligned() }; | ||
assert_eq!(ALIGNED, 42); | ||
|
||
const UNALIGNED_PTR: *const u16 = unaligned_ptr(); | ||
|
||
const UNALIGNED: u16 = unsafe { UNALIGNED_PTR.read_unaligned() }; | ||
assert_eq!(UNALIGNED, u16::from_ne_bytes([0x23, 0x45])); | ||
} | ||
|
||
#[test] | ||
fn mut_ptr_read() { | ||
const FOO: i32 = unsafe { (&42 as *const i32 as *mut i32).read() }; | ||
assert_eq!(FOO, 42); | ||
|
||
const ALIGNED: i32 = unsafe { (&42 as *const i32 as *mut i32).read_unaligned() }; | ||
assert_eq!(ALIGNED, 42); | ||
|
||
const UNALIGNED_PTR: *mut u16 = unaligned_ptr() as *mut u16; | ||
|
||
const UNALIGNED: u16 = unsafe { UNALIGNED_PTR.read_unaligned() }; | ||
assert_eq!(UNALIGNED, u16::from_ne_bytes([0x23, 0x45])); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// error-pattern: any use of this value will cause an error | ||
|
||
#![feature(const_ptr_read)] | ||
#![feature(const_ptr_offset)] | ||
|
||
fn main() { | ||
use std::ptr; | ||
|
||
const DATA: [u32; 1] = [42]; | ||
|
||
const PAST_END_PTR: *const u32 = unsafe { DATA.as_ptr().add(1) }; | ||
|
||
const _READ: u32 = unsafe { ptr::read(PAST_END_PTR) }; | ||
const _CONST_READ: u32 = unsafe { PAST_END_PTR.read() }; | ||
const _MUT_READ: u32 = unsafe { (PAST_END_PTR as *mut u32).read() }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
error: any use of this value will cause an error | ||
--> $SRC_DIR/core/src/intrinsics.rs:LL:COL | ||
| | ||
LL | unsafe { copy_nonoverlapping(src, dst, count) } | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | | ||
| memory access failed: pointer must be in-bounds at offset 8, but is outside bounds of alloc6 which has size 4 | ||
| inside `copy_nonoverlapping::<u32>` at $SRC_DIR/core/src/intrinsics.rs:LL:COL | ||
| inside `std::ptr::read::<u32>` at $SRC_DIR/core/src/ptr/mod.rs:LL:COL | ||
| inside `_READ` at $DIR/out_of_bounds_read.rs:13:33 | ||
| | ||
::: $DIR/out_of_bounds_read.rs:13:5 | ||
| | ||
LL | const _READ: u32 = unsafe { ptr::read(PAST_END_PTR) }; | ||
| ------------------------------------------------------ | ||
| | ||
= note: `#[deny(const_err)]` on by default | ||
|
||
error: any use of this value will cause an error | ||
--> $SRC_DIR/core/src/intrinsics.rs:LL:COL | ||
| | ||
LL | unsafe { copy_nonoverlapping(src, dst, count) } | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | | ||
| memory access failed: pointer must be in-bounds at offset 8, but is outside bounds of alloc6 which has size 4 | ||
| inside `copy_nonoverlapping::<u32>` at $SRC_DIR/core/src/intrinsics.rs:LL:COL | ||
| inside `std::ptr::read::<u32>` at $SRC_DIR/core/src/ptr/mod.rs:LL:COL | ||
| inside `ptr::const_ptr::<impl *const u32>::read` at $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL | ||
| inside `_CONST_READ` at $DIR/out_of_bounds_read.rs:14:39 | ||
| | ||
::: $DIR/out_of_bounds_read.rs:14:5 | ||
| | ||
LL | const _CONST_READ: u32 = unsafe { PAST_END_PTR.read() }; | ||
| -------------------------------------------------------- | ||
|
||
error: any use of this value will cause an error | ||
--> $SRC_DIR/core/src/intrinsics.rs:LL:COL | ||
| | ||
LL | unsafe { copy_nonoverlapping(src, dst, count) } | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | | ||
| memory access failed: pointer must be in-bounds at offset 8, but is outside bounds of alloc6 which has size 4 | ||
| inside `copy_nonoverlapping::<u32>` at $SRC_DIR/core/src/intrinsics.rs:LL:COL | ||
| inside `std::ptr::read::<u32>` at $SRC_DIR/core/src/ptr/mod.rs:LL:COL | ||
| inside `ptr::mut_ptr::<impl *mut u32>::read` at $SRC_DIR/core/src/ptr/mut_ptr.rs:LL:COL | ||
| inside `_MUT_READ` at $DIR/out_of_bounds_read.rs:15:37 | ||
| | ||
::: $DIR/out_of_bounds_read.rs:15:5 | ||
| | ||
LL | const _MUT_READ: u32 = unsafe { (PAST_END_PTR as *mut u32).read() }; | ||
| -------------------------------------------------------------------- | ||
|
||
error: aborting due to 3 previous errors | ||
|