Skip to content

Commit c8fb0d7

Browse files
End unification of exit codes in librustdoc
1 parent 7e855b5 commit c8fb0d7

File tree

3 files changed

+25
-21
lines changed

3 files changed

+25
-21
lines changed

src/librustdoc/lib.rs

+18-9
Original file line numberDiff line numberDiff line change
@@ -450,20 +450,29 @@ fn main_args(args: &[String]) -> i32 {
450450
rustc_interface::interface::default_thread_pool(options.edition, move || main_options(options))
451451
}
452452

453+
fn wrap_return(diag: &rustc_errors::Handler, res: Result<(), String>) -> i32 {
454+
match res {
455+
Ok(()) => 0,
456+
Err(err) => {
457+
if !err.is_empty() {
458+
diag.struct_err(&err).emit();
459+
}
460+
1
461+
}
462+
}
463+
}
464+
453465
fn main_options(options: config::Options) -> i32 {
454466
let diag = core::new_handler(options.error_format, None, &options.debugging_options);
455467

456468
match (options.should_test, options.markdown_input()) {
457-
(true, true) => return markdown::test(options, &diag),
458-
(true, false) => return test::run(options),
469+
(true, true) => return wrap_return(&diag, markdown::test(options)),
470+
(true, false) => return wrap_return(&diag, test::run(options)),
459471
(false, true) => {
460-
match markdown::render(&options.input, options.render_options, options.edition) {
461-
Ok(()) => return 0,
462-
Err(err) => {
463-
diag.struct_err(&err).emit();
464-
return 1;
465-
}
466-
}
472+
return wrap_return(
473+
&diag,
474+
markdown::render(&options.input, options.render_options, options.edition),
475+
);
467476
}
468477
(false, false) => {}
469478
}

src/librustdoc/markdown.rs

+4-9
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use rustc_span::edition::Edition;
77
use rustc_span::source_map::DUMMY_SP;
88

99
use crate::config::{Options, RenderOptions};
10-
use crate::externalfiles::{load_string, LoadStringError};
1110
use crate::html::escape::Escape;
1211
use crate::html::markdown;
1312
use crate::html::markdown::{find_testable_code, ErrorCodes, IdMap, Markdown, MarkdownWithToc};
@@ -116,13 +115,9 @@ pub fn render<P: AsRef<Path>>(
116115
}
117116

118117
/// Runs any tests/code examples in the markdown file `input`.
119-
pub fn test(mut options: Options, diag: &rustc_errors::Handler) -> i32 {
120-
let input_str = match load_string(&options.input, diag) {
121-
Ok(s) => s,
122-
Err(LoadStringError::ReadFail) => return 1,
123-
Err(LoadStringError::BadUtf8) => return 2,
124-
};
125-
118+
pub fn test(mut options: Options) -> Result<(), String> {
119+
let input_str = read_to_string(&options.input)
120+
.map_err(|err| format!("{}: {}", options.input.display(), err))?;
126121
let mut opts = TestOptions::default();
127122
opts.no_crate_inject = true;
128123
opts.display_warnings = options.display_warnings;
@@ -146,5 +141,5 @@ pub fn test(mut options: Options, diag: &rustc_errors::Handler) -> i32 {
146141
collector.tests,
147142
Some(testing::Options::new().display_output(options.display_warnings)),
148143
);
149-
0
144+
Ok(())
150145
}

src/librustdoc/test.rs

+3-3
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) -> i32 {
45+
pub fn run(options: Options) -> Result<(), String> {
4646
let input = config::Input::File(options.input.clone());
4747

4848
let warnings_lint_name = lint::builtin::WARNINGS.name;
@@ -175,7 +175,7 @@ pub fn run(options: Options) -> i32 {
175175
});
176176
let tests = match tests {
177177
Ok(tests) => tests,
178-
Err(ErrorReported) => return 1,
178+
Err(ErrorReported) => return Err(String::new()),
179179
};
180180

181181
test_args.insert(0, "rustdoctest".to_string());
@@ -186,7 +186,7 @@ pub fn run(options: Options) -> i32 {
186186
Some(testing::Options::new().display_output(display_warnings)),
187187
);
188188

189-
0
189+
Ok(())
190190
}
191191

192192
// Look for `#![doc(test(no_crate_inject))]`, used by crates in the std facade.

0 commit comments

Comments
 (0)