Skip to content

Commit 8d75898

Browse files
authored
Rollup merge of #84458 - jyn514:cleanup-after-krate, r=GuillaumeGomez
Remove unnecessary fields and parameters in rustdoc r? `@GuillaumeGomez`
2 parents ed5646b + edb60a9 commit 8d75898

File tree

7 files changed

+39
-79
lines changed

7 files changed

+39
-79
lines changed

src/librustdoc/formats/renderer.rs

+12-23
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use rustc_middle::ty::TyCtxt;
2-
use rustc_span::{edition::Edition, Symbol};
2+
use rustc_span::Symbol;
33

44
use crate::clean;
55
use crate::config::RenderOptions;
@@ -23,7 +23,6 @@ crate trait FormatRenderer<'tcx>: Sized {
2323
fn init(
2424
krate: clean::Crate,
2525
options: RenderOptions,
26-
edition: Edition,
2726
cache: Cache,
2827
tcx: TyCtxt<'tcx>,
2928
) -> Result<(Self, clean::Crate), Error>;
@@ -35,19 +34,15 @@ crate trait FormatRenderer<'tcx>: Sized {
3534
fn item(&mut self, item: clean::Item) -> Result<(), Error>;
3635

3736
/// Renders a module (should not handle recursing into children).
38-
fn mod_item_in(&mut self, item: &clean::Item, item_name: &str) -> Result<(), Error>;
37+
fn mod_item_in(&mut self, item: &clean::Item) -> Result<(), Error>;
3938

4039
/// Runs after recursively rendering all sub-items of a module.
41-
fn mod_item_out(&mut self, item_name: &str) -> Result<(), Error>;
40+
fn mod_item_out(&mut self) -> Result<(), Error> {
41+
Ok(())
42+
}
4243

4344
/// Post processing hook for cleanup and dumping output to files.
44-
///
45-
/// A handler is available if the renderer wants to report errors.
46-
fn after_krate(
47-
&mut self,
48-
crate_name: Symbol,
49-
diag: &rustc_errors::Handler,
50-
) -> Result<(), Error>;
45+
fn after_krate(&mut self) -> Result<(), Error>;
5146

