Skip to content

Commit d02543a

Browse files
committed
fmt, tweak messages and bless
1 parent f5efb68 commit d02543a

14 files changed

+88
-70
lines changed

src/librustc/mir/interpret/error.rs

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use rustc_hir as hir;
1414
use rustc_macros::HashStable;
1515
use rustc_session::CtfeBacktrace;
1616
use rustc_span::{Pos, Span, def_id::DefId};
17-
use std::{any::Any, env, fmt};
17+
use std::{any::Any, fmt};
1818

1919
#[derive(Debug, Copy, Clone, PartialEq, Eq, HashStable, RustcEncodable, RustcDecodable)]
2020
pub enum ErrorHandled {
@@ -326,7 +326,10 @@ pub enum UndefinedBehaviorInfo {
326326
/// An enum discriminant was set to a value which was outside the range of valid values.
327327
InvalidDiscriminant(ScalarMaybeUndef),
328328
/// A slice/array index projection went out-of-bounds.
329-
BoundsCheckFailed { len: u64, index: u64 },
329+
BoundsCheckFailed {
330+
len: u64,
331+
index: u64,
332+
},
330333
/// Something was divided by 0 (x / 0).
331334
DivisionByZero,
332335
/// Something was "remainded" by 0 (x % 0).
@@ -395,16 +398,14 @@ impl fmt::Debug for UndefinedBehaviorInfo {
395398
"reading a null-terminated string starting at {:?} with no null found before end of allocation",
396399
p,
397400
),
398-
PointerUseAfterFree(a) => write!(
399-
f,
400-
"pointer to allocation {:?} was dereferenced after allocation got freed",
401-
a
402-
),
401+
PointerUseAfterFree(a) => {
402+
write!(f, "pointer to {:?} was dereferenced after this allocation got freed", a)
403+
}
403404
InvalidNullPointerUsage => write!(f, "invalid use of NULL pointer"),
404405
PointerOutOfBounds { ptr, msg, allocation_size } => write!(
405406
f,
406407
"{} failed: pointer must be in-bounds at offset {}, \
407-
but is outside bounds of allocation {} which has size {}",
408+
but is outside bounds of {} which has size {}",
408409
msg,
409410
ptr.offset.bytes(),
410411
ptr.alloc_id,
@@ -416,16 +417,23 @@ impl fmt::Debug for UndefinedBehaviorInfo {
416417
has.bytes(),
417418
required.bytes()
418419
),
419-
WriteToReadOnly(a) => write!(f, "writing to read-only allocation {:?}", a),
420+
WriteToReadOnly(a) => write!(f, "writing to {:?} which is read-only", a),
420421
InvalidFunctionPointer(p) => {
421422
write!(f, "using {:?} as function pointer but it does not point to a function", p)
422423
}
423-
DerefFunctionPointer(a) => write!(f, "accessing data behind function pointer allocation {:?}", a),
424+
DerefFunctionPointer(a) => write!(f, "accessing {:?} which contains a function", a),
424425
ValidationFailure(ref err) => write!(f, "type validation failed: {}", err),
425426
InvalidBool(b) => write!(f, "interpreting an invalid 8-bit value as a bool: {}", b),
426427
InvalidChar(c) => write!(f, "interpreting an invalid 32-bit value as a char: {}", c),
427-
InvalidUndefBytes(Some(p)) => write!(f, "reading uninitialized memory at {:?}, but this operation requires initialized memory", p),
428-
InvalidUndefBytes(None) => write!(f, "using uninitialized data, but this operation requires initialized memory"),
428+
InvalidUndefBytes(Some(p)) => write!(
429+
f,
430+
"reading uninitialized memory at {:?}, but this operation requires initialized memory",
431+
p
432+
),
433+
InvalidUndefBytes(None) => write!(
434+
f,
435+
"using uninitialized data, but this operation requires initialized memory"
436+
),
429437
DeadLocal => write!(f, "accessing a dead local variable"),
430438
ReadFromReturnPlace => write!(f, "tried to read from the return place"),
431439
}
@@ -472,21 +480,18 @@ impl fmt::Debug for UnsupportedOpInfo {
472480
ConstPropUnsupported(ref msg) => {
473481
write!(f, "Constant propagation encountered an unsupported situation: {}", msg)
474482
}
475-
ReadForeignStatic(did) => write!(f, "tried to read from foreign (extern) static {:?}", did),
483+
ReadForeignStatic(did) => {
484+
write!(f, "tried to read from foreign (extern) static {:?}", did)
485+
}
476486
NoMirFor(did) => write!(f, "could not load MIR for {:?}", did),
477487
ModifiedStatic => write!(
478488
f,
479489
"tried to modify a static's initial value from another static's \
480490
initializer"
481491
),
482492

483-
ReadPointerAsBytes => write!(
484-
f,
485-
"unable to turn this pointer into raw bytes",
486-
),
487-
ReadBytesAsPointer => {
488-
write!(f, "unable to turn these bytes into a pointer")
489-
}
493+
ReadPointerAsBytes => write!(f, "unable to turn this pointer into raw bytes",),
494+
ReadBytesAsPointer => write!(f, "unable to turn these bytes into a pointer"),
490495
}
491496
}
492497
}

