@@ -189,58 +189,61 @@ impl Level {
189
189
}
190
190
}
191
191
192
- fn print_maybe_styled ( msg : & str , color : term:: attr:: Attr ) {
193
- local_data_key ! ( tls_terminal: ~Option <term:: Terminal <StdWriter >>)
192
+ fn print_maybe_styled ( msg : & str , color : term:: attr:: Attr ) -> io:: IoResult < ( ) > {
193
+ local_data_key ! ( tls_terminal: Option <term:: Terminal <StdWriter >>)
194
+
194
195
195
196
fn is_stderr_screen ( ) -> bool {
196
197
use std:: libc;
197
198
unsafe { libc:: isatty ( libc:: STDERR_FILENO ) != 0 }
198
199
}
199
- fn write_pretty < T : Writer > ( term : & mut term:: Terminal < T > , s : & str , c : term:: attr:: Attr ) {
200
- term. attr ( c) ;
201
- term. write ( s. as_bytes ( ) ) ;
202
- term. reset ( ) ;
200
+ fn write_pretty < T : Writer > ( term : & mut term:: Terminal < T > , s : & str ,
201
+ c : term:: attr:: Attr ) -> io:: IoResult < ( ) > {
202
+ if_ok ! ( term. attr( c) ) ;
203
+ if_ok ! ( term. write( s. as_bytes( ) ) ) ;
204
+ if_ok ! ( term. reset( ) ) ;
205
+ Ok ( ( ) )
203
206
}
204
207
205
208
if is_stderr_screen ( ) {
206
209
local_data:: get_mut ( tls_terminal, |term| {
207
210
match term {
208
211
Some ( term) => {
209
- match * * term {
212
+ match * term {
210
213
Some ( ref mut term) => write_pretty ( term, msg, color) ,
211
214
None => io:: stderr ( ) . write ( msg. as_bytes ( ) )
212
215
}
213
216
}
214
217
None => {
215
- let t = ~ match term:: Terminal :: new ( io:: stderr ( ) ) {
218
+ let ( t , ret ) = match term:: Terminal :: new ( io:: stderr ( ) ) {
216
219
Ok ( mut term) => {
217
- write_pretty ( & mut term, msg, color) ;
218
- Some ( term)
220
+ let r = write_pretty ( & mut term, msg, color) ;
221
+ ( Some ( term) , r )
219
222
}
220
223
Err ( _) => {
221
- io:: stderr ( ) . write ( msg. as_bytes ( ) ) ;
222
- None
224
+ ( None , io:: stderr ( ) . write ( msg. as_bytes ( ) ) )
223
225
}
224
226
} ;
225
227
local_data:: set ( tls_terminal, t) ;
228
+ ret
226
229
}
227
230
}
228
- } ) ;
231
+ } )
229
232
} else {
230
- io:: stderr ( ) . write ( msg. as_bytes ( ) ) ;
233
+ io:: stderr ( ) . write ( msg. as_bytes ( ) )
231
234
}
232
235
}
233
236
234
- fn print_diagnostic ( topic : & str , lvl : Level , msg : & str ) {
235
- let mut stderr = io:: stderr ( ) ;
236
-
237
+ fn print_diagnostic ( topic : & str , lvl : Level , msg : & str ) -> io:: IoResult < ( ) > {
237
238
if !topic. is_empty ( ) {
238
- write ! ( & mut stderr as & mut io:: Writer , "{} " , topic) ;
239
+ let mut stderr = io:: stderr ( ) ;
240
+ if_ok ! ( write!( & mut stderr as & mut io:: Writer , "{} " , topic) ) ;
239
241
}
240
242
241
- print_maybe_styled ( format ! ( "{}: " , lvl. to_str( ) ) ,
242
- term:: attr:: ForegroundColor ( lvl. color ( ) ) ) ;
243
- print_maybe_styled ( format ! ( "{}\n " , msg) , term:: attr:: Bold ) ;
243
+ if_ok ! ( print_maybe_styled( format!( "{}: " , lvl. to_str( ) ) ,
244
+ term:: attr:: ForegroundColor ( lvl. color( ) ) ) ) ;
245
+ if_ok ! ( print_maybe_styled( format!( "{}\n " , msg) , term:: attr:: Bold ) ) ;
246
+ Ok ( ( ) )
244
247
}
245
248
246
249
pub struct DefaultEmitter ;
@@ -250,20 +253,28 @@ impl Emitter for DefaultEmitter {
250
253
cmsp : Option < ( & codemap:: CodeMap , Span ) > ,
251
254
msg : & str ,
252
255
lvl : Level ) {
253
- match cmsp {
256
+ let error = match cmsp {
254
257
Some ( ( cm, sp) ) => emit ( cm, sp, msg, lvl, false ) ,
255
258
None => print_diagnostic ( "" , lvl, msg) ,
259
+ } ;
260
+
261
+ match error {
262
+ Ok ( ( ) ) => { }
263
+ Err ( e) => fail ! ( "failed to print diagnostics: {}" , e) ,
256
264
}
257
265
}
258
266
259
267
fn custom_emit ( & self , cm : & codemap:: CodeMap ,
260
268
sp : Span , msg : & str , lvl : Level ) {
261
- emit ( cm, sp, msg, lvl, true ) ;
269
+ match emit ( cm, sp, msg, lvl, true ) {
270
+ Ok ( ( ) ) => { }
271
+ Err ( e) => fail ! ( "failed to print diagnostics: {}" , e) ,
272
+ }
262
273
}
263
274
}
264
275
265
276
fn emit ( cm : & codemap:: CodeMap , sp : Span ,
266
- msg : & str , lvl : Level , custom : bool ) {
277
+ msg : & str , lvl : Level , custom : bool ) -> io :: IoResult < ( ) > {
267
278
let ss = cm. span_to_str ( sp) ;
268
279
let lines = cm. span_to_lines ( sp) ;
269
280
if custom {
@@ -272,19 +283,19 @@ fn emit(cm: &codemap::CodeMap, sp: Span,
272
283
// the span)
273
284
let span_end = Span { lo : sp. hi , hi : sp. hi , expn_info : sp. expn_info } ;
274
285
let ses = cm. span_to_str ( span_end) ;
275
- print_diagnostic ( ses, lvl, msg) ;
276
- custom_highlight_lines ( cm, sp, lvl, lines) ;
286
+ if_ok ! ( print_diagnostic( ses, lvl, msg) ) ;
287
+ if_ok ! ( custom_highlight_lines( cm, sp, lvl, lines) ) ;
277
288
} else {
278
- print_diagnostic ( ss, lvl, msg) ;
279
- highlight_lines ( cm, sp, lvl, lines) ;
289
+ if_ok ! ( print_diagnostic( ss, lvl, msg) ) ;
290
+ if_ok ! ( highlight_lines( cm, sp, lvl, lines) ) ;
280
291
}
281
- print_macro_backtrace ( cm, sp) ;
292
+ print_macro_backtrace ( cm, sp)
282
293
}
283
294
284
295
fn highlight_lines ( cm : & codemap:: CodeMap ,
285
296
sp : Span ,
286
297
lvl : Level ,
287
- lines : & codemap:: FileLines ) {
298
+ lines : & codemap:: FileLines ) -> io :: IoResult < ( ) > {
288
299
let fm = lines. file ;
289
300
let mut err = io:: stderr ( ) ;
290
301
let err = & mut err as & mut io:: Writer ;
@@ -297,12 +308,13 @@ fn highlight_lines(cm: &codemap::CodeMap,
297
308
}
298
309
// Print the offending lines
299
310
for line in display_lines. iter ( ) {
300
- write ! ( err, "{}:{} {}\n " , fm. name, * line + 1 , fm. get_line( * line as int) ) ;
311
+ if_ok ! ( write!( err, "{}:{} {}\n " , fm. name, * line + 1 ,
312
+ fm. get_line( * line as int) ) ) ;
301
313
}
302
314
if elided {
303
315
let last_line = display_lines[ display_lines. len ( ) - 1 u] ;
304
316
let s = format ! ( "{}:{} " , fm. name, last_line + 1 u) ;
305
- write ! ( err, "{0:1$}...\n " , "" , s. len( ) ) ;
317
+ if_ok ! ( write!( err, "{0:1$}...\n " , "" , s. len( ) ) ) ;
306
318
}
307
319
308
320
// FIXME (#3260)
@@ -334,16 +346,18 @@ fn highlight_lines(cm: &codemap::CodeMap,
334
346
_ => s. push_char ( ' ' ) ,
335
347
} ;
336
348
}
337
- write ! ( err, "{}" , s) ;
349
+ if_ok ! ( write!( err, "{}" , s) ) ;
338
350
let mut s = ~"^";
339
351
let hi = cm. lookup_char_pos ( sp. hi ) ;
340
352
if hi. col != lo. col {
341
353
// the ^ already takes up one space
342
354
let num_squigglies = hi. col . to_uint ( ) -lo. col . to_uint ( ) -1 u;
343
355
for _ in range ( 0 , num_squigglies) { s. push_char ( '~' ) ; }
344
356
}
345
- print_maybe_styled ( s + "\n " , term:: attr:: ForegroundColor ( lvl. color ( ) ) ) ;
357
+ if_ok ! ( print_maybe_styled( s + "\n " ,
358
+ term:: attr:: ForegroundColor ( lvl. color( ) ) ) ) ;
346
359
}
360
+ Ok ( ( ) )
347
361
}
348
362
349
363
// Here are the differences between this and the normal `highlight_lines`:
@@ -355,23 +369,23 @@ fn highlight_lines(cm: &codemap::CodeMap,
355
369
fn custom_highlight_lines ( cm : & codemap:: CodeMap ,
356
370
sp : Span ,
357
371
lvl : Level ,
358
- lines : & codemap:: FileLines ) {
372
+ lines : & codemap:: FileLines ) -> io :: IoResult < ( ) > {
359
373
let fm = lines. file ;
360
374
let mut err = io:: stderr ( ) ;
361
375
let err = & mut err as & mut io:: Writer ;
362
376
363
377
let lines = lines. lines . as_slice ( ) ;
364
378
if lines. len ( ) > MAX_LINES {
365
- write ! ( err, "{}:{} {}\n " , fm. name,
366
- lines[ 0 ] + 1 , fm. get_line( lines[ 0 ] as int) ) ;
367
- write ! ( err, "...\n " ) ;
379
+ if_ok ! ( write!( err, "{}:{} {}\n " , fm. name,
380
+ lines[ 0 ] + 1 , fm. get_line( lines[ 0 ] as int) ) ) ;
381
+ if_ok ! ( write!( err, "...\n " ) ) ;
368
382
let last_line = lines[ lines. len ( ) -1 ] ;
369
- write ! ( err, "{}:{} {}\n " , fm. name,
370
- last_line + 1 , fm. get_line( last_line as int) ) ;
383
+ if_ok ! ( write!( err, "{}:{} {}\n " , fm. name,
384
+ last_line + 1 , fm. get_line( last_line as int) ) ) ;
371
385
} else {
372
386
for line in lines. iter ( ) {
373
- write ! ( err, "{}:{} {}\n " , fm. name,
374
- * line + 1 , fm. get_line( * line as int) ) ;
387
+ if_ok ! ( write!( err, "{}:{} {}\n " , fm. name,
388
+ * line + 1 , fm. get_line( * line as int) ) ) ;
375
389
}
376
390
}
377
391
let last_line_start = format ! ( "{}:{} " , fm. name, lines[ lines. len( ) -1 ] +1 ) ;
@@ -381,22 +395,24 @@ fn custom_highlight_lines(cm: &codemap::CodeMap,
381
395
let mut s = ~"";
382
396
for _ in range ( 0 , skip) { s. push_char ( ' ' ) ; }
383
397
s. push_char ( '^' ) ;
384
- print_maybe_styled ( s + "\n " , term:: attr:: ForegroundColor ( lvl. color ( ) ) ) ;
398
+ print_maybe_styled ( s + "\n " , term:: attr:: ForegroundColor ( lvl. color ( ) ) )
385
399
}
386
400
387
- fn print_macro_backtrace ( cm : & codemap:: CodeMap , sp : Span ) {
401
+ fn print_macro_backtrace ( cm : & codemap:: CodeMap , sp : Span ) -> io :: IoResult < ( ) > {
388
402
for ei in sp. expn_info . iter ( ) {
389
403
let ss = ei. callee . span . as_ref ( ) . map_or ( ~"", |span| cm. span_to_str ( * span) ) ;
390
404
let ( pre, post) = match ei. callee . format {
391
405
codemap:: MacroAttribute => ( "#[" , "]" ) ,
392
406
codemap:: MacroBang => ( "" , "!" )
393
407
} ;
394
- print_diagnostic ( ss, Note ,
395
- format ! ( "in expansion of {}{}{}" , pre, ei. callee. name, post) ) ;
408
+ if_ok ! ( print_diagnostic( ss, Note ,
409
+ format!( "in expansion of {}{}{}" , pre,
410
+ ei. callee. name, post) ) ) ;
396
411
let ss = cm. span_to_str ( ei. call_site ) ;
397
- print_diagnostic ( ss, Note , "expansion site" ) ;
398
- print_macro_backtrace ( cm, ei. call_site ) ;
412
+ if_ok ! ( print_diagnostic( ss, Note , "expansion site" ) ) ;
413
+ if_ok ! ( print_macro_backtrace( cm, ei. call_site) ) ;
399
414
}
415
+ Ok ( ( ) )
400
416
}
401
417
402
418
pub fn expect < T : Clone > ( diag : @SpanHandler , opt : Option < T > , msg: || -> ~str)
0 commit comments