forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of rust-lang#63810 - oli-obk:const_offset_from, r=RalfJu…
…ng,nikic Make <*const/mut T>::offset_from `const fn` This reenables offset_of cc @mjbshaw after rust-lang#63075 broke it
- Loading branch information
Showing
10 changed files
with
253 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// run-pass | ||
|
||
#![feature(const_raw_ptr_deref)] | ||
#![feature(const_ptr_offset_from)] | ||
#![feature(ptr_offset_from)] | ||
|
||
struct Struct { | ||
field: (), | ||
} | ||
|
||
#[repr(C)] | ||
struct Struct2 { | ||
data: u8, | ||
field: u8, | ||
} | ||
|
||
pub const OFFSET: usize = { | ||
let uninit = std::mem::MaybeUninit::<Struct>::uninit(); | ||
let base_ptr: *const Struct = &uninit as *const _ as *const Struct; | ||
// The following statement is UB (taking the address of an uninitialized field). | ||
// Const eval doesn't detect this right now, but it may stop compiling at some point | ||
// in the future. | ||
let field_ptr = unsafe { &(*base_ptr).field as *const () as *const u8 }; | ||
let offset = unsafe { field_ptr.offset_from(base_ptr as *const u8) }; | ||
offset as usize | ||
}; | ||
|
||
pub const OFFSET_2: usize = { | ||
let uninit = std::mem::MaybeUninit::<Struct2>::uninit(); | ||
let base_ptr: *const Struct2 = &uninit as *const _ as *const Struct2; | ||
let field_ptr = unsafe { &(*base_ptr).field as *const u8 }; | ||
let offset = unsafe { field_ptr.offset_from(base_ptr as *const u8) }; | ||
offset as usize | ||
}; | ||
|
||
pub const OVERFLOW: isize = { | ||
let uninit = std::mem::MaybeUninit::<Struct2>::uninit(); | ||
let base_ptr: *const Struct2 = &uninit as *const _ as *const Struct2; | ||
let field_ptr = unsafe { &(*base_ptr).field as *const u8 }; | ||
unsafe { (base_ptr as *const u8).offset_from(field_ptr) } | ||
}; | ||
|
||
fn main() { | ||
assert_eq!(OFFSET, 0); | ||
assert_eq!(OFFSET_2, 1); | ||
assert_eq!(OVERFLOW, -1); | ||
} |
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,38 @@ | ||
// ingoring on musl because it's ui output hides libcore backtraces | ||
// ignore-musl | ||
|
||
#![feature(const_raw_ptr_deref)] | ||
#![feature(const_ptr_offset_from)] | ||
#![feature(ptr_offset_from)] | ||
|
||
#[repr(C)] | ||
struct Struct { | ||
data: u8, | ||
field: u8, | ||
} | ||
|
||
pub const DIFFERENT_ALLOC: usize = { | ||
//~^ NOTE | ||
let uninit = std::mem::MaybeUninit::<Struct>::uninit(); | ||
let base_ptr: *const Struct = &uninit as *const _ as *const Struct; | ||
let uninit2 = std::mem::MaybeUninit::<Struct>::uninit(); | ||
let field_ptr: *const Struct = &uninit2 as *const _ as *const Struct; | ||
let offset = unsafe { field_ptr.offset_from(base_ptr) }; | ||
offset as usize | ||
}; | ||
|
||
pub const NOT_PTR: usize = { | ||
//~^ NOTE | ||
unsafe { (42 as *const u8).offset_from(&5u8) as usize } | ||
}; | ||
|
||
pub const NOT_MULTIPLE_OF_SIZE: usize = { | ||
//~^ NOTE | ||
let data = [5u8, 6, 7]; | ||
let base_ptr = data.as_ptr(); | ||
let field_ptr = &data[1] as *const u8 as *const u16; | ||
let offset = unsafe { field_ptr.offset_from(base_ptr as *const u16) }; | ||
offset as usize | ||
}; | ||
|
||
fn main() {} |
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,61 @@ | ||
error: any use of this value will cause an error | ||
--> $SRC_DIR/libcore/ptr/mod.rs:LL:COL | ||
| | ||
LL | intrinsics::ptr_offset_from(self, origin) | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | | ||
| ptr_offset_from cannot compute offset of pointers into different allocations. | ||
| inside call to `std::ptr::<impl *const Struct>::offset_from` at $DIR/offset_from_ub.rs:20:27 | ||
| | ||
::: $DIR/offset_from_ub.rs:14:1 | ||
| | ||
LL | / pub const DIFFERENT_ALLOC: usize = { | ||
LL | | | ||
LL | | let uninit = std::mem::MaybeUninit::<Struct>::uninit(); | ||
LL | | let base_ptr: *const Struct = &uninit as *const _ as *const Struct; | ||
... | | ||
LL | | offset as usize | ||
LL | | }; | ||
| |__- | ||
| | ||
= note: `#[deny(const_err)]` on by default | ||
|
||
error: any use of this value will cause an error | ||
--> $SRC_DIR/libcore/ptr/mod.rs:LL:COL | ||
| | ||
LL | intrinsics::ptr_offset_from(self, origin) | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | | ||
| a memory access tried to interpret some bytes as a pointer | ||
| inside call to `std::ptr::<impl *const u8>::offset_from` at $DIR/offset_from_ub.rs:26:14 | ||
| | ||
::: $DIR/offset_from_ub.rs:24:1 | ||
| | ||
LL | / pub const NOT_PTR: usize = { | ||
LL | | | ||
LL | | unsafe { (42 as *const u8).offset_from(&5u8) as usize } | ||
LL | | }; | ||
| |__- | ||
|
||
error: any use of this value will cause an error | ||
--> $SRC_DIR/libcore/ptr/mod.rs:LL:COL | ||
| | ||
LL | intrinsics::ptr_offset_from(self, origin) | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | | ||
| exact_div: 1 cannot be divided by 2 without remainder | ||
| inside call to `std::ptr::<impl *const u16>::offset_from` at $DIR/offset_from_ub.rs:34:27 | ||
| | ||
::: $DIR/offset_from_ub.rs:29:1 | ||
| | ||
LL | / pub const NOT_MULTIPLE_OF_SIZE: usize = { | ||
LL | | | ||
LL | | let data = [5u8, 6, 7]; | ||
LL | | let base_ptr = data.as_ptr(); | ||
... | | ||
LL | | offset as usize | ||
LL | | }; | ||
| |__- | ||
|
||
error: aborting due to 3 previous errors | ||
|
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,15 @@ | ||
// run-pass | ||
|
||
#![feature(ptr_offset_from)] | ||
|
||
fn main() { | ||
let mut a = [0; 5]; | ||
let ptr1: *mut i32 = &mut a[1]; | ||
let ptr2: *mut i32 = &mut a[3]; | ||
unsafe { | ||
assert_eq!(ptr2.offset_from(ptr1), 2); | ||
assert_eq!(ptr1.offset_from(ptr2), -2); | ||
assert_eq!(ptr1.offset(2), ptr2); | ||
assert_eq!(ptr2.offset(-2), ptr1); | ||
} | ||
} |