diff options
| author | Ralf Jung <post@ralfj.de> | 2019-12-12 16:48:46 +0100 |
|---|---|---|
| committer | Ralf Jung <post@ralfj.de> | 2019-12-12 16:48:46 +0100 |
| commit | 14b2436993461c3c703baab29a9d9579bea461fc (patch) | |
| tree | cc07593a86f12766d0a8ff204b8ff48604407bb9 | |
| parent | 3ddc0278d36179dc485a093b5bc2fc2347a746da (diff) | |
| download | rust-14b2436993461c3c703baab29a9d9579bea461fc.tar.gz rust-14b2436993461c3c703baab29a9d9579bea461fc.zip | |
avoid more intermediate allocations in validation errors
| -rw-r--r-- | src/librustc_mir/interpret/validity.rs | 39 |
1 files changed, 16 insertions, 23 deletions
diff --git a/src/librustc_mir/interpret/validity.rs b/src/librustc_mir/interpret/validity.rs index e4ac9bfec02..1dbcfe5588e 100644 --- a/src/librustc_mir/interpret/validity.rs +++ b/src/librustc_mir/interpret/validity.rs @@ -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)) }}; } @@ -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), @@ -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` |
