Skip to content

Commit

Permalink
auto merge of #14115 : alexcrichton/rust/core-fmt, r=brson
Browse files Browse the repository at this point in the history
This was a more difficult change than I thought it would be, and it is unfortunately a breaking change rather than a drop-in replacement. Most of the rationale can be found in the third commit.

cc #13851
  • Loading branch information
bors committed May 16, 2014
2 parents 84406d4 + 2e2160b commit b545a49
Show file tree
Hide file tree
Showing 113 changed files with 3,237 additions and 2,959 deletions.
16 changes: 8 additions & 8 deletions src/compiletest/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ impl FromStr for Mode {
impl fmt::Show for Mode {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let msg = match *self {
CompileFail => "compile-fail",
RunFail => "run-fail",
RunPass => "run-pass",
Pretty => "pretty",
DebugInfoGdb => "debuginfo-gdb",
DebugInfoLldb => "debuginfo-lldb",
Codegen => "codegen",
CompileFail => "compile-fail",
RunFail => "run-fail",
RunPass => "run-pass",
Pretty => "pretty",
DebugInfoGdb => "debuginfo-gdb",
DebugInfoLldb => "debuginfo-lldb",
Codegen => "codegen",
};
write!(f.buf, "{}", msg)
msg.fmt(f)
}
}

Expand Down
14 changes: 7 additions & 7 deletions src/libcollections/btree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -425,8 +425,8 @@ impl<K: fmt::Show + TotalOrd, V: fmt::Show> fmt::Show for Leaf<K, V> {
///Returns a string representation of a Leaf.
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
for (i, s) in self.elts.iter().enumerate() {
if i != 0 { try!(write!(f.buf, " // ")) }
try!(write!(f.buf, "{}", *s))
if i != 0 { try!(write!(f, " // ")) }
try!(write!(f, "{}", *s))
}
Ok(())
}
Expand Down Expand Up @@ -654,10 +654,10 @@ impl<K: fmt::Show + TotalOrd, V: fmt::Show> fmt::Show for Branch<K, V> {
///Returns a string representation of a Branch.
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
for (i, s) in self.elts.iter().enumerate() {
if i != 0 { try!(write!(f.buf, " // ")) }
try!(write!(f.buf, "{}", *s))
if i != 0 { try!(write!(f, " // ")) }
try!(write!(f, "{}", *s))
}
write!(f.buf, " // rightmost child: ({}) ", *self.rightmost_child)
write!(f, " // rightmost child: ({}) ", *self.rightmost_child)
}
}

Expand Down Expand Up @@ -715,7 +715,7 @@ impl<K: TotalOrd, V: TotalEq> TotalOrd for LeafElt<K, V> {
impl<K: fmt::Show + TotalOrd, V: fmt::Show> fmt::Show for LeafElt<K, V> {
///Returns a string representation of a LeafElt.
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f.buf, "Key: {}, value: {};", self.key, self.value)
write!(f, "Key: {}, value: {};", self.key, self.value)
}
}

Expand Down Expand Up @@ -765,7 +765,7 @@ impl<K: fmt::Show + TotalOrd, V: fmt::Show> fmt::Show for BranchElt<K, V> {
/// Returns string containing key, value, and child (which should recur to a
/// leaf) Consider changing in future to be more readable.
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f.buf, "Key: {}, value: {}, (child: {})",
write!(f, "Key: {}, value: {}, (child: {})",
self.key, self.value, *self.left)
}
}
Expand Down
16 changes: 8 additions & 8 deletions src/libcollections/hashmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1418,14 +1418,14 @@ impl<K: TotalEq + Hash<S>, V: Eq, S, H: Hasher<S>> Eq for HashMap<K, V, H> {

impl<K: TotalEq + Hash<S> + Show, V: Show, S, H: Hasher<S>> Show for HashMap<K, V, H> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
try!(write!(f.buf, r"\{"));
try!(write!(f, r"\{"));

for (i, (k, v)) in self.iter().enumerate() {
if i != 0 { try!(write!(f.buf, ", ")); }
try!(write!(f.buf, "{}: {}", *k, *v));
if i != 0 { try!(write!(f, ", ")); }
try!(write!(f, "{}: {}", *k, *v));
}

write!(f.buf, r"\}")
write!(f, r"\}")
}
}

