test offset_from with two integers

This commit is contained in:
Ralf Jung 2019-11-04 13:30:00 +01:00
parent 047e702c66
commit 3c944feb65
3 changed files with 37 additions and 8 deletions

View File

@ -40,8 +40,14 @@ struct Struct2 {
unsafe { (base_ptr as *const u8).offset_from(field_ptr) }
};
pub const OFFSET_EQUAL_INTS: isize = {
let ptr = 1 as *const u8;
unsafe { ptr.offset_from(ptr) }
};
fn main() {
assert_eq!(OFFSET, 0);
assert_eq!(OFFSET_2, 1);
assert_eq!(OVERFLOW, -1);
assert_eq!(OFFSET_EQUAL_INTS, 0);
}

View File

@ -25,13 +25,18 @@ struct Struct {
unsafe { (42 as *const u8).offset_from(&5u8) as usize }
};
pub const NOT_MULTIPLE_OF_SIZE: usize = {
pub const NOT_MULTIPLE_OF_SIZE: isize = {
//~^ 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
unsafe { field_ptr.offset_from(base_ptr as *const u16) }
};
pub const OFFSET_FROM_NULL: isize = {
//~^ NOTE
let ptr = 0 as *const u8;
unsafe { ptr.offset_from(ptr) }
};
fn main() {}

View File

@ -44,18 +44,36 @@ 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:33:27
| inside call to `std::ptr::<impl *const u16>::offset_from` at $DIR/offset_from_ub.rs:33:14
|
::: $DIR/offset_from_ub.rs:28:1
|
LL | / pub const NOT_MULTIPLE_OF_SIZE: usize = {
LL | / pub const NOT_MULTIPLE_OF_SIZE: isize = {
LL | |
LL | | let data = [5u8, 6, 7];
LL | | let base_ptr = data.as_ptr();
... |
LL | | offset as usize
LL | | let field_ptr = &data[1] as *const u8 as *const u16;
LL | | unsafe { field_ptr.offset_from(base_ptr as *const u16) }
LL | | };
| |__-
error: aborting due to 3 previous errors
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)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
| invalid use of NULL pointer
| inside call to `std::ptr::<impl *const u8>::offset_from` at $DIR/offset_from_ub.rs:39:14
|
::: $DIR/offset_from_ub.rs:36:1
|
LL | / pub const OFFSET_FROM_NULL: isize = {
LL | |
LL | | let ptr = 0 as *const u8;
LL | | unsafe { ptr.offset_from(ptr) }
LL | | };
| |__-
error: aborting due to 4 previous errors