@@ -117,42 +117,10 @@ impl ColorConfig {
117
117
}
118
118
}
119
119
120
- /// A basic emitter for when we don't have access to a codemap or registry. Used
121
- /// for reporting very early errors, etc.
122
- pub struct BasicEmitter {
123
- dst : Destination ,
124
- }
125
-
126
- impl CoreEmitter for BasicEmitter {
127
- fn emit_message ( & mut self ,
128
- _rsp : & RenderSpan ,
129
- msg : & str ,
130
- code : Option < & str > ,
131
- lvl : Level ,
132
- _is_header : bool ,
133
- _show_snippet : bool ) {
134
- // we ignore the span as we have no access to a codemap at this point
135
- if let Err ( e) = print_diagnostic ( & mut self . dst , "" , lvl, msg, code) {
136
- panic ! ( "failed to print diagnostics: {:?}" , e) ;
137
- }
138
- }
139
- }
140
-
141
- impl BasicEmitter {
142
- pub fn stderr ( color_config : ColorConfig ) -> BasicEmitter {
143
- if color_config. use_color ( ) {
144
- let dst = Destination :: from_stderr ( ) ;
145
- BasicEmitter { dst : dst }
146
- } else {
147
- BasicEmitter { dst : Raw ( Box :: new ( io:: stderr ( ) ) ) }
148
- }
149
- }
150
- }
151
-
152
120
pub struct EmitterWriter {
153
121
dst : Destination ,
154
122
registry : Option < registry:: Registry > ,
155
- cm : Rc < CodeMapper > ,
123
+ cm : Option < Rc < CodeMapper > > ,
156
124
157
125
/// Is this the first error emitted thus far? If not, we emit a
158
126
/// `\n` before the top-level errors.
@@ -194,7 +162,7 @@ macro_rules! println_maybe_styled {
194
162
impl EmitterWriter {
195
163
pub fn stderr ( color_config : ColorConfig ,
196
164
registry : Option < registry:: Registry > ,
197
- code_map : Rc < CodeMapper > ,
165
+ code_map : Option < Rc < CodeMapper > > ,
198
166
format_mode : FormatMode )
199
167
-> EmitterWriter {
200
168
if color_config. use_color ( ) {
@@ -215,7 +183,7 @@ impl EmitterWriter {
215
183
216
184
pub fn new ( dst : Box < Write + Send > ,
217
185
registry : Option < registry:: Registry > ,
218
- code_map : Rc < CodeMapper > ,
186
+ code_map : Option < Rc < CodeMapper > > ,
219
187
format_mode : FormatMode )
220
188
-> EmitterWriter {
221
189
EmitterWriter { dst : Raw ( dst) ,
@@ -257,7 +225,11 @@ impl EmitterWriter {
257
225
if old_school {
258
226
let loc = match rsp. span ( ) . primary_span ( ) {
259
227
Some ( COMMAND_LINE_SP ) | Some ( DUMMY_SP ) => "" . to_string ( ) ,
260
- Some ( ps) => self . cm . span_to_string ( ps) ,
228
+ Some ( ps) => if let Some ( ref cm) = self . cm {
229
+ cm. span_to_string ( ps)
230
+ } else {
231
+ "" . to_string ( )
232
+ } ,
261
233
None => "" . to_string ( )
262
234
} ;
263
235
print_diagnostic ( & mut self . dst , & loc, lvl, msg, Some ( code) ) ?
@@ -270,7 +242,11 @@ impl EmitterWriter {
270
242
if old_school {
271
243
let loc = match rsp. span ( ) . primary_span ( ) {
272
244
Some ( COMMAND_LINE_SP ) | Some ( DUMMY_SP ) => "" . to_string ( ) ,
273
- Some ( ps) => self . cm . span_to_string ( ps) ,
245
+ Some ( ps) => if let Some ( ref cm) = self . cm {
246
+ cm. span_to_string ( ps)
247
+ } else {
248
+ "" . to_string ( )
249
+ } ,
274
250
None => "" . to_string ( )
275
251
} ;
276
252
print_diagnostic ( & mut self . dst , & loc, lvl, msg, code) ?
@@ -316,7 +292,11 @@ impl EmitterWriter {
316
292
. is_some ( ) => {
317
293
let loc = match rsp. span ( ) . primary_span ( ) {
318
294
Some ( COMMAND_LINE_SP ) | Some ( DUMMY_SP ) => "" . to_string ( ) ,
319
- Some ( ps) => self . cm . span_to_string ( ps) ,
295
+ Some ( ps) => if let Some ( ref cm) = self . cm {
296
+ cm. span_to_string ( ps)
297
+ } else {
298
+ "" . to_string ( )
299
+ } ,
320
300
None => "" . to_string ( )
321
301
} ;
322
302
let msg = "run `rustc --explain " . to_string ( ) + & code. to_string ( ) +
@@ -335,32 +315,34 @@ impl EmitterWriter {
335
315
use std:: borrow:: Borrow ;
336
316
337
317
let primary_span = suggestion. msp . primary_span ( ) . unwrap ( ) ;
338
- let lines = self . cm . span_to_lines ( primary_span) . unwrap ( ) ;
339
- assert ! ( !lines. lines. is_empty( ) ) ;
340
-
341
- let complete = suggestion. splice_lines ( self . cm . borrow ( ) ) ;
342
- let line_count = cmp:: min ( lines. lines . len ( ) , MAX_HIGHLIGHT_LINES ) ;
343
- let display_lines = & lines. lines [ ..line_count] ;
344
-
345
- let fm = & * lines. file ;
346
- // Calculate the widest number to format evenly
347
- let max_digits = line_num_max_digits ( display_lines. last ( ) . unwrap ( ) ) ;
348
-
349
- // print the suggestion without any line numbers, but leave
350
- // space for them. This helps with lining up with previous
351
- // snippets from the actual error being reported.
352
- let mut lines = complete. lines ( ) ;
353
- for line in lines. by_ref ( ) . take ( MAX_HIGHLIGHT_LINES ) {
354
- write ! ( & mut self . dst, "{0}:{1:2$} {3}\n " ,
355
- fm. name, "" , max_digits, line) ?;
356
- }
318
+ if let Some ( ref cm) = self . cm {
319
+ let lines = cm. span_to_lines ( primary_span) . unwrap ( ) ;
320
+
321
+ assert ! ( !lines. lines. is_empty( ) ) ;
322
+
323
+ let complete = suggestion. splice_lines ( cm. borrow ( ) ) ;
324
+ let line_count = cmp:: min ( lines. lines . len ( ) , MAX_HIGHLIGHT_LINES ) ;
325
+ let display_lines = & lines. lines [ ..line_count] ;
326
+
327
+ let fm = & * lines. file ;
328
+ // Calculate the widest number to format evenly
329
+ let max_digits = line_num_max_digits ( display_lines. last ( ) . unwrap ( ) ) ;
330
+
331
+ // print the suggestion without any line numbers, but leave
332
+ // space for them. This helps with lining up with previous
333
+ // snippets from the actual error being reported.
334
+ let mut lines = complete. lines ( ) ;
335
+ for line in lines. by_ref ( ) . take ( MAX_HIGHLIGHT_LINES ) {
336
+ write ! ( & mut self . dst, "{0}:{1:2$} {3}\n " ,
337
+ fm. name, "" , max_digits, line) ?;
338
+ }
357
339
358
- // if we elided some lines, add an ellipsis
359
- if let Some ( _) = lines. next ( ) {
360
- write ! ( & mut self . dst, "{0:1$} {0:2$} ...\n " ,
361
- "" , fm. name. len( ) , max_digits) ?;
340
+ // if we elided some lines, add an ellipsis
341
+ if let Some ( _) = lines. next ( ) {
342
+ write ! ( & mut self . dst, "{0:1$} {0:2$} ...\n " ,
343
+ "" , fm. name. len( ) , max_digits) ?;
344
+ }
362
345
}
363
-
364
346
Ok ( ( ) )
365
347
}
366
348
@@ -369,20 +351,26 @@ impl EmitterWriter {
369
351
lvl : Level )
370
352
-> io:: Result < ( ) >
371
353
{
354
+ // Check to see if we have any lines to highlight, exit early if not
355
+ match self . cm {
356
+ None => return Ok ( ( ) ) ,
357
+ _ => ( )
358
+ }
359
+
372
360
let old_school = match self . format_mode {
373
361
FormatMode :: NewErrorFormat => false ,
374
362
FormatMode :: OriginalErrorFormat => true ,
375
363
FormatMode :: EnvironmentSelected => check_old_skool ( )
376
364
} ;
377
365
378
- let mut snippet_data = SnippetData :: new ( self . cm . clone ( ) ,
366
+ let mut snippet_data = SnippetData :: new ( self . cm . as_ref ( ) . unwrap ( ) . clone ( ) ,
379
367
msp. primary_span ( ) ,
380
368
self . format_mode . clone ( ) ) ;
381
369
if old_school {
382
370
let mut output_vec = vec ! [ ] ;
383
371
384
372
for span_label in msp. span_labels ( ) {
385
- let mut snippet_data = SnippetData :: new ( self . cm . clone ( ) ,
373
+ let mut snippet_data = SnippetData :: new ( self . cm . as_ref ( ) . unwrap ( ) . clone ( ) ,
386
374
Some ( span_label. span ) ,
387
375
self . format_mode . clone ( ) ) ;
388
376
@@ -431,16 +419,18 @@ impl EmitterWriter {
431
419
fn print_macro_backtrace ( & mut self ,
432
420
sp : Span )
433
421
-> io:: Result < ( ) > {
434
- for trace in self . cm . macro_backtrace ( sp) {
435
- let mut diag_string =
436
- format ! ( "in this expansion of {}" , trace. macro_decl_name) ;
437
- if let Some ( def_site_span) = trace. def_site_span {
438
- diag_string. push_str (
439
- & format ! ( " (defined in {})" ,
440
- self . cm. span_to_filename( def_site_span) ) ) ;
422
+ if let Some ( ref cm) = self . cm {
423
+ for trace in cm. macro_backtrace ( sp) {
424
+ let mut diag_string =
425
+ format ! ( "in this expansion of {}" , trace. macro_decl_name) ;
426
+ if let Some ( def_site_span) = trace. def_site_span {
427
+ diag_string. push_str (
428
+ & format ! ( " (defined in {})" ,
429
+ cm. span_to_filename( def_site_span) ) ) ;
430
+ }
431
+ let snippet = cm. span_to_string ( trace. call_site ) ;
432
+ print_diagnostic ( & mut self . dst , & snippet, Note , & diag_string, None ) ?;
441
433
}
442
- let snippet = self . cm . span_to_string ( trace. call_site ) ;
443
- print_diagnostic ( & mut self . dst , & snippet, Note , & diag_string, None ) ?;
444
434
}
445
435
Ok ( ( ) )
446
436
}
0 commit comments