Skip to content

Commit

Permalink
fix null pointer error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
RalfJung committed May 7, 2021
1 parent ac888e8 commit 4dddc38
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
19 changes: 11 additions & 8 deletions compiler/rustc_middle/src/mir/interpret/error.rs
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.64bit.stderr
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
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 4dddc38

Please sign in to comment.