Skip to content

Commit 88e702a

Browse files
authored
Rollup merge of #67256 - RalfJung:reduce-allocs, r=oli-obk
Reduce allocs for validation errors This probably doesn't really matter, but I just felt like I had to do this... r? @oli-obk
2 parents a0be3a6 + 14b2436 commit 88e702a

File tree

1 file changed

+22
-29
lines changed

1 file changed

+22
-29
lines changed

src/librustc_mir/interpret/validity.rs

+22-29
Original file line numberDiff line numberDiff line change
@@ -22,28 +22,23 @@ use super::{
2222

2323
macro_rules! throw_validation_failure {
2424
($what:expr, $where:expr, $details:expr) => {{
25-
let where_ = path_format(&$where);
26-
let where_ = if where_.is_empty() {
27-
String::new()
28-
} else {
29-
format!(" at {}", where_)
30-
};
31-
throw_unsup!(ValidationFailure(format!(
32-
"encountered {}{}, but expected {}",
33-
$what, where_, $details,
34-
)))
25+
let mut msg = format!("encountered {}", $what);
26+
let where_ = &$where;
27+
if !where_.is_empty() {
28+
msg.push_str(" at ");
29+
write_path(&mut msg, where_);
30+
}
31+
write!(&mut msg, ", but expected {}", $details).unwrap();
32+
throw_unsup!(ValidationFailure(msg))
3533
}};
3634
($what:expr, $where:expr) => {{
37-
let where_ = path_format(&$where);
38-
let where_ = if where_.is_empty() {
39-
String::new()
40-
} else {
41-
format!(" at {}", where_)
42-
};
43-
throw_unsup!(ValidationFailure(format!(
44-
"encountered {}{}",
45-
$what, where_,
46-
)))
35+
let mut msg = format!("encountered {}", $what);
36+
let where_ = &$where;
37+
if !where_.is_empty() {
38+
msg.push_str(" at ");
39+
write_path(&mut msg, where_);
40+
}
41+
throw_unsup!(ValidationFailure(msg))
4742
}};
4843
}
4944

@@ -60,7 +55,7 @@ macro_rules! try_validation {
6055
Ok(x) => x,
6156
Err(_) => throw_validation_failure!($what, $where),
6257
}
63-
}}
58+
}};
6459
}
6560

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

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

119-
let mut out = String::new();
120114
for elem in path.iter() {
121115
match elem {
122116
Field(name) => write!(out, ".{}", name),
@@ -135,7 +129,6 @@ fn path_format(path: &Vec<PathElem>) -> String {
135129
DynDowncast => write!(out, ".<dyn-downcast>"),
136130
}.unwrap()
137131
}
138-
out
139132
}
140133

141134
// Test if a range that wraps at overflow contains `test`
@@ -428,7 +421,7 @@ impl<'rt, 'mir, 'tcx, M: Machine<'mir, 'tcx>> ValueVisitor<'mir, 'tcx, M>
428421
err_unsup!(InvalidNullPointerUsage) =>
429422
throw_validation_failure!("NULL reference", self.path),
430423
err_unsup!(AlignmentCheckFailed { required, has }) =>
431-
throw_validation_failure!(format!("unaligned reference \
424+
throw_validation_failure!(format_args!("unaligned reference \
432425
(required {} byte alignment but found {})",
433426
required.bytes(), has.bytes()), self.path),
434427
err_unsup!(ReadBytesAsPointer) =>
@@ -519,7 +512,7 @@ impl<'rt, 'mir, 'tcx, M: Machine<'mir, 'tcx>> ValueVisitor<'mir, 'tcx, M>
519512
let value = try_validation!(value.not_undef(),
520513
value,
521514
self.path,
522-
format!(
515+
format_args!(
523516
"something {}",
524517
wrapping_range_format(&layout.valid_range, max_hi),
525518
)
@@ -532,7 +525,7 @@ impl<'rt, 'mir, 'tcx, M: Machine<'mir, 'tcx>> ValueVisitor<'mir, 'tcx, M>
532525
throw_validation_failure!(
533526
"a potentially NULL pointer",
534527
self.path,
535-
format!(
528+
format_args!(
536529
"something that cannot possibly fail to be {}",
537530
wrapping_range_format(&layout.valid_range, max_hi)
538531
)
@@ -545,7 +538,7 @@ impl<'rt, 'mir, 'tcx, M: Machine<'mir, 'tcx>> ValueVisitor<'mir, 'tcx, M>
545538
throw_validation_failure!(
546539
"a pointer",
547540
self.path,
548-
format!(
541+
format_args!(
549542
"something that cannot possibly fail to be {}",
550543
wrapping_range_format(&layout.valid_range, max_hi)
551544
)
@@ -562,7 +555,7 @@ impl<'rt, 'mir, 'tcx, M: Machine<'mir, 'tcx>> ValueVisitor<'mir, 'tcx, M>
562555
throw_validation_failure!(
563556
bits,
564557
self.path,
565-
format!("something {}", wrapping_range_format(&layout.valid_range, max_hi))
558+
format_args!("something {}", wrapping_range_format(&layout.valid_range, max_hi))
566559
)
567560
}
568561
}

0 commit comments

Comments
 (0)