From 02882fbd7edcb8d0d152afcdc8571216efcbd664 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Fri, 28 Feb 2014 01:23:06 -0800 Subject: 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. --- src/libsyntax/parse/lexer.rs | 2 +- src/libsyntax/parse/mod.rs | 12 ++++++------ src/libsyntax/parse/token.rs | 22 ++++++++++++++++++++-- 3 files changed, 27 insertions(+), 9 deletions(-) (limited to 'src/libsyntax/parse') diff --git a/src/libsyntax/parse/lexer.rs b/src/libsyntax/parse/lexer.rs index 5bace75a5ea..93fdcb41d2b 100644 --- a/src/libsyntax/parse/lexer.rs +++ b/src/libsyntax/parse/lexer.rs @@ -32,7 +32,7 @@ pub trait Reader { fn dup(&self) -> ~Reader:; } -#[deriving(Clone, Eq)] +#[deriving(Clone, Eq, Show)] pub struct TokenAndSpan { tok: token::Token, sp: Span, diff --git a/src/libsyntax/parse/mod.rs b/src/libsyntax/parse/mod.rs index 9d0c9d0f4d3..6bcb7afb120 100644 --- a/src/libsyntax/parse/mod.rs +++ b/src/libsyntax/parse/mod.rs @@ -312,7 +312,7 @@ mod test { } #[test] fn path_exprs_1() { - assert_eq!(string_to_expr(~"a"), + assert!(string_to_expr(~"a") == @ast::Expr{ id: ast::DUMMY_NODE_ID, node: ast::ExprPath(ast::Path { @@ -331,7 +331,7 @@ mod test { } #[test] fn path_exprs_2 () { - assert_eq!(string_to_expr(~"::a::b"), + assert!(string_to_expr(~"::a::b") == @ast::Expr { id: ast::DUMMY_NODE_ID, node: ast::ExprPath(ast::Path { @@ -542,7 +542,7 @@ mod test { } #[test] fn ret_expr() { - assert_eq!(string_to_expr(~"return d"), + assert!(string_to_expr(~"return d") == @ast::Expr{ id: ast::DUMMY_NODE_ID, node:ast::ExprRet(Some(@ast::Expr{ @@ -565,7 +565,7 @@ mod test { } #[test] fn parse_stmt_1 () { - assert_eq!(string_to_stmt(~"b;"), + assert!(string_to_stmt(~"b;") == @Spanned{ node: ast::StmtExpr(@ast::Expr { id: ast::DUMMY_NODE_ID, @@ -592,7 +592,7 @@ mod test { #[test] fn parse_ident_pat () { let mut parser = string_to_parser(~"b"); - assert_eq!(parser.parse_pat(), + assert!(parser.parse_pat() == @ast::Pat{id: ast::DUMMY_NODE_ID, node: ast::PatIdent( ast::BindByValue(ast::MutImmutable), @@ -615,7 +615,7 @@ mod test { // check the contents of the tt manually: #[test] fn parse_fundecl () { // this test depends on the intern order of "fn" and "int" - assert_eq!(string_to_item(~"fn a (b : int) { b; }"), + assert!(string_to_item(~"fn a (b : int) { b; }") == Some( @ast::Item{ident:str_to_ident("a"), attrs:~[], diff --git a/src/libsyntax/parse/token.rs b/src/libsyntax/parse/token.rs index 528eb7d54f3..edc5e613f91 100644 --- a/src/libsyntax/parse/token.rs +++ b/src/libsyntax/parse/token.rs @@ -23,7 +23,7 @@ use std::local_data; use std::path::BytesContainer; #[allow(non_camel_case_types)] -#[deriving(Clone, Encodable, Decodable, Eq, Hash)] +#[deriving(Clone, Encodable, Decodable, Eq, Hash, Show)] pub enum BinOp { PLUS, MINUS, @@ -38,7 +38,7 @@ pub enum BinOp { } #[allow(non_camel_case_types)] -#[deriving(Clone, Encodable, Decodable, Eq, Hash)] +#[deriving(Clone, Encodable, Decodable, Eq, Hash, Show)] pub enum Token { /* Expression-operator symbols. */ EQ, @@ -118,6 +118,24 @@ pub enum Nonterminal { NtMatchers(~[ast::Matcher]) } +impl fmt::Show for Nonterminal { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match *self { + NtItem(..) => f.pad("NtItem(..)"), + NtBlock(..) => f.pad("NtBlock(..)"), + NtStmt(..) => f.pad("NtStmt(..)"), + NtPat(..) => f.pad("NtPat(..)"), + NtExpr(..) => f.pad("NtExpr(..)"), + NtTy(..) => f.pad("NtTy(..)"), + NtIdent(..) => f.pad("NtIdent(..)"), + NtAttr(..) => f.pad("NtAttr(..)"), + NtPath(..) => f.pad("NtPath(..)"), + NtTT(..) => f.pad("NtTT(..)"), + NtMatchers(..) => f.pad("NtMatchers(..)"), + } + } +} + pub fn binop_to_str(o: BinOp) -> ~str { match o { PLUS => ~"+", -- cgit 1.4.1-3-g733a5