File tree 2 files changed +35
-0
lines changed
2 files changed +35
-0
lines changed Original file line number Diff line number Diff line change @@ -16,6 +16,7 @@ use cell::Cell;
16
16
use cell:: RefCell ;
17
17
use intrinsics;
18
18
use sync:: StaticRwLock ;
19
+ use sync:: atomic:: { AtomicBool , Ordering } ;
19
20
use sys:: stdio:: Stderr ;
20
21
use sys_common:: backtrace;
21
22
use sys_common:: thread_info;
@@ -38,6 +39,7 @@ enum Handler {
38
39
39
40
static HANDLER_LOCK : StaticRwLock = StaticRwLock :: new ( ) ;
40
41
static mut HANDLER : Handler = Handler :: Default ;
42
+ static FIRST_PANIC : AtomicBool = AtomicBool :: new ( true ) ;
41
43
42
44
/// Registers a custom panic handler, replacing any that was previously
43
45
/// registered.
@@ -173,8 +175,11 @@ fn default_handler(info: &PanicInfo) {
173
175
let write = |err : & mut :: io:: Write | {
174
176
let _ = writeln ! ( err, "thread '{}' panicked at '{}', {}:{}" ,
175
177
name, msg, file, line) ;
178
+
176
179
if log_backtrace {
177
180
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." ) ;
178
183
}
179
184
} ;
180
185
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