Skip to content

Commit

Permalink
Rollup merge of rust-lang#67256 - RalfJung:reduce-allocs, r=oli-obk
Browse files Browse the repository at this point in the history
Reduce allocs for validation errors

This probably doesn't really matter, but I just felt like I had to do this...

r? @oli-obk
  • Loading branch information
Centril committed Dec 13, 2019
2 parents a0be3a6 + 14b2436 commit 88e702a
Showing 1 changed file with 22 additions and 29 deletions.
51 changes: 22 additions & 29 deletions src/librustc_mir/interpret/validity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,23 @@ use super::{

macro_rules! throw_validation_failure {
($what:expr, $where:expr, $details:expr) => {{
let where_ = path_format(&$where);
let where_ = if where_.is_empty() {
String::new()
} else {
format!(" at {}", where_)
};
throw_unsup!(ValidationFailure(format!(
"encountered {}{}, but expected {}",
$what, where_, $details,
)))
let mut msg = format!("encountered {}", $what);
let where_ = &$where;
if !where_.is_empty() {
msg.push_str(" at ");
write_path(&mut msg, where_);
}
write!(&mut msg, ", but expected {}", $details).unwrap();
throw_unsup!(ValidationFailure(msg))
}};
($what:expr, $where:expr) => {{
let where_ = path_format(&$where);
let where_ = if where_.is_empty() {
String::new()
} else {
format!(" at {}", where_)
};
throw_unsup!(ValidationFailure(format!(
"encountered {}{}",
$what, where_,
)))
let mut msg = format!("encountered {}", $what);
let where_ = &$where;
if !where_.is_empty() {
msg.push_str(" at ");
write_path(&mut msg, where_);
}
throw_unsup!(ValidationFailure(msg))
}};
}

Expand All @@ -60,7 +55,7 @@ macro_rules! try_validation {
Ok(x) => x,
Err(_) => throw_validation_failure!($what, $where),
}
}}
}};
}

/// We want to show a nice path to the invalid field for diagnostics,
Expand Down Expand Up @@ -113,10 +108,9 @@ impl<T: Copy + Eq + Hash + std::fmt::Debug, PATH: Default> RefTracking<T, PATH>
}

/// Format a path
fn path_format(path: &Vec<PathElem>) -> String {
fn write_path(out: &mut String, path: &Vec<PathElem>) {
use self::PathElem::*;

let mut out = String::new();
for elem in path.iter() {
match elem {
Field(name) => write!(out, ".{}", name),
Expand All @@ -135,7 +129,6 @@ fn path_format(path: &Vec<PathElem>) -> String {
DynDowncast => write!(out, ".<dyn-downcast>"),
}.unwrap()
}
out
}

// Test if a range that wraps at overflow contains `test`
Expand Down Expand Up @@ -428,7 +421,7 @@ impl<'rt, 'mir, 'tcx, M: Machine<'mir, 'tcx>> ValueVisitor<'mir, 'tcx, M>
err_unsup!(InvalidNullPointerUsage) =>
throw_validation_failure!("NULL reference", self.path),
err_unsup!(AlignmentCheckFailed { required, has }) =>
throw_validation_failure!(format!("unaligned reference \
throw_validation_failure!(format_args!("unaligned reference \
(required {} byte alignment but found {})",
required.bytes(), has.bytes()), self.path),
err_unsup!(ReadBytesAsPointer) =>
Expand Down Expand Up @@ -519,7 +512,7 @@ impl<'rt, 'mir, 'tcx, M: Machine<'mir, 'tcx>> ValueVisitor<'mir, 'tcx, M>
let value = try_validation!(value.not_undef(),
value,
self.path,
format!(
format_args!(
"something {}",
wrapping_range_format(&layout.valid_range, max_hi),
)
Expand All @@ -532,7 +525,7 @@ impl<'rt, 'mir, 'tcx, M: Machine<'mir, 'tcx>> ValueVisitor<'mir, 'tcx, M>
throw_validation_failure!(
"a potentially NULL pointer",
self.path,
format!(
format_args!(
"something that cannot possibly fail to be {}",
wrapping_range_format(&layout.valid_range, max_hi)
)
Expand All @@ -545,7 +538,7 @@ impl<'rt, 'mir, 'tcx, M: Machine<'mir, 'tcx>> ValueVisitor<'mir, 'tcx, M>
throw_validation_failure!(
"a pointer",
self.path,
format!(
format_args!(
"something that cannot possibly fail to be {}",
wrapping_range_format(&layout.valid_range, max_hi)
)
Expand All @@ -562,7 +555,7 @@ impl<'rt, 'mir, 'tcx, M: Machine<'mir, 'tcx>> ValueVisitor<'mir, 'tcx, M>
throw_validation_failure!(
bits,
self.path,
format!("something {}", wrapping_range_format(&layout.valid_range, max_hi))
format_args!("something {}", wrapping_range_format(&layout.valid_range, max_hi))
)
}
}
Expand Down

0 comments on commit 88e702a

Please sign in to comment.