rustdoc: Move most shared fields to SharedContext
...and remove `Rc`s for the moved fields. The only shared one that I didn't move was `cache`; see the doc-comment I added to `cache` for details.
This commit is contained in:
parent
8fd946c63a
commit
9763eb87a3
2 changed files with 48 additions and 37 deletions
|
@ -3,11 +3,11 @@ use std::collections::BTreeMap;
|
||||||
use std::io;
|
use std::io;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
use std::sync::mpsc::{channel, Receiver};
|
use std::sync::mpsc::channel;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
use rustc_data_structures::fx::FxHashMap;
|
use rustc_data_structures::fx::FxHashMap;
|
||||||
use rustc_hir::def_id::{DefId, LOCAL_CRATE};
|
use rustc_hir::def_id::LOCAL_CRATE;
|
||||||
use rustc_middle::ty::TyCtxt;
|
use rustc_middle::ty::TyCtxt;
|
||||||
use rustc_session::Session;
|
use rustc_session::Session;
|
||||||
use rustc_span::edition::Edition;
|
use rustc_span::edition::Edition;
|
||||||
|
@ -31,7 +31,7 @@ use crate::formats::item_type::ItemType;
|
||||||
use crate::formats::FormatRenderer;
|
use crate::formats::FormatRenderer;
|
||||||
use crate::html::escape::Escape;
|
use crate::html::escape::Escape;
|
||||||
use crate::html::format::Buffer;
|
use crate::html::format::Buffer;
|
||||||
use crate::html::markdown::{self, plain_text_summary, ErrorCodes, IdMap};
|
use crate::html::markdown::{self, plain_text_summary, ErrorCodes};
|
||||||
use crate::html::{layout, sources};
|
use crate::html::{layout, sources};
|
||||||
|
|
||||||
/// Major driving force in all rustdoc rendering. This contains information
|
/// Major driving force in all rustdoc rendering. This contains information
|
||||||
|
@ -53,20 +53,16 @@ crate struct Context<'tcx> {
|
||||||
/// real location of an item. This is used to allow external links to
|
/// real location of an item. This is used to allow external links to
|
||||||
/// publicly reused items to redirect to the right location.
|
/// publicly reused items to redirect to the right location.
|
||||||
crate render_redirect_pages: bool,
|
crate render_redirect_pages: bool,
|
||||||
/// `None` by default, depends on the `generate-redirect-map` option flag. If this field is set
|
|
||||||
/// to `Some(...)`, it'll store redirections and then generate a JSON file at the top level of
|
|
||||||
/// the crate.
|
|
||||||
crate redirections: Option<Rc<RefCell<FxHashMap<String, String>>>>,
|
|
||||||
/// The map used to ensure all generated 'id=' attributes are unique.
|
|
||||||
pub(super) id_map: Rc<RefCell<IdMap>>,
|
|
||||||
/// Tracks section IDs for `Deref` targets so they match in both the main
|
|
||||||
/// body and the sidebar.
|
|
||||||
pub(super) deref_id_map: Rc<RefCell<FxHashMap<DefId, String>>>,
|
|
||||||
crate shared: Arc<SharedContext<'tcx>>,
|
crate shared: Arc<SharedContext<'tcx>>,
|
||||||
all: Rc<RefCell<AllTypes>>,
|
/// The [`Cache`] used during rendering.
|
||||||
/// Storage for the errors produced while generating documentation so they
|
///
|
||||||
/// can be printed together at the end.
|
/// Ideally the cache would be in [`SharedContext`], but it's mutated
|
||||||
crate errors: Rc<Receiver<String>>,
|
/// between when the `SharedContext` is created and when `Context`
|
||||||
|
/// is created, so more refactoring would be needed.
|
||||||
|
///
|
||||||
|
/// It's immutable once in `Context`, so it's not as bad that it's not in
|
||||||
|
/// `SharedContext`.
|
||||||
|
// FIXME: move `cache` to `SharedContext`
|
||||||
crate cache: Rc<Cache>,
|
crate cache: Rc<Cache>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -91,7 +87,7 @@ impl<'tcx> Context<'tcx> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(super) fn derive_id(&self, id: String) -> String {
|
pub(super) fn derive_id(&self, id: String) -> String {
|
||||||
let mut map = self.id_map.borrow_mut();
|
let mut map = self.shared.id_map.borrow_mut();
|
||||||
map.derive(id)
|
map.derive(id)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -149,8 +145,8 @@ impl<'tcx> Context<'tcx> {
|
||||||
};
|
};
|
||||||
|
|
||||||
{
|
{
|
||||||
self.id_map.borrow_mut().reset();
|
self.shared.id_map.borrow_mut().reset();
|
||||||
self.id_map.borrow_mut().populate(&INITIAL_IDS);
|
self.shared.id_map.borrow_mut().populate(&INITIAL_IDS);
|
||||||
}
|
}
|
||||||
|
|
||||||
if !self.render_redirect_pages {
|
if !self.render_redirect_pages {
|
||||||
|
@ -169,7 +165,7 @@ impl<'tcx> Context<'tcx> {
|
||||||
path.push('/');
|
path.push('/');
|
||||||
}
|
}
|
||||||
path.push_str(&item_path(ty, names.last().unwrap()));
|
path.push_str(&item_path(ty, names.last().unwrap()));
|
||||||
match self.redirections {
|
match self.shared.redirections {
|
||||||
Some(ref redirections) => {
|
Some(ref redirections) => {
|
||||||
let mut current_path = String::new();
|
let mut current_path = String::new();
|
||||||
for name in &self.current {
|
for name in &self.current {
|
||||||
|
@ -383,6 +379,11 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
|
||||||
edition,
|
edition,
|
||||||
codes: ErrorCodes::from(unstable_features.is_nightly_build()),
|
codes: ErrorCodes::from(unstable_features.is_nightly_build()),
|
||||||
playground,
|
playground,
|
||||||
|
id_map: RefCell::new(id_map),
|
||||||
|
deref_id_map: RefCell::new(FxHashMap::default()),
|
||||||
|
all: RefCell::new(AllTypes::new()),
|
||||||
|
errors: receiver,
|
||||||
|
redirections: if generate_redirect_map { Some(Default::default()) } else { None },
|
||||||
};
|
};
|
||||||
|
|
||||||
// Add the default themes to the `Vec` of stylepaths
|
// Add the default themes to the `Vec` of stylepaths
|
||||||
|
@ -409,13 +410,8 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
|
||||||
current: Vec::new(),
|
current: Vec::new(),
|
||||||
dst,
|
dst,
|
||||||
render_redirect_pages: false,
|
render_redirect_pages: false,
|
||||||
id_map: Rc::new(RefCell::new(id_map)),
|
|
||||||
deref_id_map: Rc::new(RefCell::new(FxHashMap::default())),
|
|
||||||
shared: Arc::new(scx),
|
shared: Arc::new(scx),
|
||||||
all: Rc::new(RefCell::new(AllTypes::new())),
|
|
||||||
errors: Rc::new(receiver),
|
|
||||||
cache: Rc::new(cache),
|
cache: Rc::new(cache),
|
||||||
redirections: if generate_redirect_map { Some(Default::default()) } else { None },
|
|
||||||
};
|
};
|
||||||
|
|
||||||
CURRENT_DEPTH.with(|s| s.set(0));
|
CURRENT_DEPTH.with(|s| s.set(0));
|
||||||
|
@ -464,7 +460,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
|
||||||
} else {
|
} else {
|
||||||
String::new()
|
String::new()
|
||||||
};
|
};
|
||||||
let all = self.all.replace(AllTypes::new());
|
let all = self.shared.all.replace(AllTypes::new());
|
||||||
let v = layout::render(
|
let v = layout::render(
|
||||||
&self.shared.layout,
|
&self.shared.layout,
|
||||||
&page,
|
&page,
|
||||||
|
@ -494,7 +490,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
|
||||||
&style_files,
|
&style_files,
|
||||||
);
|
);
|
||||||
self.shared.fs.write(&settings_file, v.as_bytes())?;
|
self.shared.fs.write(&settings_file, v.as_bytes())?;
|
||||||
if let Some(redirections) = self.redirections.take() {
|
if let Some(ref redirections) = self.shared.redirections {
|
||||||
if !redirections.borrow().is_empty() {
|
if !redirections.borrow().is_empty() {
|
||||||
let redirect_map_path =
|
let redirect_map_path =
|
||||||
self.dst.join(&*krate.name.as_str()).join("redirect-map.json");
|
self.dst.join(&*krate.name.as_str()).join("redirect-map.json");
|
||||||
|
@ -506,7 +502,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
|
||||||
|
|
||||||
// Flush pending errors.
|
// Flush pending errors.
|
||||||
Arc::get_mut(&mut self.shared).unwrap().fs.close();
|
Arc::get_mut(&mut self.shared).unwrap().fs.close();
|
||||||
let nb_errors = self.errors.iter().map(|err| diag.struct_err(&err).emit()).count();
|
let nb_errors = self.shared.errors.iter().map(|err| diag.struct_err(&err).emit()).count();
|
||||||
if nb_errors > 0 {
|
if nb_errors > 0 {
|
||||||
Err(Error::new(io::Error::new(io::ErrorKind::Other, "I/O error"), ""))
|
Err(Error::new(io::Error::new(io::ErrorKind::Other, "I/O error"), ""))
|
||||||
} else {
|
} else {
|
||||||
|
@ -585,13 +581,13 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
|
||||||
self.shared.fs.write(&joint_dst, buf.as_bytes())?;
|
self.shared.fs.write(&joint_dst, buf.as_bytes())?;
|
||||||
|
|
||||||
if !self.render_redirect_pages {
|
if !self.render_redirect_pages {
|
||||||
self.all.borrow_mut().append(full_path(self, &item), &item_type);
|
self.shared.all.borrow_mut().append(full_path(self, &item), &item_type);
|
||||||
}
|
}
|
||||||
// If the item is a macro, redirect from the old macro URL (with !)
|
// If the item is a macro, redirect from the old macro URL (with !)
|
||||||
// to the new one (without).
|
// to the new one (without).
|
||||||
if item_type == ItemType::Macro {
|
if item_type == ItemType::Macro {
|
||||||
let redir_name = format!("{}.{}!.html", item_type, name);
|
let redir_name = format!("{}.{}!.html", item_type, name);
|
||||||
if let Some(ref redirections) = self.redirections {
|
if let Some(ref redirections) = self.shared.redirections {
|
||||||
let crate_name = &self.shared.layout.krate;
|
let crate_name = &self.shared.layout.krate;
|
||||||
redirections.borrow_mut().insert(
|
redirections.borrow_mut().insert(
|
||||||
format!("{}/{}", crate_name, redir_name),
|
format!("{}/{}", crate_name, redir_name),
|
||||||
|
|
|
@ -41,6 +41,7 @@ use std::fmt;
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
use std::str;
|
use std::str;
|
||||||
use std::string::ToString;
|
use std::string::ToString;
|
||||||
|
use std::sync::mpsc::Receiver;
|
||||||
|
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
use rustc_ast_pretty::pprust;
|
use rustc_ast_pretty::pprust;
|
||||||
|
@ -69,7 +70,7 @@ use crate::html::format::{
|
||||||
PrintWithSpace, WhereClause,
|
PrintWithSpace, WhereClause,
|
||||||
};
|
};
|
||||||
use crate::html::layout;
|
use crate::html::layout;
|
||||||
use crate::html::markdown::{self, ErrorCodes, Markdown, MarkdownHtml, MarkdownSummaryLine};
|
use crate::html::markdown::{self, ErrorCodes, IdMap, Markdown, MarkdownHtml, MarkdownSummaryLine};
|
||||||
|
|
||||||
/// A pair of name and its optional document.
|
/// A pair of name and its optional document.
|
||||||
crate type NameDoc = (String, Option<String>);
|
crate type NameDoc = (String, Option<String>);
|
||||||
|
@ -119,6 +120,19 @@ crate struct SharedContext<'tcx> {
|
||||||
crate edition: Edition,
|
crate edition: Edition,
|
||||||
crate codes: ErrorCodes,
|
crate codes: ErrorCodes,
|
||||||
playground: Option<markdown::Playground>,
|
playground: Option<markdown::Playground>,
|
||||||
|
/// The map used to ensure all generated 'id=' attributes are unique.
|
||||||
|
id_map: RefCell<IdMap>,
|
||||||
|
/// Tracks section IDs for `Deref` targets so they match in both the main
|
||||||
|
/// body and the sidebar.
|
||||||
|
deref_id_map: RefCell<FxHashMap<DefId, String>>,
|
||||||
|
all: RefCell<AllTypes>,
|
||||||
|
/// Storage for the errors produced while generating documentation so they
|
||||||
|
/// can be printed together at the end.
|
||||||
|
crate errors: Receiver<String>,
|
||||||
|
/// `None` by default, depends on the `generate-redirect-map` option flag. If this field is set
|
||||||
|
/// to `Some(...)`, it'll store redirections and then generate a JSON file at the top level of
|
||||||
|
/// the crate.
|
||||||
|
crate redirections: Option<RefCell<FxHashMap<String, String>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SharedContext<'_> {
|
impl SharedContext<'_> {
|
||||||
|
@ -635,7 +649,7 @@ fn render_markdown(
|
||||||
prefix: &str,
|
prefix: &str,
|
||||||
is_hidden: bool,
|
is_hidden: bool,
|
||||||
) {
|
) {
|
||||||
let mut ids = cx.id_map.borrow_mut();
|
let mut ids = cx.shared.id_map.borrow_mut();
|
||||||
write!(
|
write!(
|
||||||
w,
|
w,
|
||||||
"<div class=\"docblock{}\">{}{}</div>",
|
"<div class=\"docblock{}\">{}{}</div>",
|
||||||
|
@ -795,7 +809,7 @@ fn short_item_info(
|
||||||
|
|
||||||
if let Some(note) = note {
|
if let Some(note) = note {
|
||||||
let note = note.as_str();
|
let note = note.as_str();
|
||||||
let mut ids = cx.id_map.borrow_mut();
|
let mut ids = cx.shared.id_map.borrow_mut();
|
||||||
let html = MarkdownHtml(
|
let html = MarkdownHtml(
|
||||||
¬e,
|
¬e,
|
||||||
&mut ids,
|
&mut ids,
|
||||||
|
@ -834,7 +848,7 @@ fn short_item_info(
|
||||||
message.push_str(&format!(" ({})", feature));
|
message.push_str(&format!(" ({})", feature));
|
||||||
|
|
||||||
if let Some(unstable_reason) = reason {
|
if let Some(unstable_reason) = reason {
|
||||||
let mut ids = cx.id_map.borrow_mut();
|
let mut ids = cx.shared.id_map.borrow_mut();
|
||||||
message = format!(
|
message = format!(
|
||||||
"<details><summary>{}</summary>{}</details>",
|
"<details><summary>{}</summary>{}</details>",
|
||||||
message,
|
message,
|
||||||
|
@ -1174,7 +1188,8 @@ fn render_assoc_items(
|
||||||
type_.print(cx.cache())
|
type_.print(cx.cache())
|
||||||
)));
|
)));
|
||||||
debug!("Adding {} to deref id map", type_.print(cx.cache()));
|
debug!("Adding {} to deref id map", type_.print(cx.cache()));
|
||||||
cx.deref_id_map
|
cx.shared
|
||||||
|
.deref_id_map
|
||||||
.borrow_mut()
|
.borrow_mut()
|
||||||
.insert(type_.def_id_full(cx.cache()).unwrap(), id.clone());
|
.insert(type_.def_id_full(cx.cache()).unwrap(), id.clone());
|
||||||
write!(
|
write!(
|
||||||
|
@ -1481,7 +1496,7 @@ fn render_impl(
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(ref dox) = cx.shared.maybe_collapsed_doc_value(&i.impl_item) {
|
if let Some(ref dox) = cx.shared.maybe_collapsed_doc_value(&i.impl_item) {
|
||||||
let mut ids = cx.id_map.borrow_mut();
|
let mut ids = cx.shared.id_map.borrow_mut();
|
||||||
write!(
|
write!(
|
||||||
w,
|
w,
|
||||||
"<div class=\"docblock\">{}</div>",
|
"<div class=\"docblock\">{}</div>",
|
||||||
|
@ -2030,7 +2045,7 @@ fn sidebar_deref_methods(cx: &Context<'_>, out: &mut Buffer, impl_: &Impl, v: &V
|
||||||
.flat_map(|i| get_methods(i.inner_impl(), true, &mut used_links, deref_mut, c))
|
.flat_map(|i| get_methods(i.inner_impl(), true, &mut used_links, deref_mut, c))
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
if !ret.is_empty() {
|
if !ret.is_empty() {
|
||||||
let deref_id_map = cx.deref_id_map.borrow();
|
let deref_id_map = cx.shared.deref_id_map.borrow();
|
||||||
let id = deref_id_map
|
let id = deref_id_map
|
||||||
.get(&real_target.def_id_full(cx.cache()).unwrap())
|
.get(&real_target.def_id_full(cx.cache()).unwrap())
|
||||||
.expect("Deref section without derived id");
|
.expect("Deref section without derived id");
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue