Skip to content

Commit

Permalink
Rollup merge of rust-lang#85034 - RalfJung:null-ptr, r=oli-obk
Browse files Browse the repository at this point in the history
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`
  • Loading branch information
Dylan-DPC authored May 7, 2021
2 parents 44bee53 + 6386656 commit bba8f99
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 11 deletions.
19 changes: 11 additions & 8 deletions compiler/rustc_middle/src/mir/interpret/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,22 +170,25 @@ impl fmt::Display for InvalidProgramInfo<'_> {
/// Details of why a pointer had to be in-bounds.
#[derive(Debug, Copy, Clone, TyEncodable, TyDecodable, HashStable)]
pub enum CheckInAllocMsg {
/// We are access memory.
MemoryAccessTest,
/// We are doing pointer arithmetic.
PointerArithmeticTest,
/// None of the above -- generic/unspecific inbounds test.
InboundsTest,
}

impl fmt::Display for CheckInAllocMsg {
/// When this is printed as an error the context looks like this
/// "{test name} failed: pointer must be in-bounds at offset..."
/// "{msg}pointer must be in-bounds at offset..."
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"{}",
match *self {
CheckInAllocMsg::MemoryAccessTest => "memory access",
CheckInAllocMsg::PointerArithmeticTest => "pointer arithmetic",
CheckInAllocMsg::InboundsTest => "inbounds test",
CheckInAllocMsg::MemoryAccessTest => "memory access failed: ",
CheckInAllocMsg::PointerArithmeticTest => "pointer arithmetic failed: ",
CheckInAllocMsg::InboundsTest => "",
}
)
}
Expand Down Expand Up @@ -299,18 +302,18 @@ impl fmt::Display for UndefinedBehaviorInfo<'_> {
}
PointerOutOfBounds { ptr, msg, allocation_size } => write!(
f,
"{} failed: pointer must be in-bounds at offset {}, \
"{}pointer must be in-bounds at offset {}, \
but is outside bounds of {} which has size {}",
msg,
ptr.offset.bytes(),
ptr.alloc_id,
allocation_size.bytes()
),
DanglingIntPointer(_, CheckInAllocMsg::InboundsTest) => {
write!(f, "null pointer is not allowed for this operation")
DanglingIntPointer(0, CheckInAllocMsg::InboundsTest) => {
write!(f, "null pointer is not a valid pointer for this operation")
}
DanglingIntPointer(i, msg) => {
write!(f, "{} failed: 0x{:x} is not a valid pointer", msg, i)
write!(f, "{}0x{:x} is not a valid pointer", msg, i)
}
AlignmentCheckFailed { required, has } => write!(
f,
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/consts/const-eval/ub-wide-ptr.32bit.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ error[E0080]: could not evaluate static initializer
--> $DIR/ub-wide-ptr.rs:135:5
|
LL | mem::transmute::<_, &dyn Trait>((&92u8, 0usize))
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ null pointer is not allowed for this operation
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ null pointer is not a valid pointer for this operation

error[E0080]: could not evaluate static initializer
--> $DIR/ub-wide-ptr.rs:139:5
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/consts/const-eval/ub-wide-ptr.64bit.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ error[E0080]: could not evaluate static initializer
--> $DIR/ub-wide-ptr.rs:135:5
|
LL | mem::transmute::<_, &dyn Trait>((&92u8, 0usize))
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ null pointer is not allowed for this operation
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ null pointer is not a valid pointer for this operation

error[E0080]: could not evaluate static initializer
--> $DIR/ub-wide-ptr.rs:139:5
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/consts/offset_from_ub.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ error: any use of this value will cause an error
LL | unsafe { intrinsics::ptr_offset_from(self, origin) }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
| null pointer is not allowed for this operation
| null pointer is not a valid pointer for this operation
| inside `ptr::const_ptr::<impl *const u8>::offset_from` at $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
| inside `OFFSET_FROM_NULL` at $DIR/offset_from_ub.rs:36:14
|
Expand Down

0 comments on commit bba8f99

Please sign in to comment.