Skip to content

Commit 1d69e3b

Browse files
committed
Auto merge of #75124 - nnethercote:clean-up-rustdoc-main, r=oli-obk
Clean up rustdoc's `main()` It can be simplified and made more similar to rustc's `main()`. r? @oli-obk
2 parents 7f8ff84 + 5f8a112 commit 1d69e3b

File tree

3 files changed

+53
-73
lines changed

3 files changed

+53
-73
lines changed

src/librustc_interface/interface.rs

-11
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ use rustc_session::early_error;
1717
use rustc_session::lint;
1818
use rustc_session::parse::{CrateConfig, ParseSess};
1919
use rustc_session::{DiagnosticOutput, Session};
20-
use rustc_span::edition;
2120
use rustc_span::source_map::{FileLoader, FileName};
2221
use std::path::PathBuf;
2322
use std::result;
@@ -208,13 +207,3 @@ pub fn run_compiler<R: Send>(mut config: Config, f: impl FnOnce(&Compiler) -> R
208207
|| create_compiler_and_run(config, f),
209208
)
210209
}
211-
212-
pub fn setup_callbacks_and_run_in_default_thread_pool_with_globals<R: Send>(
213-
edition: edition::Edition,
214-
f: impl FnOnce() -> R + Send,
215-
) -> R {
216-
// the 1 here is duplicating code in config.opts.debugging_opts.threads
217-
// which also defaults to 1; it ultimately doesn't matter as the default
218-
// isn't threaded, and just ignores this parameter
219-
util::setup_callbacks_and_run_in_thread_pool_with_globals(edition, 1, &None, f)
220-
}

src/librustdoc/lib.rs

+51-60
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ extern crate tracing;
4747

4848
use std::default::Default;
4949
use std::env;
50-
use std::panic;
5150
use std::process;
5251

52+
use rustc_errors::ErrorReported;
5353
use rustc_session::config::{make_crate_type_option, ErrorOutputType, RustcOptGroup};
5454
use rustc_session::getopts;
5555
use rustc_session::{early_error, early_warn};
@@ -82,22 +82,14 @@ struct Output {
8282
}
8383

8484
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-
};
9085
rustc_driver::set_sigpipe_handler();
9186
rustc_driver::install_ice_hook();
9287
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);
10193
}
10294

10395
fn get_args() -> Option<Vec<String>> {
@@ -418,7 +410,10 @@ fn usage(argv0: &str) {
418410
println!("{}", options.usage(&format!("{} [options] <input>", argv0)));
419411
}
420412

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 {
422417
let mut options = getopts::Options::new();
423418
for option in opts() {
424419
(option.apply)(&mut options);
@@ -429,24 +424,27 @@ fn main_args(args: &[String]) -> i32 {
429424
early_error(ErrorOutputType::default(), &err.to_string());
430425
}
431426
};
427+
428+
// Note that we discard any distinction between different non-zero exit
429+
// codes from `from_matches` here.
432430
let options = match config::Options::from_matches(&matches) {
433431
Ok(opts) => opts,
434-
Err(code) => return code,
432+
Err(code) => return if code == 0 { Ok(()) } else { Err(ErrorReported) },
435433
};
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(
437435
options.edition,
436+
1, // this runs single-threaded, even in a parallel compiler
437+
&None,
438438
move || main_options(options),
439439
)
440440
}
441441

442-
fn wrap_return(diag: &rustc_errors::Handler, res: Result<(), String>) -> i32 {
442+
fn wrap_return(diag: &rustc_errors::Handler, res: Result<(), String>) -> MainResult {
443443
match res {
444-
Ok(()) => 0,
444+
Ok(()) => Ok(()),
445445
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)
450448
}
451449
}
452450
}
@@ -457,9 +455,9 @@ fn run_renderer<T: formats::FormatRenderer>(
457455
render_info: config::RenderInfo,
458456
diag: &rustc_errors::Handler,
459457
edition: rustc_span::edition::Edition,
460-
) -> i32 {
458+
) -> MainResult {
461459
match formats::run_format::<T>(krate, renderopts, render_info, &diag, edition) {
462-
Ok(_) => rustc_driver::EXIT_SUCCESS,
460+
Ok(_) => Ok(()),
463461
Err(e) => {
464462
let mut msg = diag.struct_err(&format!("couldn't generate documentation: {}", e.error));
465463
let file = e.file.display().to_string();
@@ -468,17 +466,17 @@ fn run_renderer<T: formats::FormatRenderer>(
468466
} else {
469467
msg.note(&format!("failed to create or modify \"{}\"", file)).emit()
470468
}
471-
rustc_driver::EXIT_FAILURE
469+
Err(ErrorReported)
472470
}
473471
}
474472
}
475473

476-
fn main_options(options: config::Options) -> i32 {
474+
fn main_options(options: config::Options) -> MainResult {
477475
let diag = core::new_handler(options.error_format, None, &options.debugging_options);
478476

479477
match (options.should_test, options.markdown_input()) {
480478
(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),
482480
(false, true) => {
483481
return wrap_return(
484482
&diag,
@@ -500,44 +498,37 @@ fn main_options(options: config::Options) -> i32 {
500498
// compiler all the way through the analysis passes. The rustdoc output is
501499
// then generated from the cleaned AST of the crate. This runs all the
502500
// 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);
508505

509-
info!("finished with rustc");
506+
info!("finished with rustc");
510507

511-
if let Some(name) = crate_name {
512-
krate.name = name
513-
}
508+
if let Some(name) = crate_name {
509+
krate.name = name
510+
}
514511

515-
krate.version = crate_version;
512+
krate.version = crate_version;
516513

517-
let out = Output { krate, renderinfo, renderopts };
514+
let out = Output { krate, renderinfo, renderopts };
518515

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+
}
524521

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)
536532
}
537-
});
538-
539-
match result {
540-
Ok(output) => output,
541-
Err(_) => panic::resume_unwind(Box::new(rustc_errors::FatalErrorMarker)),
542533
}
543534
}

src/librustdoc/test.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ pub struct TestOptions {
4242
pub attrs: Vec<String>,
4343
}
4444

45-
pub fn run(options: Options) -> Result<(), String> {
45+
pub fn run(options: Options) -> Result<(), ErrorReported> {
4646
let input = config::Input::File(options.input.clone());
4747

4848
let invalid_codeblock_attributes_name = rustc_lint::builtin::INVALID_CODEBLOCK_ATTRIBUTES.name;
@@ -150,7 +150,7 @@ pub fn run(options: Options) -> Result<(), String> {
150150
});
151151
let tests = match tests {
152152
Ok(tests) => tests,
153-
Err(ErrorReported) => return Err(String::new()),
153+
Err(ErrorReported) => return Err(ErrorReported),
154154
};
155155

156156
test_args.insert(0, "rustdoctest".to_string());

0 commit comments

Comments
 (0)