Skip to content

Commit

Permalink
Rollup merge of #84458 - jyn514:cleanup-after-krate, r=GuillaumeGomez
Browse files Browse the repository at this point in the history
Remove unnecessary fields and parameters in rustdoc

r? `@GuillaumeGomez`
  • Loading branch information
JohnTitor authored Apr 24, 2021
2 parents ed5646b + edb60a9 commit 8d75898
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 79 deletions.
35 changes: 12 additions & 23 deletions src/librustdoc/formats/renderer.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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>;
Expand All @@ -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;
}
Expand All @@ -57,37 +52,31 @@ 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;

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>");
while let Some((mut cx, item)) = work.pop() {
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!(),
Expand All @@ -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() {
Expand All @@ -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())
}
29 changes: 14 additions & 15 deletions src/librustdoc/html/render/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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>,
Expand Down Expand Up @@ -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> {
Expand Down Expand Up @@ -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> {
Expand Down Expand Up @@ -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()),
Expand Down Expand Up @@ -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");

Expand Down Expand Up @@ -572,15 +569,16 @@ 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 {
Ok(())
}
}

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.
Expand All @@ -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());

Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions src/librustdoc/html/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ fn render_markdown(
&links,
&mut ids,
cx.shared.codes,
cx.shared.edition,
cx.shared.edition(),
&cx.shared.playground
)
.into_string()
Expand Down Expand Up @@ -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()));
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/html/render/write_shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/html/sources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())?;
Expand Down
14 changes: 2 additions & 12 deletions src/librustdoc/json/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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> {
Expand Down Expand Up @@ -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 {
Expand All @@ -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());
Expand Down
28 changes: 5 additions & 23 deletions src/librustdoc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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)
}),
}
})
Expand Down

0 comments on commit 8d75898

Please sign in to comment.