From c8fb0d7ef38957cd2b8f6e5b9b7ea47eaa6cca28 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sat, 9 May 2020 13:37:26 +0200 Subject: [PATCH] End unification of exit codes in librustdoc --- src/librustdoc/lib.rs | 27 ++++++++++++++++++--------- src/librustdoc/markdown.rs | 13 ++++--------- src/librustdoc/test.rs | 6 +++--- 3 files changed, 25 insertions(+), 21 deletions(-) diff --git a/src/librustdoc/lib.rs b/src/librustdoc/lib.rs index 8e5dfbeeda5f..82d6cda986a9 100644 --- a/src/librustdoc/lib.rs +++ b/src/librustdoc/lib.rs @@ -450,20 +450,29 @@ fn main_args(args: &[String]) -> i32 { rustc_interface::interface::default_thread_pool(options.edition, move || main_options(options)) } +fn wrap_return(diag: &rustc_errors::Handler, res: Result<(), String>) -> i32 { + match res { + Ok(()) => 0, + Err(err) => { + if !err.is_empty() { + diag.struct_err(&err).emit(); + } + 1 + } + } +} + fn main_options(options: config::Options) -> i32 { let diag = core::new_handler(options.error_format, None, &options.debugging_options); match (options.should_test, options.markdown_input()) { - (true, true) => return markdown::test(options, &diag), - (true, false) => return test::run(options), + (true, true) => return wrap_return(&diag, markdown::test(options)), + (true, false) => return wrap_return(&diag, test::run(options)), (false, true) => { - match markdown::render(&options.input, options.render_options, options.edition) { - Ok(()) => return 0, - Err(err) => { - diag.struct_err(&err).emit(); - return 1; - } - } + return wrap_return( + &diag, + markdown::render(&options.input, options.render_options, options.edition), + ); } (false, false) => {} } diff --git a/src/librustdoc/markdown.rs b/src/librustdoc/markdown.rs index 1977d3653ba2..e0753bcd70f2 100644 --- a/src/librustdoc/markdown.rs +++ b/src/librustdoc/markdown.rs @@ -7,7 +7,6 @@ use rustc_span::edition::Edition; use rustc_span::source_map::DUMMY_SP; use crate::config::{Options, RenderOptions}; -use crate::externalfiles::{load_string, LoadStringError}; use crate::html::escape::Escape; use crate::html::markdown; use crate::html::markdown::{find_testable_code, ErrorCodes, IdMap, Markdown, MarkdownWithToc}; @@ -116,13 +115,9 @@ pub fn render>( } /// Runs any tests/code examples in the markdown file `input`. -pub fn test(mut options: Options, diag: &rustc_errors::Handler) -> i32 { - let input_str = match load_string(&options.input, diag) { - Ok(s) => s, - Err(LoadStringError::ReadFail) => return 1, - Err(LoadStringError::BadUtf8) => return 2, - }; - +pub fn test(mut options: Options) -> Result<(), String> { + let input_str = read_to_string(&options.input) + .map_err(|err| format!("{}: {}", options.input.display(), err))?; let mut opts = TestOptions::default(); opts.no_crate_inject = true; opts.display_warnings = options.display_warnings; @@ -146,5 +141,5 @@ pub fn test(mut options: Options, diag: &rustc_errors::Handler) -> i32 { collector.tests, Some(testing::Options::new().display_output(options.display_warnings)), ); - 0 + Ok(()) } diff --git a/src/librustdoc/test.rs b/src/librustdoc/test.rs index 5028bb46b006..4253318e35fc 100644 --- a/src/librustdoc/test.rs +++ b/src/librustdoc/test.rs @@ -42,7 +42,7 @@ pub struct TestOptions { pub attrs: Vec, } -pub fn run(options: Options) -> i32 { +pub fn run(options: Options) -> Result<(), String> { let input = config::Input::File(options.input.clone()); let warnings_lint_name = lint::builtin::WARNINGS.name; @@ -175,7 +175,7 @@ pub fn run(options: Options) -> i32 { }); let tests = match tests { Ok(tests) => tests, - Err(ErrorReported) => return 1, + Err(ErrorReported) => return Err(String::new()), }; test_args.insert(0, "rustdoctest".to_string()); @@ -186,7 +186,7 @@ pub fn run(options: Options) -> i32 { Some(testing::Options::new().display_output(display_warnings)), ); - 0 + Ok(()) } // Look for `#![doc(test(no_crate_inject))]`, used by crates in the std facade.