Skip to content

Commit 14b2436

Browse files
committed
avoid more intermediate allocations in validation errors
1 parent 3ddc027 commit 14b2436

File tree

1 file changed

+16
-23
lines changed

1 file changed

+16
-23
lines changed

src/librustc_mir/interpret/validity.rs

+16-23
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

@@ -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`

0 commit comments

Comments
 (0)