@@ -135,6 +135,9 @@ pub struct EmitterWriter {
135135 /// Is this the first error emitted thus far? If not, we emit a
136136 /// `\n` before the top-level errors.
137137 first : bool ,
138+
139+ // For now, allow an old-school mode while we transition
140+ old_school : bool ,
138141}
139142
140143impl CoreEmitter for EmitterWriter {
@@ -170,22 +173,39 @@ impl EmitterWriter {
170173 registry : Option < diagnostics:: registry:: Registry > ,
171174 code_map : Rc < codemap:: CodeMap > )
172175 -> EmitterWriter {
176+ let old_school = match :: std:: env:: var ( "RUST_NEW_ERROR_FORMAT" ) {
177+ Ok ( _) => false ,
178+ Err ( _) => true ,
179+ } ;
173180 if color_config. use_color ( ) {
174181 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 }
176187 } else {
177188 EmitterWriter { dst : Raw ( Box :: new ( io:: stderr ( ) ) ) ,
178189 registry : registry,
179190 cm : code_map,
180- first : true }
191+ first : true ,
192+ old_school : old_school }
181193 }
182194 }
183195
184196 pub fn new ( dst : Box < Write + Send > ,
185197 registry : Option < diagnostics:: registry:: Registry > ,
186198 code_map : Rc < codemap:: CodeMap > )
187199 -> 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 }
189209 }
190210
191211 fn emit_message_ ( & mut self ,
@@ -199,7 +219,9 @@ impl EmitterWriter {
199219 if self . first {
200220 self . first = false ;
201221 } else {
202- write ! ( self . dst, "\n " ) ?;
222+ if !self . old_school {
223+ write ! ( self . dst, "\n " ) ?;
224+ }
203225 }
204226 }
205227
@@ -208,7 +230,17 @@ impl EmitterWriter {
208230 . and_then ( |registry| registry. find_description ( code) )
209231 . is_some ( ) => {
210232 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+ }
212244 }
213245 _ => {
214246 print_diagnostic ( & mut self . dst , "" , lvl, msg, code) ?
@@ -239,7 +271,24 @@ impl EmitterWriter {
239271 }
240272 }
241273 }
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+ }
243292 Ok ( ( ) )
244293 }
245294
@@ -282,19 +331,48 @@ impl EmitterWriter {
282331 {
283332 let mut snippet_data = SnippetData :: new ( self . cm . clone ( ) ,
284333 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+ }
289360 }
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 " ) ?;
296375 }
297- write ! ( & mut self . dst, "\n " ) ?;
298376 }
299377 Ok ( ( ) )
300378 }
@@ -327,15 +405,13 @@ fn line_num_max_digits(line: &codemap::LineInfo) -> usize {
327405 digits
328406}
329407
330-
331408fn print_diagnostic ( dst : & mut Destination ,
332409 topic : & str ,
333410 lvl : Level ,
334411 msg : & str ,
335412 code : Option < & str > )
336413 -> io:: Result < ( ) > {
337414 if !topic. is_empty ( ) {
338- dst. start_attr ( term:: Attr :: ForegroundColor ( lvl. color ( ) ) ) ?;
339415 write ! ( dst, "{}: " , topic) ?;
340416 dst. reset_attrs ( ) ?;
341417 }
@@ -346,10 +422,12 @@ fn print_diagnostic(dst: &mut Destination,
346422 write ! ( dst, ": " ) ?;
347423 dst. start_attr ( term:: Attr :: Bold ) ?;
348424 write ! ( dst, "{}" , msg) ?;
425+
349426 if let Some ( code) = code {
350427 let style = term:: Attr :: ForegroundColor ( term:: color:: BRIGHT_MAGENTA ) ;
351428 print_maybe_styled ! ( dst, style, " [{}]" , code. clone( ) ) ?;
352429 }
430+
353431 dst. reset_attrs ( ) ?;
354432 write ! ( dst, "\n " ) ?;
355433 Ok ( ( ) )
0 commit comments