Expand Down Expand Up @@ -1605,14 +1605,14 @@ impl<T: TotalEq + Hash<S>, S, H: Hasher<S>> HashSet<T, H> {

impl<T: TotalEq + Hash<S> + fmt::Show, S, H: Hasher<S>> fmt::Show for HashSet<T, H> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
try!(write!(f.buf, r"\{"));
try!(write!(f, r"\{"));

for (i, x) in self.iter().enumerate() {
if i != 0 { try!(write!(f.buf, ", ")); }
try!(write!(f.buf, "{}", *x));
if i != 0 { try!(write!(f, ", ")); }
try!(write!(f, "{}", *x));
}

write!(f.buf, r"\}")
write!(f, r"\}")
}
}

Expand Down
12 changes: 6 additions & 6 deletions src/libcollections/lru_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,20 +205,20 @@ impl<A: fmt::Show + Hash + TotalEq, B: fmt::Show> fmt::Show for LruCache<A, B> {
/// Return a string that lists the key-value pairs from most-recently
/// used to least-recently used.
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
try!(write!(f.buf, r"\{"));
try!(write!(f, r"\{"));
let mut cur = self.head;
for i in range(0, self.len()) {
if i > 0 { try!(write!(f.buf, ", ")) }
if i > 0 { try!(write!(f, ", ")) }
unsafe {
cur = (*cur).next;
try!(write!(f.buf, "{}", (*cur).key));
try!(write!(f, "{}", (*cur).key));
}
try!(write!(f.buf, ": "));
try!(write!(f, ": "));
unsafe {
try!(write!(f.buf, "{}", (*cur).value));
try!(write!(f, "{}", (*cur).value));
}
}
write!(f.buf, r"\}")
write!(f, r"\}")
}
}

Expand Down
35 changes: 18 additions & 17 deletions src/libcore/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,12 @@ mod tests {

match a.as_ref::<uint>() {
Some(&5) => {}
x => fail!("Unexpected value {:?}", x)
x => fail!("Unexpected value {}", x)
}

match a.as_ref::<Test>() {
None => {}
x => fail!("Unexpected value {:?}", x)
x => fail!("Unexpected value {}", x)
}
}

Expand All @@ -189,35 +189,35 @@ mod tests {
assert_eq!(*x, 5u);
*x = 612;
}
x => fail!("Unexpected value {:?}", x)
x => fail!("Unexpected value {}", x)
}

match b_r.as_mut::<uint>() {
Some(x) => {
assert_eq!(*x, 7u);
*x = 413;
}
x => fail!("Unexpected value {:?}", x)
x => fail!("Unexpected value {}", x)
}

match a_r.as_mut::<Test>() {
None => (),
x => fail!("Unexpected value {:?}", x)
x => fail!("Unexpected value {}", x)
}

match b_r.as_mut::<Test>() {
None => (),
x => fail!("Unexpected value {:?}", x)
x => fail!("Unexpected value {}", x)
}

match a_r.as_mut::<uint>() {
Some(&612) => {}
x => fail!("Unexpected value {:?}", x)
x => fail!("Unexpected value {}", x)
}

match b_r.as_mut::<uint>() {
Some(&413) => {}
x => fail!("Unexpected value {:?}", x)
x => fail!("Unexpected value {}", x)
}
}

