@@ -56,15 +56,13 @@ use errors::ColorConfig;
56
56
use std:: collections:: { BTreeMap , BTreeSet } ;
57
57
use std:: default:: Default ;
58
58
use std:: env;
59
- use std:: fmt:: Display ;
60
- use std:: io;
61
- use std:: io:: Write ;
62
59
use std:: path:: { Path , PathBuf } ;
63
60
use std:: process;
64
61
use std:: sync:: mpsc:: channel;
65
62
66
63
use syntax:: edition:: Edition ;
67
64
use externalfiles:: ExternalHtml ;
65
+ use rustc:: session:: { early_warn, early_error} ;
68
66
use rustc:: session:: search_paths:: SearchPaths ;
69
67
use rustc:: session:: config:: { ErrorOutputType , RustcOptGroup , Externs , CodegenOptions } ;
70
68
use rustc:: session:: config:: { nightly_options, build_codegen_options} ;
@@ -118,7 +116,8 @@ pub fn main() {
118
116
fn get_args ( ) -> Option < Vec < String > > {
119
117
env:: args_os ( ) . enumerate ( )
120
118
. map ( |( i, arg) | arg. into_string ( ) . map_err ( |arg| {
121
- print_error ( format ! ( "Argument {} is not valid Unicode: {:?}" , i, arg) ) ;
119
+ early_warn ( ErrorOutputType :: default ( ) ,
120
+ & format ! ( "Argument {} is not valid Unicode: {:?}" , i, arg) ) ;
122
121
} ) . ok ( ) )
123
122
. collect ( )
124
123
}
@@ -318,16 +317,12 @@ pub fn main_args(args: &[String]) -> isize {
318
317
let matches = match options. parse ( & args[ 1 ..] ) {
319
318
Ok ( m) => m,
320
319
Err ( err) => {
321
- print_error ( err) ;
322
- return 1 ;
320
+ early_error ( ErrorOutputType :: default ( ) , & err. to_string ( ) ) ;
323
321
}
324
322
} ;
325
323
// Check for unstable options.
326
324
nightly_options:: check_nightly_options ( & matches, & opts ( ) ) ;
327
325
328
- // check for deprecated options
329
- check_deprecated_options ( & matches) ;
330
-
331
326
if matches. opt_present ( "h" ) || matches. opt_present ( "help" ) {
332
327
usage ( "rustdoc" ) ;
333
328
return 0 ;
@@ -348,6 +343,35 @@ pub fn main_args(args: &[String]) -> isize {
348
343
return 0 ;
349
344
}
350
345
346
+ let color = match matches. opt_str ( "color" ) . as_ref ( ) . map ( |s| & s[ ..] ) {
347
+ Some ( "auto" ) => ColorConfig :: Auto ,
348
+ Some ( "always" ) => ColorConfig :: Always ,
349
+ Some ( "never" ) => ColorConfig :: Never ,
350
+ None => ColorConfig :: Auto ,
351
+ Some ( arg) => {
352
+ early_error ( ErrorOutputType :: default ( ) ,
353
+ & format ! ( "argument for --color must be `auto`, `always` or `never` \
354
+ (instead was `{}`)", arg) ) ;
355
+ }
356
+ } ;
357
+ let error_format = match matches. opt_str ( "error-format" ) . as_ref ( ) . map ( |s| & s[ ..] ) {
358
+ Some ( "human" ) => ErrorOutputType :: HumanReadable ( color) ,
359
+ Some ( "json" ) => ErrorOutputType :: Json ( false ) ,
360
+ Some ( "pretty-json" ) => ErrorOutputType :: Json ( true ) ,
361
+ Some ( "short" ) => ErrorOutputType :: Short ( color) ,
362
+ None => ErrorOutputType :: HumanReadable ( color) ,
363
+ Some ( arg) => {
364
+ early_error ( ErrorOutputType :: default ( ) ,
365
+ & format ! ( "argument for --error-format must be `human`, `json` or \
366
+ `short` (instead was `{}`)", arg) ) ;
367
+ }
368
+ } ;
369
+
370
+ let diag = core:: new_handler ( error_format, None ) ;
371
+
372
+ // check for deprecated options
373
+ check_deprecated_options ( & matches, & diag) ;
374
+
351
375
let to_check = matches. opt_strs ( "theme-checker" ) ;
352
376
if !to_check. is_empty ( ) {
353
377
let paths = theme:: load_css_paths ( include_bytes ! ( "html/static/themes/light.css" ) ) ;
@@ -356,7 +380,7 @@ pub fn main_args(args: &[String]) -> isize {
356
380
println ! ( "rustdoc: [theme-checker] Starting tests!" ) ;
357
381
for theme_file in to_check. iter ( ) {
358
382
print ! ( " - Checking \" {}\" ..." , theme_file) ;
359
- let ( success, differences) = theme:: test_theme_against ( theme_file, & paths) ;
383
+ let ( success, differences) = theme:: test_theme_against ( theme_file, & paths, & diag ) ;
360
384
if !differences. is_empty ( ) || !success {
361
385
println ! ( " FAILED" ) ;
362
386
errors += 1 ;
@@ -374,47 +398,23 @@ pub fn main_args(args: &[String]) -> isize {
374
398
}
375
399
376
400
if matches. free . is_empty ( ) {
377
- print_error ( "missing file operand" ) ;
401
+ diag . struct_err ( "missing file operand" ) . emit ( ) ;
378
402
return 1 ;
379
403
}
380
404
if matches. free . len ( ) > 1 {
381
- print_error ( "too many file operands" ) ;
405
+ diag . struct_err ( "too many file operands" ) . emit ( ) ;
382
406
return 1 ;
383
407
}
384
408
let input = & matches. free [ 0 ] ;
385
409
386
- let color = match matches. opt_str ( "color" ) . as_ref ( ) . map ( |s| & s[ ..] ) {
387
- Some ( "auto" ) => ColorConfig :: Auto ,
388
- Some ( "always" ) => ColorConfig :: Always ,
389
- Some ( "never" ) => ColorConfig :: Never ,
390
- None => ColorConfig :: Auto ,
391
- Some ( arg) => {
392
- print_error ( & format ! ( "argument for --color must be `auto`, `always` or `never` \
393
- (instead was `{}`)", arg) ) ;
394
- return 1 ;
395
- }
396
- } ;
397
- let error_format = match matches. opt_str ( "error-format" ) . as_ref ( ) . map ( |s| & s[ ..] ) {
398
- Some ( "human" ) => ErrorOutputType :: HumanReadable ( color) ,
399
- Some ( "json" ) => ErrorOutputType :: Json ( false ) ,
400
- Some ( "pretty-json" ) => ErrorOutputType :: Json ( true ) ,
401
- Some ( "short" ) => ErrorOutputType :: Short ( color) ,
402
- None => ErrorOutputType :: HumanReadable ( color) ,
403
- Some ( arg) => {
404
- print_error ( & format ! ( "argument for --error-format must be `human`, `json` or \
405
- `short` (instead was `{}`)", arg) ) ;
406
- return 1 ;
407
- }
408
- } ;
409
-
410
410
let mut libs = SearchPaths :: new ( ) ;
411
411
for s in & matches. opt_strs ( "L" ) {
412
412
libs. add_path ( s, error_format) ;
413
413
}
414
414
let externs = match parse_externs ( & matches) {
415
415
Ok ( ex) => ex,
416
416
Err ( err) => {
417
- print_error ( err) ;
417
+ diag . struct_err ( & err. to_string ( ) ) . emit ( ) ;
418
418
return 1 ;
419
419
}
420
420
} ;
@@ -435,10 +435,7 @@ pub fn main_args(args: &[String]) -> isize {
435
435
436
436
if let Some ( ref p) = css_file_extension {
437
437
if !p. is_file ( ) {
438
- writeln ! (
439
- & mut io:: stderr( ) ,
440
- "rustdoc: option --extend-css argument must be a file."
441
- ) . unwrap ( ) ;
438
+ diag. struct_err ( "option --extend-css argument must be a file" ) . emit ( ) ;
442
439
return 1 ;
443
440
}
444
441
}
@@ -451,13 +448,14 @@ pub fn main_args(args: &[String]) -> isize {
451
448
. iter ( )
452
449
. map ( |s| ( PathBuf :: from ( & s) , s. to_owned ( ) ) ) {
453
450
if !theme_file. is_file ( ) {
454
- println ! ( "rustdoc: option --themes arguments must all be files") ;
451
+ diag . struct_err ( " option --themes arguments must all be files") . emit ( ) ;
455
452
return 1 ;
456
453
}
457
- let ( success, ret) = theme:: test_theme_against ( & theme_file, & paths) ;
454
+ let ( success, ret) = theme:: test_theme_against ( & theme_file, & paths, & diag ) ;
458
455
if !success || !ret. is_empty ( ) {
459
- println ! ( "rustdoc: invalid theme: \" {}\" " , theme_s) ;
460
- println ! ( " Check what's wrong with the \" theme-checker\" option" ) ;
456
+ diag. struct_err ( & format ! ( "invalid theme: \" {}\" " , theme_s) )
457
+ . help ( "check what's wrong with the --theme-checker option" )
458
+ . emit ( ) ;
461
459
return 1 ;
462
460
}
463
461
themes. push ( theme_file) ;
@@ -469,7 +467,7 @@ pub fn main_args(args: &[String]) -> isize {
469
467
& matches. opt_strs ( "html-before-content" ) ,
470
468
& matches. opt_strs ( "html-after-content" ) ,
471
469
& matches. opt_strs ( "markdown-before-content" ) ,
472
- & matches. opt_strs ( "markdown-after-content" ) ) {
470
+ & matches. opt_strs ( "markdown-after-content" ) , & diag ) {
473
471
Some ( eh) => eh,
474
472
None => return 3 ,
475
473
} ;
@@ -485,7 +483,7 @@ pub fn main_args(args: &[String]) -> isize {
485
483
let edition = match edition. parse ( ) {
486
484
Ok ( e) => e,
487
485
Err ( _) => {
488
- print_error ( "could not parse edition" ) ;
486
+ diag . struct_err ( "could not parse edition" ) . emit ( ) ;
489
487
return 1 ;
490
488
}
491
489
} ;
@@ -495,7 +493,7 @@ pub fn main_args(args: &[String]) -> isize {
495
493
match ( should_test, markdown_input) {
496
494
( true , true ) => {
497
495
return markdown:: test ( input, cfgs, libs, externs, test_args, maybe_sysroot,
498
- display_warnings, linker, edition, cg)
496
+ display_warnings, linker, edition, cg, & diag )
499
497
}
500
498
( true , false ) => {
501
499
return test:: run ( Path :: new ( input) , cfgs, libs, externs, test_args, crate_name,
@@ -504,7 +502,7 @@ pub fn main_args(args: &[String]) -> isize {
504
502
( false , true ) => return markdown:: render ( Path :: new ( input) ,
505
503
output. unwrap_or ( PathBuf :: from ( "doc" ) ) ,
506
504
& matches, & external_html,
507
- !matches. opt_present ( "markdown-no-toc" ) ) ,
505
+ !matches. opt_present ( "markdown-no-toc" ) , & diag ) ,
508
506
( false , false ) => { }
509
507
}
510
508
@@ -513,6 +511,7 @@ pub fn main_args(args: &[String]) -> isize {
513
511
let res = acquire_input ( PathBuf :: from ( input) , externs, edition, cg, & matches, error_format,
514
512
move |out| {
515
513
let Output { krate, passes, renderinfo } = out;
514
+ let diag = core:: new_handler ( error_format, None ) ;
516
515
info ! ( "going to format" ) ;
517
516
match output_format. as_ref ( ) . map ( |s| & * * s) {
518
517
Some ( "html" ) | None => {
@@ -528,26 +527,17 @@ pub fn main_args(args: &[String]) -> isize {
528
527
0
529
528
}
530
529
Some ( s) => {
531
- print_error ( format ! ( "unknown output format: {}" , s) ) ;
530
+ diag . struct_err ( & format ! ( "unknown output format: {}" , s) ) . emit ( ) ;
532
531
1
533
532
}
534
533
}
535
534
} ) ;
536
535
res. unwrap_or_else ( |s| {
537
- print_error ( format ! ( "input error: {}" , s) ) ;
536
+ diag . struct_err ( & format ! ( "input error: {}" , s) ) . emit ( ) ;
538
537
1
539
538
} )
540
539
}
541
540
542
- /// Prints an uniformized error message on the standard error output
543
- fn print_error < T > ( error_message : T ) where T : Display {
544
- writeln ! (
545
- & mut io:: stderr( ) ,
546
- "rustdoc: {}\n Try 'rustdoc --help' for more information." ,
547
- error_message
548
- ) . unwrap ( ) ;
549
- }
550
-
551
541
/// Looks inside the command line arguments to extract the relevant input format
552
542
/// and files and then generates the necessary rustdoc output for formatting.
553
543
fn acquire_input < R , F > ( input : PathBuf ,
@@ -714,7 +704,7 @@ where R: 'static + Send,
714
704
}
715
705
716
706
/// Prints deprecation warnings for deprecated options
717
- fn check_deprecated_options ( matches : & getopts:: Matches ) {
707
+ fn check_deprecated_options ( matches : & getopts:: Matches , diag : & errors :: Handler ) {
718
708
let deprecated_flags = [
719
709
"input-format" ,
720
710
"output-format" ,
@@ -726,12 +716,14 @@ fn check_deprecated_options(matches: &getopts::Matches) {
726
716
727
717
for flag in deprecated_flags. into_iter ( ) {
728
718
if matches. opt_present ( flag) {
729
- eprintln ! ( "WARNING: the '{}' flag is considered deprecated" , flag) ;
730
- eprintln ! ( "WARNING: please see https://github.com/rust-lang/rust/issues/44136" ) ;
731
- }
732
- }
719
+ let mut err = diag. struct_warn ( & format ! ( "the '{}' flag is considered deprecated" , flag) ) ;
720
+ err. warn ( "please see https://github.com/rust-lang/rust/issues/44136" ) ;
733
721
734
- if matches. opt_present ( "no-defaults" ) {
735
- eprintln ! ( "WARNING: (you may want to use --document-private-items)" ) ;
722
+ if * flag == "no-defaults" {
723
+ err. help ( "you may want to use --document-private-items" ) ;
724
+ }
725
+
726
+ err. emit ( ) ;
727
+ }
736
728
}
737
729
}
0 commit comments