| <!DOCTYPE HTML> |
| <html lang="en" class="light sidebar-visible" dir="ltr"> |
| <head> |
| <!-- Book generated using mdBook --> |
| <meta charset="UTF-8"> |
| <title>The compiler source code - 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/compiler-src.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="high-level-overview-of-the-compiler-source"><a class="header" href="#high-level-overview-of-the-compiler-source">High-level overview of the compiler source</a></h1> |
| <ul> |
| <li><a href="#workspace-structure">Workspace structure</a></li> |
| <li><a href="#compiler">Compiler</a> |
| <ul> |
| <li><a href="#big-picture">Big picture</a></li> |
| </ul> |
| </li> |
| <li><a href="#rustdoc">rustdoc</a></li> |
| <li><a href="#tests">Tests</a></li> |
| <li><a href="#build-system">Build System</a></li> |
| <li><a href="#standard-library">Standard library</a></li> |
| <li><a href="#other">Other</a></li> |
| </ul> |
| <p>Now that we have <a href="./overview.html">seen what the compiler does</a>, |
| let's take a look at the structure of the <a href="https://github.com/rust-lang/rust"><code>rust-lang/rust</code></a> repository, |
| where the rustc source code lives.</p> |
| <blockquote> |
| <p>You may find it helpful to read the <a href="./overview.html">"Overview of the compiler"</a> |
| chapter, which introduces how the compiler works, before this one.</p> |
| </blockquote> |
| <h2 id="workspace-structure"><a class="header" href="#workspace-structure">Workspace structure</a></h2> |
| <p>The <a href="https://github.com/rust-lang/rust"><code>rust-lang/rust</code></a> repository consists of a single large cargo workspace |
| containing the compiler, the standard libraries (<a href="https://github.com/rust-lang/rust/tree/master/library/core"><code>core</code></a>, <a href="https://github.com/rust-lang/rust/tree/master/library/alloc"><code>alloc</code></a>, <a href="https://github.com/rust-lang/rust/tree/master/library/std"><code>std</code></a>, |
| <a href="https://github.com/rust-lang/rust/tree/master/library/proc_macro"><code>proc_macro</code></a>, <a href="https://github.com/rust-lang/rust/tree/master/src/etc"><code>etc</code></a>), and <a href="https://github.com/rust-lang/rust/tree/master/src/tools/rustdoc"><code>rustdoc</code></a>, along with the build system and a |
| bunch of tools and submodules for building a full Rust distribution.</p> |
| <p>The repository consists of three main directories:</p> |
| <ul> |
| <li> |
| <p><a href="https://github.com/rust-lang/rust/tree/master/compiler"><code>compiler/</code></a> contains the source code for <code>rustc</code>. It consists of many crates |
| that together make up the compiler.</p> |
| </li> |
| <li> |
| <p><a href="https://github.com/rust-lang/rust/tree/master/library"><code>library/</code></a> contains the standard libraries (<a href="https://github.com/rust-lang/rust/tree/master/library/core"><code>core</code></a>, <a href="https://github.com/rust-lang/rust/tree/master/library/alloc"><code>alloc</code></a>, <a href="https://github.com/rust-lang/rust/tree/master/library/std"><code>std</code></a>, |
| <a href="https://github.com/rust-lang/rust/tree/master/library/proc_macro"><code>proc_macro</code></a>, <a href="https://github.com/rust-lang/rust/tree/master/library/test"><code>test</code></a>), as well as the Rust runtime (<a href="https://github.com/rust-lang/backtrace-rs/"><code>backtrace</code></a>, <a href="https://github.com/rust-lang/rust/tree/master/library/rtstartup"><code>rtstartup</code></a>, |
| <a href="https://github.com/rust-lang/rust/blob/master/library/std/src/rt.rs"><code>lang_start</code></a>).</p> |
| </li> |
| <li> |
| <p><a href="https://github.com/rust-lang/rust/tree/master/tests"><code>tests/</code></a> contains the compiler tests.</p> |
| </li> |
| <li> |
| <p><a href="https://github.com/rust-lang/rust/tree/master/src"><code>src/</code></a> contains the source code for <a href="https://github.com/rust-lang/rust/tree/master/src/tools/rustdoc"><code>rustdoc</code></a>, <a href="https://github.com/rust-lang/rust/tree/master/src/tools/clippy"><code>clippy</code></a>, <a href="https://github.com/rust-lang/cargo"><code>cargo</code></a>, the build system, |
| language docs, etc.</p> |
| </li> |
| </ul> |
| <h2 id="compiler"><a class="header" href="#compiler">Compiler</a></h2> |
| <p>The compiler is implemented in the various <a href="https://github.com/rust-lang/rust/tree/master/compiler"><code>compiler/</code></a> crates. |
| The <a href="https://github.com/rust-lang/rust/tree/master/compiler"><code>compiler/</code></a> crates all have names starting with <code>rustc_*</code>. These are a |
| collection of around 50 interdependent crates ranging in size from tiny to |
| huge. There is also the <code>rustc</code> crate which is the actual binary (i.e. the |
| <code>main</code> function); it doesn't actually do anything besides calling the |
| <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_driver/index.html"><code>rustc_driver</code></a> crate, which drives the various parts of compilation in other |
| crates.</p> |
| <p>The dependency order of these crates is complex, but roughly it is |
| something like this:</p> |
| <ol> |
| <li><code>rustc</code> (the binary) calls <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_driver/fn.main.html"><code>rustc_driver::main</code></a>.</li> |
| <li><a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_driver/index.html"><code>rustc_driver</code></a> depends on a lot of other crates, but the main one is |
| <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_interface/index.html"><code>rustc_interface</code></a>.</li> |
| <li><a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_interface/index.html"><code>rustc_interface</code></a> depends on most of the other compiler crates. It is a |
| fairly generic interface for driving the whole compilation.</li> |
| <li>Most of the other <code>rustc_*</code> crates depend on <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/index.html"><code>rustc_middle</code></a>, which defines |
| a lot of central data structures in the compiler.</li> |
| <li><a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/index.html"><code>rustc_middle</code></a> and most of the other crates depend on a handful of crates |
| representing the early parts of the compiler (e.g. the parser), fundamental |
| data structures (e.g. <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_span/struct.Span.html"><code>Span</code></a>), or error reporting: |
| <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_data_structures/index.html"><code>rustc_data_structures</code></a>, <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_span/index.html"><code>rustc_span</code></a>, <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_errors/index.html"><code>rustc_errors</code></a>, etc.</li> |
| </ol> |
| <p>You can see the exact dependencies by running <code>cargo tree</code>, |
| just like you would for any other Rust package:</p> |
| <pre><code class="language-console">cargo tree --package rustc_driver |
| </code></pre> |
| <p>One final thing: <a href="https://github.com/rust-lang/rust/tree/master/src/"><code>src/llvm-project</code></a> is a submodule for our fork of LLVM. |
| During bootstrapping, LLVM is built and the <a href="https://github.com/rust-lang/rust/tree/master/compiler/rustc_llvm"><code>compiler/rustc_llvm</code></a> crate |
| contains Rust wrappers around LLVM (which is written in C++), so that the |
| compiler can interface with it.</p> |
| <p>Most of this book is about the compiler, so we won't have any further |
| explanation of these crates here.</p> |
| <h3 id="big-picture"><a class="header" href="#big-picture">Big picture</a></h3> |
| <p>The dependency structure of the compiler is influenced by two main factors:</p> |
| <ol> |
| <li>Organization. The compiler is a <em>huge</em> codebase; it would be an impossibly |
| large crate. In part, the dependency structure reflects the code structure |
| of the compiler.</li> |
| <li>Compile-time. By breaking the compiler into multiple crates, we can take |
| better advantage of incremental/parallel compilation using cargo. In |
| particular, we try to have as few dependencies between crates as possible so |
| that we don't have to rebuild as many crates if you change one.</li> |
| </ol> |
| <p>At the very bottom of the dependency tree are a handful of crates that are used |
| by the whole compiler (e.g. <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_span/index.html"><code>rustc_span</code></a>). The very early parts of the |
| compilation process (e.g. <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_parse/index.html">parsing and the Abstract Syntax Tree (<code>AST</code>)</a>) |
| depend on only these.</p> |
| <p>After the <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_parse/index.html"><code>AST</code></a> is constructed and other early analysis is done, the |
| compiler's <a href="./query.html">query system</a> gets set up. The query system is set up in a |
| clever way using function pointers. This allows us to break dependencies |
| between crates, allowing more parallel compilation. The query system is defined |
| in <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/index.html"><code>rustc_middle</code></a>, so nearly all subsequent parts of the compiler depend on |
| this crate. It is a really large crate, leading to long compile times. Some |
| efforts have been made to move stuff out of it with varying success. Another |
| side-effect is that sometimes related functionality gets scattered across |
| different crates. For example, linting functionality is found across earlier |
| parts of the crate, <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_lint/index.html"><code>rustc_lint</code></a>, <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/index.html"><code>rustc_middle</code></a>, and other places.</p> |
| <p>Ideally there would be fewer, more cohesive crates, with incremental and |
| parallel compilation making sure compile times stay reasonable. However, |
| incremental and parallel compilation haven't gotten good enough for that yet, |
| so breaking things into separate crates has been our solution so far.</p> |
| <p>At the top of the dependency tree is <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_driver/index.html"><code>rustc_driver</code></a> and <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_interface/index.html"><code>rustc_interface</code></a> |
| which is an unstable wrapper around the query system helping drive various |
| stages of compilation. Other consumers of the compiler may use this interface |
| in different ways (e.g. <a href="https://github.com/rust-lang/rust/tree/master/src/tools/rustdoc"><code>rustdoc</code></a> or maybe eventually <code>rust-analyzer</code>). The |
| <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_driver/index.html"><code>rustc_driver</code></a> crate first parses command line arguments and then uses |
| <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_interface/index.html"><code>rustc_interface</code></a> to drive the compilation to completion.</p> |
| <h2 id="rustdoc"><a class="header" href="#rustdoc">rustdoc</a></h2> |
| <p>The bulk of <a href="https://github.com/rust-lang/rust/tree/master/src/tools/rustdoc"><code>rustdoc</code></a> is in <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustdoc/index.html"><code>librustdoc</code></a>. However, the <a href="https://github.com/rust-lang/rust/tree/master/src/tools/rustdoc"><code>rustdoc</code></a> binary |
| itself is <a href="https://github.com/rust-lang/rust/tree/master/src/tools/rustdoc"><code>src/tools/rustdoc</code></a>, which does nothing except call <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustdoc/fn.main.html"><code>rustdoc::main</code></a>.</p> |
| <p>There is also <code>JavaScript</code> and <code>CSS</code> for the docs in <a href="https://github.com/rust-lang/rust/tree/master/src/tools/rustdoc-js"><code>src/tools/rustdoc-js</code></a> |
| and <a href="https://github.com/rust-lang/rust/tree/master/src/tools/rustdoc-themes"><code>src/tools/rustdoc-themes</code></a>.</p> |
| <p>You can read more about <a href="https://github.com/rust-lang/rust/tree/master/src/tools/rustdoc"><code>rustdoc</code></a> in <a href="./rustdoc.html">this chapter</a>.</p> |
| <h2 id="tests"><a class="header" href="#tests">Tests</a></h2> |
| <p>The test suite for all of the above is in <a href="https://github.com/rust-lang/rust/tree/master/tests"><code>tests/</code></a>. You can read more |
| about the test suite <a href="./tests/intro.html">in this chapter</a>.</p> |
| <p>The test harness is in <a href="https://github.com/rust-lang/rust/tree/master/src/tools/compiletest"><code>src/tools/compiletest/</code></a>.</p> |
| <h2 id="build-system"><a class="header" href="#build-system">Build System</a></h2> |
| <p>There are a number of tools in the repository just for building the compiler, |
| standard library, <a href="https://github.com/rust-lang/rust/tree/master/src/tools/rustdoc"><code>rustdoc</code></a>, etc, along with testing, building a full Rust |
| distribution, etc.</p> |
| <p>One of the primary tools is <a href="https://github.com/rust-lang/rust/tree/master/src/bootstrap"><code>src/bootstrap/</code></a>. You can read more about |
| bootstrapping <a href="./building/bootstrapping/intro.html">in this chapter</a>. The process may also use other tools |
| from <a href="https://github.com/rust-lang/rust/tree/master/src/tools"><code>src/tools/</code></a>, such as <a href="https://github.com/rust-lang/rust/tree/master/src/tools/tidy"><code>tidy/</code></a> or <a href="https://github.com/rust-lang/rust/tree/master/src/tools/compiletest"><code>compiletest/</code></a>.</p> |
| <h2 id="standard-library"><a class="header" href="#standard-library">Standard library</a></h2> |
| <p>This code is fairly similar to most other Rust crates except that it must be |
| built in a special way because it can use unstable (<a href="https://doc.rust-lang.org/nightly/nightly-rustc/"><code>nightly</code></a>) features. |
| The standard library is sometimes referred to as <a href="https://rust-lang.github.io/rfcs/0040-libstd-facade.html"><code>libstd or the "standard facade"</code></a>.</p> |
| <h2 id="other"><a class="header" href="#other">Other</a></h2> |
| <p>There are a lot of other things in the <code>rust-lang/rust</code> repo that are related |
| to building a full Rust distribution. Most of the time you don't need to worry about them.</p> |
| <p>These include:</p> |
| <ul> |
| <li><a href="https://github.com/rust-lang/rust/tree/master/src/ci"><code>src/ci</code></a>: The CI configuration. This actually quite extensive because we |
| run a lot of tests on a lot of platforms.</li> |
| <li><a href="https://github.com/rust-lang/rust/tree/master/src/doc"><code>src/doc</code></a>: Various documentation, including submodules for a few books.</li> |
| <li><a href="https://github.com/rust-lang/rust/tree/master/src/etc"><code>src/etc</code></a>: Miscellaneous utilities.</li> |
| <li>And more...</li> |
| </ul> |
| |
| </main> |
| |
| <nav class="nav-wrapper" aria-label="Page navigation"> |
| <!-- Mobile navigation buttons --> |
| <a rel="prev" href="overview.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="query.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="overview.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="query.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> |