Skip to content

Commit 44440e5

Browse files
committed
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]
1 parent 8efd990 commit 44440e5

File tree

252 files changed

+2005
-1375
lines changed

Some content is hidden

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

252 files changed

+2005
-1375
lines changed

src/compiletest/common.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ impl FromStr for Mode {
4343
}
4444
}
4545

46-
impl fmt::Show for Mode {
46+
impl fmt::String for Mode {
4747
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
48-
let msg = match *self {
48+
fmt::String::fmt(match *self {
4949
CompileFail => "compile-fail",
5050
RunFail => "run-fail",
5151
RunPass => "run-pass",
@@ -54,8 +54,13 @@ impl fmt::Show for Mode {
5454
DebugInfoGdb => "debuginfo-gdb",
5555
DebugInfoLldb => "debuginfo-lldb",
5656
Codegen => "codegen",
57-
};
58-
msg.fmt(f)
57+
}, f)
58+
}
59+
}
60+
61+
impl fmt::Show for Mode {
62+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
63+
fmt::String::fmt(self, f)
5964
}
6065
}
6166

src/compiletest/compiletest.rs

+13-13
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ pub fn parse_config(args: Vec<String> ) -> Config {
108108
let matches =
109109
&match getopts::getopts(args_.as_slice(), groups.as_slice()) {
110110
Ok(m) => m,
111-
Err(f) => panic!("{}", f)
111+
Err(f) => panic!("{:?}", f)
112112
};
113113

114114
if matches.opt_present("h") || matches.opt_present("help") {
@@ -127,7 +127,7 @@ pub fn parse_config(args: Vec<String> ) -> Config {
127127
match regex::Regex::new(s) {
128128
Ok(re) => Some(re),
129129
Err(e) => {
130-
println!("failed to parse filter /{}/: {}", s, e);
130+
println!("failed to parse filter /{}/: {:?}", s, e);
131131
panic!()
132132
}
133133
}
@@ -186,11 +186,11 @@ pub fn parse_config(args: Vec<String> ) -> Config {
186186
pub fn log_config(config: &Config) {
187187
let c = config;
188188
logv(c, format!("configuration:"));
189-
logv(c, format!("compile_lib_path: {}", config.compile_lib_path));
190-
logv(c, format!("run_lib_path: {}", config.run_lib_path));
191-
logv(c, format!("rustc_path: {}", config.rustc_path.display()));
192-
logv(c, format!("src_base: {}", config.src_base.display()));
193-
logv(c, format!("build_base: {}", config.build_base.display()));
189+
logv(c, format!("compile_lib_path: {:?}", config.compile_lib_path));
190+
logv(c, format!("run_lib_path: {:?}", config.run_lib_path));
191+
logv(c, format!("rustc_path: {:?}", config.rustc_path.display()));
192+
logv(c, format!("src_base: {:?}", config.src_base.display()));
193+
logv(c, format!("build_base: {:?}", config.build_base.display()));
194194
logv(c, format!("stage_id: {}", config.stage_id));
195195
logv(c, format!("mode: {}", config.mode));
196196
logv(c, format!("run_ignored: {}", config.run_ignored));
@@ -206,10 +206,10 @@ pub fn log_config(config: &Config) {
206206
logv(c, format!("jit: {}", config.jit));
207207
logv(c, format!("target: {}", config.target));
208208
logv(c, format!("host: {}", config.host));
209-
logv(c, format!("android-cross-path: {}",
209+
logv(c, format!("android-cross-path: {:?}",
210210
config.android_cross_path.display()));
211-
logv(c, format!("adb_path: {}", config.adb_path));
212-
logv(c, format!("adb_test_dir: {}", config.adb_test_dir));
211+
logv(c, format!("adb_path: {:?}", config.adb_path));
212+
logv(c, format!("adb_test_dir: {:?}", config.adb_test_dir));
213213
logv(c, format!("adb_device_status: {}",
214214
config.adb_device_status));
215215
match config.test_shard {
@@ -271,7 +271,7 @@ pub fn run_tests(config: &Config) {
271271
Ok(true) => {}
272272
Ok(false) => panic!("Some tests failed"),
273273
Err(e) => {
274-
println!("I/O failure during tests: {}", e);
274+
println!("I/O failure during tests: {:?}", e);
275275
}
276276
}
277277
}
@@ -299,13 +299,13 @@ pub fn test_opts(config: &Config) -> test::TestOpts {
299299
}
300300

301301
pub fn make_tests(config: &Config) -> Vec<test::TestDescAndFn> {
302-
debug!("making tests from {}",
302+
debug!("making tests from {:?}",
303303
config.src_base.display());
304304
let mut tests = Vec::new();
305305
let dirs = fs::readdir(&config.src_base).unwrap();
306306
for file in dirs.iter() {
307307
let file = file.clone();
308-
debug!("inspecting file {}", file.display());
308+
debug!("inspecting file {:?}", file.display());
309309
if is_test(config, &file) {
310310
let t = make_test(config, &file, || {
311311
match config.mode {

src/compiletest/errors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ fn parse_expected(last_nonfollow_error: Option<uint>,
8484
(which, line)
8585
};
8686

87-
debug!("line={} which={} kind={} msg={}", line_num, which, kind, msg);
87+
debug!("line={} which={:?} kind={:?} msg={:?}", line_num, which, kind, msg);
8888
Some((which, ExpectedError { line: line,
8989
kind: kind,
9090
msg: msg, }))

src/compiletest/runtest.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ pub fn run_metrics(config: Config, testfile: String, mm: &mut MetricMap) {
6161
print!("\n\n");
6262
}
6363
let testfile = Path::new(testfile);
64-
debug!("running {}", testfile.display());
64+
debug!("running {:?}", testfile.display());
6565
let props = header::load_props(&testfile);
6666
debug!("loaded props");
6767
match config.mode {
@@ -141,7 +141,7 @@ fn check_correct_failure_status(proc_res: &ProcRes) {
141141
static RUST_ERR: int = 101;
142142
if !proc_res.status.matches_exit_status(RUST_ERR) {
143143
fatal_proc_rec(
144-
format!("failure produced the wrong error: {}",
144+
format!("failure produced the wrong error: {:?}",
145145
proc_res.status).as_slice(),
146146
proc_res);
147147
}
@@ -410,7 +410,7 @@ fn run_debuginfo_gdb_test(config: &Config, props: &TestProps, testfile: &Path) {
410410
],
411411
vec!(("".to_string(), "".to_string())),
412412
Some("".to_string()))
413-
.expect(format!("failed to exec `{}`", config.adb_path).as_slice());
413+
.expect(format!("failed to exec `{:?}`", config.adb_path).as_slice());
414414

415415
procsrv::run("",
416416
config.adb_path.as_slice(),
@@ -422,7 +422,7 @@ fn run_debuginfo_gdb_test(config: &Config, props: &TestProps, testfile: &Path) {
422422
],
423423
vec!(("".to_string(), "".to_string())),
424424
Some("".to_string()))
425-
.expect(format!("failed to exec `{}`", config.adb_path).as_slice());
425+
.expect(format!("failed to exec `{:?}`", config.adb_path).as_slice());
426426

427427
let adb_arg = format!("export LD_LIBRARY_PATH={}; \
428428
gdbserver :5039 {}/{}",
@@ -443,7 +443,7 @@ fn run_debuginfo_gdb_test(config: &Config, props: &TestProps, testfile: &Path) {
443443
vec!(("".to_string(),
444444
"".to_string())),
445445
Some("".to_string()))
446-
.expect(format!("failed to exec `{}`", config.adb_path).as_slice());
446+
.expect(format!("failed to exec `{:?}`", config.adb_path).as_slice());
447447
loop {
448448
//waiting 1 second for gdbserver start
449449
timer::sleep(Duration::milliseconds(1000));
@@ -481,7 +481,7 @@ fn run_debuginfo_gdb_test(config: &Config, props: &TestProps, testfile: &Path) {
481481
debugger_opts.as_slice(),
482482
vec!(("".to_string(), "".to_string())),
483483
None)
484-
.expect(format!("failed to exec `{}`", gdb_path).as_slice());
484+
.expect(format!("failed to exec `{:?}`", gdb_path).as_slice());
485485
let cmdline = {
486486
let cmdline = make_cmdline("",
487487
"arm-linux-androideabi-gdb",
@@ -548,7 +548,7 @@ fn run_debuginfo_gdb_test(config: &Config, props: &TestProps, testfile: &Path) {
548548

549549
// Add line breakpoints
550550
for line in breakpoint_lines.iter() {
551-
script_str.push_str(format!("break '{}':{}\n",
551+
script_str.push_str(format!("break '{:?}':{}\n",
552552
testfile.filename_display(),
553553
*line)[]);
554554
}
@@ -889,7 +889,7 @@ fn check_error_patterns(props: &TestProps,
889889
output_to_check: &str,
890890
proc_res: &ProcRes) {
891891
if props.error_patterns.is_empty() {
892-
fatal(format!("no error pattern specified in {}",
892+
fatal(format!("no error pattern specified in {:?}",
893893
testfile.display()).as_slice());
894894
}
895895
let mut next_err_idx = 0u;
@@ -955,7 +955,7 @@ fn check_expected_errors(expected_errors: Vec<errors::ExpectedError> ,
955955
}
956956

957957
let prefixes = expected_errors.iter().map(|ee| {
958-
format!("{}:{}:", testfile.display(), ee.line)
958+
format!("{:?}:{}:", testfile.display(), ee.line)
959959
}).collect::<Vec<String> >();
960960

961961
#[cfg(windows)]
@@ -1191,7 +1191,7 @@ fn compose_and_run_compiler(
11911191
None);
11921192
if !auxres.status.success() {
11931193
fatal_proc_rec(
1194-
format!("auxiliary build of {} failed to compile: ",
1194+
format!("auxiliary build of {:?} failed to compile: ",
11951195
abs_ab.display()).as_slice(),
11961196
&auxres);
11971197
}
@@ -1601,7 +1601,7 @@ fn _arm_push_aux_shared_library(config: &Config, testfile: &Path) {
16011601
.expect(format!("failed to exec `{}`", config.adb_path).as_slice());
16021602

16031603
if config.verbose {
1604-
println!("push ({}) {} {} {}",
1604+
println!("push ({}) {:?} {} {}",
16051605
config.target, file.display(),
16061606
copy_result.out, copy_result.err);
16071607
}

src/liballoc/arc.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,7 @@ impl<T: Eq> Eq for Arc<T> {}
581581

582582
impl<T: fmt::Show> fmt::Show for Arc<T> {
583583
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
584-
(**self).fmt(f)
584+
write!(f, "Arc({:?})", (**self))
585585
}
586586
}
587587

@@ -794,7 +794,7 @@ mod tests {
794794
#[test]
795795
fn show_arc() {
796796
let a = Arc::new(5u32);
797-
assert!(format!("{}", a) == "5")
797+
assert!(format!("{:?}", a) == "Arc(5u32)")
798798
}
799799

800800
// Make sure deriving works with Arc<T>

src/liballoc/boxed.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,13 @@ impl BoxAny for Box<Any> {
145145

146146
impl<T: ?Sized + fmt::Show> fmt::Show for Box<T> {
147147
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
148-
(**self).fmt(f)
148+
write!(f, "Box({:?})", &**self)
149+
}
150+
}
151+
152+
impl<Sized? T: fmt::String> fmt::String for Box<T> {
153+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
154+
fmt::String::fmt(&**self, f)
149155
}
150156
}
151157

src/liballoc/rc.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,7 @@ impl<S: hash::Writer, T: Hash<S>> Hash<S> for Rc<T> {
607607
#[experimental = "Show is experimental."]
608608
impl<T: fmt::Show> fmt::Show for Rc<T> {
609609
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
610-
(**self).fmt(f)
610+
write!(f, "Rc({:?})", **self)
611611
}
612612
}
613613

@@ -962,4 +962,10 @@ mod tests {
962962
assert!(cow1_weak.upgrade().is_none());
963963
}
964964

965+
#[test]
966+
fn test_show() {
967+
let foo = Rc::new(75u);
968+
assert!(format!("{:?}", foo) == "Rc(75u)")
969+
}
970+
965971
}

src/libcollections/bit.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1729,13 +1729,13 @@ impl BitvSet {
17291729

17301730
impl fmt::Show for BitvSet {
17311731
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
1732-
try!(write!(fmt, "{{"));
1732+
try!(write!(fmt, "BitvSet {{"));
17331733
let mut first = true;
17341734
for n in self.iter() {
17351735
if !first {
17361736
try!(write!(fmt, ", "));
17371737
}
1738-
try!(write!(fmt, "{}", n));
1738+
try!(write!(fmt, "{:?}", n));
17391739
first = false;
17401740
}
17411741
write!(fmt, "}}")

src/libcollections/btree/map.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -866,11 +866,11 @@ impl<K: Ord, V: Ord> Ord for BTreeMap<K, V> {
866866
#[stable]
867867
impl<K: Show, V: Show> Show for BTreeMap<K, V> {
868868
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
869-
try!(write!(f, "{{"));
869+
try!(write!(f, "BTreeMap {{"));
870870

871871
for (i, (k, v)) in self.iter().enumerate() {
872872
if i != 0 { try!(write!(f, ", ")); }
873-
try!(write!(f, "{}: {}", *k, *v));
873+
try!(write!(f, "{:?}: {:?}", *k, *v));
874874
}
875875

876876
write!(f, "}}")

src/libcollections/btree/node.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@ impl<K: Clone, V: Clone> Clone for Node<K, V> {
493493
/// // Now the handle still points at index 75, but on the small node, which has no index 75.
494494
/// flag.set(true);
495495
///
496-
/// println!("Uninitialized memory: {}", handle.into_kv());
496+
/// println!("Uninitialized memory: {:?}", handle.into_kv());
497497
/// }
498498
/// ```
499499
#[derive(Copy)]

src/libcollections/btree/set.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -556,11 +556,11 @@ impl<'a, 'b, T: Ord + Clone> BitOr<&'b BTreeSet<T>> for &'a BTreeSet<T> {
556556
#[stable]
557557
impl<T: Show> Show for BTreeSet<T> {
558558
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
559-
try!(write!(f, "{{"));
559+
try!(write!(f, "BTreeSet {{"));
560560

561561
for (i, x) in self.iter().enumerate() {
562562
if i != 0 { try!(write!(f, ", ")); }
563-
try!(write!(f, "{}", *x));
563+
try!(write!(f, "{:?}", *x));
564564
}
565565

566566
write!(f, "}}")

src/libcollections/dlist.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -663,11 +663,11 @@ impl<A: Clone> Clone for DList<A> {
663663
#[stable]
664664
impl<A: fmt::Show> fmt::Show for DList<A> {
665665
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
666-
try!(write!(f, "["));
666+
try!(write!(f, "DList ["));
667667

668668
for (i, e) in self.iter().enumerate() {
669669
if i != 0 { try!(write!(f, ", ")); }
670-
try!(write!(f, "{}", *e));
670+
try!(write!(f, "{:?}", *e));
671671
}
672672

673673
write!(f, "]")

src/libcollections/enum_set.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@ impl<E> Copy for EnumSet<E> {}
3333

3434
impl<E:CLike+fmt::Show> fmt::Show for EnumSet<E> {
3535
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
36-
try!(write!(fmt, "{{"));
36+
try!(write!(fmt, "EnumSet {{"));
3737
let mut first = true;
3838
for e in self.iter() {
3939
if !first {
4040
try!(write!(fmt, ", "));
4141
}
42-
try!(write!(fmt, "{}", e));
42+
try!(write!(fmt, "{:?}", e));
4343
first = false;
4444
}
4545
write!(fmt, "}}")
@@ -287,11 +287,11 @@ mod test {
287287
#[test]
288288
fn test_show() {
289289
let mut e = EnumSet::new();
290-
assert_eq!("{}", e.to_string());
290+
assert!(format!("{:?}", e) == "EnumSet {}");
291291
e.insert(A);
292-
assert_eq!("{A}", e.to_string());
292+
assert!(format!("{:?}", e) == "EnumSet {A}");
293293
e.insert(C);
294-
assert_eq!("{A, C}", e.to_string());
294+
assert!(format!("{:?}", e) == "EnumSet {A, C}");
295295
}
296296

297297
#[test]

src/libcollections/ring_buf.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1613,11 +1613,11 @@ impl<A> Extend<A> for RingBuf<A> {
16131613
#[stable]
16141614
impl<T: fmt::Show> fmt::Show for RingBuf<T> {
16151615
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1616-
try!(write!(f, "["));
1616+
try!(write!(f, "RingBuf ["));
16171617

16181618
for (i, e) in self.iter().enumerate() {
16191619
if i != 0 { try!(write!(f, ", ")); }
1620-
try!(write!(f, "{}", *e));
1620+
try!(write!(f, "{:?}", *e));
16211621
}
16221622

16231623
write!(f, "]")

0 commit comments

Comments
 (0)