diff --git a/src/librustdoc/config.rs b/src/librustdoc/config.rs index ecb6378f31fb4..67526fd1ec4b4 100644 --- a/src/librustdoc/config.rs +++ b/src/librustdoc/config.rs @@ -265,6 +265,9 @@ crate struct RenderOptions { crate document_hidden: bool, /// If `true`, generate a JSON file in the crate folder instead of HTML redirection files. crate generate_redirect_map: bool, + /// If this option is set to `true`, HTML files will be generated as it would on a + /// case-insensitive file system. + crate generate_case_insensitive: bool, crate unstable_features: rustc_feature::UnstableFeatures, } @@ -580,6 +583,7 @@ impl Options { let document_hidden = matches.opt_present("document-hidden-items"); let run_check = matches.opt_present("check"); let generate_redirect_map = matches.opt_present("generate-redirect-map"); + let generate_case_insensitive = matches.opt_present("generate-case-insensitive"); let (lint_opts, describe_lints, lint_cap) = get_cmd_lint_options(matches, error_format); @@ -638,6 +642,7 @@ impl Options { document_private, document_hidden, generate_redirect_map, + generate_case_insensitive, unstable_features: rustc_feature::UnstableFeatures::from_environment( crate_name.as_deref(), ), diff --git a/src/librustdoc/formats/renderer.rs b/src/librustdoc/formats/renderer.rs index 4e0f3a4e3c317..68f1aeb91eba1 100644 --- a/src/librustdoc/formats/renderer.rs +++ b/src/librustdoc/formats/renderer.rs @@ -1,10 +1,16 @@ +use rustc_data_structures::fx::{FxHashMap, FxHashSet}; use rustc_middle::ty::TyCtxt; use rustc_span::{edition::Edition, Symbol}; +use std::fs; +use std::io::Write; +use std::path::Path; + use crate::clean; use crate::config::RenderOptions; use crate::error::Error; use crate::formats::cache::Cache; +use crate::html::render::print_item::item_path; /// Allows for different backends to rustdoc to be used with the `run_format()` function. Each /// backend renderer has hooks for initialization, documenting an item, entering and exiting a @@ -26,6 +32,7 @@ crate trait FormatRenderer<'tcx>: Sized { edition: Edition, cache: Cache, tcx: TyCtxt<'tcx>, + case_insensitive_conflicts: Option>, ) -> Result<(Self, clean::Crate), Error>; /// Make a new renderer to render a child of the item currently being rendered. @@ -52,6 +59,71 @@ crate trait FormatRenderer<'tcx>: Sized { fn cache(&self) -> &Cache; } +fn handle_module(dst: &Path, item: &clean::Item, paths_map: &mut FxHashMap) { + // 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 module = match *item.kind { + clean::StrippedItem(box clean::ModuleItem(ref m)) | clean::ModuleItem(ref m) => m, + _ => unreachable!(), + }; + let mod_path = dst.join(&name); + for it in &module.items { + if it.is_mod() { + handle_module(&mod_path, it, paths_map); + } else if it.name.is_some() && !it.is_extern_crate() { + let name = it.name.as_ref().unwrap(); + let item_type = it.type_(); + let file_name = &item_path(item_type, &name.as_str()); + let insensitive_path = mod_path.join(file_name).display().to_string(); + + let entry = paths_map.entry(insensitive_path.to_lowercase()).or_insert(0); + *entry += 1; + } + } +} + +fn build_case_insensitive_map( + krate: &clean::Crate, + options: &RenderOptions, +) -> Option> { + let mut paths_map: FxHashMap = FxHashMap::default(); + + handle_module(&options.output, &krate.module, &mut paths_map); + Some(paths_map.into_iter().filter(|(_, count)| *count > 1).map(|(path, _)| path).collect()) +} + +fn check_if_case_insensitive(dst: &Path) -> bool { + fn create_and_write(dst: &Path, content: &str) { + if let Ok(mut f) = fs::OpenOptions::new().write(true).create(true).truncate(true).open(dst) + { + // Ignoring potential errors. + let _ = f.write(content.as_bytes()); + } + } + fn compare_content(dst: &Path, content: &str) -> bool { + fs::read_to_string(dst).unwrap_or_else(|_| String::new()).as_str() == content + } + + let path1 = dst.join("___a.tmp"); + let content1 = "a"; + let path2 = dst.join("___A.tmp"); + let content2 = "A"; + + create_and_write(&path1, content1); + create_and_write(&path1, content2); + + let res = compare_content(&path1, content1) && compare_content(&path2, content2); + // We ignore the errors when removing the files. + let _ = fs::remove_file(&path1); + let _ = fs::remove_file(&path2); + + res +} + /// Main method for rendering a crate. crate fn run_format<'tcx, T: FormatRenderer<'tcx>>( krate: clean::Crate, @@ -63,9 +135,16 @@ crate fn run_format<'tcx, T: FormatRenderer<'tcx>>( ) -> Result<(), Error> { let prof = &tcx.sess.prof; + let case_insensitive_conflicts = + if options.generate_case_insensitive || check_if_case_insensitive(&options.output) { + build_case_insensitive_map(&krate, &options) + } else { + None + }; + 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, edition, cache, tcx, case_insensitive_conflicts))?; // Render the crate documentation let crate_name = krate.name; diff --git a/src/librustdoc/html/layout.rs b/src/librustdoc/html/layout.rs index 68d70f27c8c78..a5ce727913105 100644 --- a/src/librustdoc/html/layout.rs +++ b/src/librustdoc/html/layout.rs @@ -222,6 +222,42 @@ crate fn render( ) } +/// Since this is a "conflict file" on case insensitive file system, it'll be loaded by JS instead +/// of being loaded directly. Which is why we need to modify its output a bit. +crate fn conflict_layout(page: &Page<'_>, sidebar: S, t: T) -> String +where + S: FnOnce(&mut Buffer) -> Option, +{ + let content = Buffer::html().to_display(t).to_string(); + let mut sidebar_buf = Buffer::html(); + let script_src = sidebar(&mut sidebar_buf); + format!( + "\ +document.getElementById('main').innerHTML = \"{content}\";\ +document.getElementsByClassName('sidebar')[0].innerHTML += \"{sidebar}\";\ +document.title = \"{title}\";\ +window.initSidebarVars();\ +rustdocInit();{script}", + content = content.replace("\\", "\\\\").replace("\"", "\\\"").replace("\n", "\\n"), + title = page.title.replace("\\", "\\\\").replace("\"", "\\\""), + sidebar = sidebar_buf + .into_inner() + .replace("\\", "\\\\") + .replace("\"", "\\\"") + .replace("\n", "\\n"), + script = if let Some(script_src) = script_src { + format!( + "var script = document.createElement('script');\ + script.src = {:?};\ + document.body.appendChild(script);", + script_src, + ) + } else { + String::new() + }, + ) +} + crate fn redirect(url: &str) -> String { // ", + entries + )) + }, + &style_files, + ); + self.shared.fs.write(&path, v.as_bytes())?; + } + } + // 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(); @@ -548,7 +643,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> { info!("Recursing into {}", self.dst.display()); - let buf = self.render_item(item, false); + let buf = self.render_item(item, false, false); // buf will be empty if the module is stripped and there is no redirect for it if !buf.is_empty() { self.shared.ensure_dir(&self.dst)?; @@ -591,15 +686,21 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> { self.render_redirect_pages = item.is_stripped(); } - let buf = self.render_item(&item, true); + let name = item.name.as_ref().unwrap(); + let item_type = item.type_(); + let file_name = &item_path(item_type, &name.as_str()); + let joint_dst = self.dst.join(file_name); + let is_conflict_path = self.is_conflict_path(&joint_dst); + + let buf = self.render_item(&item, true, is_conflict_path); // buf will be empty if the item is stripped and there is no redirect for it if !buf.is_empty() { - let name = item.name.as_ref().unwrap(); - let item_type = item.type_(); - let file_name = &item_path(item_type, &name.as_str()); self.shared.ensure_dir(&self.dst)?; - let joint_dst = self.dst.join(file_name); - self.shared.fs.write(&joint_dst, buf.as_bytes())?; + if is_conflict_path { + self.write_unconflicted_path(joint_dst, buf.as_bytes(), file_name)?; + } else { + self.shared.fs.write(&joint_dst, buf.as_bytes())?; + } if !self.render_redirect_pages { self.shared.all.borrow_mut().append(full_path(self, &item), &item_type); diff --git a/src/librustdoc/html/render/mod.rs b/src/librustdoc/html/render/mod.rs index 07bd26a4c5ebe..79a54f6b8c45d 100644 --- a/src/librustdoc/html/render/mod.rs +++ b/src/librustdoc/html/render/mod.rs @@ -24,12 +24,12 @@ //! both occur before the crate is rendered. crate mod cache; +crate mod print_item; #[cfg(test)] mod tests; mod context; -mod print_item; mod write_shared; crate use context::*; @@ -130,6 +130,12 @@ crate struct SharedContext<'tcx> { /// to `Some(...)`, it'll store redirections and then generate a JSON file at the top level of /// the crate. redirections: Option>>, + /// This map contains the conflicts of paths on case insensitive file systems. + /// + /// The HashMap key is the conflict path (the path in lowercase). + /// The value is a tuple composed of a vector of the original path (it is used when generating + /// the HTML page to know which file to load depending on the URL) and of the root path. + case_insensitive_conflicts: Option, String)>>>, } impl SharedContext<'_> { @@ -1720,6 +1726,19 @@ fn render_impl( } fn print_sidebar(cx: &Context<'_>, it: &clean::Item, buffer: &mut Buffer) { + print_sidebar_full(cx, it, buffer, true); +} + +/// Unfortunately, we cannot simply insert a script with `innerHTML`, otherwise it'll simply be +/// ignored. To go around this limitation, we have to manipulate it as DOM element. +/// +/// This function returns the "script" src in case it wasn't put in the buffer. +fn print_sidebar_full( + cx: &Context<'_>, + it: &clean::Item, + buffer: &mut Buffer, + use_script: bool, +) -> Option { let parentlen = cx.current.len() - if it.is_mod() { 1 } else { 0 }; if it.is_struct() @@ -1817,14 +1836,19 @@ fn print_sidebar(cx: &Context<'_>, it: &clean::Item, buffer: &mut Buffer) { ty = it.type_(), path = relpath ); - if parentlen == 0 { + let ret = if parentlen == 0 { // There is no sidebar-items.js beyond the crate root path // FIXME maybe dynamic crate loading can be merged here - } else { + None + } else if use_script { write!(buffer, "", path = relpath); - } + None + } else { + Some(format!("{path}sidebar-items.js", path = relpath)) + }; // Closes sidebar-elems div. buffer.write_str(""); + ret } fn get_next_url(used_links: &mut FxHashSet, url: String) -> String { diff --git a/src/librustdoc/html/render/print_item.rs b/src/librustdoc/html/render/print_item.rs index cc93e55fc676e..3cf56ca4c9e95 100644 --- a/src/librustdoc/html/render/print_item.rs +++ b/src/librustdoc/html/render/print_item.rs @@ -1138,7 +1138,7 @@ pub(super) fn full_path(cx: &Context<'_>, item: &clean::Item) -> String { s } -pub(super) fn item_path(ty: ItemType, name: &str) -> String { +crate fn item_path(ty: ItemType, name: &str) -> String { match ty { ItemType::Module => format!("{}index.html", ensure_trailing_slash(name)), _ => format!("{}.{}.html", ty, name), diff --git a/src/librustdoc/html/static/main.js b/src/librustdoc/html/static/main.js index da2952bbebdbe..083b175d23f73 100644 --- a/src/librustdoc/html/static/main.js +++ b/src/librustdoc/html/static/main.js @@ -2,7 +2,7 @@ // Local js definitions: /* global addClass, getSettingValue, hasClass */ /* global onEach, onEachLazy, hasOwnProperty, removeClass, updateLocalStorage */ -/* global switchTheme, useSystemTheme */ +/* global switchTheme, useSystemTheme, getNakedUrl */ if (!String.prototype.startsWith) { String.prototype.startsWith = function(searchString, position) { @@ -38,13 +38,7 @@ if (!DOMTokenList.prototype.remove) { }; } -(function () { - var rustdocVars = document.getElementById("rustdoc-vars"); - if (rustdocVars) { - window.rootPath = rustdocVars.attributes["data-root-path"].value; - window.currentCrate = rustdocVars.attributes["data-current-crate"].value; - window.searchJS = rustdocVars.attributes["data-search-js"].value; - } +function initSidebarVars() { var sidebarVars = document.getElementById("sidebar-vars"); if (sidebarVars) { window.sidebarCurrent = { @@ -53,6 +47,16 @@ if (!DOMTokenList.prototype.remove) { relpath: sidebarVars.attributes["data-relpath"].value, }; } +} + +(function () { + var rustdocVars = document.getElementById("rustdoc-vars"); + if (rustdocVars) { + window.rootPath = rustdocVars.attributes["data-root-path"].value; + window.currentCrate = rustdocVars.attributes["data-current-crate"].value; + window.searchJS = rustdocVars.attributes["data-search-js"].value; + } + initSidebarVars(); }()); // Gets the human-readable string for the virtual-key code of the @@ -96,11 +100,6 @@ function getThemePickerElement() { return document.getElementById(THEME_PICKER_ELEMENT_ID); } -// Returns the current URL without any query parameter or hash. -function getNakedUrl() { - return window.location.href.split("?")[0].split("#")[0]; -} - // Sets the focus on the search bar at the top of the page function focusSearchBar() { getSearchInput().focus(); @@ -170,7 +169,7 @@ function hideThemeButtonState() { }); }()); -(function() { +function rustdocInit() { "use strict"; // This mapping table should match the discriminants of @@ -2149,7 +2148,7 @@ function hideThemeButtonState() { sidebar.appendChild(div); } } - } + }; // delayed sidebar rendering. window.initSidebarItems = function(items) { @@ -3060,4 +3059,6 @@ function hideThemeButtonState() { onHashChange(null); window.onhashchange = onHashChange; setupSearchLoader(); -}()); +} + +rustdocInit(); diff --git a/src/librustdoc/html/static/storage.js b/src/librustdoc/html/static/storage.js index c68128516d252..8a5b8ac74ec07 100644 --- a/src/librustdoc/html/static/storage.js +++ b/src/librustdoc/html/static/storage.js @@ -31,6 +31,11 @@ function getSettingValue(settingName) { return null; } +// Returns the current URL without any query parameter or hash. +function getNakedUrl() { + return window.location.href.split("?")[0].split("#")[0]; +} + var localStoredTheme = getSettingValue("theme"); var savedHref = []; diff --git a/src/librustdoc/json/mod.rs b/src/librustdoc/json/mod.rs index db3a0c5ceb167..88debeceb8133 100644 --- a/src/librustdoc/json/mod.rs +++ b/src/librustdoc/json/mod.rs @@ -11,7 +11,7 @@ use std::fs::File; use std::path::PathBuf; use std::rc::Rc; -use rustc_data_structures::fx::FxHashMap; +use rustc_data_structures::fx::{FxHashMap, FxHashSet}; use rustc_middle::ty::TyCtxt; use rustc_session::Session; use rustc_span::{edition::Edition, Symbol}; @@ -137,6 +137,7 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> { _edition: Edition, cache: Cache, tcx: TyCtxt<'tcx>, + _case_insensitive_conflicts: Option>, ) -> Result<(Self, clean::Crate), Error> { debug!("Initializing json renderer"); Ok(( diff --git a/src/librustdoc/lib.rs b/src/librustdoc/lib.rs index dabc21e3a447c..7105b979c52d3 100644 --- a/src/librustdoc/lib.rs +++ b/src/librustdoc/lib.rs @@ -527,6 +527,13 @@ fn opts() -> Vec { unstable("print", |o| { o.optmulti("", "print", "Rustdoc information to print on stdout", "[unversioned-files]") }), + unstable("generate-case-insensitive", |o| { + o.optflag( + "", + "generate-case-insensitive", + "Force the generation of HTML to be case insensitive", + ) + }), ] }