1 use std::cell::RefCell;
2 use std::collections::BTreeMap;
4 use std::path::{Path, PathBuf};
6 use std::sync::mpsc::{channel, Receiver};
8 use rustc_data_structures::fx::{FxHashMap, FxHashSet};
9 use rustc_hir::def_id::{DefId, LOCAL_CRATE};
10 use rustc_middle::ty::TyCtxt;
11 use rustc_session::Session;
12 use rustc_span::edition::Edition;
13 use rustc_span::source_map::FileName;
14 use rustc_span::{sym, Symbol};
16 use super::print_item::{full_path, item_path, print_item};
17 use super::search_index::build_index;
18 use super::write_shared::write_shared;
20 collect_spans_and_sources, print_sidebar, scrape_examples_help, sidebar_module_like, AllTypes,
21 LinkFromSrc, NameDoc, StylePath, BASIC_KEYWORDS,
24 use crate::clean::{self, types::ExternalLocation, ExternalCrate};
25 use crate::config::{ModuleSorting, RenderOptions};
26 use crate::docfs::{DocFS, PathError};
27 use crate::error::Error;
28 use crate::formats::cache::Cache;
29 use crate::formats::item_type::ItemType;
30 use crate::formats::FormatRenderer;
31 use crate::html::escape::Escape;
32 use crate::html::format::{join_with_double_colon, Buffer};
33 use crate::html::markdown::{self, plain_text_summary, ErrorCodes, IdMap};
34 use crate::html::url_parts_builder::UrlPartsBuilder;
35 use crate::html::{layout, sources, static_files};
36 use crate::scrape_examples::AllCallLocations;
39 /// Major driving force in all rustdoc rendering. This contains information
40 /// about where in the tree-like hierarchy rendering is occurring and controls
41 /// how the current page is being rendered.
43 /// It is intended that this context is a lightweight object which can be fairly
44 /// easily cloned because it is cloned per work-job (about once per item in the
46 pub(crate) struct Context<'tcx> {
47 /// Current hierarchy of components leading down to what's currently being
49 pub(crate) current: Vec<Symbol>,
50 /// The current destination folder of where HTML artifacts should be placed.
51 /// This changes as the context descends into the module hierarchy.
52 pub(crate) dst: PathBuf,
53 /// A flag, which when `true`, will render pages which redirect to the
54 /// real location of an item. This is used to allow external links to
55 /// publicly reused items to redirect to the right location.
56 pub(super) render_redirect_pages: bool,
57 /// Tracks section IDs for `Deref` targets so they match in both the main
58 /// body and the sidebar.
59 pub(super) deref_id_map: FxHashMap<DefId, String>,
60 /// The map used to ensure all generated 'id=' attributes are unique.
61 pub(super) id_map: IdMap,
62 /// Shared mutable state.
64 /// Issue for improving the situation: [#82381][]
66 /// [#82381]: https://github.com/rust-lang/rust/issues/82381
67 pub(crate) shared: Rc<SharedContext<'tcx>>,
68 /// This flag indicates whether source links should be generated or not. If
69 /// the source files are present in the html rendering, then this will be
71 pub(crate) include_sources: bool,
72 /// Collection of all types with notable traits referenced in the current module.
73 pub(crate) types_with_notable_traits: FxHashSet<clean::Type>,
76 // `Context` is cloned a lot, so we don't want the size to grow unexpectedly.
77 #[cfg(all(not(windows), target_arch = "x86_64", target_pointer_width = "64"))]
78 rustc_data_structures::static_assert_size!(Context<'_>, 160);
80 /// Shared mutable state used in [`Context`] and elsewhere.
81 pub(crate) struct SharedContext<'tcx> {
82 pub(crate) tcx: TyCtxt<'tcx>,
83 /// The path to the crate root source minus the file name.
84 /// Used for simplifying paths to the highlighted source code files.
85 pub(crate) src_root: PathBuf,
86 /// This describes the layout of each page, and is not modified after
87 /// creation of the context (contains info like the favicon and added html).
88 pub(crate) layout: layout::Layout,
89 /// The local file sources we've emitted and their respective url-paths.
90 pub(crate) local_sources: FxHashMap<PathBuf, String>,
91 /// Show the memory layout of types in the docs.
92 pub(super) show_type_layout: bool,
93 /// The base-URL of the issue tracker for when an item has been tagged with
95 pub(super) issue_tracker_base_url: Option<String>,
96 /// The directories that have already been created in this doc run. Used to reduce the number
97 /// of spurious `create_dir_all` calls.
98 created_dirs: RefCell<FxHashSet<PathBuf>>,
99 /// This flag indicates whether listings of modules (in the side bar and documentation itself)
100 /// should be ordered alphabetically or in order of appearance (in the source code).
101 pub(super) module_sorting: ModuleSorting,
102 /// Additional CSS files to be added to the generated docs.
103 pub(crate) style_files: Vec<StylePath>,
104 /// Suffix to be added on resource files (if suffix is "-v2" then "light.css" becomes
106 pub(crate) resource_suffix: String,
107 /// Optional path string to be used to load static files on output pages. If not set, uses
108 /// combinations of `../` to reach the documentation root.
109 pub(crate) static_root_path: Option<String>,
110 /// The fs handle we are working with.
111 pub(crate) fs: DocFS,
112 pub(super) codes: ErrorCodes,
113 pub(super) playground: Option<markdown::Playground>,
114 all: RefCell<AllTypes>,
115 /// Storage for the errors produced while generating documentation so they
116 /// can be printed together at the end.
117 errors: Receiver<String>,
118 /// `None` by default, depends on the `generate-redirect-map` option flag. If this field is set
119 /// to `Some(...)`, it'll store redirections and then generate a JSON file at the top level of
121 redirections: Option<RefCell<FxHashMap<String, String>>>,
123 /// Correspondance map used to link types used in the source code pages to allow to click on
124 /// links to jump to the type's definition.
125 pub(crate) span_correspondance_map: FxHashMap<rustc_span::Span, LinkFromSrc>,
126 /// The [`Cache`] used during rendering.
127 pub(crate) cache: Cache,
129 pub(crate) call_locations: AllCallLocations,
132 impl SharedContext<'_> {
133 pub(crate) fn ensure_dir(&self, dst: &Path) -> Result<(), Error> {
134 let mut dirs = self.created_dirs.borrow_mut();
135 if !dirs.contains(dst) {
136 try_err!(self.fs.create_dir_all(dst), dst);
137 dirs.insert(dst.to_path_buf());
143 pub(crate) fn edition(&self) -> Edition {
144 self.tcx.sess.edition()
148 impl<'tcx> Context<'tcx> {
149 pub(crate) fn tcx(&self) -> TyCtxt<'tcx> {
153 pub(crate) fn cache(&self) -> &Cache {
157 pub(super) fn sess(&self) -> &'tcx Session {
161 pub(super) fn derive_id(&mut self, id: String) -> String {
162 self.id_map.derive(id)
165 /// String representation of how to get back to the root path of the 'doc/'
166 /// folder in terms of a relative URL.
167 pub(super) fn root_path(&self) -> String {
168 "../".repeat(self.current.len())
171 fn render_item(&mut self, it: &clean::Item, is_module: bool) -> String {
172 let mut title = String::new();
174 title.push_str(it.name.unwrap().as_str());
176 if !it.is_primitive() && !it.is_keyword() {
178 title.push_str(" in ");
180 // No need to include the namespace for primitive types and keywords
181 title.push_str(&join_with_double_colon(&self.current));
183 title.push_str(" - Rust");
184 let tyname = it.type_();
185 let desc = it.doc_value().as_ref().map(|doc| plain_text_summary(doc));
186 let desc = if let Some(desc) = desc {
188 } else if it.is_crate() {
189 format!("API documentation for the Rust `{}` crate.", self.shared.layout.krate)
192 "API documentation for the Rust `{}` {} in crate `{}`.",
193 it.name.as_ref().unwrap(),
195 self.shared.layout.krate
198 let keywords = make_item_keywords(it);
200 let tyname_s = if it.is_crate() {
201 name = format!("{} crate", tyname);
207 if !self.render_redirect_pages {
208 let clone_shared = Rc::clone(&self.shared);
209 let page = layout::Page {
211 root_path: &self.root_path(),
212 static_root_path: clone_shared.static_root_path.as_deref(),
216 resource_suffix: &clone_shared.resource_suffix,
218 let mut page_buffer = Buffer::html();
219 print_item(self, it, &mut page_buffer, &page);
221 &clone_shared.layout,
223 |buf: &mut _| print_sidebar(self, it, buf),
224 move |buf: &mut Buffer| buf.push_buffer(page_buffer),
225 &clone_shared.style_files,
228 if let Some(&(ref names, ty)) = self.cache().paths.get(&it.item_id.expect_def_id()) {
229 if self.current.len() + 1 != names.len()
230 || self.current.iter().zip(names.iter()).any(|(a, b)| a != b)
232 // We checked that the redirection isn't pointing to the current file,
233 // preventing an infinite redirection loop in the generated
236 let mut path = String::new();
237 for name in &names[..names.len() - 1] {
238 path.push_str(name.as_str());
241 path.push_str(&item_path(ty, names.last().unwrap().as_str()));
242 match self.shared.redirections {
243 Some(ref redirections) => {
244 let mut current_path = String::new();
245 for name in &self.current {
246 current_path.push_str(name.as_str());
247 current_path.push('/');
249 current_path.push_str(&item_path(ty, names.last().unwrap().as_str()));
250 redirections.borrow_mut().insert(current_path, path);
252 None => return layout::redirect(&format!("{}{}", self.root_path(), path)),
260 /// Construct a map of items shown in the sidebar to a plain-text summary of their docs.
261 fn build_sidebar_items(&self, m: &clean::Module) -> BTreeMap<String, Vec<NameDoc>> {
262 // BTreeMap instead of HashMap to get a sorted output
263 let mut map: BTreeMap<_, Vec<_>> = BTreeMap::new();
264 let mut inserted: FxHashMap<ItemType, FxHashSet<Symbol>> = FxHashMap::default();
266 for item in &m.items {
267 if item.is_stripped() {
271 let short = item.type_();
272 let myname = match item.name {
276 if inserted.entry(short).or_default().insert(myname) {
277 let short = short.to_string();
278 let myname = myname.to_string();
279 map.entry(short).or_default().push((
281 Some(item.doc_value().map_or_else(String::new, |s| plain_text_summary(&s))),
286 match self.shared.module_sorting {
287 ModuleSorting::Alphabetical => {
288 for items in map.values_mut() {
292 ModuleSorting::DeclarationOrder => {}
297 /// Generates a url appropriate for an `href` attribute back to the source of
300 /// The url generated, when clicked, will redirect the browser back to the
301 /// original source code.
303 /// If `None` is returned, then a source link couldn't be generated. This
304 /// may happen, for example, with externally inlined items where the source
305 /// of their crate documentation isn't known.
306 pub(super) fn src_href(&self, item: &clean::Item) -> Option<String> {
307 self.href_from_span(item.span(self.tcx())?, true)
310 pub(crate) fn href_from_span(&self, span: clean::Span, with_lines: bool) -> Option<String> {
311 let mut root = self.root_path();
312 let mut path = String::new();
313 let cnum = span.cnum(self.sess());
315 // We can safely ignore synthetic `SourceFile`s.
316 let file = match span.filename(self.sess()) {
317 FileName::Real(ref path) => path.local_path_if_available().to_path_buf(),
323 let (krate, path) = if cnum == LOCAL_CRATE {
324 if let Some(path) = self.shared.local_sources.get(file) {
325 (self.shared.layout.krate.as_str(), path)
330 let (krate, src_root) = match *self.cache().extern_locations.get(&cnum)? {
331 ExternalLocation::Local => {
332 let e = ExternalCrate { crate_num: cnum };
333 (e.name(self.tcx()), e.src_root(self.tcx()))
335 ExternalLocation::Remote(ref s) => {
336 root = s.to_string();
337 let e = ExternalCrate { crate_num: cnum };
338 (e.name(self.tcx()), e.src_root(self.tcx()))
340 ExternalLocation::Unknown => return None,
343 sources::clean_path(&src_root, file, false, |component| {
344 path.push_str(&component.to_string_lossy());
347 let mut fname = file.file_name().expect("source has no filename").to_os_string();
349 path.push_str(&fname.to_string_lossy());
351 (krate_sym.as_str(), &path)
354 let anchor = if with_lines {
355 let loline = span.lo(self.sess()).line;
356 let hiline = span.hi(self.sess()).line;
359 if loline == hiline {
362 format!("{}-{}", loline, hiline)
369 "{root}src/{krate}/{path}{anchor}",
370 root = Escape(&root),
377 pub(crate) fn href_from_span_relative(
381 ) -> Option<String> {
382 self.href_from_span(span, false).map(|s| {
383 let mut url = UrlPartsBuilder::new();
384 let mut dest_href_parts = s.split('/');
385 let mut cur_href_parts = relative_to.split('/');
386 for (cur_href_part, dest_href_part) in (&mut cur_href_parts).zip(&mut dest_href_parts) {
387 if cur_href_part != dest_href_part {
388 url.push(dest_href_part);
392 for dest_href_part in dest_href_parts {
393 url.push(dest_href_part);
395 let loline = span.lo(self.sess()).line;
396 let hiline = span.hi(self.sess()).line;
399 "../".repeat(cur_href_parts.count()),
401 if loline == hiline { loline.to_string() } else { format!("{loline}-{hiline}") }
407 /// Generates the documentation for `crate` into the directory `dst`
408 impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
409 fn descr() -> &'static str {
413 const RUN_ON_MODULE: bool = true;
417 options: RenderOptions,
420 ) -> Result<(Self, clean::Crate), Error> {
421 // need to save a copy of the options for rendering the index page
422 let md_opts = options.clone();
423 let emit_crate = options.should_emit_crate();
435 generate_redirect_map,
437 generate_link_to_definition,
443 let src_root = match krate.src(tcx) {
444 FileName::Real(ref p) => match p.local_path_if_available().parent() {
445 Some(p) => p.to_path_buf(),
446 None => PathBuf::new(),
450 // If user passed in `--playground-url` arg, we fill in crate name here
451 let mut playground = None;
452 if let Some(url) = playground_url {
454 Some(markdown::Playground { crate_name: Some(krate.name(tcx).to_string()), url });
456 let mut layout = layout::Layout {
458 favicon: String::new(),
461 krate: krate.name(tcx).to_string(),
462 css_file_extension: extension_css,
463 scrape_examples_extension: !call_locations.is_empty(),
465 let mut issue_tracker_base_url = None;
466 let mut include_sources = true;
468 // Crawl the crate attributes looking for attributes which control how we're
469 // going to emit HTML
470 for attr in krate.module.attrs.lists(sym::doc) {
471 match (attr.name_or_empty(), attr.value_str()) {
472 (sym::html_favicon_url, Some(s)) => {
473 layout.favicon = s.to_string();
475 (sym::html_logo_url, Some(s)) => {
476 layout.logo = s.to_string();
478 (sym::html_playground_url, Some(s)) => {
479 playground = Some(markdown::Playground {
480 crate_name: Some(krate.name(tcx).to_string()),
484 (sym::issue_tracker_base_url, Some(s)) => {
485 issue_tracker_base_url = Some(s.to_string());
487 (sym::html_no_source, None) if attr.is_word() => {
488 include_sources = false;
494 let (local_sources, matches) = collect_spans_and_sources(
499 generate_link_to_definition,
502 let (sender, receiver) = channel();
503 let scx = SharedContext {
507 issue_tracker_base_url,
509 created_dirs: Default::default(),
514 fs: DocFS::new(sender),
515 codes: ErrorCodes::from(options.unstable_features.is_nightly_build()),
517 all: RefCell::new(AllTypes::new()),
519 redirections: if generate_redirect_map { Some(Default::default()) } else { None },
521 span_correspondance_map: matches,
527 scx.ensure_dir(&dst)?;
529 let mut cx = Context {
532 render_redirect_pages: false,
534 deref_id_map: FxHashMap::default(),
535 shared: Rc::new(scx),
537 types_with_notable_traits: FxHashSet::default(),
541 sources::render(&mut cx, &krate)?;
545 // Build our search index
546 let index = build_index(&krate, &mut Rc::get_mut(&mut cx.shared).unwrap().cache, tcx);
548 // Write shared runs within a flock; disable thread dispatching of IO temporarily.
549 Rc::get_mut(&mut cx.shared).unwrap().fs.set_sync_only(true);
550 write_shared(&mut cx, &krate, index, &md_opts)?;
551 Rc::get_mut(&mut cx.shared).unwrap().fs.set_sync_only(false);
557 fn make_child_renderer(&self) -> Self {
559 current: self.current.clone(),
560 dst: self.dst.clone(),
561 render_redirect_pages: self.render_redirect_pages,
562 deref_id_map: FxHashMap::default(),
563 id_map: IdMap::new(),
564 shared: Rc::clone(&self.shared),
565 include_sources: self.include_sources,
566 types_with_notable_traits: FxHashSet::default(),
570 fn after_krate(&mut self) -> Result<(), Error> {
571 let crate_name = self.tcx().crate_name(LOCAL_CRATE);
572 let final_file = self.dst.join(crate_name.as_str()).join("all.html");
573 let settings_file = self.dst.join("settings.html");
574 let help_file = self.dst.join("help.html");
575 let scrape_examples_help_file = self.dst.join("scrape-examples-help.html");
577 let mut root_path = self.dst.to_str().expect("invalid path").to_owned();
578 if !root_path.ends_with('/') {
581 let shared = Rc::clone(&self.shared);
582 let mut page = layout::Page {
583 title: "List of all items in this crate",
586 static_root_path: shared.static_root_path.as_deref(),
587 description: "List of all items in this crate",
588 keywords: BASIC_KEYWORDS,
589 resource_suffix: &shared.resource_suffix,
591 let all = shared.all.replace(AllTypes::new());
592 let mut sidebar = Buffer::html();
593 if shared.cache.crate_version.is_some() {
594 write!(sidebar, "<h2 class=\"location\">Crate {}</h2>", crate_name)
597 let mut items = Buffer::html();
598 sidebar_module_like(&mut items, all.item_sections());
599 if !items.is_empty() {
600 sidebar.push_str("<div class=\"sidebar-elems\">");
601 sidebar.push_buffer(items);
602 sidebar.push_str("</div>");
605 let v = layout::render(
608 sidebar.into_inner(),
609 |buf: &mut Buffer| all.print(buf),
612 shared.fs.write(final_file, v)?;
614 // Generating settings page.
615 page.title = "Rustdoc settings";
616 page.description = "Settings of Rustdoc";
617 page.root_path = "./";
619 let sidebar = "<h2 class=\"location\">Settings</h2><div class=\"sidebar-elems\"></div>";
620 let v = layout::render(
627 "<div class=\"main-heading\">\
628 <h1 class=\"fqn\">Rustdoc settings</h1>\
629 <span class=\"out-of-band\">\
630 <a id=\"back\" href=\"javascript:void(0)\" onclick=\"history.back();\">\
637 You need to enable Javascript be able to update your settings.\
640 <link rel=\"stylesheet\" type=\"text/css\" \
641 href=\"{static_root_path}{settings_css}\">\
642 <script defer src=\"{static_root_path}{settings_js}\"></script>",
643 static_root_path = page.get_static_root_path(),
644 settings_css = static_files::STATIC_FILES.settings_css,
645 settings_js = static_files::STATIC_FILES.settings_js,
650 shared.fs.write(settings_file, v)?;
652 // Generating help page.
653 page.title = "Rustdoc help";
654 page.description = "Documentation for Rustdoc";
655 page.root_path = "./";
657 let sidebar = "<h2 class=\"location\">Help</h2><div class=\"sidebar-elems\"></div>";
658 let v = layout::render(
665 "<div class=\"main-heading\">\
666 <h1 class=\"fqn\">Rustdoc help</h1>\
667 <span class=\"out-of-band\">\
668 <a id=\"back\" href=\"javascript:void(0)\" onclick=\"history.back();\">\
675 <p>You need to enable Javascript to use keyboard commands or search.</p>\
676 <p>For more information, browse the <a href=\"https://doc.rust-lang.org/rustdoc/\">rustdoc handbook</a>.</p>\
683 shared.fs.write(help_file, v)?;
685 if shared.layout.scrape_examples_extension {
686 page.title = "About scraped examples";
687 page.description = "How the scraped examples feature works in Rustdoc";
688 let v = layout::render(
692 scrape_examples_help(&*shared),
695 shared.fs.write(scrape_examples_help_file, v)?;
698 if let Some(ref redirections) = shared.redirections {
699 if !redirections.borrow().is_empty() {
700 let redirect_map_path =
701 self.dst.join(crate_name.as_str()).join("redirect-map.json");
702 let paths = serde_json::to_string(&*redirections.borrow()).unwrap();
703 shared.ensure_dir(&self.dst.join(crate_name.as_str()))?;
704 shared.fs.write(redirect_map_path, paths)?;
708 // No need for it anymore.
711 // Flush pending errors.
712 Rc::get_mut(&mut self.shared).unwrap().fs.close();
714 self.shared.errors.iter().map(|err| self.tcx().sess.struct_err(&err).emit()).count();
716 Err(Error::new(io::Error::new(io::ErrorKind::Other, "I/O error"), ""))
722 fn mod_item_in(&mut self, item: &clean::Item) -> Result<(), Error> {
723 // Stripped modules survive the rustdoc passes (i.e., `strip-private`)
724 // if they contain impls for public types. These modules can also
725 // contain items such as publicly re-exported structures.
727 // External crates will provide links to these structures, so
728 // these modules are recursed into, but not rendered normally
729 // (a flag on the context).
730 if !self.render_redirect_pages {
731 self.render_redirect_pages = item.is_stripped();
733 let item_name = item.name.unwrap();
734 self.dst.push(&*item_name.as_str());
735 self.current.push(item_name);
737 info!("Recursing into {}", self.dst.display());
739 let buf = self.render_item(item, true);
740 // buf will be empty if the module is stripped and there is no redirect for it
742 self.shared.ensure_dir(&self.dst)?;
743 let joint_dst = self.dst.join("index.html");
744 self.shared.fs.write(joint_dst, buf)?;
747 // Render sidebar-items.js used throughout this module.
748 if !self.render_redirect_pages {
749 let (clean::StrippedItem(box clean::ModuleItem(ref module)) | clean::ModuleItem(ref module)) = *item.kind
750 else { unreachable!() };
751 let items = self.build_sidebar_items(module);
752 let js_dst = self.dst.join(&format!("sidebar-items{}.js", self.shared.resource_suffix));
753 let v = format!("window.SIDEBAR_ITEMS = {};", serde_json::to_string(&items).unwrap());
754 self.shared.fs.write(js_dst, v)?;
759 fn mod_item_out(&mut self) -> Result<(), Error> {
760 info!("Recursed; leaving {}", self.dst.display());
762 // Go back to where we were at
768 fn item(&mut self, item: clean::Item) -> Result<(), Error> {
769 // Stripped modules survive the rustdoc passes (i.e., `strip-private`)
770 // if they contain impls for public types. These modules can also
771 // contain items such as publicly re-exported structures.
773 // External crates will provide links to these structures, so
774 // these modules are recursed into, but not rendered normally
775 // (a flag on the context).
776 if !self.render_redirect_pages {
777 self.render_redirect_pages = item.is_stripped();
780 let buf = self.render_item(&item, false);
781 // buf will be empty if the item is stripped and there is no redirect for it
783 let name = item.name.as_ref().unwrap();
784 let item_type = item.type_();
785 let file_name = &item_path(item_type, name.as_str());
786 self.shared.ensure_dir(&self.dst)?;
787 let joint_dst = self.dst.join(file_name);
788 self.shared.fs.write(joint_dst, buf)?;
790 if !self.render_redirect_pages {
791 self.shared.all.borrow_mut().append(full_path(self, &item), &item_type);
793 // If the item is a macro, redirect from the old macro URL (with !)
794 // to the new one (without).
795 if item_type == ItemType::Macro {
796 let redir_name = format!("{}.{}!.html", item_type, name);
797 if let Some(ref redirections) = self.shared.redirections {
798 let crate_name = &self.shared.layout.krate;
799 redirections.borrow_mut().insert(
800 format!("{}/{}", crate_name, redir_name),
801 format!("{}/{}", crate_name, file_name),
804 let v = layout::redirect(file_name);
805 let redir_dst = self.dst.join(redir_name);
806 self.shared.fs.write(redir_dst, v)?;
814 fn cache(&self) -> &Cache {
819 fn make_item_keywords(it: &clean::Item) -> String {
820 format!("{}, {}", BASIC_KEYWORDS, it.name.as_ref().unwrap())