1
Fork 0

Rollup merge of #84458 - jyn514:cleanup-after-krate, r=GuillaumeGomez

Remove unnecessary fields and parameters in rustdoc

r? `@GuillaumeGomez`
This commit is contained in:
Yuki Okushi 2021-04-24 12:17:07 +09:00 committed by GitHub
commit 8d75898dbb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 39 additions and 79 deletions

View file

@ -1,5 +1,5 @@
use rustc_middle::ty::TyCtxt;
use rustc_span::{edition::Edition, Symbol};
use rustc_span::Symbol;
use crate::clean;
use crate::config::RenderOptions;
@ -23,7 +23,6 @@ crate trait FormatRenderer<'tcx>: Sized {
fn init(
krate: clean::Crate,
options: RenderOptions,
edition: Edition,
cache: Cache,
tcx: TyCtxt<'tcx>,
) -> Result<(Self, clean::Crate), Error>;
@ -35,19 +34,15 @@ crate trait FormatRenderer<'tcx>: Sized {
fn item(&mut self, item: clean::Item) -> Result<(), Error>;
/// Renders a module (should not handle recursing into children).
fn mod_item_in(&mut self, item: &clean::Item, item_name: &str) -> Result<(), Error>;
fn mod_item_in(&mut self, item: &clean::Item) -> Result<(), Error>;
/// Runs after recursively rendering all sub-items of a module.
fn mod_item_out(&mut self, item_name: &str) -> Result<(), Error>;
fn mod_item_out(&mut self) -> Result<(), Error> {
Ok(())
}
/// Post processing hook for cleanup and dumping output to files.
///
/// A handler is available if the renderer wants to report errors.
fn after_krate(
&mut self,
crate_name: Symbol,
diag: &rustc_errors::Handler,
) -> Result<(), Error>;
fn after_krate(&mut self) -> Result<(), Error>;
fn cache(&self) -> &Cache;
}
@ -57,8 +52,6 @@ crate fn run_format<'tcx, T: FormatRenderer<'tcx>>(
krate: clean::Crate,
options: RenderOptions,
cache: Cache,
diag: &rustc_errors::Handler,
edition: Edition,
tcx: TyCtxt<'tcx>,
) -> Result<(), Error> {
let prof = &tcx.sess.prof;
@ -66,14 +59,13 @@ crate fn run_format<'tcx, T: FormatRenderer<'tcx>>(
let emit_crate = options.should_emit_crate();
let (mut format_renderer, krate) = prof
.extra_verbose_generic_activity("create_renderer", T::descr())
.run(|| T::init(krate, options, edition, cache, tcx))?;
.run(|| T::init(krate, options, cache, tcx))?;
if !emit_crate {
return Ok(());
}
// Render the crate documentation
let crate_name = krate.name;
let mut work = vec![(format_renderer.make_child_renderer(), krate.module)];
let unknown = Symbol::intern("<unknown item>");
@ -81,13 +73,10 @@ crate fn run_format<'tcx, T: FormatRenderer<'tcx>>(
if item.is_mod() && T::RUN_ON_MODULE {
// modules are special because they add a namespace. We also need to
// recurse into the items of the module as well.
let name = item.name.as_ref().unwrap().to_string();
if name.is_empty() {
panic!("Unexpected module with empty name");
}
let _timer = prof.generic_activity_with_arg("render_mod_item", name.as_str());
let _timer =
prof.generic_activity_with_arg("render_mod_item", item.name.unwrap().to_string());
cx.mod_item_in(&item, &name)?;
cx.mod_item_in(&item)?;
let module = match *item.kind {
clean::StrippedItem(box clean::ModuleItem(m)) | clean::ModuleItem(m) => m,
_ => unreachable!(),
@ -97,7 +86,7 @@ crate fn run_format<'tcx, T: FormatRenderer<'tcx>>(
work.push((cx.make_child_renderer(), it));
}
cx.mod_item_out(&name)?;
cx.mod_item_out()?;
// FIXME: checking `item.name.is_some()` is very implicit and leads to lots of special
// cases. Use an explicit match instead.
} else if item.name.is_some() && !item.is_extern_crate() {
@ -106,5 +95,5 @@ crate fn run_format<'tcx, T: FormatRenderer<'tcx>>(
}
}
prof.extra_verbose_generic_activity("renderer_after_krate", T::descr())
.run(|| format_renderer.after_krate(crate_name, diag))
.run(|| format_renderer.after_krate())
}

View file

@ -11,7 +11,7 @@ use rustc_middle::ty::TyCtxt;
use rustc_session::Session;
use rustc_span::edition::Edition;
use rustc_span::source_map::FileName;
use rustc_span::{symbol::sym, Symbol};
use rustc_span::symbol::sym;
use super::cache::{build_index, ExternalLocation};
use super::print_item::{full_path, item_path, print_item};
@ -111,8 +111,6 @@ crate struct SharedContext<'tcx> {
crate static_root_path: Option<String>,
/// The fs handle we are working with.
crate fs: DocFS,
/// The default edition used to parse doctests.
crate edition: Edition,
pub(super) codes: ErrorCodes,
pub(super) playground: Option<markdown::Playground>,
all: RefCell<AllTypes>,
@ -141,6 +139,10 @@ impl SharedContext<'_> {
crate fn maybe_collapsed_doc_value<'a>(&self, item: &'a clean::Item) -> Option<String> {
if self.collapsed { item.collapsed_doc_value() } else { item.doc_value() }
}
crate fn edition(&self) -> Edition {
self.tcx.sess.edition()
}
}
impl<'tcx> Context<'tcx> {
@ -346,7 +348,6 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
fn init(
mut krate: clean::Crate,
options: RenderOptions,
edition: Edition,
mut cache: Cache,
tcx: TyCtxt<'tcx>,
) -> Result<(Self, clean::Crate), Error> {
@ -435,7 +436,6 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
resource_suffix,
static_root_path,
fs: DocFS::new(sender),
edition,
codes: ErrorCodes::from(unstable_features.is_nightly_build()),
playground,
all: RefCell::new(AllTypes::new()),
@ -494,11 +494,8 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
}
}
fn after_krate(
&mut self,
crate_name: Symbol,
diag: &rustc_errors::Handler,
) -> Result<(), Error> {
fn after_krate(&mut self) -> Result<(), Error> {
let crate_name = self.tcx().crate_name(LOCAL_CRATE);
let final_file = self.dst.join(&*crate_name.as_str()).join("all.html");
let settings_file = self.dst.join("settings.html");
@ -572,7 +569,8 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
// Flush pending errors.
Rc::get_mut(&mut self.shared).unwrap().fs.close();
let nb_errors = self.shared.errors.iter().map(|err| diag.struct_err(&err).emit()).count();
let nb_errors =
self.shared.errors.iter().map(|err| self.tcx().sess.struct_err(&err).emit()).count();
if nb_errors > 0 {
Err(Error::new(io::Error::new(io::ErrorKind::Other, "I/O error"), ""))
} else {
@ -580,7 +578,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
}
}
fn mod_item_in(&mut self, item: &clean::Item, item_name: &str) -> Result<(), Error> {
fn mod_item_in(&mut self, item: &clean::Item) -> Result<(), Error> {
// Stripped modules survive the rustdoc passes (i.e., `strip-private`)
// if they contain impls for public types. These modules can also
// contain items such as publicly re-exported structures.
@ -592,8 +590,9 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
self.render_redirect_pages = item.is_stripped();
}
let scx = &self.shared;
self.dst.push(item_name);
self.current.push(item_name.to_owned());
let item_name = item.name.as_ref().unwrap().to_string();
self.dst.push(&item_name);
self.current.push(item_name);
info!("Recursing into {}", self.dst.display());
@ -619,7 +618,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
Ok(())
}
fn mod_item_out(&mut self, _item_name: &str) -> Result<(), Error> {
fn mod_item_out(&mut self) -> Result<(), Error> {
info!("Recursed; leaving {}", self.dst.display());
// Go back to where we were at

View file

@ -530,7 +530,7 @@ fn render_markdown(
&links,
&mut ids,
cx.shared.codes,
cx.shared.edition,
cx.shared.edition(),
&cx.shared.playground
)
.into_string()
@ -660,7 +660,7 @@ fn short_item_info(
&note,
&mut ids,
error_codes,
cx.shared.edition,
cx.shared.edition(),
&cx.shared.playground,
);
message.push_str(&format!(": {}", html.into_string()));
@ -702,7 +702,7 @@ fn short_item_info(
&unstable_reason.as_str(),
&mut ids,
error_codes,
cx.shared.edition,
cx.shared.edition(),
&cx.shared.playground,
)
.into_string()
@ -1366,7 +1366,7 @@ fn render_impl(
&i.impl_item.links(cx),
&mut ids,
cx.shared.codes,
cx.shared.edition,
cx.shared.edition(),
&cx.shared.playground
)
.into_string()

View file

@ -425,7 +425,7 @@ pub(super) fn write_shared(
md_opts.output = cx.dst.clone();
md_opts.external_html = (*cx.shared).layout.external_html.clone();
crate::markdown::render(&index_page, md_opts, cx.shared.edition)
crate::markdown::render(&index_page, md_opts, cx.shared.edition())
.map_err(|e| Error::new(e, &index_page))?;
} else {
let dst = cx.dst.join("index.html");

View file

@ -129,7 +129,7 @@ impl SourceCollector<'_, 'tcx> {
&self.scx.layout,
&page,
"",
|buf: &mut _| print_src(buf, contents, self.scx.edition),
|buf: &mut _| print_src(buf, contents, self.scx.edition()),
&self.scx.style_files,
);
self.scx.fs.write(&cur, v.as_bytes())?;

View file

@ -14,7 +14,6 @@ use std::rc::Rc;
use rustc_data_structures::fx::FxHashMap;
use rustc_middle::ty::TyCtxt;
use rustc_session::Session;
use rustc_span::{edition::Edition, Symbol};
use rustdoc_json_types as types;
@ -134,7 +133,6 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> {
fn init(
krate: clean::Crate,
options: RenderOptions,
_edition: Edition,
cache: Cache,
tcx: TyCtxt<'tcx>,
) -> Result<(Self, clean::Crate), Error> {
@ -183,7 +181,7 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> {
Ok(())
}
fn mod_item_in(&mut self, item: &clean::Item, _module_name: &str) -> Result<(), Error> {
fn mod_item_in(&mut self, item: &clean::Item) -> Result<(), Error> {
use clean::types::ItemKind::*;
if let ModuleItem(m) = &*item.kind {
for item in &m.items {
@ -200,15 +198,7 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> {
Ok(())
}
fn mod_item_out(&mut self, _item_name: &str) -> Result<(), Error> {
Ok(())
}
fn after_krate(
&mut self,
_crate_name: Symbol,
_diag: &rustc_errors::Handler,
) -> Result<(), Error> {
fn after_krate(&mut self) -> Result<(), Error> {
debug!("Done with crate");
let mut index = (*self.index).clone().into_inner();
index.extend(self.get_trait_items());

View file

@ -656,14 +656,13 @@ fn run_renderer<'tcx, T: formats::FormatRenderer<'tcx>>(
krate: clean::Crate,
renderopts: config::RenderOptions,
cache: formats::cache::Cache,
diag: &rustc_errors::Handler,
edition: rustc_span::edition::Edition,
tcx: TyCtxt<'tcx>,
) -> MainResult {
match formats::run_format::<T>(krate, renderopts, cache, &diag, edition, tcx) {
match formats::run_format::<T>(krate, renderopts, cache, tcx) {
Ok(_) => Ok(()),
Err(e) => {
let mut msg = diag.struct_err(&format!("couldn't generate documentation: {}", e.error));
let mut msg =
tcx.sess.struct_err(&format!("couldn't generate documentation: {}", e.error));
let file = e.file.display().to_string();
if file.is_empty() {
msg.emit()
@ -692,7 +691,6 @@ fn main_options(options: config::Options) -> MainResult {
// need to move these items separately because we lose them by the time the closure is called,
// but we can't create the Handler ahead of time because it's not Send
let diag_opts = (options.error_format, options.edition, options.debugging_opts.clone());
let show_coverage = options.show_coverage;
let run_check = options.run_check;
@ -758,28 +756,12 @@ fn main_options(options: config::Options) -> MainResult {
}
info!("going to format");
let (error_format, edition, debugging_options) = diag_opts;
let diag = core::new_handler(error_format, None, &debugging_options);
match output_format {
config::OutputFormat::Html => sess.time("render_html", || {
run_renderer::<html::render::Context<'_>>(
krate,
render_opts,
cache,
&diag,
edition,
tcx,
)
run_renderer::<html::render::Context<'_>>(krate, render_opts, cache, tcx)
}),
config::OutputFormat::Json => sess.time("render_json", || {
run_renderer::<json::JsonRenderer<'_>>(
krate,
render_opts,
cache,
&diag,
edition,
tcx,
)
run_renderer::<json::JsonRenderer<'_>>(krate, render_opts, cache, tcx)
}),
}
})