Skip to content

Commit aeca4d6

Browse files
committed
Auto merge of #71900 - GuillaumeGomez:clean-up-rustdoc, r=ollie27,kinnison
Clean up rustdoc source code Fixes #70498. r? @kinnison cc @rust-lang/rustdoc
2 parents cb272d5 + c8fb0d7 commit aeca4d6

File tree

5 files changed

+51
-50
lines changed

5 files changed

+51
-50
lines changed

src/librustdoc/docfs.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use std::fs;
1313
use std::io;
1414
use std::path::Path;
15+
use std::string::ToString;
1516
use std::sync::mpsc::{channel, Receiver, Sender};
1617
use std::sync::Arc;
1718

@@ -25,7 +26,9 @@ macro_rules! try_err {
2526
}
2627

2728
pub trait PathError {
28-
fn new<P: AsRef<Path>>(e: io::Error, path: P) -> Self;
29+
fn new<S, P: AsRef<Path>>(e: S, path: P) -> Self
30+
where
31+
S: ToString + Sized;
2932
}
3033

3134
pub struct ErrorStorage {

src/librustdoc/html/render.rs

+10-7
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ use std::cmp::Ordering;
3131
use std::collections::{BTreeMap, VecDeque};
3232
use std::default::Default;
3333
use std::error;
34-
3534
use std::ffi::OsStr;
3635
use std::fmt::{self, Formatter, Write};
3736
use std::fs::{self, File};
@@ -40,6 +39,7 @@ use std::io::{self, BufReader};
4039
use std::path::{Component, Path, PathBuf};
4140
use std::rc::Rc;
4241
use std::str;
42+
use std::string::ToString;
4343
use std::sync::Arc;
4444

4545
use rustc_ast_pretty::pprust;
@@ -92,7 +92,7 @@ crate fn ensure_trailing_slash(v: &str) -> impl fmt::Display + '_ {
9292
#[derive(Debug)]
9393
pub struct Error {
9494
pub file: PathBuf,
95-
pub error: io::Error,
95+
pub error: String,
9696
}
9797

9898
impl error::Error for Error {}
@@ -109,8 +109,11 @@ impl std::fmt::Display for Error {
109109
}
110110

111111
impl PathError for Error {
112-
fn new<P: AsRef<Path>>(e: io::Error, path: P) -> Error {
113-
Error { file: path.as_ref().to_path_buf(), error: e }
112+
fn new<S, P: AsRef<Path>>(e: S, path: P) -> Error
113+
where
114+
S: ToString + Sized,
115+
{
116+
Error { file: path.as_ref().to_path_buf(), error: e.to_string() }
114117
}
115118
}
116119

@@ -562,7 +565,7 @@ pub fn run(
562565

563566
// Write shared runs within a flock; disable thread dispatching of IO temporarily.
564567
Arc::get_mut(&mut cx.shared).unwrap().fs.set_sync_only(true);
565-
write_shared(&cx, &krate, index, &md_opts, diag)?;
568+
write_shared(&cx, &krate, index, &md_opts)?;
566569
Arc::get_mut(&mut cx.shared).unwrap().fs.set_sync_only(false);
567570

568571
// And finally render the whole crate's documentation
@@ -582,7 +585,6 @@ fn write_shared(
582585
krate: &clean::Crate,
583586
search_index: String,
584587
options: &RenderOptions,
585-
diag: &rustc_errors::Handler,
586588
) -> Result<(), Error> {
587589
// Write out the shared files. Note that these are shared among all rustdoc
588590
// docs placed in the output directory, so this needs to be a synchronized
@@ -928,7 +930,8 @@ themePicker.onblur = handleThemeButtonsBlur;
928930
md_opts.output = cx.dst.clone();
929931
md_opts.external_html = (*cx.shared).layout.external_html.clone();
930932

931-
crate::markdown::render(index_page, md_opts, diag, cx.shared.edition);
933+
crate::markdown::render(&index_page, md_opts, cx.shared.edition)
934+
.map_err(|e| Error::new(e, &index_page))?;
932935
} else {
933936
let dst = cx.dst.join("index.html");
934937
let page = layout::Page {

src/librustdoc/lib.rs

+18-3
Original file line numberDiff line numberDiff line change
@@ -450,14 +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-
return markdown::render(options.input, options.render_options, &diag, options.edition);
472+
return wrap_return(
473+
&diag,
474+
markdown::render(&options.input, options.render_options, options.edition),
475+
);
461476
}
462477
(false, false) => {}
463478
}

src/librustdoc/markdown.rs

+16-36
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
use std::fs::{create_dir_all, File};
1+
use std::fs::{create_dir_all, read_to_string, File};
22
use std::io::prelude::*;
3-
use std::path::PathBuf;
3+
use std::path::Path;
44

55
use rustc_feature::UnstableFeatures;
66
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};
@@ -34,17 +33,16 @@ fn extract_leading_metadata(s: &str) -> (Vec<&str>, &str) {
3433

3534
/// Render `input` (e.g., "foo.md") into an HTML file in `output`
3635
/// (e.g., output = "bar" => "bar/foo.html").
37-
pub fn render(
38-
input: PathBuf,
36+
pub fn render<P: AsRef<Path>>(
37+
input: P,
3938
options: RenderOptions,
40-
diag: &rustc_errors::Handler,
4139
edition: Edition,
42-
) -> i32 {
40+
) -> Result<(), String> {
4341
if let Err(e) = create_dir_all(&options.output) {
44-
diag.struct_err(&format!("{}: {}", options.output.display(), e)).emit();
45-
return 4;
42+
return Err(format!("{}: {}", options.output.display(), e));
4643
}
4744

45+
let input = input.as_ref();
4846
let mut output = options.output;
4947
output.push(input.file_name().unwrap());
5048
output.set_extension("html");
@@ -55,26 +53,15 @@ pub fn render(
5553
css.push_str(&s)
5654
}
5755

58-
let input_str = match load_string(&input, diag) {
59-
Ok(s) => s,
60-
Err(LoadStringError::ReadFail) => return 1,
61-
Err(LoadStringError::BadUtf8) => return 2,
62-
};
56+
let input_str = read_to_string(input).map_err(|err| format!("{}: {}", input.display(), err))?;
6357
let playground_url = options.markdown_playground_url.or(options.playground_url);
6458
let playground = playground_url.map(|url| markdown::Playground { crate_name: None, url });
6559

66-
let mut out = match File::create(&output) {
67-
Err(e) => {
68-
diag.struct_err(&format!("{}: {}", output.display(), e)).emit();
69-
return 4;
70-
}
71-
Ok(f) => f,
72-
};
60+
let mut out = File::create(&output).map_err(|e| format!("{}: {}", output.display(), e))?;
7361

7462
let (metadata, text) = extract_leading_metadata(&input_str);
7563
if metadata.is_empty() {
76-
diag.struct_err("invalid markdown file: no initial lines starting with `# ` or `%`").emit();
77-
return 5;
64+
return Err("invalid markdown file: no initial lines starting with `# ` or `%`".to_owned());
7865
}
7966
let title = metadata[0];
8067

@@ -122,22 +109,15 @@ pub fn render(
122109
);
123110

124111
match err {
125-
Err(e) => {
126-
diag.struct_err(&format!("cannot write to `{}`: {}", output.display(), e)).emit();
127-
6
128-
}
129-
Ok(_) => 0,
112+
Err(e) => Err(format!("cannot write to `{}`: {}", output.display(), e)),
113+
Ok(_) => Ok(()),
130114
}
131115
}
132116

133117
/// Runs any tests/code examples in the markdown file `input`.
134-
pub fn test(mut options: Options, diag: &rustc_errors::Handler) -> i32 {
135-
let input_str = match load_string(&options.input, diag) {
136-
Ok(s) => s,
137-
Err(LoadStringError::ReadFail) => return 1,
138-
Err(LoadStringError::BadUtf8) => return 2,
139-
};
140-
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))?;
141121
let mut opts = TestOptions::default();
142122
opts.no_crate_inject = true;
143123
opts.display_warnings = options.display_warnings;
@@ -161,5 +141,5 @@ pub fn test(mut options: Options, diag: &rustc_errors::Handler) -> i32 {
161141
collector.tests,
162142
Some(testing::Options::new().display_output(options.display_warnings)),
163143
);
164-
0
144+
Ok(())
165145
}

src/librustdoc/test.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ pub struct TestOptions {
4343
pub attrs: Vec<String>,
4444
}
4545

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

4949
let invalid_codeblock_attribute_name = rustc_lint::builtin::INVALID_CODEBLOCK_ATTRIBUTE.name;
@@ -151,7 +151,7 @@ pub fn run(options: Options) -> i32 {
151151
});
152152
let tests = match tests {
153153
Ok(tests) => tests,
154-
Err(ErrorReported) => return 1,
154+
Err(ErrorReported) => return Err(String::new()),
155155
};
156156

157157
test_args.insert(0, "rustdoctest".to_string());
@@ -162,7 +162,7 @@ pub fn run(options: Options) -> i32 {
162162
Some(testing::Options::new().display_output(display_warnings)),
163163
);
164164

165-
0
165+
Ok(())
166166
}
167167

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

0 commit comments

Comments
 (0)