std: Change assert_eq!() to use {} instead of {:?}

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.
This commit is contained in:
Alex Crichton 2014-02-28 01:23:06 -08:00
parent 123eb4ebea
commit 02882fbd7e
97 changed files with 354 additions and 301 deletions

View file

@ -13,6 +13,7 @@ use std::cmp::Eq;
pub trait MyNum : Add<Self,Self> + Sub<Self,Self> + Mul<Self,Self> + Eq {
}
#[deriving(Show)]
pub struct MyInt {
val: int
}

View file

@ -8,7 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#[deriving(Eq)]
#[deriving(Eq, Show)]
struct Point { x : int }
pub fn main() {

View file

@ -63,7 +63,7 @@ fn test_ptr() {
}
}
#[deriving(Eq)]
#[deriving(Eq, Show)]
struct p {
x: int,
y: int,

View file

@ -12,6 +12,7 @@
use std::cmp;
#[deriving(Show)]
enum cat_type { tuxedo, tabby, tortoiseshell }
impl cmp::Eq for cat_type {

View file

@ -12,10 +12,10 @@ fn id<T>(x: T) -> T {
x
}
#[deriving(Eq)]
#[deriving(Eq, Show)]
struct Foo<T>(T);
#[deriving(Eq)]
#[deriving(Eq, Show)]
enum Bar<T> {
Bar(T)
}

View file

@ -16,5 +16,5 @@ use cci_const::bar;
static foo: extern "C" fn() = bar;
pub fn main() {
assert_eq!(foo, bar);
assert!(foo == bar);
}

View file

@ -18,6 +18,6 @@ struct S {
}
pub fn main() {
assert_eq!(foopy, f);
assert_eq!(f, s.f);
assert!(foopy == f);
assert!(f == s.f);
}

View file

@ -10,6 +10,7 @@
use std::cmp;
#[deriving(Show)]
struct foo { a: int, b: int, c: int }
impl cmp::Eq for foo {

View file

@ -11,7 +11,7 @@
use std::num::FromPrimitive;
use std::int;
#[deriving(Eq, FromPrimitive)]
#[deriving(Eq, FromPrimitive, Show)]
enum A {
Foo = int::MAX,
Bar = 1,

View file

@ -8,7 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#[deriving(Eq)]
#[deriving(Eq, Show)]
enum Foo {
Bar,
Baz,

View file

@ -8,7 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#[deriving(Eq)]
#[deriving(Eq, Show)]
enum Foo {
Bar(int, int),
Baz(f64, f64)

View file

@ -8,7 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#[deriving(Eq)]
#[deriving(Eq, Show)]
struct Foo;
pub fn main() {

View file

@ -10,7 +10,7 @@
#[feature(struct_variant)];
#[deriving(Eq)]
#[deriving(Eq, Show)]
enum S {
X { x: int, y: int },
Y

View file

@ -8,7 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#[deriving(Eq)]
#[deriving(Eq, Show)]
struct Foo(int, int, ~str);
pub fn main() {

View file

@ -8,7 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#[deriving(Eq)]
#[deriving(Eq, Show)]
struct Foo {
x: int,
y: int,

View file

@ -10,7 +10,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#[deriving(Eq, Hash)]
#[deriving(Eq, Hash, Show)]
struct Foo<T> {
x: int,
y: T,

View file

@ -8,6 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#[deriving(Show)]
enum chan { chan_t, }
impl Eq for chan {

View file

@ -21,6 +21,7 @@ fn test_rec() {
assert_eq!(rs.i, 100);
}
#[deriving(Show)]
enum mood { happy, sad, }
impl Eq for mood {

View file

@ -20,6 +20,7 @@ fn test_rec() {
assert_eq!(rs.i, 100);
}
#[deriving(Show)]
enum mood { happy, sad, }
impl Eq for mood {

View file

@ -20,13 +20,13 @@ extern fn uintvoidret(_x: uint) {}
extern fn uintuintuintuintret(x: uint, y: uint, z: uint) -> uint { x+y+z }
pub fn main() {
assert_eq!(voidret1, voidret1);
assert!(voidret1 == voidret1);
assert!(voidret1 != voidret2);
assert_eq!(uintret, uintret);
assert!(uintret == uintret);
assert_eq!(uintvoidret, uintvoidret);
assert!(uintvoidret == uintvoidret);
assert_eq!(uintuintuintuintret, uintuintuintuintret);
assert!(uintuintuintuintret == uintuintuintuintret);
}

View file

@ -11,7 +11,7 @@
// Test a foreign function that accepts and returns a struct
// by value.
#[deriving(Eq)]
#[deriving(Eq, Show)]
struct TwoU32s {
one: u32, two: u32
}

View file

@ -13,7 +13,7 @@
// ignore-win32 #9205
#[deriving(Eq)]
#[deriving(Eq, Show)]
struct TwoU64s {
one: u64, two: u64
}

View file

@ -19,6 +19,6 @@ pub fn main() {
let b: extern "C" fn() = f;
let c: extern "C" fn() = g;
assert_eq!(a, b);
assert!(a == b);
assert!(a != c);
}

View file

@ -50,10 +50,10 @@ fn default_foo(x: Foo) {
assert_eq!(x.baz(), (1, 'a'));
}
#[deriving(Eq)]
#[deriving(Eq, Show)]
struct BazHelper<T>(T);
#[deriving(Eq)]
#[deriving(Eq, Show)]
// Ensure that we can use previous type parameters in defaults.
struct Baz<T, U = BazHelper<T>, V = Option<U>>(T, U, V);

View file

@ -11,6 +11,8 @@
// ignore-fast check-fast doesn't like 'extern crate extra'
// ignore-win32 TempDir may cause IoError on windows: #10462
#[feature(macro_rules)];
extern crate extra;
extern crate glob;
@ -20,6 +22,12 @@ use std::unstable::finally::Finally;
use std::{os, unstable};
use std::io;
macro_rules! assert_eq ( ($e1:expr, $e2:expr) => (
if $e1 != $e2 {
fail!("{} != {}", stringify!($e1), stringify!($e2))
}
) )
pub fn main() {
fn mk_file(path: &str, directory: bool) {
if directory {

View file

@ -26,7 +26,7 @@ pub mod pipes {
payload: Option<T>
}
#[deriving(Eq)]
#[deriving(Eq, Show)]
#[repr(int)]
pub enum state {
empty,

View file

@ -20,9 +20,9 @@
struct S<T> { i:u8, t:T }
impl<T> S<T> { fn unwrap(self) -> T { self.t } }
#[deriving(Eq)]
#[deriving(Eq, Show)]
struct A((u32, u32));
#[deriving(Eq)]
#[deriving(Eq, Show)]
struct B(u64);
pub fn main() {

View file

@ -8,7 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#[deriving(Eq)]
#[deriving(Eq, Show)]
struct Foo(uint);
fn foo() -> Foo {

View file

@ -13,6 +13,7 @@
use std::cmp;
use std::ops;
#[deriving(Show)]
struct Point {
x: int,
y: int

View file

@ -22,7 +22,7 @@ pub fn main() {
let s = S { a: 0xff_ff_ff_ffu32, b: 1, c: 0xaa_aa_aa_aa as i32 };
let transd : [u8, .. 9] = cast::transmute(s);
// Don't worry about endianness, the numbers are palindromic.
assert_eq!(transd,
assert!(transd ==
[0xff, 0xff, 0xff, 0xff,
1,
0xaa, 0xaa, 0xaa, 0xaa]);
@ -31,7 +31,7 @@ pub fn main() {
let s = S { a: 1u8, b: 2u8, c: 0b10000001_10000001 as i16};
let transd : [u8, .. 4] = cast::transmute(s);
// Again, no endianness problems.
assert_eq!(transd,
assert!(transd ==
[1, 2, 0b10000001, 0b10000001]);
}
}

View file

@ -26,11 +26,11 @@ pub fn main() {
unsafe {
let s4 = S4 { a: 1, b: [2,3,4] };
let transd : [u8, .. 4] = cast::transmute(s4);
assert_eq!(transd, [1, 2, 3, 4]);
assert!(transd == [1, 2, 3, 4]);
let s5 = S5 { a: 1, b: 0xff_00_00_ff };
let transd : [u8, .. 5] = cast::transmute(s5);
// Don't worry about endianness, the u32 is palindromic.
assert_eq!(transd, [1, 0xff, 0, 0, 0xff]);
assert!(transd == [1, 0xff, 0, 0, 0xff]);
}
}

View file

@ -13,7 +13,7 @@
use std::mem;
#[packed]
#[deriving(Eq)]
#[deriving(Eq, Show)]
struct Foo {
bar: u8,
baz: u64

View file

@ -20,11 +20,11 @@ pub fn main() {
unsafe {
let s4 = S4(1, [2,3,4]);
let transd : [u8, .. 4] = cast::transmute(s4);
assert_eq!(transd, [1, 2, 3, 4]);
assert!(transd == [1, 2, 3, 4]);
let s5 = S5(1, 0xff_00_00_ff);
let transd : [u8, .. 5] = cast::transmute(s5);
// Don't worry about endianness, the u32 is palindromic.
assert_eq!(transd, [1, 0xff, 0, 0, 0xff]);
assert!(transd == [1, 0xff, 0, 0, 0xff]);
}
}

View file

@ -27,6 +27,7 @@ use std::mem;
type Type<'tcx> = &'tcx TypeStructure<'tcx>;
#[deriving(Show)]
enum TypeStructure<'tcx> {
TypeInt,
TypeFunction(Type<'tcx>, Type<'tcx>),

View file

@ -12,5 +12,5 @@ static FOO: [int, ..4] = [32, ..4];
static BAR: [int, ..4] = [32, 32, 32, 32];
pub fn main() {
assert_eq!(FOO, BAR);
assert!(FOO == BAR);
}

View file

@ -12,7 +12,7 @@
use std::mem::size_of;
#[deriving(Eq)]
#[deriving(Eq, Show)]
enum Either<T, U> { Left(T), Right(U) }
macro_rules! check {

View file

@ -8,7 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#[deriving(Eq,Clone)]
#[deriving(Show,Eq,Clone)]
struct Foo<T> {
bar: T,
baz: T

View file

@ -10,6 +10,7 @@
#[deriving(Show)]
enum foo { large, small, }
impl Eq for foo {

View file

@ -45,6 +45,7 @@ fn test_str() {
assert_eq!(s1[3], 't' as u8);
}
#[deriving(Show)]
enum t {
tag1,
tag2(int),

View file

@ -12,6 +12,7 @@ use std::cmp::Eq;
trait MyNum : Eq { }
#[deriving(Show)]
struct MyInt { val: int }
impl Eq for MyInt {

View file

@ -12,6 +12,7 @@ use std::cmp::Eq;
trait MyNum : Add<Self,Self> + Sub<Self,Self> + Mul<Self,Self> + Eq { }
#[deriving(Show)]
struct MyInt { val: int }
impl Add<MyInt, MyInt> for MyInt {

View file

@ -8,9 +8,9 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#[deriving(Eq)]
#[deriving(Eq, Show)]
struct Foo(int);
#[deriving(Eq)]
#[deriving(Eq, Show)]
struct Bar(int, int);
pub fn main() {

View file

@ -13,7 +13,7 @@ use std::cmp::Eq;
fn sendable() {
fn f<T:Send + Eq>(i: T, j: T) {
assert_eq!(i, j);
assert!(i == j);
}
fn g<T:Send + Eq>(i: T, j: T) {
@ -31,7 +31,7 @@ fn sendable() {
fn copyable() {
fn f<T:Eq>(i: T, j: T) {
assert_eq!(i, j);
assert!(i == j);
}
fn g<T:Eq>(i: T, j: T) {
@ -49,7 +49,7 @@ fn copyable() {
fn noncopyable() {
fn f<T:Eq>(i: T, j: T) {
assert_eq!(i, j);
assert!(i == j);
}
fn g<T:Eq>(i: T, j: T) {

View file

@ -13,7 +13,7 @@ pub fn main() {
match x {
[2, _, _] => fail!(),
[1, a, b] => {
assert_eq!([a, b], [2, 3]);
assert!([a, b] == [2, 3]);
}
[_, _, _] => fail!(),
}