about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/libstd/panicking.rs5
-rw-r--r--src/test/run-pass/multi-panic.rs30
2 files changed, 35 insertions, 0 deletions
diff --git a/src/libstd/panicking.rs b/src/libstd/panicking.rs
index 8561ecd9c4c..490c5f4b352 100644
--- a/src/libstd/panicking.rs
+++ b/src/libstd/panicking.rs
@@ -16,6 +16,7 @@ use cell::Cell;
 use cell::RefCell;
 use intrinsics;
 use sync::StaticRwLock;
+use sync::atomic::{AtomicBool, Ordering};
 use sys::stdio::Stderr;
 use sys_common::backtrace;
 use sys_common::thread_info;
@@ -38,6 +39,7 @@ enum Handler {
 
 static HANDLER_LOCK: StaticRwLock = StaticRwLock::new();
 static mut HANDLER: Handler = Handler::Default;
+static FIRST_PANIC: AtomicBool = AtomicBool::new(true);
 
 /// Registers a custom panic handler, replacing any that was previously
 /// registered.
@@ -173,8 +175,11 @@ fn default_handler(info: &PanicInfo) {
     let write = |err: &mut ::io::Write| {
         let _ = writeln!(err, "thread '{}' panicked at '{}', {}:{}",
                          name, msg, file, line);
+
         if log_backtrace {
             let _ = backtrace::write(err);
+        } else if FIRST_PANIC.compare_and_swap(true, false, Ordering::SeqCst) {
+            let _ = writeln!(err, "note: Run with `RUST_BACKTRACE=1` for a backtrace.");
         }
     };
 
diff --git a/src/test/run-pass/multi-panic.rs b/src/test/run-pass/multi-panic.rs
new file mode 100644
index 00000000000..7bf07314dcc
--- /dev/null
+++ b/src/test/run-pass/multi-panic.rs
@@ -0,0 +1,30 @@
+// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+fn main() {
+    let args: Vec<String> = std::env::args().collect();
+    if args.len() > 1 && args[1] == "run_test" {
+        let _ = std::thread::spawn(|| {
+            panic!();
+        }).join();
+
+        panic!();
+    } else {
+        let test = std::process::Command::new(&args[0]).arg("run_test").output().unwrap();
+        assert!(!test.status.success());
+        let err = String::from_utf8_lossy(&test.stderr);
+        let mut it = err.lines();
+
+        assert_eq!(it.next().map(|l| l.starts_with("thread '<unnamed>' panicked at")), Some(true));
+        assert_eq!(it.next(), Some("note: Run with `RUST_BACKTRACE=1` for a backtrace."));
+        assert_eq!(it.next().map(|l| l.starts_with("thread '<main>' panicked at")), Some(true));
+        assert_eq!(it.next(), None);
+    }
+}