]> git.lizzy.rs Git - rust.git/blob - src/librustdoc/html/render/context.rs
Rollup merge of #84321 - Swatinem:subvariant-details, r=GuillaumeGomez
[rust.git] / src / librustdoc / html / render / context.rs
1 use std::cell::RefCell;
2 use std::collections::BTreeMap;
3 use std::io;
4 use std::path::{Path, PathBuf};
5 use std::rc::Rc;
6 use std::sync::mpsc::{channel, Receiver};
7
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::{symbol::sym, Symbol};
15
16 use super::cache::{build_index, ExternalLocation};
17 use super::print_item::{full_path, item_path, print_item};
18 use super::write_shared::write_shared;
19 use super::{print_sidebar, settings, AllTypes, NameDoc, StylePath, BASIC_KEYWORDS};
20
21 use crate::clean::{self, AttributesExt};
22 use crate::config::RenderOptions;
23 use crate::docfs::{DocFS, PathError};
24 use crate::error::Error;
25 use crate::formats::cache::Cache;
26 use crate::formats::item_type::ItemType;
27 use crate::formats::FormatRenderer;
28 use crate::html::escape::Escape;
29 use crate::html::format::Buffer;
30 use crate::html::markdown::{self, plain_text_summary, ErrorCodes, IdMap};
31 use crate::html::{layout, sources};
32
33 /// Major driving force in all rustdoc rendering. This contains information
34 /// about where in the tree-like hierarchy rendering is occurring and controls
35 /// how the current page is being rendered.
36 ///
37 /// It is intended that this context is a lightweight object which can be fairly
38 /// easily cloned because it is cloned per work-job (about once per item in the
39 /// rustdoc tree).
40 crate struct Context<'tcx> {
41     /// Current hierarchy of components leading down to what's currently being
42     /// rendered
43     pub(crate) current: Vec<String>,
44     /// The current destination folder of where HTML artifacts should be placed.
45     /// This changes as the context descends into the module hierarchy.
46     pub(super) dst: PathBuf,
47     /// A flag, which when `true`, will render pages which redirect to the
48     /// real location of an item. This is used to allow external links to
49     /// publicly reused items to redirect to the right location.
50     pub(super) render_redirect_pages: bool,
51     /// The map used to ensure all generated 'id=' attributes are unique.
52     pub(super) id_map: RefCell<IdMap>,
53     /// Tracks section IDs for `Deref` targets so they match in both the main
54     /// body and the sidebar.
55     pub(super) deref_id_map: RefCell<FxHashMap<DefId, String>>,
56     /// Shared mutable state.
57     ///
58     /// Issue for improving the situation: [#82381][]
59     ///
60     /// [#82381]: https://github.com/rust-lang/rust/issues/82381
61     pub(super) shared: Rc<SharedContext<'tcx>>,
62     /// The [`Cache`] used during rendering.
63     ///
64     /// Ideally the cache would be in [`SharedContext`], but it's mutated
65     /// between when the `SharedContext` is created and when `Context`
66     /// is created, so more refactoring would be needed.
67     ///
68     /// It's immutable once in `Context`, so it's not as bad that it's not in
69     /// `SharedContext`.
70     // FIXME: move `cache` to `SharedContext`
71     pub(super) cache: Rc<Cache>,
72 }
73
74 // `Context` is cloned a lot, so we don't want the size to grow unexpectedly.
75 #[cfg(target_arch = "x86_64")]
76 rustc_data_structures::static_assert_size!(Context<'_>, 152);
77
78 /// Shared mutable state used in [`Context`] and elsewhere.
79 crate struct SharedContext<'tcx> {
80     crate tcx: TyCtxt<'tcx>,
81     /// The path to the crate root source minus the file name.
82     /// Used for simplifying paths to the highlighted source code files.
83     crate src_root: PathBuf,
84     /// This describes the layout of each page, and is not modified after
85     /// creation of the context (contains info like the favicon and added html).
86     crate layout: layout::Layout,
87     /// This flag indicates whether `[src]` links should be generated or not. If
88     /// the source files are present in the html rendering, then this will be
89     /// `true`.
90     crate include_sources: bool,
91     /// The local file sources we've emitted and their respective url-paths.
92     crate local_sources: FxHashMap<PathBuf, String>,
93     /// Whether the collapsed pass ran
94     collapsed: bool,
95     /// The base-URL of the issue tracker for when an item has been tagged with
96     /// an issue number.
97     pub(super) issue_tracker_base_url: Option<String>,
98     /// The directories that have already been created in this doc run. Used to reduce the number
99     /// of spurious `create_dir_all` calls.
100     created_dirs: RefCell<FxHashSet<PathBuf>>,
101     /// This flag indicates whether listings of modules (in the side bar and documentation itself)
102     /// should be ordered alphabetically or in order of appearance (in the source code).
103     pub(super) sort_modules_alphabetically: bool,
104     /// Additional CSS files to be added to the generated docs.
105     crate style_files: Vec<StylePath>,
106     /// Suffix to be added on resource files (if suffix is "-v2" then "light.css" becomes
107     /// "light-v2.css").
108     crate resource_suffix: String,
109     /// Optional path string to be used to load static files on output pages. If not set, uses
110     /// combinations of `../` to reach the documentation root.
111     crate static_root_path: Option<String>,
112     /// The fs handle we are working with.
113     crate fs: DocFS,
114     /// The default edition used to parse doctests.
115     crate edition: Edition,
116     pub(super) codes: ErrorCodes,
117     pub(super) playground: Option<markdown::Playground>,
118     all: RefCell<AllTypes>,
119     /// Storage for the errors produced while generating documentation so they
120     /// can be printed together at the end.
121     errors: Receiver<String>,
122     /// `None` by default, depends on the `generate-redirect-map` option flag. If this field is set
123     /// to `Some(...)`, it'll store redirections and then generate a JSON file at the top level of
124     /// the crate.
125     redirections: Option<RefCell<FxHashMap<String, String>>>,
126 }
127
128 impl SharedContext<'_> {
129     crate fn ensure_dir(&self, dst: &Path) -> Result<(), Error> {
130         let mut dirs = self.created_dirs.borrow_mut();
131         if !dirs.contains(dst) {
132             try_err!(self.fs.create_dir_all(dst), dst);
133             dirs.insert(dst.to_path_buf());
134         }
135
136         Ok(())
137     }
138
139     /// Based on whether the `collapse-docs` pass was run, return either the `doc_value` or the
140     /// `collapsed_doc_value` of the given item.
141     crate fn maybe_collapsed_doc_value<'a>(&self, item: &'a clean::Item) -> Option<String> {
142         if self.collapsed { item.collapsed_doc_value() } else { item.doc_value() }
143     }
144 }
145
146 impl<'tcx> Context<'tcx> {
147     pub(crate) fn tcx(&self) -> TyCtxt<'tcx> {
148         self.shared.tcx
149     }
150
151     pub(crate) fn cache(&self) -> &Cache {
152         &self.cache
153     }
154
155     fn sess(&self) -> &'tcx Session {
156         &self.shared.tcx.sess
157     }
158
159     pub(super) fn derive_id(&self, id: String) -> String {
160         let mut map = self.id_map.borrow_mut();
161         map.derive(id)
162     }
163
164     /// String representation of how to get back to the root path of the 'doc/'
165     /// folder in terms of a relative URL.
166     pub(super) fn root_path(&self) -> String {
167         "../".repeat(self.current.len())
168     }
169
170     fn render_item(&self, it: &clean::Item, is_module: bool) -> String {
171         let mut title = String::new();
172         if !is_module {
173             title.push_str(&it.name.unwrap().as_str());
174         }
175         if !it.is_primitive() && !it.is_keyword() {
176             if !is_module {
177                 title.push_str(" in ");
178             }
179             // No need to include the namespace for primitive types and keywords
180             title.push_str(&self.current.join("::"));
181         };
182         title.push_str(" - Rust");
183         let tyname = it.type_();
184         let desc = it.doc_value().as_ref().map(|doc| plain_text_summary(&doc));
185         let desc = if let Some(desc) = desc {
186             desc
187         } else if it.is_crate() {
188             format!("API documentation for the Rust `{}` crate.", self.shared.layout.krate)
189         } else {
190             format!(
191                 "API documentation for the Rust `{}` {} in crate `{}`.",
192                 it.name.as_ref().unwrap(),
193                 tyname,
194                 self.shared.layout.krate
195             )
196         };
197         let keywords = make_item_keywords(it);
198         let page = layout::Page {
199             css_class: tyname.as_str(),
200             root_path: &self.root_path(),
201             static_root_path: self.shared.static_root_path.as_deref(),
202             title: &title,
203             description: &desc,
204             keywords: &keywords,
205             resource_suffix: &self.shared.resource_suffix,
206             extra_scripts: &[],
207             static_extra_scripts: &[],
208         };
209
210         if !self.render_redirect_pages {
211             layout::render(
212                 &self.shared.layout,
213                 &page,
214                 |buf: &mut _| print_sidebar(self, it, buf),
215                 |buf: &mut _| print_item(self, it, buf),
216                 &self.shared.style_files,
217             )
218         } else {
219             if let Some(&(ref names, ty)) = self.cache.paths.get(&it.def_id) {
220                 let mut path = String::new();
221                 for name in &names[..names.len() - 1] {
222                     path.push_str(name);
223                     path.push('/');
224                 }
225                 path.push_str(&item_path(ty, names.last().unwrap()));
226                 match self.shared.redirections {
227                     Some(ref redirections) => {
228                         let mut current_path = String::new();
229                         for name in &self.current {
230                             current_path.push_str(name);
231                             current_path.push('/');
232                         }
233                         current_path.push_str(&item_path(ty, names.last().unwrap()));
234                         redirections.borrow_mut().insert(current_path, path);
235                     }
236                     None => return layout::redirect(&format!("{}{}", self.root_path(), path)),
237                 }
238             }
239             String::new()
240         }
241     }
242
243     /// Construct a map of items shown in the sidebar to a plain-text summary of their docs.
244     fn build_sidebar_items(&self, m: &clean::Module) -> BTreeMap<String, Vec<NameDoc>> {
245         // BTreeMap instead of HashMap to get a sorted output
246         let mut map: BTreeMap<_, Vec<_>> = BTreeMap::new();
247         for item in &m.items {
248             if item.is_stripped() {
249                 continue;
250             }
251
252             let short = item.type_();
253             let myname = match item.name {
254                 None => continue,
255                 Some(ref s) => s.to_string(),
256             };
257             let short = short.to_string();
258             map.entry(short).or_default().push((
259                 myname,
260                 Some(item.doc_value().map_or_else(String::new, |s| plain_text_summary(&s))),
261             ));
262         }
263
264         if self.shared.sort_modules_alphabetically {
265             for items in map.values_mut() {
266                 items.sort();
267             }
268         }
269         map
270     }
271
272     /// Generates a url appropriate for an `href` attribute back to the source of
273     /// this item.
274     ///
275     /// The url generated, when clicked, will redirect the browser back to the
276     /// original source code.
277     ///
278     /// If `None` is returned, then a source link couldn't be generated. This
279     /// may happen, for example, with externally inlined items where the source
280     /// of their crate documentation isn't known.
281     pub(super) fn src_href(&self, item: &clean::Item) -> Option<String> {
282         if item.span.is_dummy() {
283             return None;
284         }
285         let mut root = self.root_path();
286         let mut path = String::new();
287         let cnum = item.span.cnum(self.sess());
288
289         // We can safely ignore synthetic `SourceFile`s.
290         let file = match item.span.filename(self.sess()) {
291             FileName::Real(ref path) => path.local_path().to_path_buf(),
292             _ => return None,
293         };
294         let file = &file;
295
296         let symbol;
297         let (krate, path) = if cnum == LOCAL_CRATE {
298             if let Some(path) = self.shared.local_sources.get(file) {
299                 (self.shared.layout.krate.as_str(), path)
300             } else {
301                 return None;
302             }
303         } else {
304             let (krate, src_root) = match *self.cache.extern_locations.get(&cnum)? {
305                 (name, ref src, ExternalLocation::Local) => (name, src),
306                 (name, ref src, ExternalLocation::Remote(ref s)) => {
307                     root = s.to_string();
308                     (name, src)
309                 }
310                 (_, _, ExternalLocation::Unknown) => return None,
311             };
312
313             sources::clean_path(&src_root, file, false, |component| {
314                 path.push_str(&component.to_string_lossy());
315                 path.push('/');
316             });
317             let mut fname = file.file_name().expect("source has no filename").to_os_string();
318             fname.push(".html");
319             path.push_str(&fname.to_string_lossy());
320             symbol = krate.as_str();
321             (&*symbol, &path)
322         };
323
324         let loline = item.span.lo(self.sess()).line;
325         let hiline = item.span.hi(self.sess()).line;
326         let lines =
327             if loline == hiline { loline.to_string() } else { format!("{}-{}", loline, hiline) };
328         Some(format!(
329             "{root}src/{krate}/{path}#{lines}",
330             root = Escape(&root),
331             krate = krate,
332             path = path,
333             lines = lines
334         ))
335     }
336 }
337
338 /// Generates the documentation for `crate` into the directory `dst`
339 impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
340     fn descr() -> &'static str {
341         "html"
342     }
343
344     const RUN_ON_MODULE: bool = true;
345
346     fn init(
347         mut krate: clean::Crate,
348         options: RenderOptions,
349         edition: Edition,
350         mut cache: Cache,
351         tcx: TyCtxt<'tcx>,
352     ) -> Result<(Self, clean::Crate), Error> {
353         // need to save a copy of the options for rendering the index page
354         let md_opts = options.clone();
355         let emit_crate = options.should_emit_crate();
356         let RenderOptions {
357             output,
358             external_html,
359             id_map,
360             playground_url,
361             sort_modules_alphabetically,
362             themes: style_files,
363             default_settings,
364             extension_css,
365             resource_suffix,
366             static_root_path,
367             generate_search_filter,
368             unstable_features,
369             generate_redirect_map,
370             ..
371         } = options;
372
373         let src_root = match krate.src {
374             FileName::Real(ref p) => match p.local_path().parent() {
375                 Some(p) => p.to_path_buf(),
376                 None => PathBuf::new(),
377             },
378             _ => PathBuf::new(),
379         };
380         // If user passed in `--playground-url` arg, we fill in crate name here
381         let mut playground = None;
382         if let Some(url) = playground_url {
383             playground =
384                 Some(markdown::Playground { crate_name: Some(krate.name.to_string()), url });
385         }
386         let mut layout = layout::Layout {
387             logo: String::new(),
388             favicon: String::new(),
389             external_html,
390             default_settings,
391             krate: krate.name.to_string(),
392             css_file_extension: extension_css,
393             generate_search_filter,
394         };
395         let mut issue_tracker_base_url = None;
396         let mut include_sources = true;
397
398         // Crawl the crate attributes looking for attributes which control how we're
399         // going to emit HTML
400         for attr in krate.module.attrs.lists(sym::doc) {
401             match (attr.name_or_empty(), attr.value_str()) {
402                 (sym::html_favicon_url, Some(s)) => {
403                     layout.favicon = s.to_string();
404                 }
405                 (sym::html_logo_url, Some(s)) => {
406                     layout.logo = s.to_string();
407                 }
408                 (sym::html_playground_url, Some(s)) => {
409                     playground = Some(markdown::Playground {
410                         crate_name: Some(krate.name.to_string()),
411                         url: s.to_string(),
412                     });
413                 }
414                 (sym::issue_tracker_base_url, Some(s)) => {
415                     issue_tracker_base_url = Some(s.to_string());
416                 }
417                 (sym::html_no_source, None) if attr.is_word() => {
418                     include_sources = false;
419                 }
420                 _ => {}
421             }
422         }
423         let (sender, receiver) = channel();
424         let mut scx = SharedContext {
425             tcx,
426             collapsed: krate.collapsed,
427             src_root,
428             include_sources,
429             local_sources: Default::default(),
430             issue_tracker_base_url,
431             layout,
432             created_dirs: Default::default(),
433             sort_modules_alphabetically,
434             style_files,
435             resource_suffix,
436             static_root_path,
437             fs: DocFS::new(sender),
438             edition,
439             codes: ErrorCodes::from(unstable_features.is_nightly_build()),
440             playground,
441             all: RefCell::new(AllTypes::new()),
442             errors: receiver,
443             redirections: if generate_redirect_map { Some(Default::default()) } else { None },
444         };
445
446         // Add the default themes to the `Vec` of stylepaths
447         //
448         // Note that these must be added before `sources::render` is called
449         // so that the resulting source pages are styled
450         //
451         // `light.css` is not disabled because it is the stylesheet that stays loaded
452         // by the browser as the theme stylesheet. The theme system (hackily) works by
453         // changing the href to this stylesheet. All other themes are disabled to
454         // prevent rule conflicts
455         scx.style_files.push(StylePath { path: PathBuf::from("light.css"), disabled: false });
456         scx.style_files.push(StylePath { path: PathBuf::from("dark.css"), disabled: true });
457         scx.style_files.push(StylePath { path: PathBuf::from("ayu.css"), disabled: true });
458
459         let dst = output;
460         scx.ensure_dir(&dst)?;
461         if emit_crate {
462             krate = sources::render(&dst, &mut scx, krate)?;
463         }
464
465         // Build our search index
466         let index = build_index(&krate, &mut cache, tcx);
467
468         let mut cx = Context {
469             current: Vec::new(),
470             dst,
471             render_redirect_pages: false,
472             id_map: RefCell::new(id_map),
473             deref_id_map: RefCell::new(FxHashMap::default()),
474             shared: Rc::new(scx),
475             cache: Rc::new(cache),
476         };
477
478         // Write shared runs within a flock; disable thread dispatching of IO temporarily.
479         Rc::get_mut(&mut cx.shared).unwrap().fs.set_sync_only(true);
480         write_shared(&cx, &krate, index, &md_opts)?;
481         Rc::get_mut(&mut cx.shared).unwrap().fs.set_sync_only(false);
482         Ok((cx, krate))
483     }
484
485     fn make_child_renderer(&self) -> Self {
486         Self {
487             current: self.current.clone(),
488             dst: self.dst.clone(),
489             render_redirect_pages: self.render_redirect_pages,
490             id_map: RefCell::new(IdMap::new()),
491             deref_id_map: RefCell::new(FxHashMap::default()),
492             shared: Rc::clone(&self.shared),
493             cache: Rc::clone(&self.cache),
494         }
495     }
496
497     fn after_krate(
498         &mut self,
499         crate_name: Symbol,
500         diag: &rustc_errors::Handler,
501     ) -> Result<(), Error> {
502         let final_file = self.dst.join(&*crate_name.as_str()).join("all.html");
503         let settings_file = self.dst.join("settings.html");
504
505         let mut root_path = self.dst.to_str().expect("invalid path").to_owned();
506         if !root_path.ends_with('/') {
507             root_path.push('/');
508         }
509         let mut page = layout::Page {
510             title: "List of all items in this crate",
511             css_class: "mod",
512             root_path: "../",
513             static_root_path: self.shared.static_root_path.as_deref(),
514             description: "List of all items in this crate",
515             keywords: BASIC_KEYWORDS,
516             resource_suffix: &self.shared.resource_suffix,
517             extra_scripts: &[],
518             static_extra_scripts: &[],
519         };
520         let sidebar = if let Some(ref version) = self.cache.crate_version {
521             format!(
522                 "<p class=\"location\">Crate {}</p>\
523                      <div class=\"block version\">\
524                          <p>Version {}</p>\
525                      </div>\
526                      <a id=\"all-types\" href=\"index.html\"><p>Back to index</p></a>",
527                 crate_name,
528                 Escape(version),
529             )
530         } else {
531             String::new()
532         };
533         let all = self.shared.all.replace(AllTypes::new());
534         let v = layout::render(
535             &self.shared.layout,
536             &page,
537             sidebar,
538             |buf: &mut Buffer| all.print(buf),
539             &self.shared.style_files,
540         );
541         self.shared.fs.write(final_file, v.as_bytes())?;
542
543         // Generating settings page.
544         page.title = "Rustdoc settings";
545         page.description = "Settings of Rustdoc";
546         page.root_path = "./";
547
548         let mut style_files = self.shared.style_files.clone();
549         let sidebar = "<p class=\"location\">Settings</p><div class=\"sidebar-elems\"></div>";
550         style_files.push(StylePath { path: PathBuf::from("settings.css"), disabled: false });
551         let v = layout::render(
552             &self.shared.layout,
553             &page,
554             sidebar,
555             settings(
556                 self.shared.static_root_path.as_deref().unwrap_or("./"),
557                 &self.shared.resource_suffix,
558                 &self.shared.style_files,
559             )?,
560             &style_files,
561         );
562         self.shared.fs.write(&settings_file, v.as_bytes())?;
563         if let Some(ref redirections) = self.shared.redirections {
564             if !redirections.borrow().is_empty() {
565                 let redirect_map_path =
566                     self.dst.join(&*crate_name.as_str()).join("redirect-map.json");
567                 let paths = serde_json::to_string(&*redirections.borrow()).unwrap();
568                 self.shared.ensure_dir(&self.dst.join(&*crate_name.as_str()))?;
569                 self.shared.fs.write(&redirect_map_path, paths.as_bytes())?;
570             }
571         }
572
573         // Flush pending errors.
574         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();
576         if nb_errors > 0 {
577             Err(Error::new(io::Error::new(io::ErrorKind::Other, "I/O error"), ""))
578         } else {
579             Ok(())
580         }
581     }
582
583     fn mod_item_in(&mut self, item: &clean::Item, item_name: &str) -> Result<(), Error> {
584         // Stripped modules survive the rustdoc passes (i.e., `strip-private`)
585         // if they contain impls for public types. These modules can also
586         // contain items such as publicly re-exported structures.
587         //
588         // External crates will provide links to these structures, so
589         // these modules are recursed into, but not rendered normally
590         // (a flag on the context).
591         if !self.render_redirect_pages {
592             self.render_redirect_pages = item.is_stripped();
593         }
594         let scx = &self.shared;
595         self.dst.push(item_name);
596         self.current.push(item_name.to_owned());
597
598         info!("Recursing into {}", self.dst.display());
599
600         let buf = self.render_item(item, true);
601         // buf will be empty if the module is stripped and there is no redirect for it
602         if !buf.is_empty() {
603             self.shared.ensure_dir(&self.dst)?;
604             let joint_dst = self.dst.join("index.html");
605             scx.fs.write(&joint_dst, buf.as_bytes())?;
606         }
607
608         // Render sidebar-items.js used throughout this module.
609         if !self.render_redirect_pages {
610             let module = match *item.kind {
611                 clean::StrippedItem(box clean::ModuleItem(ref m)) | clean::ModuleItem(ref m) => m,
612                 _ => unreachable!(),
613             };
614             let items = self.build_sidebar_items(module);
615             let js_dst = self.dst.join("sidebar-items.js");
616             let v = format!("initSidebarItems({});", serde_json::to_string(&items).unwrap());
617             scx.fs.write(&js_dst, &v)?;
618         }
619         Ok(())
620     }
621
622     fn mod_item_out(&mut self, _item_name: &str) -> Result<(), Error> {
623         info!("Recursed; leaving {}", self.dst.display());
624
625         // Go back to where we were at
626         self.dst.pop();
627         self.current.pop();
628         Ok(())
629     }
630
631     fn item(&mut self, item: clean::Item) -> Result<(), Error> {
632         // Stripped modules survive the rustdoc passes (i.e., `strip-private`)
633         // if they contain impls for public types. These modules can also
634         // contain items such as publicly re-exported structures.
635         //
636         // External crates will provide links to these structures, so
637         // these modules are recursed into, but not rendered normally
638         // (a flag on the context).
639         if !self.render_redirect_pages {
640             self.render_redirect_pages = item.is_stripped();
641         }
642
643         let buf = self.render_item(&item, false);
644         // buf will be empty if the item is stripped and there is no redirect for it
645         if !buf.is_empty() {
646             let name = item.name.as_ref().unwrap();
647             let item_type = item.type_();
648             let file_name = &item_path(item_type, &name.as_str());
649             self.shared.ensure_dir(&self.dst)?;
650             let joint_dst = self.dst.join(file_name);
651             self.shared.fs.write(&joint_dst, buf.as_bytes())?;
652
653             if !self.render_redirect_pages {
654                 self.shared.all.borrow_mut().append(full_path(self, &item), &item_type);
655             }
656             // If the item is a macro, redirect from the old macro URL (with !)
657             // to the new one (without).
658             if item_type == ItemType::Macro {
659                 let redir_name = format!("{}.{}!.html", item_type, name);
660                 if let Some(ref redirections) = self.shared.redirections {
661                     let crate_name = &self.shared.layout.krate;
662                     redirections.borrow_mut().insert(
663                         format!("{}/{}", crate_name, redir_name),
664                         format!("{}/{}", crate_name, file_name),
665                     );
666                 } else {
667                     let v = layout::redirect(file_name);
668                     let redir_dst = self.dst.join(redir_name);
669                     self.shared.fs.write(&redir_dst, v.as_bytes())?;
670                 }
671             }
672         }
673         Ok(())
674     }
675
676     fn cache(&self) -> &Cache {
677         &self.cache
678     }
679 }
680
681 fn make_item_keywords(it: &clean::Item) -> String {
682     format!("{}, {}", BASIC_KEYWORDS, it.name.as_ref().unwrap())
683 }