@@ -135,6 +135,9 @@ pub struct EmitterWriter {
135
135
/// Is this the first error emitted thus far? If not, we emit a
136
136
/// `\n` before the top-level errors.
137
137
first : bool ,
138
+
139
+ // For now, allow an old-school mode while we transition
140
+ old_school : bool ,
138
141
}
139
142
140
143
impl CoreEmitter for EmitterWriter {
@@ -170,22 +173,39 @@ impl EmitterWriter {
170
173
registry : Option < diagnostics:: registry:: Registry > ,
171
174
code_map : Rc < codemap:: CodeMap > )
172
175
-> EmitterWriter {
176
+ let old_school = match :: std:: env:: var ( "RUST_NEW_ERROR_FORMAT" ) {
177
+ Ok ( _) => false ,
178
+ Err ( _) => true ,
179
+ } ;
173
180
if color_config. use_color ( ) {
174
181
let dst = Destination :: from_stderr ( ) ;
175
- EmitterWriter { dst : dst, registry : registry, cm : code_map, first : true }
182
+ EmitterWriter { dst : dst,
183
+ registry : registry,
184
+ cm : code_map,
185
+ first : true ,
186
+ old_school : old_school }
176
187
} else {
177
188
EmitterWriter { dst : Raw ( Box :: new ( io:: stderr ( ) ) ) ,
178
189
registry : registry,
179
190
cm : code_map,
180
- first : true }
191
+ first : true ,
192
+ old_school : old_school }
181
193
}
182
194
}
183
195
184
196
pub fn new ( dst : Box < Write + Send > ,
185
197
registry : Option < diagnostics:: registry:: Registry > ,
186
198
code_map : Rc < codemap:: CodeMap > )
187
199
-> EmitterWriter {
188
- EmitterWriter { dst : Raw ( dst) , registry : registry, cm : code_map, first : true }
200
+ let old_school = match :: std:: env:: var ( "RUST_NEW_ERROR_FORMAT" ) {
201
+ Ok ( _) => false ,
202
+ Err ( _) => true ,
203
+ } ;
204
+ EmitterWriter { dst : Raw ( dst) ,
205
+ registry : registry,
206
+ cm : code_map,
207
+ first : true ,
208
+ old_school : old_school }
189
209
}
190
210
191
211
fn emit_message_ ( & mut self ,
@@ -199,7 +219,9 @@ impl EmitterWriter {
199
219
if self . first {
200
220
self . first = false ;
201
221
} else {
202
- write ! ( self . dst, "\n " ) ?;
222
+ if !self . old_school {
223
+ write ! ( self . dst, "\n " ) ?;
224
+ }
203
225
}
204
226
}
205
227
@@ -208,7 +230,17 @@ impl EmitterWriter {
208
230
. and_then ( |registry| registry. find_description ( code) )
209
231
. is_some ( ) => {
210
232
let code_with_explain = String :: from ( "--explain " ) + code;
211
- print_diagnostic ( & mut self . dst , "" , lvl, msg, Some ( & code_with_explain) ) ?
233
+ if self . old_school {
234
+ let loc = match rsp. span ( ) . primary_span ( ) {
235
+ Some ( COMMAND_LINE_SP ) | Some ( DUMMY_SP ) => "" . to_string ( ) ,
236
+ Some ( ps) => self . cm . span_to_string ( ps) ,
237
+ None => "" . to_string ( )
238
+ } ;
239
+ print_diagnostic ( & mut self . dst , & loc, lvl, msg, Some ( code) ) ?
240
+ }
241
+ else {
242
+ print_diagnostic ( & mut self . dst , "" , lvl, msg, Some ( & code_with_explain) ) ?
243
+ }
212
244
}
213
245
_ => {
214
246
print_diagnostic ( & mut self . dst , "" , lvl, msg, code) ?
@@ -239,7 +271,24 @@ impl EmitterWriter {
239
271
}
240
272
}
241
273
}
242
-
274
+ if self . old_school {
275
+ match code {
276
+ Some ( code) if self . registry . as_ref ( )
277
+ . and_then ( |registry| registry. find_description ( code) )
278
+ . is_some ( ) => {
279
+ let loc = match rsp. span ( ) . primary_span ( ) {
280
+ Some ( COMMAND_LINE_SP ) | Some ( DUMMY_SP ) => "" . to_string ( ) ,
281
+ Some ( ps) => self . cm . span_to_string ( ps) ,
282
+ None => "" . to_string ( )
283
+ } ;
284
+ let msg = "run `rustc --explain " . to_string ( ) + & code. to_string ( ) +
285
+ "` to see a detailed explanation" ;
286
+ print_diagnostic ( & mut self . dst , & loc, Level :: Help , & msg,
287
+ None ) ?
288
+ }
289
+ _ => ( )
290
+ }
291
+ }
243
292
Ok ( ( ) )
244
293
}
245
294
@@ -282,19 +331,48 @@ impl EmitterWriter {
282
331
{
283
332
let mut snippet_data = SnippetData :: new ( self . cm . clone ( ) ,
284
333
msp. primary_span ( ) ) ;
285
- for span_label in msp. span_labels ( ) {
286
- snippet_data. push ( span_label. span ,
287
- span_label. is_primary ,
288
- span_label. label ) ;
334
+ if self . old_school {
335
+ let mut output_vec = vec ! [ ] ;
336
+ for span_label in msp. span_labels ( ) {
337
+ let mut snippet_data = snippet_data. clone ( ) ;
338
+ snippet_data. push ( span_label. span ,
339
+ span_label. is_primary ,
340
+ span_label. label ) ;
341
+ if span_label. is_primary {
342
+ output_vec. insert ( 0 , snippet_data) ;
343
+ }
344
+ else {
345
+ output_vec. push ( snippet_data) ;
346
+ }
347
+ }
348
+
349
+ for snippet_data in output_vec. iter ( ) {
350
+ let rendered_lines = snippet_data. render_lines ( ) ;
351
+ for rendered_line in & rendered_lines {
352
+ for styled_string in & rendered_line. text {
353
+ self . dst . apply_style ( lvl, & rendered_line. kind , styled_string. style ) ?;
354
+ write ! ( & mut self . dst, "{}" , styled_string. text) ?;
355
+ self . dst . reset_attrs ( ) ?;
356
+ }
357
+ write ! ( & mut self . dst, "\n " ) ?;
358
+ }
359
+ }
289
360
}
290
- let rendered_lines = snippet_data. render_lines ( ) ;
291
- for rendered_line in & rendered_lines {
292
- for styled_string in & rendered_line. text {
293
- self . dst . apply_style ( lvl, & rendered_line. kind , styled_string. style ) ?;
294
- write ! ( & mut self . dst, "{}" , styled_string. text) ?;
295
- self . dst . reset_attrs ( ) ?;
361
+ else {
362
+ for span_label in msp. span_labels ( ) {
363
+ snippet_data. push ( span_label. span ,
364
+ span_label. is_primary ,
365
+ span_label. label ) ;
366
+ }
367
+ let rendered_lines = snippet_data. render_lines ( ) ;
368
+ for rendered_line in & rendered_lines {
369
+ for styled_string in & rendered_line. text {
370
+ self . dst . apply_style ( lvl, & rendered_line. kind , styled_string. style ) ?;
371
+ write ! ( & mut self . dst, "{}" , styled_string. text) ?;
372
+ self . dst . reset_attrs ( ) ?;
373
+ }
374
+ write ! ( & mut self . dst, "\n " ) ?;
296
375
}
297
- write ! ( & mut self . dst, "\n " ) ?;
298
376
}
299
377
Ok ( ( ) )
300
378
}
@@ -327,15 +405,13 @@ fn line_num_max_digits(line: &codemap::LineInfo) -> usize {
327
405
digits
328
406
}
329
407
330
-
331
408
fn print_diagnostic ( dst : & mut Destination ,
332
409
topic : & str ,
333
410
lvl : Level ,
334
411
msg : & str ,
335
412
code : Option < & str > )
336
413
-> io:: Result < ( ) > {
337
414
if !topic. is_empty ( ) {
338
- dst. start_attr ( term:: Attr :: ForegroundColor ( lvl. color ( ) ) ) ?;
339
415
write ! ( dst, "{}: " , topic) ?;
340
416
dst. reset_attrs ( ) ?;
341
417
}
@@ -346,10 +422,12 @@ fn print_diagnostic(dst: &mut Destination,
346
422
write ! ( dst, ": " ) ?;
347
423
dst. start_attr ( term:: Attr :: Bold ) ?;
348
424
write ! ( dst, "{}" , msg) ?;
425
+
349
426
if let Some ( code) = code {
350
427
let style = term:: Attr :: ForegroundColor ( term:: color:: BRIGHT_MAGENTA ) ;
351
428
print_maybe_styled ! ( dst, style, " [{}]" , code. clone( ) ) ?;
352
429
}
430
+
353
431
dst. reset_attrs ( ) ?;
354
432
write ! ( dst, "\n " ) ?;
355
433
Ok ( ( ) )
0 commit comments