@@ -35,10 +35,10 @@ use crate::snippet::{
3535} ;
3636use crate :: styled_buffer:: StyledBuffer ;
3737use crate :: timings:: TimingRecord ;
38- use crate :: translation:: { Translate , to_fluent_args} ;
38+ use crate :: translation:: { Translator , to_fluent_args} ;
3939use crate :: {
40- CodeSuggestion , DiagInner , DiagMessage , ErrCode , FluentBundle , LazyFallbackBundle , Level ,
41- MultiSpan , Subdiag , SubstitutionHighlight , SuggestionStyle , TerminalUrl ,
40+ CodeSuggestion , DiagInner , DiagMessage , ErrCode , Level , MultiSpan , Subdiag ,
41+ SubstitutionHighlight , SuggestionStyle , TerminalUrl ,
4242} ;
4343
4444/// Default column width, used in tests and when terminal dimensions cannot be determined.
@@ -175,7 +175,7 @@ const ANONYMIZED_LINE_NUM: &str = "LL";
175175pub type DynEmitter = dyn Emitter + DynSend ;
176176
177177/// Emitter trait for emitting errors and other structured information.
178- pub trait Emitter : Translate {
178+ pub trait Emitter {
179179 /// Emit a structured diagnostic.
180180 fn emit_diagnostic ( & mut self , diag : DiagInner , registry : & Registry ) ;
181181
@@ -212,6 +212,8 @@ pub trait Emitter: Translate {
212212
213213 fn source_map ( & self ) -> Option < & SourceMap > ;
214214
215+ fn translator ( & self ) -> & Translator ;
216+
215217 /// Formats the substitutions of the primary_span
216218 ///
217219 /// There are a lot of conditions to this method, but in short:
@@ -224,13 +226,17 @@ pub trait Emitter: Translate {
224226 /// * If the current `DiagInner` has multiple suggestions,
225227 /// we leave `primary_span` and the suggestions untouched.
226228 fn primary_span_formatted (
227- & mut self ,
229+ & self ,
228230 primary_span : & mut MultiSpan ,
229231 suggestions : & mut Vec < CodeSuggestion > ,
230232 fluent_args : & FluentArgs < ' _ > ,
231233 ) {
232234 if let Some ( ( sugg, rest) ) = suggestions. split_first ( ) {
233- let msg = self . translate_message ( & sugg. msg , fluent_args) . map_err ( Report :: new) . unwrap ( ) ;
235+ let msg = self
236+ . translator ( )
237+ . translate_message ( & sugg. msg , fluent_args)
238+ . map_err ( Report :: new)
239+ . unwrap ( ) ;
234240 if rest. is_empty ( )
235241 // ^ if there is only one suggestion
236242 // don't display multi-suggestions as labels
@@ -491,16 +497,6 @@ pub trait Emitter: Translate {
491497 }
492498}
493499
494- impl Translate for HumanEmitter {
495- fn fluent_bundle ( & self ) -> Option < & FluentBundle > {
496- self . fluent_bundle . as_deref ( )
497- }
498-
499- fn fallback_fluent_bundle ( & self ) -> & FluentBundle {
500- & self . fallback_bundle
501- }
502- }
503-
504500impl Emitter for HumanEmitter {
505501 fn source_map ( & self ) -> Option < & SourceMap > {
506502 self . sm . as_deref ( )
@@ -538,39 +534,52 @@ impl Emitter for HumanEmitter {
538534 fn supports_color ( & self ) -> bool {
539535 self . dst . supports_color ( )
540536 }
537+
538+ fn translator ( & self ) -> & Translator {
539+ & self . translator
540+ }
541541}
542542
543543/// An emitter that does nothing when emitting a non-fatal diagnostic.
544544/// Fatal diagnostics are forwarded to `fatal_emitter` to avoid silent
545545/// failures of rustc, as witnessed e.g. in issue #89358.
546- pub struct SilentEmitter {
546+ pub struct FatalOnlyEmitter {
547547 pub fatal_emitter : Box < dyn Emitter + DynSend > ,
548548 pub fatal_note : Option < String > ,
549- pub emit_fatal_diagnostic : bool ,
550549}
551550
552- impl Translate for SilentEmitter {
553- fn fluent_bundle ( & self ) -> Option < & FluentBundle > {
551+ impl Emitter for FatalOnlyEmitter {
552+ fn source_map ( & self ) -> Option < & SourceMap > {
554553 None
555554 }
556555
557- fn fallback_fluent_bundle ( & self ) -> & FluentBundle {
558- self . fatal_emitter . fallback_fluent_bundle ( )
556+ fn emit_diagnostic ( & mut self , mut diag : DiagInner , registry : & Registry ) {
557+ if diag. level == Level :: Fatal {
558+ if let Some ( fatal_note) = & self . fatal_note {
559+ diag. sub ( Level :: Note , fatal_note. clone ( ) , MultiSpan :: new ( ) ) ;
560+ }
561+ self . fatal_emitter . emit_diagnostic ( diag, registry) ;
562+ }
563+ }
564+
565+ fn translator ( & self ) -> & Translator {
566+ self . fatal_emitter . translator ( )
559567 }
560568}
561569
570+ pub struct SilentEmitter {
571+ pub translator : Translator ,
572+ }
573+
562574impl Emitter for SilentEmitter {
563575 fn source_map ( & self ) -> Option < & SourceMap > {
564576 None
565577 }
566578
567- fn emit_diagnostic ( & mut self , mut diag : DiagInner , registry : & Registry ) {
568- if self . emit_fatal_diagnostic && diag. level == Level :: Fatal {
569- if let Some ( fatal_note) = & self . fatal_note {
570- diag. sub ( Level :: Note , fatal_note. clone ( ) , MultiSpan :: new ( ) ) ;
571- }
572- self . fatal_emitter . emit_diagnostic ( diag, registry) ;
573- }
579+ fn emit_diagnostic ( & mut self , _diag : DiagInner , _registry : & Registry ) { }
580+
581+ fn translator ( & self ) -> & Translator {
582+ & self . translator
574583 }
575584}
576585
@@ -615,9 +624,8 @@ pub struct HumanEmitter {
615624 #[ setters( skip) ]
616625 dst : IntoDynSyncSend < Destination > ,
617626 sm : Option < Arc < SourceMap > > ,
618- fluent_bundle : Option < Arc < FluentBundle > > ,
619627 #[ setters( skip) ]
620- fallback_bundle : LazyFallbackBundle ,
628+ translator : Translator ,
621629 short_message : bool ,
622630 ui_testing : bool ,
623631 ignored_directories_in_source_blocks : Vec < String > ,
@@ -637,12 +645,11 @@ pub(crate) struct FileWithAnnotatedLines {
637645}
638646
639647impl HumanEmitter {
640- pub fn new ( dst : Destination , fallback_bundle : LazyFallbackBundle ) -> HumanEmitter {
648+ pub fn new ( dst : Destination , translator : Translator ) -> HumanEmitter {
641649 HumanEmitter {
642650 dst : IntoDynSyncSend ( dst) ,
643651 sm : None ,
644- fluent_bundle : None ,
645- fallback_bundle,
652+ translator,
646653 short_message : false ,
647654 ui_testing : false ,
648655 ignored_directories_in_source_blocks : Vec :: new ( ) ,
@@ -1433,7 +1440,7 @@ impl HumanEmitter {
14331440 // very *weird* formats
14341441 // see?
14351442 for ( text, style) in msgs. iter ( ) {
1436- let text = self . translate_message ( text, args) . map_err ( Report :: new) . unwrap ( ) ;
1443+ let text = self . translator . translate_message ( text, args) . map_err ( Report :: new) . unwrap ( ) ;
14371444 let text = & normalize_whitespace ( & text) ;
14381445 let lines = text. split ( '\n' ) . collect :: < Vec < _ > > ( ) ;
14391446 if lines. len ( ) > 1 {
@@ -1528,7 +1535,8 @@ impl HumanEmitter {
15281535 }
15291536 let mut line = 0 ;
15301537 for ( text, style) in msgs. iter ( ) {
1531- let text = self . translate_message ( text, args) . map_err ( Report :: new) . unwrap ( ) ;
1538+ let text =
1539+ self . translator . translate_message ( text, args) . map_err ( Report :: new) . unwrap ( ) ;
15321540 // Account for newlines to align output to its label.
15331541 for text in normalize_whitespace ( & text) . lines ( ) {
15341542 buffer. append (
@@ -1560,7 +1568,7 @@ impl HumanEmitter {
15601568 . into_iter ( )
15611569 . filter_map ( |label| match label. label {
15621570 Some ( msg) if label. is_primary => {
1563- let text = self . translate_message ( & msg, args) . ok ( ) ?;
1571+ let text = self . translator . translate_message ( & msg, args) . ok ( ) ?;
15641572 if !text. trim ( ) . is_empty ( ) { Some ( text. to_string ( ) ) } else { None }
15651573 }
15661574 _ => None ,
@@ -3104,7 +3112,11 @@ impl FileWithAnnotatedLines {
31043112
31053113 let label = label. as_ref ( ) . map ( |m| {
31063114 normalize_whitespace (
3107- & emitter. translate_message ( m, args) . map_err ( Report :: new) . unwrap ( ) ,
3115+ & emitter
3116+ . translator ( )
3117+ . translate_message ( m, args)
3118+ . map_err ( Report :: new)
3119+ . unwrap ( ) ,
31083120 )
31093121 } ) ;
31103122
0 commit comments