about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--src/librustdoc/lib.rs27
-rw-r--r--src/librustdoc/markdown.rs13
-rw-r--r--src/librustdoc/test.rs6
3 files changed, 25 insertions, 21 deletions
diff --git a/src/librustdoc/lib.rs b/src/librustdoc/lib.rs
index 8e5dfbeeda5..82d6cda986a 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 1977d3653ba..e0753bcd70f 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<P: AsRef<Path>>(
 }
 
 /// 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 5028bb46b00..4253318e35f 100644
--- a/src/librustdoc/test.rs
+++ b/src/librustdoc/test.rs
@@ -42,7 +42,7 @@ pub struct TestOptions {
     pub attrs: Vec<String>,
 }
 
-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.