| <!DOCTYPE HTML> |
| <html lang="en" class="light sidebar-visible" dir="ltr"> |
| <head> |
| <!-- Book generated using mdBook --> |
| <meta charset="UTF-8"> |
| <title>Rustdoc overview - Rust Compiler Development Guide</title> |
| |
| |
| <!-- Custom HTML head --> |
| |
| <meta name="description" content="A guide to developing the Rust compiler (rustc)"> |
| <meta name="viewport" content="width=device-width, initial-scale=1"> |
| <meta name="theme-color" content="#ffffff"> |
| |
| <link rel="icon" href="favicon.svg"> |
| <link rel="shortcut icon" href="favicon.png"> |
| <link rel="stylesheet" href="css/variables.css"> |
| <link rel="stylesheet" href="css/general.css"> |
| <link rel="stylesheet" href="css/chrome.css"> |
| <link rel="stylesheet" href="css/print.css" media="print"> |
| |
| <!-- Fonts --> |
| <link rel="stylesheet" href="FontAwesome/css/font-awesome.css"> |
| <link rel="stylesheet" href="fonts/fonts.css"> |
| |
| <!-- Highlight.js Stylesheets --> |
| <link rel="stylesheet" id="highlight-css" href="highlight.css"> |
| <link rel="stylesheet" id="tomorrow-night-css" href="tomorrow-night.css"> |
| <link rel="stylesheet" id="ayu-highlight-css" href="ayu-highlight.css"> |
| |
| <!-- Custom theme stylesheets --> |
| |
| |
| <!-- Provide site root and default themes to javascript --> |
| <script> |
| const path_to_root = ""; |
| const default_light_theme = "light"; |
| const default_dark_theme = "navy"; |
| </script> |
| <!-- Start loading toc.js asap --> |
| <script src="toc.js"></script> |
| </head> |
| <body> |
| <div id="body-container"> |
| <!-- Work around some values being stored in localStorage wrapped in quotes --> |
| <script> |
| try { |
| let theme = localStorage.getItem('mdbook-theme'); |
| let sidebar = localStorage.getItem('mdbook-sidebar'); |
| |
| if (theme.startsWith('"') && theme.endsWith('"')) { |
| localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1)); |
| } |
| |
| if (sidebar.startsWith('"') && sidebar.endsWith('"')) { |
| localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1)); |
| } |
| } catch (e) { } |
| </script> |
| |
| <!-- Set the theme before any content is loaded, prevents flash --> |
| <script> |
| const default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? default_dark_theme : default_light_theme; |
| let theme; |
| try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { } |
| if (theme === null || theme === undefined) { theme = default_theme; } |
| const html = document.documentElement; |
| html.classList.remove('light') |
| html.classList.add(theme); |
| html.classList.add("js"); |
| </script> |
| |
| <input type="checkbox" id="sidebar-toggle-anchor" class="hidden"> |
| |
| <!-- Hide / unhide sidebar before it is displayed --> |
| <script> |
| let sidebar = null; |
| const sidebar_toggle = document.getElementById("sidebar-toggle-anchor"); |
| if (document.body.clientWidth >= 1080) { |
| try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { } |
| sidebar = sidebar || 'visible'; |
| } else { |
| sidebar = 'hidden'; |
| } |
| sidebar_toggle.checked = sidebar === 'visible'; |
| html.classList.remove('sidebar-visible'); |
| html.classList.add("sidebar-" + sidebar); |
| </script> |
| |
| <nav id="sidebar" class="sidebar" aria-label="Table of contents"> |
| <!-- populated by js --> |
| <mdbook-sidebar-scrollbox class="sidebar-scrollbox"></mdbook-sidebar-scrollbox> |
| <noscript> |
| <iframe class="sidebar-iframe-outer" src="toc.html"></iframe> |
| </noscript> |
| <div id="sidebar-resize-handle" class="sidebar-resize-handle"> |
| <div class="sidebar-resize-indicator"></div> |
| </div> |
| </nav> |
| |
| <div id="page-wrapper" class="page-wrapper"> |
| |
| <div class="page"> |
| <div id="menu-bar-hover-placeholder"></div> |
| <div id="menu-bar" class="menu-bar sticky"> |
| <div class="left-buttons"> |
| <label id="sidebar-toggle" class="icon-button" for="sidebar-toggle-anchor" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar"> |
| <i class="fa fa-bars"></i> |
| </label> |
| <button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list"> |
| <i class="fa fa-paint-brush"></i> |
| </button> |
| <ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu"> |
| <li role="none"><button role="menuitem" class="theme" id="default_theme">Auto</button></li> |
| <li role="none"><button role="menuitem" class="theme" id="light">Light</button></li> |
| <li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li> |
| <li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li> |
| <li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li> |
| <li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li> |
| </ul> |
| <button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar"> |
| <i class="fa fa-search"></i> |
| </button> |
| </div> |
| |
| <h1 class="menu-title">Rust Compiler Development Guide</h1> |
| |
| <div class="right-buttons"> |
| <a href="print.html" title="Print this book" aria-label="Print this book"> |
| <i id="print-button" class="fa fa-print"></i> |
| </a> |
| <a href="https://github.com/rust-lang/rustc-dev-guide" title="Git repository" aria-label="Git repository"> |
| <i id="git-repository-button" class="fa fa-github"></i> |
| </a> |
| <a href="https://github.com/rust-lang/rustc-dev-guide/edit/master/src/rustdoc.md" title="Suggest an edit" aria-label="Suggest an edit"> |
| <i id="git-edit-button" class="fa fa-edit"></i> |
| </a> |
| |
| </div> |
| </div> |
| |
| <div id="search-wrapper" class="hidden"> |
| <form id="searchbar-outer" class="searchbar-outer"> |
| <input type="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header"> |
| </form> |
| <div id="searchresults-outer" class="searchresults-outer hidden"> |
| <div id="searchresults-header" class="searchresults-header"></div> |
| <ul id="searchresults"> |
| </ul> |
| </div> |
| </div> |
| |
| <!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM --> |
| <script> |
| document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible'); |
| document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible'); |
| Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) { |
| link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1); |
| }); |
| </script> |
| |
| <div id="content" class="content"> |
| <main> |
| <h1 id="rustdoc-overview"><a class="header" href="#rustdoc-overview">Rustdoc overview</a></h1> |
| <p><code>rustdoc</code> lives in-tree with the |
| compiler and standard library. This chapter is about how it works. |
| For information about Rustdoc's features and how to use them, see |
| the <a href="https://doc.rust-lang.org/nightly/rustdoc/">Rustdoc book</a>. |
| For more details about how rustdoc works, see the |
| <a href="./rustdoc-internals.html">"Rustdoc internals" chapter</a>.</p> |
| <ul> |
| <li><a href="#cheat-sheet">Cheat sheet</a></li> |
| <li><a href="#code-structure">Code structure</a></li> |
| <li><a href="#tests">Tests</a></li> |
| <li><a href="#constraints">Constraints</a></li> |
| <li><a href="#multiple-runs-same-output-directory">Multiple runs, same output directory</a></li> |
| <li><a href="#use-cases">Use cases</a> |
| <ul> |
| <li><a href="#standard-library-docs">Standard library docs</a></li> |
| <li><a href="#docsrs">docs.rs</a></li> |
| <li><a href="#locally-generated-docs">Locally generated docs</a></li> |
| <li><a href="#self-hosted-project-docs">Self-hosted project docs</a></li> |
| </ul> |
| </li> |
| </ul> |
| <p><code>rustdoc</code> uses <code>rustc</code> internals (and, of course, the standard library), so you |
| will have to build the compiler and <code>std</code> once before you can build <code>rustdoc</code>.</p> |
| <p>Rustdoc is implemented entirely within the crate <a href="https://github.com/rust-lang/rust/tree/master/src/librustdoc"><code>librustdoc</code></a>. It runs |
| the compiler up to the point where we have an internal representation of a |
| crate (HIR) and the ability to run some queries about the types of items. <a href="./hir.html">HIR</a> |
| and <a href="./query.html">queries</a> are discussed in the linked chapters.</p> |
| <p><code>librustdoc</code> performs two major steps after that to render a set of |
| documentation:</p> |
| <ul> |
| <li>"Clean" the AST into a form that's more suited to creating documentation (and |
| slightly more resistant to churn in the compiler).</li> |
| <li>Use this cleaned AST to render a crate's documentation, one page at a time.</li> |
| </ul> |
| <p>Naturally, there's more than just this, and those descriptions simplify out |
| lots of details, but that's the high-level overview.</p> |
| <p>(Side note: <code>librustdoc</code> is a library crate! The <code>rustdoc</code> binary is created |
| using the project in <a href="https://github.com/rust-lang/rust/tree/master/src/tools/rustdoc"><code>src/tools/rustdoc</code></a>. Note that literally all that |
| does is call the <code>main()</code> that's in this crate's <code>lib.rs</code>, though.)</p> |
| <h2 id="cheat-sheet"><a class="header" href="#cheat-sheet">Cheat sheet</a></h2> |
| <ul> |
| <li>Run <code>./x setup tools</code> before getting started. This will configure <code>x</code> |
| with nice settings for developing rustdoc and other tools, including |
| downloading a copy of rustc rather than building it.</li> |
| <li>Use <code>./x check rustdoc</code> to quickly check for compile errors.</li> |
| <li>Use <code>./x build library rustdoc</code> to make a usable |
| rustdoc you can run on other projects. |
| <ul> |
| <li>Add <code>library/test</code> to be able to use <code>rustdoc --test</code>.</li> |
| <li>Run <code>rustup toolchain link stage2 build/host/stage2</code> to add a |
| custom toolchain called <code>stage2</code> to your rustup environment. After |
| running that, <code>cargo +stage2 doc</code> in any directory will build with |
| your locally-compiled rustdoc.</li> |
| </ul> |
| </li> |
| <li>Use <code>./x doc library</code> to use this rustdoc to generate the |
| standard library docs. |
| <ul> |
| <li>The completed docs will be available in <code>build/host/doc</code> (under <code>core</code>, <code>alloc</code>, and <code>std</code>).</li> |
| <li>If you want to copy those docs to a webserver, copy all of |
| <code>build/host/doc</code>, since that's where the CSS, JS, fonts, and landing |
| page are.</li> |
| <li>For frontend debugging, disable the <code>rust.docs-minification</code> option in <a href="./building/how-to-build-and-run.html"><code>bootstrap.toml</code></a>.</li> |
| </ul> |
| </li> |
| <li>Use <code>./x test tests/rustdoc*</code> to run the tests using a stage1 |
| rustdoc. |
| <ul> |
| <li>See <a href="./rustdoc-internals.html">Rustdoc internals</a> for more information about tests.</li> |
| </ul> |
| </li> |
| </ul> |
| <h2 id="code-structure"><a class="header" href="#code-structure">Code structure</a></h2> |
| <p>All paths in this section are relative to <code>src/librustdoc/</code> in the rust-lang/rust repository.</p> |
| <ul> |
| <li>Most of the HTML printing code is in <code>html/format.rs</code> and <code>html/render/mod.rs</code>. |
| It's in a bunch of functions returning <code>impl std::fmt::Display</code>.</li> |
| <li>The data types that get rendered by the functions mentioned above are defined in <code>clean/types.rs</code>. |
| The functions responsible for creating them from the <code>HIR</code> and the <code>rustc_middle::ty</code> IR |
| live in <code>clean/mod.rs</code>.</li> |
| <li>The bits specific to using rustdoc as a test harness are in |
| <code>doctest.rs</code>.</li> |
| <li>The Markdown renderer is loaded up in <code>html/markdown.rs</code>, including functions |
| for extracting doctests from a given block of Markdown.</li> |
| <li>Frontend CSS and JavaScript are stored in <code>html/static/</code>. |
| <ul> |
| <li>Re. JavaScript, type annotations are written using <a href="https://www.typescriptlang.org/docs/handbook/jsdoc-supported-types.html">TypeScript-flavored JSDoc</a> |
| comments and an external <code>.d.ts</code> file. |
| This way, the code itself remains plain, valid JavaScript. |
| We only use <code>tsc</code> as a linter.</li> |
| </ul> |
| </li> |
| </ul> |
| <h2 id="tests"><a class="header" href="#tests">Tests</a></h2> |
| <p><code>rustdoc</code>'s integration tests are split across several test suites. |
| See <a href="tests/compiletest.html#rustdoc-test-suites">Rustdoc tests suites</a> for more details.</p> |
| <h2 id="constraints"><a class="header" href="#constraints">Constraints</a></h2> |
| <p>We try to make rustdoc work reasonably well with JavaScript disabled, and when |
| browsing local files. We support |
| <a href="https://rust-lang.github.io/rfcs/1985-tiered-browser-support.html#supported-browsers">these browsers</a>.</p> |
| <p>Supporting local files (<code>file:///</code> URLs) brings some surprising restrictions. |
| Certain browser features that require secure origins, like <code>localStorage</code> and |
| Service Workers, don't work reliably. We can still use such features but we |
| should make sure pages are still usable without them.</p> |
| <p>Rustdoc <a href="https://doc.rust-lang.org/rustdoc/advanced-features.html#interactions-between-platform-specific-docs">does not type-check function bodies</a>. |
| This works by <a href="https://github.com/rust-lang/rust/blob/52bf0cf795dfecc8b929ebb1c1e2545c3f41d4c9/src/librustdoc/core.rs#L299-L323">overriding the built-in queries for typeck</a>, |
| by <a href="https://github.com/rust-lang/rust/blob/52bf0cf795dfecc8b929ebb1c1e2545c3f41d4c9/compiler/rustc_resolve/src/late.rs#L4517">silencing name resolution errors</a>, and by <a href="https://github.com/rust-lang/rust/blob/52bf0cf795dfecc8b929ebb1c1e2545c3f41d4c9/compiler/rustc_hir_analysis/src/check/check.rs#L188-L194">not resolving opaque types</a>. |
| This comes with several caveats: in particular, rustdoc <em>cannot</em> run any parts of the compiler that |
| require type-checking bodies; for example it cannot generate <code>.rlib</code> files or run most lints. |
| We want to move away from this model eventually, but we need some alternative for |
| <a href="https://github.com/rust-lang/rust/issues/75100">the people using it</a>; see <a href="https://rust-lang.zulipchat.com/#narrow/stream/266220-rustdoc/topic/stop.20accepting.20broken.20code">various</a> |
| <a href="https://rust-lang.zulipchat.com/#narrow/channel/393423-t-rustdoc.2Fmeetings/topic/meeting.202024-07-08/near/449969836">previous</a> <a href="https://rust-lang.zulipchat.com/#narrow/channel/238009-t-compiler.2Fmeetings/topic/.5Bweekly.5D.202023-01-26/near/323755789">zulip</a> <a href="https://rust-lang.zulipchat.com/#narrow/channel/266220-t-rustdoc/topic/Pre-RFC.3A.20stop.20accepting.20broken.20code">discussion</a>. |
| For examples of code that breaks if this hack is removed, see |
| <a href="https://github.com/rust-lang/rust/tree/163cb4ea3f0ae3bc7921cc259a08a7bf92e73ee6/tests/rustdoc-ui/error-in-impl-trait"><code>tests/rustdoc-ui/error-in-impl-trait</code></a>.</p> |
| <h2 id="multiple-runs-same-output-directory"><a class="header" href="#multiple-runs-same-output-directory">Multiple runs, same output directory</a></h2> |
| <p>Rustdoc can be run multiple times for varying inputs, with its output set to the |
| same directory. That's how cargo produces documentation for dependencies of the |
| current crate. It can also be done manually if a user wants a big |
| documentation bundle with all of the docs they care about.</p> |
| <p>HTML is generated independently for each crate, but there is some cross-crate |
| information that we update as we add crates to the output directory:</p> |
| <ul> |
| <li><code>crates<SUFFIX>.js</code> holds a list of all crates in the output directory.</li> |
| <li><code>search-index<SUFFIX>.js</code> holds a list of all searchable items.</li> |
| <li>For each trait, there is a file under <code>implementors/.../trait.TraitName.js</code> |
| containing a list of implementors of that trait. The implementors may be in |
| different crates than the trait, and the JS file is updated as we discover |
| new ones.</li> |
| </ul> |
| <h2 id="use-cases"><a class="header" href="#use-cases">Use cases</a></h2> |
| <p>There are a few major use cases for rustdoc that you should keep in mind when |
| working on it:</p> |
| <h3 id="standard-library-docs"><a class="header" href="#standard-library-docs">Standard library docs</a></h3> |
| <p>These are published at <a href="https://doc.rust-lang.org/std">https://doc.rust-lang.org/std</a> as part of the Rust release |
| process. Stable releases are also uploaded to specific versioned URLs like |
| <a href="https://doc.rust-lang.org/1.57.0/std/">https://doc.rust-lang.org/1.57.0/std/</a>. Beta and nightly docs are published to |
| <a href="https://doc.rust-lang.org/beta/std/">https://doc.rust-lang.org/beta/std/</a> and <a href="https://doc.rust-lang.org/nightly/std/">https://doc.rust-lang.org/nightly/std/</a>. |
| The docs are uploaded with the <a href="https://github.com/rust-lang/promote-release">promote-release |
| tool</a> and served from S3 with |
| CloudFront.</p> |
| <p>The standard library docs contain five crates: alloc, core, proc_macro, std, and |
| test.</p> |
| <h3 id="docsrs"><a class="header" href="#docsrs">docs.rs</a></h3> |
| <p>When crates are published to crates.io, docs.rs automatically builds |
| and publishes their documentation, for instance at |
| <a href="https://docs.rs/serde/latest/serde/">https://docs.rs/serde/latest/serde/</a>. It always builds with the current nightly |
| rustdoc, so any changes you land in rustdoc are "insta-stable" in that they will |
| have an immediate public effect on docs.rs. Old documentation is not rebuilt, so |
| you will see some variation in UI when browsing old releases in docs.rs. Crate |
| authors can request rebuilds, which will be run with the latest rustdoc.</p> |
| <p>Docs.rs performs some transformations on rustdoc's output in order to save |
| storage and display a navigation bar at the top. In particular, certain static |
| files, like main.js and rustdoc.css, may be shared across multiple invocations |
| of the same version of rustdoc. Others, like crates.js and sidebar-items.js, are |
| different for different invocations. Still others, like fonts, will never |
| change. These categories are distinguished using the <code>SharedResource</code> enum in |
| <code>src/librustdoc/html/render/write_shared.rs</code></p> |
| <p>Documentation on docs.rs is always generated for a single crate at a time, so |
| the search and sidebar functionality don't include dependencies of the current |
| crate.</p> |
| <h3 id="locally-generated-docs"><a class="header" href="#locally-generated-docs">Locally generated docs</a></h3> |
| <p>Crate authors can run <code>cargo doc --open</code> in crates they have checked |
| out locally to see the docs. This is useful to check that the docs they |
| are writing are useful and display correctly. It can also be useful for |
| people to view documentation on crates they aren't authors of, but want to |
| use. In both cases, people may use <code>--document-private-items</code> Cargo flag to |
| see private methods, fields, and so on, which are normally not displayed.</p> |
| <p>By default <code>cargo doc</code> will generate documentation for a crate and all of its |
| dependencies. That can result in a very large documentation bundle, with a large |
| (and slow) search corpus. The Cargo flag <code>--no-deps</code> inhibits that behavior and |
| generates docs for just the crate.</p> |
| <h3 id="self-hosted-project-docs"><a class="header" href="#self-hosted-project-docs">Self-hosted project docs</a></h3> |
| <p>Some projects like to host their own documentation. For example: |
| <a href="https://docs.serde.rs/">https://docs.serde.rs/</a>. This is easy to do by locally generating docs, and |
| simply copying them to a web server. Rustdoc's HTML output can be extensively |
| customized by flags. Users can add a theme, set the default theme, and inject |
| arbitrary HTML. See <code>rustdoc --help</code> for details.</p> |
| |
| </main> |
| |
| <nav class="nav-wrapper" aria-label="Page navigation"> |
| <!-- Mobile navigation buttons --> |
| <a rel="prev" href="building/compiler-documenting.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left"> |
| <i class="fa fa-angle-left"></i> |
| </a> |
| |
| <a rel="next prefetch" href="building/new-target.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right"> |
| <i class="fa fa-angle-right"></i> |
| </a> |
| |
| <div style="clear: both"></div> |
| </nav> |
| </div> |
| </div> |
| |
| <nav class="nav-wide-wrapper" aria-label="Page navigation"> |
| <a rel="prev" href="building/compiler-documenting.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left"> |
| <i class="fa fa-angle-left"></i> |
| </a> |
| |
| <a rel="next prefetch" href="building/new-target.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right"> |
| <i class="fa fa-angle-right"></i> |
| </a> |
| </nav> |
| |
| </div> |
| |
| |
| |
| |
| <script> |
| window.playground_copyable = true; |
| </script> |
| |
| |
| <script src="elasticlunr.min.js"></script> |
| <script src="mark.min.js"></script> |
| <script src="searcher.js"></script> |
| |
| <script src="clipboard.min.js"></script> |
| <script src="highlight.js"></script> |
| <script src="book.js"></script> |
| |
| <!-- Custom JS scripts --> |
| <script src="mermaid.min.js"></script> |
| <script src="mermaid-init.js"></script> |
| |
| |
| </div> |
| </body> |
| </html> |