Skip to content

Commit 567201e

Browse files
committed
auto merge of #12626 : alexcrichton/rust/assert-eq, r=thestinger
Formatting via reflection has been a little questionable for some time now, and it's a little unfortunate that one of the standard macros will silently use reflection when you weren't expecting it. This adds small bits of code bloat to libraries, as well as not always being necessary. In light of this information, this commit switches assert_eq!() to using {} in the error message instead of {:?}. In updating existing code, there were a few error cases that I encountered: * It's impossible to define Show for [T, ..N]. I think DST will alleviate this because we can define Show for [T]. * A few types here and there just needed a #[deriving(Show)] * Type parameters needed a Show bound, I often moved this to `assert!(a == b)` * `Path` doesn't implement `Show`, so assert_eq!() cannot be used on two paths. I don't think this is much of a regression though because {:?} on paths looks awful (it's a byte array). Concretely speaking, this shaved 10K off a 656K binary. Not a lot, but sometime significant for smaller binaries.
2 parents 123eb4e + 02882fb commit 567201e

File tree

97 files changed

+354
-301
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

97 files changed

+354
-301
lines changed

src/doc/guide-container.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ the trailing underscore is a workaround for issue #5898 and will be removed.
384384
~~~
385385
let mut ys = [1, 2, 3, 4, 5];
386386
ys.mut_iter().reverse_();
387-
assert_eq!(ys, [5, 4, 3, 2, 1]);
387+
assert!(ys == [5, 4, 3, 2, 1]);
388388
~~~
389389

390390
## Random-access iterators

