@@ -330,7 +330,7 @@ pub struct HandlerFlags {
330330 pub can_emit_warnings : bool ,
331331 /// If true, error-level diagnostics are upgraded to bug-level.
332332 /// (rustc: see `-Z treat-err-as-bug`)
333- pub treat_err_as_bug : bool ,
333+ pub treat_err_as_bug : Option < usize > ,
334334 /// If true, immediately emit diagnostics that would otherwise be buffered.
335335 /// (rustc: see `-Z dont-buffer-diagnostics` and `-Z treat-err-as-bug`)
336336 pub dont_buffer_diagnostics : bool ,
@@ -360,7 +360,7 @@ impl Drop for Handler {
360360impl Handler {
361361 pub fn with_tty_emitter ( color_config : ColorConfig ,
362362 can_emit_warnings : bool ,
363- treat_err_as_bug : bool ,
363+ treat_err_as_bug : Option < usize > ,
364364 cm : Option < Lrc < SourceMapperDyn > > )
365365 -> Handler {
366366 Handler :: with_tty_emitter_and_flags (
@@ -382,7 +382,7 @@ impl Handler {
382382 }
383383
384384 pub fn with_emitter ( can_emit_warnings : bool ,
385- treat_err_as_bug : bool ,
385+ treat_err_as_bug : Option < usize > ,
386386 e : Box < dyn Emitter + sync:: Send > )
387387 -> Handler {
388388 Handler :: with_emitter_and_flags (
@@ -516,7 +516,7 @@ impl Handler {
516516 }
517517
518518 fn panic_if_treat_err_as_bug ( & self ) {
519- if self . flags . treat_err_as_bug {
519+ if self . treat_err_as_bug ( ) {
520520 panic ! ( "encountered error with `-Z treat_err_as_bug" ) ;
521521 }
522522 }
@@ -558,7 +558,7 @@ impl Handler {
558558 panic ! ( ExplicitBug ) ;
559559 }
560560 pub fn delay_span_bug < S : Into < MultiSpan > > ( & self , sp : S , msg : & str ) {
561- if self . flags . treat_err_as_bug {
561+ if self . treat_err_as_bug ( ) {
562562 // FIXME: don't abort here if report_delayed_bugs is off
563563 self . span_bug ( sp, msg) ;
564564 }
@@ -593,14 +593,14 @@ impl Handler {
593593 DiagnosticBuilder :: new ( self , FailureNote , msg) . emit ( )
594594 }
595595 pub fn fatal ( & self , msg : & str ) -> FatalError {
596- if self . flags . treat_err_as_bug {
596+ if self . treat_err_as_bug ( ) {
597597 self . bug ( msg) ;
598598 }
599599 DiagnosticBuilder :: new ( self , Fatal , msg) . emit ( ) ;
600600 FatalError
601601 }
602602 pub fn err ( & self , msg : & str ) {
603- if self . flags . treat_err_as_bug {
603+ if self . treat_err_as_bug ( ) {
604604 self . bug ( msg) ;
605605 }
606606 let mut db = DiagnosticBuilder :: new ( self , Error , msg) ;
@@ -610,6 +610,9 @@ impl Handler {
610610 let mut db = DiagnosticBuilder :: new ( self , Warning , msg) ;
611611 db. emit ( ) ;
612612 }
613+ fn treat_err_as_bug ( & self ) -> bool {
614+ self . flags . treat_err_as_bug . map ( |c| self . err_count ( ) >= c) . unwrap_or ( false )
615+ }
613616 pub fn note_without_error ( & self , msg : & str ) {
614617 let mut db = DiagnosticBuilder :: new ( self , Note , msg) ;
615618 db. emit ( ) ;
@@ -624,8 +627,8 @@ impl Handler {
624627 }
625628
626629 fn bump_err_count ( & self ) {
627- self . panic_if_treat_err_as_bug ( ) ;
628630 self . err_count . fetch_add ( 1 , SeqCst ) ;
631+ self . panic_if_treat_err_as_bug ( ) ;
629632 }
630633
631634 pub fn err_count ( & self ) -> usize {
@@ -643,7 +646,13 @@ impl Handler {
643646 _ => format ! ( "aborting due to {} previous errors" , self . err_count( ) )
644647 } ;
645648
646- let _ = self . fatal ( & s) ;
649+ let _ = if self . treat_err_as_bug ( ) {
650+ self . fatal ( & s)
651+ } else {
652+ // only emit one backtrace when using `-Z treat-err-as-bug=X`
653+ DiagnosticBuilder :: new ( self , Fatal , & s) . emit ( ) ;
654+ FatalError
655+ } ;
647656
648657 let can_show_explain = self . emitter . borrow ( ) . should_show_explain ( ) ;
649658 let are_there_diagnostics = !self . emitted_diagnostic_codes . borrow ( ) . is_empty ( ) ;
0 commit comments