5247
fn cache(&self) -> &Cache;
5348
}
@@ -57,37 +52,31 @@ crate fn run_format<'tcx, T: FormatRenderer<'tcx>>(
5752
krate: clean::Crate,
5853
options: RenderOptions,
5954
cache: Cache,
60-
diag: &rustc_errors::Handler,
61-
edition: Edition,
6255
tcx: TyCtxt<'tcx>,
6356
) -> Result<(), Error> {
6457
let prof = &tcx.sess.prof;
6558

6659
let emit_crate = options.should_emit_crate();
6760
let (mut format_renderer, krate) = prof
6861
.extra_verbose_generic_activity("create_renderer", T::descr())
69-
.run(|| T::init(krate, options, edition, cache, tcx))?;
62+
.run(|| T::init(krate, options, cache, tcx))?;
7063

7164
if !emit_crate {
7265
return Ok(());
7366
}
7467

7568
// Render the crate documentation
76-
let crate_name = krate.name;
7769
let mut work = vec![(format_renderer.make_child_renderer(), krate.module)];
7870

7971
let unknown = Symbol::intern("<unknown item>");
8072
while let Some((mut cx, item)) = work.pop() {
8173
if item.is_mod() && T::RUN_ON_MODULE {
8274
// modules are special because they add a namespace. We also need to
8375
// recurse into the items of the module as well.
84-
let name = item.name.as_ref().unwrap().to_string();
85-
if name.is_empty() {
86-
panic!("Unexpected module with empty name");
87-
}
88-
let _timer = prof.generic_activity_with_arg("render_mod_item", name.as_str());
76+
let _timer =
77+
prof.generic_activity_with_arg("render_mod_item", item.name.unwrap().to_string());
8978

90-
cx.mod_item_in(&item, &name)?;
79+
cx.mod_item_in(&item)?;
9180
let module = match *item.kind {
9281
clean::StrippedItem(box clean::ModuleItem(m)) | clean::ModuleItem(m) => m,
9382
_ => unreachable!(),
@@ -97,7 +86,7 @@ crate fn run_format<'tcx, T: FormatRenderer<'tcx>>(
9786
work.push((cx.make_child_renderer(), it));
9887
}
9988

100-
cx.mod_item_out(&name)?;
89+
cx.mod_item_out()?;
10190
// FIXME: checking `item.name.is_some()` is very implicit and leads to lots of special
10291
// cases. Use an explicit match instead.
10392
} else if item.name.is_some() && !item.is_extern_crate() {
@@ -106,5 +95,5 @@ crate fn run_format<'tcx, T: FormatRenderer<'tcx>>(
10695
}
10796
}
10897
prof.extra_verbose_generic_activity("renderer_after_krate", T::descr())
109-
.run(|| format_renderer.after_krate(crate_name, diag))
98+
.run(|| format_renderer.after_krate())
11099
}

src/librustdoc/html/render/context.rs

+14-15
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use rustc_middle::ty::TyCtxt;
1111
use rustc_session::Session;
1212
use rustc_span::edition::Edition;
1313
use rustc_span::source_map::FileName;
14-
use rustc_span::{symbol::sym, Symbol};
14+
use rustc_span::symbol::sym;
1515

1616
use super::cache::{build_index, ExternalLocation};
1717
use super::print_item::{full_path, item_path, print_item};
@@ -111,8 +111,6 @@ crate struct SharedContext<'tcx> {
111111
crate static_root_path: Option<String>,
112112
/// The fs handle we are working with.
113113
crate fs: DocFS,
114-
/// The default edition used to parse doctests.
115-
crate edition: Edition,
116114
pub(super) codes: ErrorCodes,
117115
pub(super) playground: Option<markdown::Playground>,
118116
all: RefCell<AllTypes>,
@@ -141,6 +139,10 @@ impl SharedContext<'_> {
141139
crate fn maybe_collapsed_doc_value<'a>(&self, item: &'a clean::Item) -> Option<String> {
142140
if self.collapsed { item.collapsed_doc_value() } else { item.doc_value() }
143141
}
142+
143+
crate fn edition(&self) -> Edition {
144+
self.tcx.sess.edition()
145+
}
144146
}
145147

146148
impl<'tcx> Context<'tcx> {
@@ -346,7 +348,6 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
346348
fn init(
347349
mut krate: clean::Crate,
348350
options: RenderOptions,
349-
edition: Edition,
350351
mut cache: Cache,
351352
tcx: TyCtxt<'tcx>,
352353
) -> Result<(Self, clean::Crate), Error> {
@@ -435,7 +436,6 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
435436
resource_suffix,
436437
static_root_path,
437438
fs: DocFS::new(sender),
438-
edition,
439439
codes: ErrorCodes::from(unstable_features.is_nightly_build()),
440440
playground,
441441
all: RefCell::new(AllTypes::new()),
@@ -494,11 +494,8 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
494494
}
495495
}
496496

