Skip to content

Commit abdba81

Browse files
committed
use CheckInAllocMsg::PointerArithmeticTest for ptr_offset error
1 parent d620ae1 commit abdba81

File tree

8 files changed

+14
-10
lines changed

8 files changed

+14
-10
lines changed

compiler/rustc_middle/src/mir/interpret/error.rs

+3
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,9 @@ impl fmt::Display for UndefinedBehaviorInfo<'_> {
306306
ptr.alloc_id,
307307
allocation_size.bytes()
308308
),
309+
DanglingIntPointer(_, CheckInAllocMsg::InboundsTest) => {
310+
write!(f, "null pointer is not allowed for this operation")
311+
}
309312
DanglingIntPointer(i, msg) => {
310313
write!(f, "{} failed: 0x{:x} is not a valid pointer", msg, i)
311314
}

compiler/rustc_mir/src/interpret/intrinsics.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
526526
min_ptr,
527527
Size::from_bytes(size),
528528
None,
529-
CheckInAllocMsg::InboundsTest,
529+
CheckInAllocMsg::PointerArithmeticTest,
530530
)?;
531531
Ok(offset_ptr)
532532
}

compiler/rustc_mir/src/interpret/machine.rs

+1
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,7 @@ pub trait Machine<'mir, 'tcx>: Sized {
369369
) -> InterpResult<'tcx, Pointer<Self::PointerTag>> {
370370
Err((if int == 0 {
371371
// This is UB, seriously.
372+
// (`DanglingIntPointer` with these exact arguments has special printing code.)
372373
err_ub!(DanglingIntPointer(0, CheckInAllocMsg::InboundsTest))
373374
} else {
374375
// This is just something we cannot support during const-eval.

compiler/rustc_mir/src/interpret/validity.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, '
330330
vtable,
331331
3 * self.ecx.tcx.data_layout.pointer_size, // drop, size, align
332332
Some(self.ecx.tcx.data_layout.pointer_align.abi),
333-
CheckInAllocMsg::InboundsTest,
333+
CheckInAllocMsg::InboundsTest, // will anyway be replaced by validity message
334334
),
335335
self.path,
336336
err_ub!(DanglingIntPointer(..)) |
@@ -416,7 +416,7 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, '
416416
place.ptr,
417417
size,
418418
Some(align),
419-
CheckInAllocMsg::InboundsTest,
419+
CheckInAllocMsg::InboundsTest, // will anyway be replaced by validity message
420420
),
421421
self.path,
422422
err_ub!(AlignmentCheckFailed { required, has }) =>

src/test/ui/consts/const-eval/ub-wide-ptr.64bit.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ error[E0080]: could not evaluate static initializer
296296
--> $DIR/ub-wide-ptr.rs:135:5
297297
|
298298
LL | mem::transmute::<_, &dyn Trait>((&92u8, 0usize))
299-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ inbounds test failed: 0x0 is not a valid pointer
299+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ null pointer is not allowed for this operation
300300

301301
error[E0080]: could not evaluate static initializer
302302
--> $DIR/ub-wide-ptr.rs:139:5

src/test/ui/consts/offset_from_ub.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ error: any use of this value will cause an error
7474
LL | unsafe { intrinsics::ptr_offset_from(self, origin) }
7575
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
7676
| |
77-
| inbounds test failed: 0x0 is not a valid pointer
77+
| null pointer is not allowed for this operation
7878
| inside `ptr::const_ptr::<impl *const u8>::offset_from` at $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
7979
| inside `OFFSET_FROM_NULL` at $DIR/offset_from_ub.rs:36:14
8080
|

src/test/ui/consts/offset_ub.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ error: any use of this value will cause an error
2323
LL | unsafe { intrinsics::offset(self, count) }
2424
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2525
| |
26-
| inbounds test failed: pointer must be in-bounds at offset 2, but is outside bounds of allocN which has size 1
26+
| pointer arithmetic failed: pointer must be in-bounds at offset 2, but is outside bounds of allocN which has size 1
2727
| inside `ptr::const_ptr::<impl *const u8>::offset` at $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
2828
| inside `AFTER_END` at $DIR/offset_ub.rs:7:43
2929
|
@@ -41,7 +41,7 @@ error: any use of this value will cause an error
4141
LL | unsafe { intrinsics::offset(self, count) }
4242
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4343
| |
44-
| inbounds test failed: pointer must be in-bounds at offset 101, but is outside bounds of allocN which has size 100
44+
| pointer arithmetic failed: pointer must be in-bounds at offset 101, but is outside bounds of allocN which has size 100
4545
| inside `ptr::const_ptr::<impl *const u8>::offset` at $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
4646
| inside `AFTER_ARRAY` at $DIR/offset_ub.rs:8:45
4747
|
@@ -131,7 +131,7 @@ error: any use of this value will cause an error
131131
LL | unsafe { intrinsics::offset(self, count) }
132132
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
133133
| |
134-
| inbounds test failed: pointer must be in-bounds at offset 1, but is outside bounds of allocN which has size 0
134+
| pointer arithmetic failed: pointer must be in-bounds at offset 1, but is outside bounds of allocN which has size 0
135135
| inside `ptr::const_ptr::<impl *const u8>::offset` at $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
136136
| inside `ZERO_SIZED_ALLOC` at $DIR/offset_ub.rs:15:50
137137
|
@@ -167,7 +167,7 @@ error: any use of this value will cause an error
167167
LL | unsafe { intrinsics::offset(self, count) }
168168
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
169169
| |
170-
| inbounds test failed: 0x0 is not a valid pointer
170+
| pointer arithmetic failed: 0x0 is not a valid pointer
171171
| inside `ptr::const_ptr::<impl *const u8>::offset` at $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
172172
| inside `NULL_OFFSET_ZERO` at $DIR/offset_ub.rs:19:50
173173
|

src/test/ui/consts/ptr_comparisons.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error: any use of this value will cause an error
44
LL | unsafe { intrinsics::offset(self, count) }
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
66
| |
7-
| inbounds test failed: pointer must be in-bounds at offset $TWO_WORDS, but is outside bounds of alloc2 which has size $WORD
7+
| pointer arithmetic failed: pointer must be in-bounds at offset $TWO_WORDS, but is outside bounds of alloc2 which has size $WORD
88
| inside `ptr::const_ptr::<impl *const usize>::offset` at $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
99
| inside `_` at $DIR/ptr_comparisons.rs:61:34
1010
|

0 commit comments

Comments
 (0)