@@ -359,11 +359,20 @@ impl<'a> DiagnosticBuilder<'a> {
359
359
fn new ( handler : & ' a Handler ,
360
360
level : Level ,
361
361
message : & str ) -> DiagnosticBuilder < ' a > {
362
+ DiagnosticBuilder :: new_with_code ( handler, level, None , message)
363
+ }
364
+
365
+ /// Convenience function for internal use, clients should use one of the
366
+ /// struct_* methods on Handler.
367
+ fn new_with_code ( handler : & ' a Handler ,
368
+ level : Level ,
369
+ code : Option < String > ,
370
+ message : & str ) -> DiagnosticBuilder < ' a > {
362
371
DiagnosticBuilder {
363
372
handler : handler,
364
373
level : level,
365
374
message : message. to_owned ( ) ,
366
- code : None ,
375
+ code : code ,
367
376
span : MultiSpan :: new ( ) ,
368
377
children : vec ! [ ] ,
369
378
}
@@ -397,10 +406,10 @@ impl<'a> fmt::Debug for DiagnosticBuilder<'a> {
397
406
impl < ' a > Drop for DiagnosticBuilder < ' a > {
398
407
fn drop ( & mut self ) {
399
408
if !panicking ( ) && !self . cancelled ( ) {
400
- self . handler . emit . borrow_mut ( ) . emit ( & MultiSpan :: new ( ) ,
401
- "Error constructed but not emitted" ,
402
- None ,
403
- Bug ) ;
409
+ let mut db = DiagnosticBuilder :: new ( self . handler ,
410
+ Bug ,
411
+ "Error constructed but not emitted" ) ;
412
+ db . emit ( ) ;
404
413
panic ! ( ) ;
405
414
}
406
415
}
@@ -588,7 +597,7 @@ impl Handler {
588
597
self . bump_err_count ( ) ;
589
598
}
590
599
pub fn span_note_without_error < S : Into < MultiSpan > > ( & self , sp : S , msg : & str ) {
591
- self . emit . borrow_mut ( ) . emit ( & sp. into ( ) , msg, None , Note ) ;
600
+ self . emit ( & sp. into ( ) , msg, Note ) ;
592
601
}
593
602
pub fn span_unimpl < S : Into < MultiSpan > > ( & self , sp : S , msg : & str ) -> ! {
594
603
self . span_bug ( sp, & format ! ( "unimplemented {}" , msg) ) ;
@@ -597,25 +606,40 @@ impl Handler {
597
606
if self . treat_err_as_bug {
598
607
self . bug ( msg) ;
599
608
}
600
- self . emit . borrow_mut ( ) . emit ( & MultiSpan :: new ( ) , msg, None , Fatal ) ;
609
+ let mut db = DiagnosticBuilder :: new ( self ,
610
+ Fatal ,
611
+ msg) ;
612
+ db. emit ( ) ;
601
613
self . bump_err_count ( ) ;
602
614
FatalError
603
615
}
604
616
pub fn err ( & self , msg : & str ) {
605
617
if self . treat_err_as_bug {
606
618
self . bug ( msg) ;
607
619
}
608
- self . emit . borrow_mut ( ) . emit ( & MultiSpan :: new ( ) , msg, None , Error ) ;
620
+ let mut db = DiagnosticBuilder :: new ( self ,
621
+ Error ,
622
+ msg) ;
623
+ db. emit ( ) ;
609
624
self . bump_err_count ( ) ;
610
625
}
611
626
pub fn warn ( & self , msg : & str ) {
612
- self . emit . borrow_mut ( ) . emit ( & MultiSpan :: new ( ) , msg, None , Warning ) ;
627
+ let mut db = DiagnosticBuilder :: new ( self ,
628
+ Warning ,
629
+ msg) ;
630
+ db. emit ( ) ;
613
631
}
614
632
pub fn note_without_error ( & self , msg : & str ) {
615
- self . emit . borrow_mut ( ) . emit ( & MultiSpan :: new ( ) , msg, None , Note ) ;
633
+ let mut db = DiagnosticBuilder :: new ( self ,
634
+ Note ,
635
+ msg) ;
636
+ db. emit ( ) ;
616
637
}
617
638
pub fn bug ( & self , msg : & str ) -> ! {
618
- self . emit . borrow_mut ( ) . emit ( & MultiSpan :: new ( ) , msg, None , Bug ) ;
639
+ let mut db = DiagnosticBuilder :: new ( self ,
640
+ Bug ,
641
+ msg) ;
642
+ db. emit ( ) ;
619
643
panic ! ( ExplicitBug ) ;
620
644
}
621
645
pub fn unimpl ( & self , msg : & str ) -> ! {
@@ -661,7 +685,9 @@ impl Handler {
661
685
msg : & str ,
662
686
lvl : Level ) {
663
687
if lvl == Warning && !self . can_emit_warnings { return }
664
- self . emit . borrow_mut ( ) . emit ( & msp, msg, None , lvl) ;
688
+ let mut db = DiagnosticBuilder :: new ( self , lvl, msg) ;
689
+ db. set_span ( msp. clone ( ) ) ;
690
+ db. emit ( ) ;
665
691
if !self . continue_after_error . get ( ) { self . abort_if_errors ( ) ; }
666
692
}
667
693
pub fn emit_with_code ( & self ,
@@ -670,7 +696,12 @@ impl Handler {
670
696
code : & str ,
671
697
lvl : Level ) {
672
698
if lvl == Warning && !self . can_emit_warnings { return }
673
- self . emit . borrow_mut ( ) . emit ( & msp, msg, Some ( code) , lvl) ;
699
+ let mut db = DiagnosticBuilder :: new_with_code ( self ,
700
+ lvl,
701
+ Some ( code. to_owned ( ) ) ,
702
+ msg) ;
703
+ db. set_span ( msp. clone ( ) ) ;
704
+ db. emit ( ) ;
674
705
if !self . continue_after_error . get ( ) { self . abort_if_errors ( ) ; }
675
706
}
676
707
}
0 commit comments