497-
fn after_krate(
498-
&mut self,
499-
crate_name: Symbol,
500-
diag: &rustc_errors::Handler,
501-
) -> Result<(), Error> {
497+
fn after_krate(&mut self) -> Result<(), Error> {
498+
let crate_name = self.tcx().crate_name(LOCAL_CRATE);
502499
let final_file = self.dst.join(&*crate_name.as_str()).join("all.html");
503500
let settings_file = self.dst.join("settings.html");
504501

@@ -572,15 +569,16 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
572569

573570
// Flush pending errors.
574571
Rc::get_mut(&mut self.shared).unwrap().fs.close();
575-
let nb_errors = self.shared.errors.iter().map(|err| diag.struct_err(&err).emit()).count();
572+
let nb_errors =
573+
self.shared.errors.iter().map(|err| self.tcx().sess.struct_err(&err).emit()).count();
576574
if nb_errors > 0 {
577575
Err(Error::new(io::Error::new(io::ErrorKind::Other, "I/O error"), ""))
578576
} else {
579577
Ok(())
580578
}
581579
}
582580

583-
fn mod_item_in(&mut self, item: &clean::Item, item_name: &str) -> Result<(), Error> {
581+
fn mod_item_in(&mut self, item: &clean::Item) -> Result<(), Error> {
584582
// Stripped modules survive the rustdoc passes (i.e., `strip-private`)
585583
// if they contain impls for public types. These modules can also
586584
// contain items such as publicly re-exported structures.
@@ -592,8 +590,9 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
592590
self.render_redirect_pages = item.is_stripped();
593591
}
594592
let scx = &self.shared;
595-
self.dst.push(item_name);
596-
self.current.push(item_name.to_owned());
593+
let item_name = item.name.as_ref().unwrap().to_string();
594+
self.dst.push(&item_name);
595+
self.current.push(item_name);
597596

598597
info!("Recursing into {}", self.dst.display());
599598

@@ -619,7 +618,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
619618
Ok(())
620619
}
621620

622-
fn mod_item_out(&mut self, _item_name: &str) -> Result<(), Error> {
621+
fn mod_item_out(&mut self) -> Result<(), Error> {
623622
info!("Recursed; leaving {}", self.dst.display());
624623

625624
// Go back to where we were at

src/librustdoc/html/render/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,7 @@ fn render_markdown(
530530
&links,
531531
&mut ids,
532532
cx.shared.codes,
533-
cx.shared.edition,
533+
cx.shared.edition(),
534534
&cx.shared.playground
535535
)
536536
.into_string()
@@ -660,7 +660,7 @@ fn short_item_info(
660660
&note,
661661
&mut ids,
662662
error_codes,
663-
cx.shared.edition,
663+
cx.shared.edition(),
664664
&cx.shared.playground,
665665
);
666666
message.push_str(&format!(": {}", html.into_string()));
@@ -702,7 +702,7 @@ fn short_item_info(
702702
&unstable_reason.as_str(),
703703
&mut ids,
704704
error_codes,
705-
cx.shared.edition,
705+
cx.shared.edition(),
706706
&cx.shared.playground,
707707
)
708708
.into_string()
@@ -1366,7 +1366,7 @@ fn render_impl(
13661366
&i.impl_item.links(cx),
13671367
&mut ids,
13681368
cx.shared.codes,
1369-
cx.shared.edition,
1369+
cx.shared.edition(),
13701370
&cx.shared.playground
13711371
)
13721372
.into_string()

src/librustdoc/html/render/write_shared.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ pub(super) fn write_shared(
425425
md_opts.output = cx.dst.clone();
426426
md_opts.external_html = (*cx.shared).layout.external_html.clone();
427427

428-
crate::markdown::render(&index_page, md_opts, cx.shared.edition)
428+
crate::markdown::render(&index_page, md_opts, cx.shared.edition())
429429
.map_err(|e| Error::new(e, &index_page))?;
430430
} else {
431431
let dst = cx.dst.join("index.html");

src/librustdoc/html/sources.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ impl SourceCollector<'_, 'tcx> {
129129
&self.scx.layout,
130130
&page,
131131
"",
132-
|buf: &mut _| print_src(buf, contents, self.scx.edition),
132+
|buf: &mut _| print_src(buf, contents, self.scx.edition()),
133133
&self.scx.style_files,
134134
);
135135
self.scx.fs.write(&cur, v.as_bytes())?;

src/librustdoc/json/mod.rs

+2-12
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ use std::rc::Rc;
1414
use rustc_data_structures::fx::FxHashMap;
1515
use rustc_middle::ty::TyCtxt;
1616
use rustc_session::Session;
17-
use rustc_span::{edition::Edition, Symbol};
1817

1918
use rustdoc_json_types as types;
2019

@@ -134,7 +133,6 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> {
134133
fn init(
135134
krate: clean::Crate,
136135
options: RenderOptions,
137-
_edition: Edition,
138136
cache: Cache,
139137
tcx: TyCtxt<'tcx>,
140138
) -> Result<(Self, clean::Crate), Error> {
@@ -183,7 +181,7 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> {
183181
Ok(())
184182
}
185183

186-
fn mod_item_in(&mut self, item: &clean::Item, _module_name: &str) -> Result<(), Error> {
184+
fn mod_item_in(&mut self, item: &clean::Item) -> Result<(), Error> {
187185
use clean::types::ItemKind::*;
188186
if let ModuleItem(m) = &*item.kind {
189187
for item in &m.items {
@@ -200,15 +198,7 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> {
200198
Ok(())
201199
}
202200

203-
fn mod_item_out(&mut self, _item_name: &str) -> Result<(), Error> {
204-
Ok(())
205-
}
206-
207-
fn after_krate(
208-
&mut self,
209-
_crate_name: Symbol,
210-
_diag: &rustc_errors::Handler,
211-
) -> Result<(), Error> {
201+
fn after_krate(&mut self) -> Result<(), Error> {
212202
debug!("Done with crate");
213203
let mut index = (*self.index).clone().into_inner();
214204
index.extend(self.get_trait_items());

src/librustdoc/lib.rs

+5-23
Original file line numberDiff line numberDiff line change
@@ -656,14 +656,13 @@ fn run_renderer<'tcx, T: formats::FormatRenderer<'tcx>>(
656656
krate: clean::Crate,
657657
renderopts: config::RenderOptions,
658658
cache: formats::cache::Cache,
659-
diag: &rustc_errors::Handler,
660-
edition: rustc_span::edition::Edition,
661659
tcx: TyCtxt<'tcx>,
662660
) -> MainResult {
663-
match formats::run_format::<T>(krate, renderopts, cache, &diag, edition, tcx) {
661+
match formats::run_format::<T>(krate, renderopts, cache, tcx) {
664662
Ok(_) => Ok(()),
665663
Err(e) => {
666-
let mut msg = diag.struct_err(&format!("couldn't generate documentation: {}", e.error));
664+
let mut msg =
665+
tcx.sess.struct_err(&format!("couldn't generate documentation: {}", e.error));
667666
let file = e.file.display().to_string();
668667
if file.is_empty() {
669668
msg.emit()
@@ -692,7 +691,6 @@ fn main_options(options: config::Options) -> MainResult {
692691

693692
// need to move these items separately because we lose them by the time the closure is called,
694693
// but we can't create the Handler ahead of time because it's not Send
695-
let diag_opts = (options.error_format, options.edition, options.debugging_opts.clone());
696694
let show_coverage = options.show_coverage;
697695
let run_check = options.run_check;
698696

@@ -758,28 +756,12 @@ fn main_options(options: config::Options) -> MainResult {
758756
}
759757

760758
info!("going to format");
761-
let (error_format, edition, debugging_options) = diag_opts;
762-
let diag = core::new_handler(error_format, None, &debugging_options);
763759
match output_format {
764760
config::OutputFormat::Html => sess.time("render_html", || {
765-
run_renderer::<html::render::Context<'_>>(
766-
krate,
767-
render_opts,
768-
cache,
769-
&diag,
770-
edition,
771-
tcx,
772-
)
761+
run_renderer::<html::render::Context<'_>>(krate, render_opts, cache, tcx)
773762
}),
774763
config::OutputFormat::Json => sess.time("render_json", || {
775-
run_renderer::<json::JsonRenderer<'_>>(
776-
krate,
777-
render_opts,
778-
cache,
779-
&diag,
780-
edition,
781-
tcx,
782-
)
764+
run_renderer::<json::JsonRenderer<'_>>(krate, render_opts, cache, tcx)
783765
}),
784766
}
785767
})

0 commit comments

Comments
 (0)