Skip to content

Commit 021d1fb

Browse files
committed
Clean up rustdoc startup.
rustc's startup has several layers, including: - `interface::run_compiler` passes a closure, `f`, to `run_in_thread_pool_with_globals`, which creates a thread pool, sets up session globals, and passes `f` to `create_compiler_and_run`. - `create_compiler_and_run` creates a `Session`, a `Compiler`, sets the source map, and calls `f`. rustdoc is a bit different. - `main_args` calls `main_options` via `run_in_thread_pool_with_globals`, which (again) creates a thread pool (hardcoded to a single thread!) and sets up session globals. - `main_options` has four different paths. - The second one calls `interface::run_compiler`, which redoes the `run_in_thread_pool_with_globals`! This is bad. - The fourth one calls `interface::create_compiler_and_run`, which is reasonable. - The first and third ones don't do anything of note involving the above functions, except for some symbol interning which requires session globals. In other words, rustdoc calls into `rustc_interface` at three different levels. It's a bit confused, and feels like code where functionality has been added by different people at different times without fully understanding how the globally accessible stuff is set up. This commit tidies things up. It removes the `run_in_thread_pool_with_globals` call in `main_args`, and adjust the four paths in `main_options` as follows. - `markdown::test` calls `test::test_main`, which provides its own parallelism and so doesn't need a thread pool. It had one small use of symbol interning, which required session globals, but the commit removes this. - `doctest::run` already calls `interface::run_compiler`, so it doesn't need further adjustment. - `markdown::render` is simple but needs session globals for interning (which can't easily be removed), so it's now wrapped in `create_session_globals_then`. - The fourth path now uses `interface::run_compiler`, which is equivalent to the old `run_in_thread_pool_with_globals` + `create_compiler_and_run` pairing.
1 parent a24a020 commit 021d1fb

File tree

3 files changed

+14
-14
lines changed

3 files changed

+14
-14
lines changed

src/librustdoc/doctest.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ use rustc_session::{lint, Session};
1818
use rustc_span::edition::Edition;
1919
use rustc_span::source_map::SourceMap;
2020
use rustc_span::symbol::sym;
21-
use rustc_span::Symbol;
2221
use rustc_span::{BytePos, FileName, Pos, Span, DUMMY_SP};
2322
use rustc_target::spec::TargetTriple;
2423
use tempfile::Builder as TempFileBuilder;
@@ -124,7 +123,7 @@ pub(crate) fn run(options: RustdocOptions) -> Result<(), ErrorGuaranteed> {
124123
let opts = scrape_test_config(crate_attrs);
125124
let enable_per_target_ignores = options.enable_per_target_ignores;
126125
let mut collector = Collector::new(
127-
tcx.crate_name(LOCAL_CRATE),
126+
tcx.crate_name(LOCAL_CRATE).to_string(),
128127
options,
129128
false,
130129
opts,
@@ -908,7 +907,7 @@ pub(crate) struct Collector {
908907
rustdoc_options: RustdocOptions,
909908
use_headers: bool,
910909
enable_per_target_ignores: bool,
911-
crate_name: Symbol,
910+
crate_name: String,
912911
opts: GlobalTestOptions,
913912
position: Span,
914913
source_map: Option<Lrc<SourceMap>>,
@@ -920,7 +919,7 @@ pub(crate) struct Collector {
920919

921920
impl Collector {
922921
pub(crate) fn new(
923-
crate_name: Symbol,
922+
crate_name: String,
924923
rustdoc_options: RustdocOptions,
925924
use_headers: bool,
926925
opts: GlobalTestOptions,
@@ -983,7 +982,7 @@ impl Tester for Collector {
983982
fn add_test(&mut self, test: String, config: LangString, line: usize) {
984983
let filename = self.get_filename();
985984
let name = self.generate_name(line, &filename);
986-
let crate_name = self.crate_name.to_string();
985+
let crate_name = self.crate_name.clone();
987986
let opts = self.opts.clone();
988987
let edition = config.edition.unwrap_or(self.rustdoc_options.edition);
989988
let rustdoc_options = self.rustdoc_options.clone();

src/librustdoc/lib.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -700,11 +700,7 @@ fn main_args(at_args: &[String]) -> MainResult {
700700
};
701701
}
702702
};
703-
rustc_interface::util::run_in_thread_pool_with_globals(
704-
options.edition,
705-
1, // this runs single-threaded, even in a parallel compiler
706-
move || main_options(options),
707-
)
703+
main_options(options)
708704
}
709705

710706
fn wrap_return(diag: &rustc_errors::Handler, res: Result<(), String>) -> MainResult {
@@ -749,9 +745,12 @@ fn main_options(options: config::Options) -> MainResult {
749745
(true, true) => return wrap_return(&diag, markdown::test(options)),
750746
(true, false) => return doctest::run(options),
751747
(false, true) => {
748+
// Session globals are required for symbol interning.
752749
return wrap_return(
753750
&diag,
754-
markdown::render(&options.input, options.render_options, options.edition),
751+
rustc_span::create_session_globals_then(options.edition, || {
752+
markdown::render(&options.input, options.render_options, options.edition)
753+
}),
755754
);
756755
}
757756
(false, false) => {}
@@ -777,9 +776,10 @@ fn main_options(options: config::Options) -> MainResult {
777776
let render_options = options.render_options.clone();
778777
let scrape_examples_options = options.scrape_examples_options.clone();
779778
let document_private = options.render_options.document_private;
779+
780780
let config = core::create_config(options);
781781

782-
interface::create_compiler_and_run(config, |compiler| {
782+
interface::run_compiler(config, |compiler| {
783783
let sess = compiler.session();
784784

785785
if sess.opts.describe_lints {

src/librustdoc/markdown.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use std::path::Path;
55

66
use rustc_span::edition::Edition;
77
use rustc_span::source_map::DUMMY_SP;
8-
use rustc_span::Symbol;
98

109
use crate::config::{Options, RenderOptions};
1110
use crate::doctest::{Collector, GlobalTestOptions};
@@ -36,6 +35,8 @@ fn extract_leading_metadata(s: &str) -> (Vec<&str>, &str) {
3635

3736
/// Render `input` (e.g., "foo.md") into an HTML file in `output`
3837
/// (e.g., output = "bar" => "bar/foo.html").
38+
///
39+
/// Requires session globals to be available, for symbol interning.
3940
pub(crate) fn render<P: AsRef<Path>>(
4041
input: P,
4142
options: RenderOptions,
@@ -133,7 +134,7 @@ pub(crate) fn test(options: Options) -> Result<(), String> {
133134
let mut opts = GlobalTestOptions::default();
134135
opts.no_crate_inject = true;
135136
let mut collector = Collector::new(
136-
Symbol::intern(&options.input.display().to_string()),
137+
options.input.display().to_string(),
137138
options.clone(),
138139
true,
139140
opts,

0 commit comments

Comments
 (0)