src/librustc_mir/interpret/memory.rs

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,10 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
215215
kind: MemoryKind<M::MemoryKinds>,
216216
) -> InterpResult<'tcx, Pointer<M::PointerTag>> {
217217
if ptr.offset.bytes() != 0 {
218-
throw_ub_format!("reallocating {:?} which does not point to the beginning of an object", ptr);
218+
throw_ub_format!(
219+
"reallocating {:?} which does not point to the beginning of an object",
220+
ptr
221+
);
219222
}
220223

221224
// For simplicities' sake, we implement reallocate as "alloc, copy, dealloc".
@@ -251,7 +254,10 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
251254
trace!("deallocating: {}", ptr.alloc_id);
252255

253256
if ptr.offset.bytes() != 0 {
254-
throw_ub_format!("deallocating {:?} which does not point to the beginning of an object", ptr);
257+
throw_ub_format!(
258+
"deallocating {:?} which does not point to the beginning of an object",
259+
ptr
260+
);
255261
}
256262

257263
let (alloc_kind, mut alloc) = match self.alloc_map.remove(&ptr.alloc_id) {
@@ -260,22 +266,30 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
260266
// Deallocating static memory -- always an error
261267
return Err(match self.tcx.alloc_map.lock().get(ptr.alloc_id) {
262268
Some(GlobalAlloc::Function(..)) => err_ub_format!("deallocating a function"),
263-
Some(GlobalAlloc::Static(..)) | Some(GlobalAlloc::Memory(..)) =>
264-
err_ub_format!("deallocating static memory"),
269+
Some(GlobalAlloc::Static(..)) | Some(GlobalAlloc::Memory(..)) => {
270+
err_ub_format!("deallocating static memory")
271+
}
265272
None => err_ub!(PointerUseAfterFree(ptr.alloc_id)),
266273
}
267274
.into());
268275
}
269276
};
270277

271278
if alloc_kind != kind {
272-
throw_ub_format!("deallocating `{:?}` memory using `{:?}` deallocation operation", alloc_kind, kind);
279+
throw_ub_format!(
280+
"deallocating `{:?}` memory using `{:?}` deallocation operation",
281+
alloc_kind,
282+
kind
283+
);
273284
}
274285
if let Some((size, align)) = old_size_and_align {
275286
if size != alloc.size || align != alloc.align {
276287
throw_ub_format!(
277288
"incorrect layout on deallocation: allocation has size {} and alignment {}, but gave size {} and alignment {}",
278-
alloc.size.bytes(), alloc.align.bytes(), size.bytes(), align.bytes(),
289+
alloc.size.bytes(),
290+
alloc.align.bytes(),
291+
size.bytes(),
292+
align.bytes(),
279293
)
280294
}
281295
}
@@ -370,7 +384,8 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
370384
// It is sufficient to check this for the end pointer. The addition
371385
// checks for overflow.
372386
let end_ptr = ptr.offset(size, self)?;
373-
if end_ptr.offset > allocation_size { // equal is okay!
387+
if end_ptr.offset > allocation_size {
388+
// equal is okay!
374389
throw_ub!(PointerOutOfBounds { ptr: end_ptr.erase_tag(), msg, allocation_size })
375390
}
376391
// Test align. Check this last; if both bounds and alignment are violated

src/librustc_mir/interpret/validity.rs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -356,18 +356,16 @@ impl<'rt, 'mir, 'tcx, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, 'tcx, M
356356
err_ub!(InvalidNullPointerUsage) => {
357357
throw_validation_failure!(format_args!("a NULL {}", kind), self.path)
358358
}
359-
err_ub!(AlignmentCheckFailed { required, has }) => {
360-
throw_validation_failure!(
361-
format_args!(
362-
"an unaligned {} \
359+
err_ub!(AlignmentCheckFailed { required, has }) => throw_validation_failure!(
360+
format_args!(
361+
"an unaligned {} \
363362
(required {} byte alignment but found {})",
364-
kind,
365-
required.bytes(),
366-
has.bytes()
367-
),
368-
self.path
369-
)
370-
}
363+
kind,
364+
required.bytes(),
365+
has.bytes()
366+
),
367+
self.path
368+
),
371369
err_unsup!(ReadBytesAsPointer) => throw_validation_failure!(
372370
format_args!("a dangling {} (created from integer)", kind),
373371
self.path

0 commit comments

Comments
 (0)