@@ -35,10 +35,10 @@ use crate::snippet::{
35
35
} ;
36
36
use crate :: styled_buffer:: StyledBuffer ;
37
37
use crate :: timings:: TimingRecord ;
38
- use crate :: translation:: { Translate , to_fluent_args} ;
38
+ use crate :: translation:: { Translator , to_fluent_args} ;
39
39
use 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 ,
42
42
} ;
43
43
44
44
/// Default column width, used in tests and when terminal dimensions cannot be determined.
@@ -175,7 +175,7 @@ const ANONYMIZED_LINE_NUM: &str = "LL";
175
175
pub type DynEmitter = dyn Emitter + DynSend ;
176
176
177
177
/// Emitter trait for emitting errors and other structured information.
178
- pub trait Emitter : Translate {
178
+ pub trait Emitter {
179
179
/// Emit a structured diagnostic.
180
180
fn emit_diagnostic ( & mut self , diag : DiagInner , registry : & Registry ) ;
181
181
@@ -212,6 +212,8 @@ pub trait Emitter: Translate {
212
212
213
213
fn source_map ( & self ) -> Option < & SourceMap > ;
214
214
215
+ fn translator ( & self ) -> & Translator ;
216
+
215
217
/// Formats the substitutions of the primary_span
216
218
///
217
219
/// There are a lot of conditions to this method, but in short:
@@ -224,13 +226,17 @@ pub trait Emitter: Translate {
224
226
/// * If the current `DiagInner` has multiple suggestions,
225
227
/// we leave `primary_span` and the suggestions untouched.
226
228
fn primary_span_formatted (
227
- & mut self ,
229
+ & self ,
228
230
primary_span : & mut MultiSpan ,
229
231
suggestions : & mut Vec < CodeSuggestion > ,
230
232
fluent_args : & FluentArgs < ' _ > ,
231
233
) {
232
234
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 ( ) ;
234
240
if rest. is_empty ( )
235
241
// ^ if there is only one suggestion
236
242
// don't display multi-suggestions as labels
@@ -491,16 +497,6 @@ pub trait Emitter: Translate {
491
497
}
492
498
}
493
499
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
-
504
500
impl Emitter for HumanEmitter {
505
501
fn source_map ( & self ) -> Option < & SourceMap > {
506
502
self . sm . as_deref ( )
@@ -538,39 +534,52 @@ impl Emitter for HumanEmitter {
538
534
fn supports_color ( & self ) -> bool {
539
535
self . dst . supports_color ( )
540
536
}
537
+
538
+ fn translator ( & self ) -> & Translator {
539
+ & self . translator
540
+ }
541
541
}
542
542
543
543
/// An emitter that does nothing when emitting a non-fatal diagnostic.
544
544
/// Fatal diagnostics are forwarded to `fatal_emitter` to avoid silent
545
545
/// failures of rustc, as witnessed e.g. in issue #89358.
546
- pub struct SilentEmitter {
546
+ pub struct FatalOnlyEmitter {
547
547
pub fatal_emitter : Box < dyn Emitter + DynSend > ,
548
548
pub fatal_note : Option < String > ,
549
- pub emit_fatal_diagnostic : bool ,
550
549
}
551
550
552
- impl Translate for SilentEmitter {
553
- fn fluent_bundle ( & self ) -> Option < & FluentBundle > {
551
+ impl Emitter for FatalOnlyEmitter {
552
+ fn source_map ( & self ) -> Option < & SourceMap > {
554
553
None
555
554
}
556
555
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 ( )
559
567
}
560
568
}
561
569
570
+ pub struct SilentEmitter {
571
+ pub translator : Translator ,
572
+ }
573
+
562
574
impl Emitter for SilentEmitter {
563
575
fn source_map ( & self ) -> Option < & SourceMap > {
564
576
None
565
577
}
566
578
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
574
583
}
575
584
}
576
585
@@ -615,9 +624,8 @@ pub struct HumanEmitter {
615
624
#[ setters( skip) ]
616
625
dst : IntoDynSyncSend < Destination > ,
617
626
sm : Option < Arc < SourceMap > > ,
618
- fluent_bundle : Option < Arc < FluentBundle > > ,
619
627
#[ setters( skip) ]
620
- fallback_bundle : LazyFallbackBundle ,
628
+ translator : Translator ,
621
629
short_message : bool ,
622
630
ui_testing : bool ,
623
631
ignored_directories_in_source_blocks : Vec < String > ,
@@ -637,12 +645,11 @@ pub(crate) struct FileWithAnnotatedLines {
637
645
}
638
646
639
647
impl HumanEmitter {
640
- pub fn new ( dst : Destination , fallback_bundle : LazyFallbackBundle ) -> HumanEmitter {
648
+ pub fn new ( dst : Destination , translator : Translator ) -> HumanEmitter {
641
649
HumanEmitter {
642
650
dst : IntoDynSyncSend ( dst) ,
643
651
sm : None ,
644
- fluent_bundle : None ,
645
- fallback_bundle,
652
+ translator,
646
653
short_message : false ,
647
654
ui_testing : false ,
648
655
ignored_directories_in_source_blocks : Vec :: new ( ) ,
@@ -1433,7 +1440,7 @@ impl HumanEmitter {
1433
1440
// very *weird* formats
1434
1441
// see?
1435
1442
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 ( ) ;
1437
1444
let text = & normalize_whitespace ( & text) ;
1438
1445
let lines = text. split ( '\n' ) . collect :: < Vec < _ > > ( ) ;
1439
1446
if lines. len ( ) > 1 {
@@ -1528,7 +1535,8 @@ impl HumanEmitter {
1528
1535
}
1529
1536
let mut line = 0 ;
1530
1537
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 ( ) ;
1532
1540
// Account for newlines to align output to its label.
1533
1541
for text in normalize_whitespace ( & text) . lines ( ) {
1534
1542
buffer. append (
@@ -1560,7 +1568,7 @@ impl HumanEmitter {
1560
1568
. into_iter ( )
1561
1569
. filter_map ( |label| match label. label {
1562
1570
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 ( ) ?;
1564
1572
if !text. trim ( ) . is_empty ( ) { Some ( text. to_string ( ) ) } else { None }
1565
1573
}
1566
1574
_ => None ,
@@ -3104,7 +3112,11 @@ impl FileWithAnnotatedLines {
3104
3112
3105
3113
let label = label. as_ref ( ) . map ( |m| {
3106
3114
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 ( ) ,
3108
3120
)
3109
3121
} ) ;
3110
3122
0 commit comments