core: split into fmt::Show and fmt::String

fmt::Show is for debugging, and can and should be implemented for
all public types. This trait is used with `{:?}` syntax. There still
exists #[derive(Show)].

fmt::String is for types that faithfully be represented as a String.
Because of this, there is no way to derive fmt::String, all
implementations must be purposeful. It is used by the default format
syntax, `{}`.

This will break most instances of `{}`, since that now requires the type
to impl fmt::String. In most cases, replacing `{}` with `{:?}` is the
correct fix. Types that were being printed specifically for users should
receive a fmt::String implementation to fix this.

Part of #20013

[breaking-change]
This commit is contained in:
Sean McArthur 2014-12-20 00:09:35 -08:00
parent 8efd9901b6
commit 44440e5c18
252 changed files with 1996 additions and 1366 deletions

View file

@ -100,13 +100,26 @@ impl Ident {
}
}
impl Show for Ident {
//NOTE(stage0): remove after snapshot
impl fmt::Show for Ident {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::String::fmt(self, f)
}
}
impl fmt::String for Ident {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}#{}", self.name, self.ctxt)
}
}
impl Show for Name {
impl fmt::Show for Name {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::String::fmt(self, f)
}
}
impl fmt::String for Name {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let Name(nm) = *self;
write!(f, "\"{}\"({})", token::get_name(*self).get(), nm)
@ -1094,7 +1107,14 @@ pub enum IntTy {
TyI64,
}
//NOTE(stage0): remove after snapshot
impl fmt::Show for IntTy {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::String::fmt(self, f)
}
}
impl fmt::String for IntTy {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", ast_util::int_ty_to_string(*self, None))
}
@ -1129,7 +1149,14 @@ impl UintTy {
}
}
//NOTE(stage0): remove after snapshot
impl fmt::Show for UintTy {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::String::fmt(self, f)
}
}
impl fmt::String for UintTy {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", ast_util::uint_ty_to_string(*self, None))
}
@ -1141,7 +1168,14 @@ pub enum FloatTy {
TyF64,
}
//NOTE(stage0): remove after snapshot
impl fmt::Show for FloatTy {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::String::fmt(self, f)
}
}
impl fmt::String for FloatTy {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", ast_util::float_ty_to_string(*self))
}
@ -1192,10 +1226,19 @@ pub enum Onceness {
impl fmt::Show for Onceness {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Once => "once".fmt(f),
Many => "many".fmt(f),
}
fmt::String::fmt(match *self {
Once => "once",
Many => "many",
}, f)
}
}
impl fmt::String for Onceness {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::String::fmt(match *self {
Once => "once",
Many => "many",
}, f)
}
}
@ -1305,18 +1348,18 @@ pub struct FnDecl {
pub variadic: bool
}
#[derive(Copy, Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash)]
#[derive(Copy, Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show)]
pub enum Unsafety {
Unsafe,
Normal,
}
impl fmt::Show for Unsafety {
impl fmt::String for Unsafety {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Unsafety::Normal => "normal".fmt(f),
Unsafety::Unsafe => "unsafe".fmt(f),
}
fmt::String::fmt(match *self {
Unsafety::Normal => "normal",
Unsafety::Unsafe => "unsafe",
}, f)
}
}