File tree Expand file tree Collapse file tree 2 files changed +35
-0
lines changed Expand file tree Collapse file tree 2 files changed +35
-0
lines changed Original file line number Diff line number Diff line change @@ -16,6 +16,7 @@ use cell::Cell;
1616use cell:: RefCell ;
1717use intrinsics;
1818use sync:: StaticRwLock ;
19+ use sync:: atomic:: { AtomicBool , Ordering } ;
1920use sys:: stdio:: Stderr ;
2021use sys_common:: backtrace;
2122use sys_common:: thread_info;
@@ -38,6 +39,7 @@ enum Handler {
3839
3940static HANDLER_LOCK : StaticRwLock = StaticRwLock :: new ( ) ;
4041static 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
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments