@@ -22,28 +22,23 @@ use super::{
22
22
23
23
macro_rules! throw_validation_failure {
24
24
( $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) )
35
33
} } ;
36
34
( $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) )
47
42
} } ;
48
43
}
49
44
@@ -60,7 +55,7 @@ macro_rules! try_validation {
60
55
Ok ( x) => x,
61
56
Err ( _) => throw_validation_failure!( $what, $where) ,
62
57
}
63
- } }
58
+ } } ;
64
59
}
65
60
66
61
/// 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>
113
108
}
114
109
115
110
/// Format a path
116
- fn path_format ( path : & Vec < PathElem > ) -> String {
111
+ fn write_path ( out : & mut String , path : & Vec < PathElem > ) {
117
112
use self :: PathElem :: * ;
118
113
119
- let mut out = String :: new ( ) ;
120
114
for elem in path. iter ( ) {
121
115
match elem {
122
116
Field ( name) => write ! ( out, ".{}" , name) ,
@@ -135,7 +129,6 @@ fn path_format(path: &Vec<PathElem>) -> String {
135
129
DynDowncast => write ! ( out, ".<dyn-downcast>" ) ,
136
130
} . unwrap ( )
137
131
}
138
- out
139
132
}
140
133
141
134
// 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>
428
421
err_unsup ! ( InvalidNullPointerUsage ) =>
429
422
throw_validation_failure ! ( "NULL reference" , self . path) ,
430
423
err_unsup ! ( AlignmentCheckFailed { required, has } ) =>
431
- throw_validation_failure ! ( format !( "unaligned reference \
424
+ throw_validation_failure ! ( format_args !( "unaligned reference \
432
425
(required {} byte alignment but found {})",
433
426
required. bytes( ) , has. bytes( ) ) , self . path) ,
434
427
err_unsup ! ( ReadBytesAsPointer ) =>
@@ -519,7 +512,7 @@ impl<'rt, 'mir, 'tcx, M: Machine<'mir, 'tcx>> ValueVisitor<'mir, 'tcx, M>
519
512
let value = try_validation ! ( value. not_undef( ) ,
520
513
value,
521
514
self . path,
522
- format !(
515
+ format_args !(
523
516
"something {}" ,
524
517
wrapping_range_format( & layout. valid_range, max_hi) ,
525
518
)
@@ -532,7 +525,7 @@ impl<'rt, 'mir, 'tcx, M: Machine<'mir, 'tcx>> ValueVisitor<'mir, 'tcx, M>
532
525
throw_validation_failure ! (
533
526
"a potentially NULL pointer" ,
534
527
self . path,
535
- format !(
528
+ format_args !(
536
529
"something that cannot possibly fail to be {}" ,
537
530
wrapping_range_format( & layout. valid_range, max_hi)
538
531
)
@@ -545,7 +538,7 @@ impl<'rt, 'mir, 'tcx, M: Machine<'mir, 'tcx>> ValueVisitor<'mir, 'tcx, M>
545
538
throw_validation_failure ! (
546
539
"a pointer" ,
547
540
self . path,
548
- format !(
541
+ format_args !(
549
542
"something that cannot possibly fail to be {}" ,
550
543
wrapping_range_format( & layout. valid_range, max_hi)
551
544
)
@@ -562,7 +555,7 @@ impl<'rt, 'mir, 'tcx, M: Machine<'mir, 'tcx>> ValueVisitor<'mir, 'tcx, M>
562
555
throw_validation_failure ! (
563
556
bits,
564
557
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) )
566
559
)
567
560
}
568
561
}
0 commit comments