Skip to content

Commit cee8023

Browse files
committed
More requested changes
1 parent 3d707a0 commit cee8023

File tree

6 files changed

+69
-89
lines changed

6 files changed

+69
-89
lines changed

src/librustdoc/core.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ pub type ExternalPaths = FxHashMap<DefId, (Vec<String>, clean::TypeKind)>;
4444
pub struct DocContext<'tcx> {
4545
pub tcx: TyCtxt<'tcx>,
4646
pub resolver: Rc<RefCell<interface::BoxedResolver>>,
47-
/// Later on moved into `formats::cache::CACHE_KEY`
47+
/// Later on moved into `CACHE_KEY`
4848
pub renderinfo: RefCell<RenderInfo>,
49-
/// Later on moved through `clean::Crate` into `formats::cache::CACHE_KEY`
49+
/// Later on moved through `clean::Crate` into `CACHE_KEY`
5050
pub external_traits: Rc<RefCell<FxHashMap<DefId, clean::Trait>>>,
5151
/// Used while populating `external_traits` to ensure we don't process the same trait twice at
5252
/// the same time.

src/librustdoc/formats/cache.rs

+2-12
Original file line numberDiff line numberDiff line change
@@ -148,29 +148,19 @@ impl Cache {
148148
external_paths.into_iter().map(|(k, (v, t))| (k, (v, ItemType::from(t)))).collect();
149149

150150
let mut cache = Cache {
151-
impls: Default::default(),
152151
external_paths,
153152
exact_paths,
154-
paths: Default::default(),
155-
implementors: Default::default(),
156-
stack: Vec::new(),
157-
parent_stack: Vec::new(),
158-
search_index: Vec::new(),
159153
parent_is_trait_impl: false,
160-
extern_locations: Default::default(),
161-
primitive_locations: Default::default(),
162154
stripped_mod: false,
163155
access_levels,
164156
crate_version: krate.version.take(),
165157
document_private,
166-
orphan_impl_items: Vec::new(),
167-
orphan_trait_impls: Vec::new(),
168158
traits: krate.external_traits.replace(Default::default()),
169159
deref_trait_did,
170160
deref_mut_trait_did,
171161
owned_box_did,
172162
masked_crates: mem::take(&mut krate.masked_crates),
173-
aliases: Default::default(),
163+
..Cache::default()
174164
};
175165

176166
// Cache where all our extern crates are located
@@ -211,7 +201,7 @@ impl Cache {
211201
for (trait_did, dids, impl_) in cache.orphan_trait_impls.drain(..) {
212202
if cache.traits.contains_key(&trait_did) {
213203
for did in dids {
214-
cache.impls.entry(did).or_insert(vec![]).push(impl_.clone());
204+
cache.impls.entry(did).or_default().push(impl_.clone());
215205
}
216206
}
217207
}

src/librustdoc/formats/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ pub mod cache;
22
pub mod item_type;
33
pub mod renderer;
44

5-
pub use renderer::{FormatRenderer, Renderer};
5+
pub use renderer::{run_format, FormatRenderer};
66

77
use rustc_span::def_id::DefId;
88

src/librustdoc/formats/renderer.rs

+58-68
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::config::{RenderInfo, RenderOptions};
77
use crate::error::Error;
88
use crate::formats::cache::{Cache, CACHE_KEY};
99

10-
/// Allows for different backends to rustdoc to be used with the `Renderer::run()` function. Each
10+
/// Allows for different backends to rustdoc to be used with the `run_format()` function. Each
1111
/// backend renderer has hooks for initialization, documenting an item, entering and exiting a
1212
/// module, and cleanup/finalizing output.
1313
pub trait FormatRenderer: Clone {
@@ -42,75 +42,65 @@ pub trait FormatRenderer: Clone {
4242
fn after_run(&mut self, diag: &rustc_errors::Handler) -> Result<(), Error>;
4343
}
4444

45-
#[derive(Clone)]
46-
pub struct Renderer;
47-
48-
impl Renderer {
49-
pub fn new() -> Renderer {
50-
Renderer
51-
}
45+
/// Main method for rendering a crate.
46+
pub fn run_format<T: FormatRenderer>(
47+
krate: clean::Crate,
48+
options: RenderOptions,
49+
render_info: RenderInfo,
50+
diag: &rustc_errors::Handler,
51+
edition: Edition,
52+
) -> Result<(), Error> {
53+
let (krate, mut cache) = Cache::from_krate(
54+
render_info.clone(),
55+
options.document_private,
56+
&options.extern_html_root_urls,
57+
&options.output,
58+
krate,
59+
);
60+
61+
let (mut format_renderer, mut krate) =
62+
T::init(krate, options, render_info, edition, &mut cache)?;
63+
64+
let cache = Arc::new(cache);
65+
// Freeze the cache now that the index has been built. Put an Arc into TLS for future
66+
// parallelization opportunities
67+
CACHE_KEY.with(|v| *v.borrow_mut() = cache.clone());
68+
69+
let mut item = match krate.module.take() {
70+
Some(i) => i,
71+
None => return Ok(()),
72+
};
73+
74+
item.name = Some(krate.name.clone());
75+
76+
// Render the crate documentation
77+
let mut work = vec![(format_renderer.clone(), item)];
78+
79+
while let Some((mut cx, item)) = work.pop() {
80+
if item.is_mod() {
81+
// modules are special because they add a namespace. We also need to
82+
// recurse into the items of the module as well.
83+
let name = item.name.as_ref().unwrap().to_string();
84+
if name.is_empty() {
85+
panic!("Unexpected module with empty name");
86+
}
5287

53-
/// Main method for rendering a crate.
54-
pub fn run<T: FormatRenderer + Clone>(
55-
self,
56-
krate: clean::Crate,
57-
options: RenderOptions,
58-
render_info: RenderInfo,
59-
diag: &rustc_errors::Handler,
60-
edition: Edition,
61-
) -> Result<(), Error> {
62-
let (krate, mut cache) = Cache::from_krate(
63-
render_info.clone(),
64-
options.document_private,
65-
&options.extern_html_root_urls,
66-
&options.output,
67-
krate,
68-
);
69-
70-
let (mut format_renderer, mut krate) =
71-
T::init(krate, options, render_info, edition, &mut cache)?;
72-
73-
let cache = Arc::new(cache);
74-
// Freeze the cache now that the index has been built. Put an Arc into TLS for future
75-
// parallelization opportunities
76-
CACHE_KEY.with(|v| *v.borrow_mut() = cache.clone());
77-
78-
let mut item = match krate.module.take() {
79-
Some(i) => i,
80-
None => return Ok(()),
81-
};
82-
83-
item.name = Some(krate.name.clone());
84-
85-
// Render the crate documentation
86-
let mut work = vec![(format_renderer.clone(), item)];
87-
88-
while let Some((mut cx, item)) = work.pop() {
89-
if item.is_mod() {
90-
// modules are special because they add a namespace. We also need to
91-
// recurse into the items of the module as well.
92-
let name = item.name.as_ref().unwrap().to_string();
93-
if name.is_empty() {
94-
panic!("Unexpected module with empty name");
95-
}
96-
97-
cx.mod_item_in(&item, &name, &cache)?;
98-
let module = match item.inner {
99-
clean::StrippedItem(box clean::ModuleItem(m)) | clean::ModuleItem(m) => m,
100-
_ => unreachable!(),
101-
};
102-
for it in module.items {
103-
debug!("Adding {:?} to worklist", it.name);
104-
work.push((cx.clone(), it));
105-
}
106-
107-
cx.mod_item_out(&name)?;
108-
} else if item.name.is_some() {
109-
cx.item(item, &cache)?;
88+
cx.mod_item_in(&item, &name, &cache)?;
89+
let module = match item.inner {
90+
clean::StrippedItem(box clean::ModuleItem(m)) | clean::ModuleItem(m) => m,
91+
_ => unreachable!(),
92+
};
93+
for it in module.items {
94+
debug!("Adding {:?} to worklist", it.name);
95+
work.push((cx.clone(), it));
11096
}
111-
}
11297

113-
format_renderer.after_krate(&krate, &cache)?;
114-
format_renderer.after_run(diag)
98+
cx.mod_item_out(&name)?;
99+
} else if item.name.is_some() {
100+
cx.item(item, &cache)?;
101+
}
115102
}
103+
104+
format_renderer.after_krate(&krate, &cache)?;
105+
format_renderer.after_run(diag)
116106
}

src/librustdoc/html/render/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ impl Serialize for IndexItem {
228228

229229
/// A type used for the search index.
230230
#[derive(Debug)]
231-
pub struct RenderType {
231+
crate struct RenderType {
232232
ty: Option<DefId>,
233233
idx: Option<usize>,
234234
name: Option<String>,
@@ -259,7 +259,7 @@ impl Serialize for RenderType {
259259

260260
/// A type used for the search index.
261261
#[derive(Debug)]
262-
pub struct Generic {
262+
crate struct Generic {
263263
name: String,
264264
defid: Option<DefId>,
265265
idx: Option<usize>,
@@ -313,7 +313,7 @@ impl Serialize for IndexItemFunctionType {
313313
}
314314

315315
#[derive(Debug)]
316-
pub struct TypeWithKind {
316+
crate struct TypeWithKind {
317317
ty: RenderType,
318318
kind: TypeKind,
319319
}

src/librustdoc/lib.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -502,9 +502,9 @@ fn main_options(options: config::Options) -> i32 {
502502
info!("going to format");
503503
let (error_format, edition, debugging_options) = diag_opts;
504504
let diag = core::new_handler(error_format, None, &debugging_options);
505-
match formats::Renderer::new()
506-
.run::<html::render::Context>(krate, renderopts, renderinfo, &diag, edition)
507-
{
505+
match formats::run_format::<html::render::Context>(
506+
krate, renderopts, renderinfo, &diag, edition,
507+
) {
508508
Ok(_) => rustc_driver::EXIT_SUCCESS,
509509
Err(e) => {
510510
diag.struct_err(&format!("couldn't generate documentation: {}", e.error))

0 commit comments

Comments
 (0)