@@ -47,9 +47,9 @@ extern crate tracing;
47
47
48
48
use std:: default:: Default ;
49
49
use std:: env;
50
- use std:: panic;
51
50
use std:: process;
52
51
52
+ use rustc_errors:: ErrorReported ;
53
53
use rustc_session:: config:: { make_crate_type_option, ErrorOutputType , RustcOptGroup } ;
54
54
use rustc_session:: getopts;
55
55
use rustc_session:: { early_error, early_warn} ;
@@ -82,22 +82,14 @@ struct Output {
82
82
}
83
83
84
84
pub fn main ( ) {
85
- let thread_stack_size: usize = if cfg ! ( target_os = "haiku" ) {
86
- 16_000_000 // 16MB on Haiku
87
- } else {
88
- 32_000_000 // 32MB on other platforms
89
- } ;
90
85
rustc_driver:: set_sigpipe_handler ( ) ;
91
86
rustc_driver:: install_ice_hook ( ) ;
92
87
rustc_driver:: init_env_logger ( "RUSTDOC_LOG" ) ;
93
-
94
- let res = std:: thread:: Builder :: new ( )
95
- . stack_size ( thread_stack_size)
96
- . spawn ( move || get_args ( ) . map ( |args| main_args ( & args) ) . unwrap_or ( 1 ) )
97
- . unwrap ( )
98
- . join ( )
99
- . unwrap_or ( rustc_driver:: EXIT_FAILURE ) ;
100
- process:: exit ( res) ;
88
+ let exit_code = rustc_driver:: catch_with_exit_code ( || match get_args ( ) {
89
+ Some ( args) => main_args ( & args) ,
90
+ _ => Err ( ErrorReported ) ,
91
+ } ) ;
92
+ process:: exit ( exit_code) ;
101
93
}
102
94
103
95
fn get_args ( ) -> Option < Vec < String > > {
@@ -418,7 +410,10 @@ fn usage(argv0: &str) {
418
410
println ! ( "{}" , options. usage( & format!( "{} [options] <input>" , argv0) ) ) ;
419
411
}
420
412
421
- fn main_args ( args : & [ String ] ) -> i32 {
413
+ /// A result type used by several functions under `main()`.
414
+ type MainResult = Result < ( ) , ErrorReported > ;
415
+
416
+ fn main_args ( args : & [ String ] ) -> MainResult {
422
417
let mut options = getopts:: Options :: new ( ) ;
423
418
for option in opts ( ) {
424
419
( option. apply ) ( & mut options) ;
@@ -429,24 +424,27 @@ fn main_args(args: &[String]) -> i32 {
429
424
early_error ( ErrorOutputType :: default ( ) , & err. to_string ( ) ) ;
430
425
}
431
426
} ;
427
+
428
+ // Note that we discard any distinction between different non-zero exit
429
+ // codes from `from_matches` here.
432
430
let options = match config:: Options :: from_matches ( & matches) {
433
431
Ok ( opts) => opts,
434
- Err ( code) => return code,
432
+ Err ( code) => return if code == 0 { Ok ( ( ) ) } else { Err ( ErrorReported ) } ,
435
433
} ;
436
- rustc_interface:: interface :: setup_callbacks_and_run_in_default_thread_pool_with_globals (
434
+ rustc_interface:: util :: setup_callbacks_and_run_in_thread_pool_with_globals (
437
435
options. edition ,
436
+ 1 , // this runs single-threaded, even in a parallel compiler
437
+ & None ,
438
438
move || main_options ( options) ,
439
439
)
440
440
}
441
441
442
- fn wrap_return ( diag : & rustc_errors:: Handler , res : Result < ( ) , String > ) -> i32 {
442
+ fn wrap_return ( diag : & rustc_errors:: Handler , res : Result < ( ) , String > ) -> MainResult {
443
443
match res {
444
- Ok ( ( ) ) => 0 ,
444
+ Ok ( ( ) ) => Ok ( ( ) ) ,
445
445
Err ( err) => {
446
- if !err. is_empty ( ) {
447
- diag. struct_err ( & err) . emit ( ) ;
448
- }
449
- 1
446
+ diag. struct_err ( & err) . emit ( ) ;
447
+ Err ( ErrorReported )
450
448
}
451
449
}
452
450
}
@@ -457,9 +455,9 @@ fn run_renderer<T: formats::FormatRenderer>(
457
455
render_info : config:: RenderInfo ,
458
456
diag : & rustc_errors:: Handler ,
459
457
edition : rustc_span:: edition:: Edition ,
460
- ) -> i32 {
458
+ ) -> MainResult {
461
459
match formats:: run_format :: < T > ( krate, renderopts, render_info, & diag, edition) {
462
- Ok ( _) => rustc_driver :: EXIT_SUCCESS ,
460
+ Ok ( _) => Ok ( ( ) ) ,
463
461
Err ( e) => {
464
462
let mut msg = diag. struct_err ( & format ! ( "couldn't generate documentation: {}" , e. error) ) ;
465
463
let file = e. file . display ( ) . to_string ( ) ;
@@ -468,17 +466,17 @@ fn run_renderer<T: formats::FormatRenderer>(
468
466
} else {
469
467
msg. note ( & format ! ( "failed to create or modify \" {}\" " , file) ) . emit ( )
470
468
}
471
- rustc_driver :: EXIT_FAILURE
469
+ Err ( ErrorReported )
472
470
}
473
471
}
474
472
}
475
473
476
- fn main_options ( options : config:: Options ) -> i32 {
474
+ fn main_options ( options : config:: Options ) -> MainResult {
477
475
let diag = core:: new_handler ( options. error_format , None , & options. debugging_options ) ;
478
476
479
477
match ( options. should_test , options. markdown_input ( ) ) {
480
478
( true , true ) => return wrap_return ( & diag, markdown:: test ( options) ) ,
481
- ( true , false ) => return wrap_return ( & diag , test:: run ( options) ) ,
479
+ ( true , false ) => return test:: run ( options) ,
482
480
( false , true ) => {
483
481
return wrap_return (
484
482
& diag,
@@ -500,44 +498,37 @@ fn main_options(options: config::Options) -> i32 {
500
498
// compiler all the way through the analysis passes. The rustdoc output is
501
499
// then generated from the cleaned AST of the crate. This runs all the
502
500
// plug/cleaning passes.
503
- let result = rustc_driver:: catch_fatal_errors ( move || {
504
- let crate_name = options. crate_name . clone ( ) ;
505
- let crate_version = options. crate_version . clone ( ) ;
506
- let output_format = options. output_format ;
507
- let ( mut krate, renderinfo, renderopts) = core:: run_core ( options) ;
501
+ let crate_name = options. crate_name . clone ( ) ;
502
+ let crate_version = options. crate_version . clone ( ) ;
503
+ let output_format = options. output_format ;
504
+ let ( mut krate, renderinfo, renderopts) = core:: run_core ( options) ;
508
505
509
- info ! ( "finished with rustc" ) ;
506
+ info ! ( "finished with rustc" ) ;
510
507
511
- if let Some ( name) = crate_name {
512
- krate. name = name
513
- }
508
+ if let Some ( name) = crate_name {
509
+ krate. name = name
510
+ }
514
511
515
- krate. version = crate_version;
512
+ krate. version = crate_version;
516
513
517
- let out = Output { krate, renderinfo, renderopts } ;
514
+ let out = Output { krate, renderinfo, renderopts } ;
518
515
519
- if show_coverage {
520
- // if we ran coverage, bail early, we don't need to also generate docs at this point
521
- // (also we didn't load in any of the useful passes)
522
- return rustc_driver :: EXIT_SUCCESS ;
523
- }
516
+ if show_coverage {
517
+ // if we ran coverage, bail early, we don't need to also generate docs at this point
518
+ // (also we didn't load in any of the useful passes)
519
+ return Ok ( ( ) ) ;
520
+ }
524
521
525
- let Output { krate, renderinfo, renderopts } = out;
526
- info ! ( "going to format" ) ;
527
- let ( error_format, edition, debugging_options) = diag_opts;
528
- let diag = core:: new_handler ( error_format, None , & debugging_options) ;
529
- match output_format {
530
- None | Some ( config:: OutputFormat :: Html ) => {
531
- run_renderer :: < html:: render:: Context > ( krate, renderopts, renderinfo, & diag, edition)
532
- }
533
- Some ( config:: OutputFormat :: Json ) => {
534
- run_renderer :: < json:: JsonRenderer > ( krate, renderopts, renderinfo, & diag, edition)
535
- }
522
+ let Output { krate, renderinfo, renderopts } = out;
523
+ info ! ( "going to format" ) ;
524
+ let ( error_format, edition, debugging_options) = diag_opts;
525
+ let diag = core:: new_handler ( error_format, None , & debugging_options) ;
526
+ match output_format {
527
+ None | Some ( config:: OutputFormat :: Html ) => {
528
+ run_renderer :: < html:: render:: Context > ( krate, renderopts, renderinfo, & diag, edition)
529
+ }
530
+ Some ( config:: OutputFormat :: Json ) => {
531
+ run_renderer :: < json:: JsonRenderer > ( krate, renderopts, renderinfo, & diag, edition)
536
532
}
537
- } ) ;
538
-
539
- match result {
540
- Ok ( output) => output,
541
- Err ( _) => panic:: resume_unwind ( Box :: new ( rustc_errors:: FatalErrorMarker ) ) ,
542
533
}
543
534
}
0 commit comments