Skip to content

Commit 94a6d4b

Browse files
committed
Adjust const eval code to reflect offset_from docs
1 parent 4a51801 commit 94a6d4b

File tree

4 files changed

+10
-36
lines changed

4 files changed

+10
-36
lines changed

src/librustc_mir/interpret/intrinsics.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -249,14 +249,9 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
249249
let usize_layout = self.layout_of(self.tcx.types.usize)?;
250250
let a_offset = ImmTy::from_uint(a.offset.bytes(), usize_layout);
251251
let b_offset = ImmTy::from_uint(b.offset.bytes(), usize_layout);
252-
let (val, overflowed, _) = self.overflowing_binary_op(
252+
let (val, _overflowed, _) = self.overflowing_binary_op(
253253
BinOp::Sub, a_offset, b_offset,
254254
)?;
255-
if overflowed {
256-
throw_ub_format!(
257-
"second argument to `ptr_offset_from` must be smaller than first",
258-
);
259-
}
260255
let pointee_layout = self.layout_of(substs.type_at(0))?;
261256
let isize_layout = self.layout_of(self.tcx.types.isize)?;
262257
let val = ImmTy::from_scalar(val, isize_layout);

src/test/ui/consts/offset_of.rs renamed to src/test/ui/consts/offset_from.rs

+8
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,15 @@ pub const OFFSET_2: usize = {
3333
offset as usize
3434
};
3535

36+
pub const OVERFLOW: isize = {
37+
let uninit = std::mem::MaybeUninit::<Struct2>::uninit();
38+
let base_ptr: *const Struct2 = &uninit as *const _ as *const Struct2;
39+
let field_ptr = unsafe { &(*base_ptr).field as *const u8 };
40+
unsafe { (base_ptr as *const u8).offset_from(field_ptr) }
41+
};
42+
3643
fn main() {
3744
assert_eq!(OFFSET, 0);
3845
assert_eq!(OFFSET_2, 1);
46+
assert_eq!(OVERFLOW, -1);
3947
}

src/test/ui/consts/offset_from_ub.rs

-9
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,4 @@ pub const NOT_MULTIPLE_OF_SIZE: usize = {
3232
offset as usize
3333
};
3434

35-
pub const OVERFLOW: usize = {
36-
//~^ NOTE
37-
let uninit = std::mem::MaybeUninit::<Struct>::uninit();
38-
let base_ptr: *const Struct = &uninit as *const _ as *const Struct;
39-
let field_ptr = unsafe { &(*base_ptr).field as *const u8 };
40-
let offset = unsafe { (base_ptr as *const u8).offset_from(field_ptr) };
41-
offset as usize
42-
};
43-
4435
fn main() {}

src/test/ui/consts/offset_from_ub.stderr

+1-21
Original file line numberDiff line numberDiff line change
@@ -57,25 +57,5 @@ LL | | offset as usize
5757
LL | | };
5858
| |__-
5959

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-
| second argument to `ptr_offset_from` must be smaller than first
67-
| inside call to `std::ptr::<impl *const u8>::offset_from` at $DIR/offset_from_ub.rs:40:27
68-
|
69-
::: $DIR/offset_from_ub.rs:35:1
70-
|
71-
LL | / pub const OVERFLOW: usize = {
72-
LL | |
73-
LL | | let uninit = std::mem::MaybeUninit::<Struct>::uninit();
74-
LL | | let base_ptr: *const Struct = &uninit as *const _ as *const Struct;
75-
... |
76-
LL | | offset as usize
77-
LL | | };
78-
| |__-
79-
80-
error: aborting due to 4 previous errors
60+
error: aborting due to 3 previous errors
8161

0 commit comments

Comments
 (0)