Expand All @@ -229,11 +229,11 @@ mod tests {
let b = box Test as Box<Any>;

match a.move::<uint>() {
Ok(a) => { assert_eq!(a, box 8u); }
Ok(a) => { assert!(a == box 8u); }
Err(..) => fail!()
}
match b.move::<Test>() {
Ok(a) => { assert_eq!(a, box Test); }
Ok(a) => { assert!(a == box Test); }
Err(..) => fail!()
}

Expand All @@ -246,13 +246,14 @@ mod tests {

#[test]
fn test_show() {
let a = box 8u as Box<::realcore::any::Any>;
let b = box Test as Box<::realcore::any::Any>;
assert_eq!(format!("{}", a), "Box<Any>".to_owned());
assert_eq!(format!("{}", b), "Box<Any>".to_owned());

let a = &8u as &::realcore::any::Any;
let b = &Test as &::realcore::any::Any;
use realstd::to_str::ToStr;
let a = box 8u as Box<::realstd::any::Any>;
let b = box Test as Box<::realstd::any::Any>;
assert_eq!(a.to_str(), "Box<Any>".to_owned());
assert_eq!(b.to_str(), "Box<Any>".to_owned());

let a = &8u as &Any;
let b = &Test as &Any;
assert_eq!(format!("{}", a), "&Any".to_owned());
assert_eq!(format!("{}", b), "&Any".to_owned());
}
Expand Down
2 changes: 1 addition & 1 deletion src/libcore/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ mod test {
fn cell_has_sensible_show() {
use str::StrSlice;

let x = ::realcore::cell::Cell::new("foo bar");
let x = Cell::new("foo bar");
assert!(format!("{}", x).contains(x.get()));

x.set("baz qux");
Expand Down
4 changes: 2 additions & 2 deletions src/libcore/char.rs
Original file line number Diff line number Diff line change
Expand Up @@ -633,9 +633,9 @@ impl Default for char {
mod test {
use super::{escape_unicode, escape_default};

use realcore::char::Char;
use char::Char;
use slice::ImmutableVector;
use realstd::option::{Some, None};
use option::{Some, None};
use realstd::strbuf::StrBuf;
use realstd::str::StrAllocating;

Expand Down
2 changes: 1 addition & 1 deletion src/libcore/clone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ mod test {
fn test_owned_clone() {
let a = box 5i;
let b: Box<int> = realclone(&a);
assert_eq!(a, b);
assert!(a == b);
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion src/libcore/cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ pub trait TotalEq: Eq {
}

/// An ordering is, e.g, a result of a comparison between two values.
#[deriving(Clone, Eq)]
#[deriving(Clone, Eq, Show)]
pub enum Ordering {
/// An ordering where a compared value is less [than another].
Less = -1,
Expand Down
50 changes: 32 additions & 18 deletions src/libcore/failure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,28 @@
// except according to those terms.

//! Failure support for libcore
//!
//! The core library cannot define failure, but it does *declare* failure. This
//! means that the functions inside of libcore are allowed to fail, but to be
//! useful an upstream crate must define failure for libcore to use. The current
//! interface for failure is:
//!
//! fn begin_unwind(fmt: &fmt::Arguments, file: &str, line: uint) -> !;
//!
//! This definition allows for failing with any general message, but it does not
//! allow for failing with a `~Any` value. The reason for this is that libcore
//! is not allowed to allocate.
//!
//! This module contains a few other failure functions, but these are just the
//! necessary lang items for the compiler. All failure is funneled through this
//! one function. Currently, the actual symbol is declared in the standard
//! library, but the location of this may change over time.

#![allow(dead_code, missing_doc)]

#[cfg(not(test))]
use str::raw::c_str_to_static_slice;

// FIXME: Once std::fmt is in libcore, all of these functions should delegate
// to a common failure function with this signature:
//
// extern {
// fn rust_unwind(f: &fmt::Arguments, file: &str, line: uint) -> !;
// }
//
// Each of these functions can create a temporary fmt::Arguments
// structure to pass to this function.
use fmt;

#[cold] #[inline(never)] // this is the slow path, always
#[lang="fail_"]
Expand All @@ -32,24 +39,31 @@ fn fail_(expr: *u8, file: *u8, line: uint) -> ! {
unsafe {
let expr = c_str_to_static_slice(expr as *i8);
let file = c_str_to_static_slice(file as *i8);
begin_unwind(expr, file, line)
format_args!(|args| -> () {
begin_unwind(args, file, line);
}, "{}", expr);

loop {}
}
}

#[cold]
#[lang="fail_bounds_check"]
#[cfg(not(test))]
fn fail_bounds_check(file: *u8, line: uint, index: uint, len: uint) -> ! {
#[allow(ctypes)]
extern { fn rust_fail_bounds_check(file: *u8, line: uint,
index: uint, len: uint,) -> !; }
unsafe { rust_fail_bounds_check(file, line, index, len) }
let file = unsafe { c_str_to_static_slice(file as *i8) };
format_args!(|args| -> () {
begin_unwind(args, file, line);
}, "index out of bounds: the len is {} but the index is {}", len, index);
loop {}
}

#[cold]
pub fn begin_unwind(msg: &str, file: &'static str, line: uint) -> ! {
pub fn begin_unwind(fmt: &fmt::Arguments, file: &'static str, line: uint) -> ! {
// FIXME: this should be a proper lang item, it should not just be some
// undefined symbol sitting in the middle of nowhere.
#[allow(ctypes)]
extern { fn rust_begin_unwind(msg: &str, file: &'static str,
extern { fn rust_begin_unwind(fmt: &fmt::Arguments, file: &'static str,
line: uint) -> !; }
unsafe { rust_begin_unwind(msg, file, line) }
unsafe { rust_begin_unwind(fmt, file, line) }
}
Loading

0 comments on commit b545a49

Please sign in to comment.