src/doc/tutorial.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1688,7 +1688,7 @@ let x = Rc::new([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
16881688
let y = x.clone(); // a new owner
16891689
let z = x; // this moves `x` into `z`, rather than creating a new owner
16901690

1691-
assert_eq!(*z.borrow(), [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
1691+
assert!(*z.borrow() == [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
16921692

16931693
// the variable is mutable, but not the contents of the box
16941694
let mut a = Rc::new([10, 9, 8, 7, 6, 5, 4, 3, 2, 1]);
@@ -1707,7 +1707,7 @@ let x = Gc::new([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
17071707
let y = x; // does not perform a move, unlike with `Rc`
17081708
let z = x;
17091709

1710-
assert_eq!(*z.borrow(), [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
1710+
assert!(*z.borrow() == [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
17111711
~~~
17121712
17131713
With shared ownership, mutability cannot be inherited so the boxes are always immutable. However,

src/libcollections/bitv.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1542,7 +1542,7 @@ mod tests {
15421542

15431543
let mut b = a.clone();
15441544

1545-
assert_eq!(&a, &b);
1545+
assert!(a == b);
15461546

15471547
assert!(b.remove(&1));
15481548
assert!(a.contains(&1));
@@ -1561,7 +1561,7 @@ mod tests {
15611561
let mut r = rng();
15621562
let mut bitv = 0 as uint;
15631563
b.iter(|| {
1564-
bitv |= (1 << ((r.next_u32() as uint) % uint::BITS));
1564+
bitv |= 1 << ((r.next_u32() as uint) % uint::BITS);
15651565
&bitv
15661566
})
15671567
}

src/libcollections/dlist.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -982,11 +982,11 @@ mod tests {
982982
fn test_eq() {
983983
let mut n: DList<u8> = list_from([]);
984984
let mut m = list_from([]);
985-
assert_eq!(&n, &m);
985+
assert!(n == m);
986986
n.push_front(1);
987987
assert!(n != m);
988988
m.push_back(1);
989-
assert_eq!(&n, &m);
989+
assert!(n == m);
990990

991991
let n = list_from([2,3,4]);
992992
let m = list_from([1,2,3]);

src/libcollections/enum_set.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ mod test {
141141

142142
use enum_set::{EnumSet, CLike};
143143

144-
#[deriving(Eq)]
144+
#[deriving(Eq, Show)]
145145
#[repr(uint)]
146146
enum Foo {
147147
A, B, C

src/libcollections/hashmap.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1065,7 +1065,7 @@ mod test_map {
10651065
let mut observed = 0;
10661066
for (k, v) in m.iter() {
10671067
assert_eq!(*v, *k * 2);
1068-
observed |= (1 << *k);
1068+
observed |= 1 << *k;
10691069
}
10701070
assert_eq!(observed, 0xFFFF_FFFF);
10711071
}
@@ -1293,7 +1293,7 @@ mod test_set {
12931293
}
12941294
let mut observed = 0;
12951295
for k in a.iter() {
1296-
observed |= (1 << *k);
1296+
observed |= 1 << *k;
12971297
}
12981298
assert_eq!(observed, 0xFFFF_FFFF);
12991299
}

src/libcollections/list.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ mod tests {
153153
#[test]
154154
fn test_from_vec_empty() {
155155
let empty : list::List<int> = List::from_vec([]);
156-
assert_eq!(empty, Nil::<int>);
156+
assert!(empty == Nil::<int>);
157157
}
158158

159159
#[test]
@@ -222,8 +222,8 @@ mod tests {
222222

223223
#[test]
224224
fn test_append() {
225-
assert_eq!(List::from_vec([1, 2, 3, 4]),
226-
List::from_vec([1, 2]).append(List::from_vec([3, 4])));
225+
assert!(List::from_vec([1, 2, 3, 4]) ==
226+
List::from_vec([1, 2]).append(List::from_vec([3, 4])));
227227
}
228228

229229
#[test]
@@ -232,6 +232,6 @@ mod tests {
232232
let new_list = list.unshift(0);
233233
assert_eq!(list.len(), 1u);
234234
assert_eq!(new_list.len(), 2u);
235-
assert_eq!(new_list, List::from_vec([0, 1]));
235+
assert!(new_list == List::from_vec([0, 1]));
236236
}
237237
}

src/libcollections/lru_cache.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ mod tests {
277277

278278
fn assert_opt_eq<V: Eq>(opt: Option<&V>, v: V) {
279279
assert!(opt.is_some());
280-
assert_eq!(opt.unwrap(), &v);
280+
assert!(opt.unwrap() == &v);
281281
}
282282

283283
#[test]

src/libcollections/ringbuf.rs

+8-7
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,7 @@ mod tests {
409409
use deque::Deque;
410410
use std::clone::Clone;
411411
use std::cmp::Eq;
412+
use std::fmt::Show;
412413
use super::RingBuf;
413414

414415
#[test]
@@ -493,7 +494,7 @@ mod tests {
493494
}
494495

495496
#[cfg(test)]
496-
fn test_parameterized<T:Clone + Eq>(a: T, b: T, c: T, d: T) {
497+
fn test_parameterized<T:Clone + Eq + Show>(a: T, b: T, c: T, d: T) {
497498
let mut deq = RingBuf::new();
498499
assert_eq!(deq.len(), 0);
499500
deq.push_front(a.clone());
@@ -578,21 +579,21 @@ mod tests {
578579
})
579580
}
580581

581-
#[deriving(Clone, Eq)]
582+
#[deriving(Clone, Eq, Show)]
582583
enum Taggy {
583584
One(int),
584585
Two(int, int),
585586
Three(int, int, int),
586587
}
587588

588-
#[deriving(Clone, Eq)]
589+
#[deriving(Clone, Eq, Show)]
589590
enum Taggypar<T> {
590591
Onepar(int),
591592
Twopar(int, int),
592593
Threepar(int, int, int),
593594
}
594595

595-
#[deriving(Clone, Eq)]
596+
#[deriving(Clone, Eq, Show)]
596597
struct RecCy {
597598
x: int,
598599
y: int,
@@ -812,7 +813,7 @@ mod tests {
812813
#[test]
813814
fn test_eq() {
814815
let mut d = RingBuf::new();
815-
assert_eq!(&d, &RingBuf::with_capacity(0));
816+
assert!(d == RingBuf::with_capacity(0));
816817
d.push_front(137);
817818
d.push_front(17);
818819
d.push_front(42);
@@ -822,11 +823,11 @@ mod tests {
822823
e.push_back(17);
823824
e.push_back(137);
824825
e.push_back(137);
825-
assert_eq!(&e, &d);
826+
assert!(&e == &d);
826827
e.pop_back();
827828
e.push_back(0);
828829
assert!(e != d);
829830
e.clear();
830-
assert_eq!(e, RingBuf::new());
831+
assert!(e == RingBuf::new());
831832
}
832833
}

src/libgetopts/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1361,7 +1361,7 @@ mod tests {
13611361
aliases: ~[] }];
13621362
let verbose = reqopt("b", "banana", "some bananas", "VAL");
13631363
1364-
assert_eq!(verbose.long_to_short(), short);
1364+
assert!(verbose.long_to_short() == short);
13651365
}
13661366
13671367
#[test]

src/libnum/bigint.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -829,7 +829,7 @@ fn get_radix_base(radix: uint) -> (uint, uint) {
829829
}
830830

831831
/// A Sign is a `BigInt`'s composing element.
832-
#[deriving(Eq, Clone)]
832+
#[deriving(Eq, Clone, Show)]
833833
pub enum Sign { Minus, Zero, Plus }
834834

835835
impl Ord for Sign {

src/librustc/back/rpath.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ mod test {
215215
#[test]
216216
fn test_minimize1() {
217217
let res = minimize_rpaths([~"rpath1", ~"rpath2", ~"rpath1"]);
218-
assert_eq!(res.as_slice(), [~"rpath1", ~"rpath2"]);
218+
assert!(res.as_slice() == [~"rpath1", ~"rpath2"]);
219219
}
220220
221221
#[test]
@@ -224,7 +224,7 @@ mod test {
224224
~"1a", ~"4a", ~"1a",
225225
~"2", ~"3", ~"4a",
226226
~"3"]);
227-
assert_eq!(res.as_slice(), [~"1a", ~"2", ~"4a", ~"3"]);
227+
assert!(res.as_slice() == [~"1a", ~"2", ~"4a", ~"3"]);
228228
}
229229
230230
#[test]

src/librustc/middle/astencode.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1468,7 +1468,7 @@ fn roundtrip(in_item: Option<@ast::Item>) {
14681468
let ebml_doc = reader::Doc(wr.get_ref());
14691469
let out_item = decode_item_ast(ebml_doc);
14701470

1471-
assert_eq!(in_item, out_item);
1471+
assert!(in_item == out_item);
14721472
}
14731473

14741474
#[test]

src/librustc/middle/graph.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -349,19 +349,19 @@ mod test {
349349
start_data: N,
350350
expected_incoming: &[(E,N)],
351351
expected_outgoing: &[(E,N)]) {
352-
assert_eq!(graph.node_data(start_index), &start_data);
352+
assert!(graph.node_data(start_index) == &start_data);
353353

354354
let mut counter = 0;
355355
graph.each_incoming_edge(start_index, |edge_index, edge| {
356-
assert_eq!(graph.edge_data(edge_index), &edge.data);
356+
assert!(graph.edge_data(edge_index) == &edge.data);
357357
assert!(counter < expected_incoming.len());
358358
debug!("counter={:?} expected={:?} edge_index={:?} edge={:?}",
359359
counter, expected_incoming[counter], edge_index, edge);
360360
match expected_incoming[counter] {
361361
(ref e, ref n) => {
362-
assert_eq!(e, &edge.data);
363-
assert_eq!(n, graph.node_data(edge.source));
364-
assert_eq!(start_index, edge.target);
362+
assert!(e == &edge.data);
363+
assert!(n == graph.node_data(edge.source));
364+
assert!(start_index == edge.target);
365365
}
366366
}
367367
counter += 1;
@@ -371,15 +371,15 @@ mod test {
371371

372372
let mut counter = 0;
373373
graph.each_outgoing_edge(start_index, |edge_index, edge| {
374-
assert_eq!(graph.edge_data(edge_index), &edge.data);
374+
assert!(graph.edge_data(edge_index) == &edge.data);
375375
assert!(counter < expected_outgoing.len());
376376
debug!("counter={:?} expected={:?} edge_index={:?} edge={:?}",
377377
counter, expected_outgoing[counter], edge_index, edge);
378378
match expected_outgoing[counter] {
379379
(ref e, ref n) => {
380-
assert_eq!(e, &edge.data);
381-
assert_eq!(start_index, edge.source);
382-
assert_eq!(n, graph.node_data(edge.target));
380+
assert!(e == &edge.data);
381+
assert!(start_index == edge.source);
382+
assert!(n == graph.node_data(edge.target));
383383
}
384384
}
385385
counter += 1;

src/librustc/middle/trans/type_.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use std::cast;
2525

2626
use std::libc::{c_uint};
2727

28-
#[deriving(Clone, Eq)]
28+
#[deriving(Clone, Eq, Show)]
2929
pub struct Type {
3030
priv rf: TypeRef
3131
}

src/librustc/middle/typeck/infer/region_inference/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -761,7 +761,7 @@ impl RegionVarBindings {
761761

762762
// ______________________________________________________________________
763763

764-
#[deriving(Eq)]
764+
#[deriving(Eq, Show)]
765765
enum Classification { Expanding, Contracting }
766766

767767
enum VarValue { NoValue, Value(Region), ErrorValue }

src/libserialize/json.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1588,20 +1588,20 @@ mod tests {
15881588
use std::io;
15891589
use collections::TreeMap;
15901590

1591-
#[deriving(Eq, Encodable, Decodable)]
1591+
#[deriving(Eq, Encodable, Decodable, Show)]
15921592
enum Animal {
15931593
Dog,
15941594
Frog(~str, int)
15951595
}
15961596

1597-
#[deriving(Eq, Encodable, Decodable)]
1597+
#[deriving(Eq, Encodable, Decodable, Show)]
15981598
struct Inner {
15991599
a: (),
16001600
b: uint,
16011601
c: ~[~str],
16021602
}
16031603

1604-
#[deriving(Eq, Encodable, Decodable)]
1604+
#[deriving(Eq, Encodable, Decodable, Show)]
16051605
struct Outer {
16061606
inner: ~[Inner],
16071607
}

src/libstd/any.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ mod tests {
167167
use prelude::*;
168168
use super::*;
169169

170-
#[deriving(Eq)]
170+
#[deriving(Eq, Show)]
171171
struct Test;
172172

173173
static TEST: &'static str = "Test";

src/libstd/ascii.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,7 @@ mod tests {
475475
use char::from_u32;
476476

477477
macro_rules! v2ascii (
478-
( [$($e:expr),*]) => ( [$(Ascii{chr:$e}),*]);
478+
( [$($e:expr),*]) => (&[$(Ascii{chr:$e}),*]);
479479
(&[$($e:expr),*]) => (&[$(Ascii{chr:$e}),*]);
480480
(~[$($e:expr),*]) => (~[$(Ascii{chr:$e}),*]);
481481
)

src/libstd/bool.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -293,9 +293,9 @@ mod tests {
293293

294294
#[test]
295295
fn test_totalord() {
296-
assert_eq!(true.cmp(&true), Equal);
297-
assert_eq!(false.cmp(&false), Equal);
298-
assert_eq!(true.cmp(&false), Greater);
299-
assert_eq!(false.cmp(&true), Less);
296+
assert!(true.cmp(&true) == Equal);
297+
assert!(false.cmp(&false) == Equal);
298+
assert!(true.cmp(&false) == Greater);
299+
assert!(false.cmp(&true) == Less);
300300
}
301301
}

src/libstd/cell.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@
1010

1111
//! Types dealing with dynamic mutability
1212
13+
use cast;
1314
use clone::{Clone, DeepClone};
1415
use cmp::Eq;
16+
use fmt;
17+
use kinds::{marker, Pod};
1518
use ops::Drop;
1619
use option::{None, Option, Some};
17-
use cast;
18-
use kinds::{marker, Pod};
1920

2021
/// A mutable memory location that admits only `Pod` data.
2122
pub struct Cell<T> {
@@ -61,6 +62,12 @@ impl<T:Eq + Pod> Eq for Cell<T> {
6162
}
6263
}
6364

65+
impl<T: fmt::Show> fmt::Show for Cell<T> {
66+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
67+
write!(f.buf, r"Cell \{ value: {} \}", self.value)
68+
}
69+
}
70+
6471
/// A mutable memory location with dynamically checked borrow rules
6572
pub struct RefCell<T> {
6673
priv value: T,

src/libstd/cmp.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ totaleq_impl!(uint)
7272

7373
totaleq_impl!(char)
7474

75-
#[deriving(Clone, Eq)]
75+
#[deriving(Clone, Eq, Show)]
7676
pub enum Ordering { Less = -1, Equal = 0, Greater = 1 }
7777

7878
/// Trait for types that form a total order

src/libstd/comm/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ pub struct Chan<T> {
313313

314314
/// This enumeration is the list of the possible reasons that try_recv could not
315315
/// return data when called.
316-
#[deriving(Eq, Clone)]
316+
#[deriving(Eq, Clone, Show)]
317317
pub enum TryRecvResult<T> {
318318
/// This channel is currently empty, but the sender(s) have not yet
319319
/// disconnected, so data may yet become available.

0 commit comments

Comments
 (0)