Skip to content

Commit 3c944fe

Browse files
committed
test offset_from with two integers
1 parent 047e702 commit 3c944fe

File tree

3 files changed

+37
-8
lines changed

3 files changed

+37
-8
lines changed

src/test/ui/consts/offset_from.rs

+6
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,14 @@ pub const OVERFLOW: isize = {
4040
unsafe { (base_ptr as *const u8).offset_from(field_ptr) }
4141
};
4242

43+
pub const OFFSET_EQUAL_INTS: isize = {
44+
let ptr = 1 as *const u8;
45+
unsafe { ptr.offset_from(ptr) }
46+
};
47+
4348
fn main() {
4449
assert_eq!(OFFSET, 0);
4550
assert_eq!(OFFSET_2, 1);
4651
assert_eq!(OVERFLOW, -1);
52+
assert_eq!(OFFSET_EQUAL_INTS, 0);
4753
}

src/test/ui/consts/offset_from_ub.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,18 @@ pub const NOT_PTR: usize = {
2525
unsafe { (42 as *const u8).offset_from(&5u8) as usize }
2626
};
2727

28-
pub const NOT_MULTIPLE_OF_SIZE: usize = {
28+
pub const NOT_MULTIPLE_OF_SIZE: isize = {
2929
//~^ NOTE
3030
let data = [5u8, 6, 7];
3131
let base_ptr = data.as_ptr();
3232
let field_ptr = &data[1] as *const u8 as *const u16;
33-
let offset = unsafe { field_ptr.offset_from(base_ptr as *const u16) };
34-
offset as usize
33+
unsafe { field_ptr.offset_from(base_ptr as *const u16) }
34+
};
35+
36+
pub const OFFSET_FROM_NULL: isize = {
37+
//~^ NOTE
38+
let ptr = 0 as *const u8;
39+
unsafe { ptr.offset_from(ptr) }
3540
};
3641

3742
fn main() {}

src/test/ui/consts/offset_from_ub.stderr

+23-5
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,36 @@ LL | intrinsics::ptr_offset_from(self, origin)
4444
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4545
| |
4646
| exact_div: 1 cannot be divided by 2 without remainder
47-
| inside call to `std::ptr::<impl *const u16>::offset_from` at $DIR/offset_from_ub.rs:33:27
47+
| inside call to `std::ptr::<impl *const u16>::offset_from` at $DIR/offset_from_ub.rs:33:14
4848
|
4949
::: $DIR/offset_from_ub.rs:28:1
5050
|
51-
LL | / pub const NOT_MULTIPLE_OF_SIZE: usize = {
51+
LL | / pub const NOT_MULTIPLE_OF_SIZE: isize = {
5252
LL | |
5353
LL | | let data = [5u8, 6, 7];
5454
LL | | let base_ptr = data.as_ptr();
55-
... |
56-
LL | | offset as usize
55+
LL | | let field_ptr = &data[1] as *const u8 as *const u16;
56+
LL | | unsafe { field_ptr.offset_from(base_ptr as *const u16) }
57+
LL | | };
58+
| |__-
59+
60+
error: any use of this value will cause an error
61+
--> $SRC_DIR/libcore/ptr/mod.rs:LL:COL
62+
|
63+
LL | intrinsics::ptr_offset_from(self, origin)
64+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
65+
| |
66+
| invalid use of NULL pointer
67+
| inside call to `std::ptr::<impl *const u8>::offset_from` at $DIR/offset_from_ub.rs:39:14
68+
|
69+
::: $DIR/offset_from_ub.rs:36:1
70+
|
71+
LL | / pub const OFFSET_FROM_NULL: isize = {
72+
LL | |
73+
LL | | let ptr = 0 as *const u8;
74+
LL | | unsafe { ptr.offset_from(ptr) }
5775
LL | | };
5876
| |__-
5977

60-
error: aborting due to 3 previous errors
78+
error: aborting due to 4 previous errors
6179

0 commit comments

Comments
 (0)