Skip to content

Commit bba8f99

Browse files
authored
Rollup merge of rust-lang#85034 - RalfJung:null-ptr, r=oli-obk
fix null pointer error messages I got the `match` in rust-lang#84972 wrong, this fixes that. Also avoid redundant "inbounds test failed:" prefix. Should fix rust-lang#85027 r? `@oli-obk`
2 parents 44bee53 + 6386656 commit bba8f99

File tree

4 files changed

+14
-11
lines changed

4 files changed

+14
-11
lines changed

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

+11-8
Original file line numberDiff line numberDiff line change
@@ -170,22 +170,25 @@ impl fmt::Display for InvalidProgramInfo<'_> {
170170
/// Details of why a pointer had to be in-bounds.
171171
#[derive(Debug, Copy, Clone, TyEncodable, TyDecodable, HashStable)]
172172
pub enum CheckInAllocMsg {
173+
/// We are access memory.
173174
MemoryAccessTest,
175+
/// We are doing pointer arithmetic.
174176
PointerArithmeticTest,
177+
/// None of the above -- generic/unspecific inbounds test.
175178
InboundsTest,
176179
}
177180

178181
impl fmt::Display for CheckInAllocMsg {
179182
/// When this is printed as an error the context looks like this
180-
/// "{test name} failed: pointer must be in-bounds at offset..."
183+
/// "{msg}pointer must be in-bounds at offset..."
181184
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
182185
write!(
183186
f,
184187
"{}",
185188
match *self {
186-
CheckInAllocMsg::MemoryAccessTest => "memory access",
187-
CheckInAllocMsg::PointerArithmeticTest => "pointer arithmetic",
188-
CheckInAllocMsg::InboundsTest => "inbounds test",
189+
CheckInAllocMsg::MemoryAccessTest => "memory access failed: ",
190+
CheckInAllocMsg::PointerArithmeticTest => "pointer arithmetic failed: ",
191+
CheckInAllocMsg::InboundsTest => "",
189192
}
190193
)
191194
}
@@ -299,18 +302,18 @@ impl fmt::Display for UndefinedBehaviorInfo<'_> {
299302
}
300303
PointerOutOfBounds { ptr, msg, allocation_size } => write!(
301304
f,
302-
"{} failed: pointer must be in-bounds at offset {}, \
305+
"{}pointer must be in-bounds at offset {}, \
303306
but is outside bounds of {} which has size {}",
304307
msg,
305308
ptr.offset.bytes(),
306309
ptr.alloc_id,
307310
allocation_size.bytes()
308311
),
309-
DanglingIntPointer(_, CheckInAllocMsg::InboundsTest) => {
310-
write!(f, "null pointer is not allowed for this operation")
312+
DanglingIntPointer(0, CheckInAllocMsg::InboundsTest) => {
313+
write!(f, "null pointer is not a valid pointer for this operation")
311314
}
312315
DanglingIntPointer(i, msg) => {
313-
write!(f, "{} failed: 0x{:x} is not a valid pointer", msg, i)
316+
write!(f, "{}0x{:x} is not a valid pointer", msg, i)
314317
}
315318
AlignmentCheckFailed { required, has } => write!(
316319
f,

src/test/ui/consts/const-eval/ub-wide-ptr.32bit.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-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ null pointer is not allowed for this operation
299+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ null pointer is not a valid pointer for this operation
300300

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

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-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ null pointer is not allowed for this operation
299+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ null pointer is not a valid pointer 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-
| null pointer is not allowed for this operation
77+
| null pointer is not a valid pointer 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
|

0 commit comments

Comments
 (0)