Skip to content

Commit 13b5eda

Browse files
committed
Auto merge of #30402 - jooert:prettypanic, r=alexcrichton
This splits the output of panics into two lines as proposed in #15239 and adds a note about how to get a backtrace. Because the default panic message consists of multiple lines now, this changes the test runner's failure output to not indent the first line anymore. Fixes #15239 and fixes #11704.
2 parents 5d6e8fc + c07413c commit 13b5eda

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

src/libstd/panicking.rs

+5
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use cell::Cell;
1616
use cell::RefCell;
1717
use intrinsics;
1818
use sync::StaticRwLock;
19+
use sync::atomic::{AtomicBool, Ordering};
1920
use sys::stdio::Stderr;
2021
use sys_common::backtrace;
2122
use sys_common::thread_info;
@@ -38,6 +39,7 @@ enum Handler {
3839

3940
static HANDLER_LOCK: StaticRwLock = StaticRwLock::new();
4041
static mut HANDLER: Handler = Handler::Default;
42+
static FIRST_PANIC: AtomicBool = AtomicBool::new(true);
4143

4244
/// Registers a custom panic handler, replacing any that was previously
4345
/// registered.
@@ -173,8 +175,11 @@ fn default_handler(info: &PanicInfo) {
173175
let write = |err: &mut ::io::Write| {
174176
let _ = writeln!(err, "thread '{}' panicked at '{}', {}:{}",
175177
name, msg, file, line);
178+
176179
if log_backtrace {
177180
let _ = backtrace::write(err);
181+
} else if FIRST_PANIC.compare_and_swap(true, false, Ordering::SeqCst) {
182+
let _ = writeln!(err, "note: Run with `RUST_BACKTRACE=1` for a backtrace.");
178183
}
179184
};
180185

src/test/run-pass/multi-panic.rs

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
fn main() {
12+
let args: Vec<String> = std::env::args().collect();
13+
if args.len() > 1 && args[1] == "run_test" {
14+
let _ = std::thread::spawn(|| {
15+
panic!();
16+
}).join();
17+
18+
panic!();
19+
} else {
20+
let test = std::process::Command::new(&args[0]).arg("run_test").output().unwrap();
21+
assert!(!test.status.success());
22+
let err = String::from_utf8_lossy(&test.stderr);
23+
let mut it = err.lines();
24+
25+
assert_eq!(it.next().map(|l| l.starts_with("thread '<unnamed>' panicked at")), Some(true));
26+
assert_eq!(it.next(), Some("note: Run with `RUST_BACKTRACE=1` for a backtrace."));
27+
assert_eq!(it.next().map(|l| l.starts_with("thread '<main>' panicked at")), Some(true));
28+
assert_eq!(it.next(), None);
29+
}
30+
}

0 commit comments

Comments
 (0)