<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>docs/dev/architecture.md - fork/rust-analyzer @ 2022-07-18</title>
<meta name="description" content="docs/dev/architecture.md in fork/rust-analyzer at 2022-07-18.">
<meta property="og:title" content="docs/dev/architecture.md - fork/rust-analyzer @ 2022-07-18">
<meta property="og:description" content="docs/dev/architecture.md in fork/rust-analyzer at 2022-07-18.">
<meta property="og:type" content="website">
<meta property="og:site_name" content="git.julia.blue">
<link rel="stylesheet" href="/css/style.css?v=62">
<script type="module" src="/js/main.js?v=62"></script>
</head>
<body>
<main data-ref="2022-07-18" data-tip="897a7ec4b826f85ec1626870e734490701138097">
<noscript>
  <div class="callout">
    <p>
      hey! this is a static git repo viewer. commit diffs, file history,
      older commits and other revisions won't load without javascript.
    </p>
    <p>
      you are still able to view source files on branch/tag tips,
      but the experience is degraded.
    </p>
  </div>
</noscript>
<header class="repo"><div>
<h1>fork/rust-analyzer</h1>
</div>
<pre class="clone">git clone https://git.julia.blue/fork/rust-analyzer</pre>
</header>
<p class="snapshot-commit"><span class="commit-author">bors</span><span class="commit-message" data-commit="df4ed94f2fb7c16daefa1722657216609f0e8d31">Auto merge of #12722 - jonas-schievink:update-urls, r=jonas-schievink</span><span class="snapshot-sha sha" data-commit="df4ed94f2fb7c16daefa1722657216609f0e8d31">df4ed94f2f</span><time datetime="2022-07-08T13:50:20+00:00" title="2022-07-08 13:50:20 +0000">2022-07-08</time></p>
<header class="topbar"><details class="switcher"><summary>2022-07-18</summary><div class="menu"><strong>branches</strong><a href="/fork/rust-analyzer/ref/master/">master</a><strong>tags</strong><a href="/fork/rust-analyzer/ref/2022-06-13/">2022-06-13</a><a href="/fork/rust-analyzer/ref/2022-06-20/">2022-06-20</a><a href="/fork/rust-analyzer/ref/2022-06-27/">2022-06-27</a><a href="/fork/rust-analyzer/ref/2022-07-04/">2022-07-04</a><a href="/fork/rust-analyzer/ref/2022-07-11/">2022-07-11</a><a class="current" href="/fork/rust-analyzer/ref/2022-07-18/">2022-07-18</a><a href="/fork/rust-analyzer/ref/2022-07-25/">2022-07-25</a><a href="/fork/rust-analyzer/ref/nightly/">nightly</a><a class="all" href="/fork/rust-analyzer/refs">all refs →</a></div></details><nav class="crumbs"><a href="/fork/rust-analyzer/">rust-analyzer</a> / <a href="/fork/rust-analyzer/ref/2022-07-18/tree/docs/">docs</a> / <a href="/fork/rust-analyzer/ref/2022-07-18/tree/docs/dev/">dev</a> / <span class="cur">architecture.md</span></nav>
<span class="view-stats"><span>26.3 KiB</span><span>497 lines</span></span><span class="actions"><a class="raw" href="/fork/rust-analyzer/raw/ea4035baf114c1599210a71f5d0630f1b413b67d/docs/dev/architecture.md">raw</a></span></header>
<div class="markdown-view"><input type="radio" name="markdown-view" id="markdown-rendered" checked><label for="markdown-rendered">rendered</label><input type="radio" name="markdown-view" id="markdown-code"><label for="markdown-code">code</label><section class="readme rendered"><h1>Architecture</h1>
<p>This document describes the high-level architecture of rust-analyzer.
If you want to familiarize yourself with the code base, you are just in the right place!</p>
<p>You might also enjoy <a href="https://www.youtube.com/playlist?list=PLhb66M_x9UmrqXhQuIpWC5VgTdrGxMx3y">"Explaining Rust Analyzer"</a> series on YouTube.
It goes deeper than what is covered in this document, but will take some time to watch.</p>
<p>See also these implementation-related blog posts:</p>
<ul>
<li>https://rust-analyzer.github.io/blog/2019/11/13/find-usages.html</li>
<li>https://rust-analyzer.github.io/blog/2020/07/20/three-architectures-for-responsive-ide.html</li>
<li>https://rust-analyzer.github.io/blog/2020/09/16/challeging-LR-parsing.html</li>
<li>https://rust-analyzer.github.io/blog/2020/09/28/how-to-make-a-light-bulb.html</li>
<li>https://rust-analyzer.github.io/blog/2020/10/24/introducing-ungrammar.html</li>
</ul>
<p>For older, by now mostly outdated stuff, see the <a href="/fork/rust-analyzer/ref/2022-07-18/blob/docs/dev/guide.md">guide</a> and <a href="https://www.youtube.com/playlist?list=PL85XCvVPmGQho7MZkdW-wtPtuJcFpzycE">another playlist</a>.</p>
<h2>Bird's Eye View</h2>
<p><img src="https://user-images.githubusercontent.com/4789492/107129398-0ab70f00-687a-11eb-9bfc-d4eb023aec06.png" alt="" /></p>
<p>On the highest level, rust-analyzer is a thing which accepts input source code from the client and produces a structured semantic model of the code.</p>
<p>More specifically, input data consists of a set of test files (<code>(PathBuf, String)</code> pairs) and information about project structure, captured in the so called <code>CrateGraph</code>.
The crate graph specifies which files are crate roots, which cfg flags are specified for each crate and what dependencies exist between the crates.
This is the input (ground) state.
The analyzer keeps all this input data in memory and never does any IO.
Because the input data is source code, which typically measures in tens of megabytes at most, keeping everything in memory is OK.</p>
<p>A "structured semantic model" is basically an object-oriented representation of modules, functions and types which appear in the source code.
This representation is fully "resolved": all expressions have types, all references are bound to declarations, etc.
This is derived state.</p>
<p>The client can submit a small delta of input data (typically, a change to a single file) and get a fresh code model which accounts for changes.</p>
<p>The underlying engine makes sure that model is computed lazily (on-demand) and can be quickly updated for small modifications.</p>
<h2>Entry Points</h2>
<p><code>crates/rust-analyzer/src/bin/main.rs</code> contains the main function which spawns LSP.
This is <em>the</em> entry point, but it front-loads a lot of complexity, so it's fine to just skim through it.</p>
<p><code>crates/rust-analyzer/src/handlers.rs</code> implements all LSP requests and is a great place to start if you are already familiar with LSP.</p>
<p><code>Analysis</code> and <code>AnalysisHost</code> types define the main API for consumers of IDE services.</p>
<h2>Code Map</h2>
<p>This section talks briefly about various important directories and data structures.
Pay attention to the <strong>Architecture Invariant</strong> sections.
They often talk about things which are deliberately absent in the source code.</p>
<p>Note also which crates are <strong>API Boundaries</strong>.
Remember, <a href="https://www.tedinski.com/2018/02/06/system-boundaries.html">rules at the boundary are different</a>.</p>
<h3><code>xtask</code></h3>
<p>This is rust-analyzer's "build system".
We use cargo to compile rust code, but there are also various other tasks, like release management or local installation.
They are handled by Rust code in the xtask directory.</p>
<h3><code>editors/code</code></h3>
<p>VS Code plugin.</p>
<h3><code>lib/</code></h3>
<p>rust-analyzer independent libraries which we publish to crates.io.
It's not heavily utilized at the moment.</p>
<h3><code>crates/parser</code></h3>
<p>It is a hand-written recursive descent parser, which produces a sequence of events like "start node X", "finish node Y".
It works similarly to
<a href="https://github.com/JetBrains/kotlin/blob/4d951de616b20feca92f3e9cc9679b2de9e65195/compiler/frontend/src/org/jetbrains/kotlin/parsing/KotlinParsing.java">kotlin's parser</a>,
which is a good source of inspiration for dealing with syntax errors and incomplete input.
Original <a href="https://github.com/rust-lang/rust/blob/6b99adeb11313197f409b4f7c4083c2ceca8a4fe/src/libsyntax/parse/parser.rs">libsyntax parser</a> is what we use for the definition of the Rust language.
<code>TreeSink</code> and <code>TokenSource</code> traits bridge the tree-agnostic parser from <code>grammar</code> with <code>rowan</code> trees.</p>
<p><strong>Architecture Invariant:</strong> the parser is independent of the particular tree structure and particular representation of the tokens.
It transforms one flat stream of events into another flat stream of events.
Token independence allows us to parse out both text-based source code and <code>tt</code>-based macro input.
Tree independence allows us to more easily vary the syntax tree implementation.
It should also unlock efficient light-parsing approaches.
For example, you can extract the set of names defined in a file (for typo correction) without building a syntax tree.</p>
<p><strong>Architecture Invariant:</strong> parsing never fails, the parser produces <code>(T, Vec&lt;Error&gt;)</code> rather than <code>Result&lt;T, Error&gt;</code>.</p>
<h3><code>crates/syntax</code></h3>
<p>Rust syntax tree structure and parser.
See <a href="https://github.com/rust-lang/rfcs/pull/2256">RFC</a> and <a href="/fork/rust-analyzer/ref/2022-07-18/blob/docs/dev/syntax.md">./syntax.md</a> for some design notes.</p>
<ul>
<li><a href="https://github.com/rust-analyzer/rowan">rowan</a> library is used for constructing syntax trees.</li>
<li><code>ast</code> provides a type safe API on top of the raw <code>rowan</code> tree.</li>
<li><code>ungrammar</code> description of the grammar, which is used to generate <code>syntax_kinds</code> and <code>ast</code> modules, using <code>cargo test -p xtask</code> command.</li>
</ul>
<p>Tests for ra_syntax are mostly data-driven.
<code>test_data/parser</code> contains subdirectories with a bunch of <code>.rs</code> (test vectors) and <code>.txt</code> files with corresponding syntax trees.
During testing, we check <code>.rs</code> against <code>.txt</code>.
If the <code>.txt</code> file is missing, it is created (this is how you update tests).
Additionally, running the xtask test suite with <code>cargo test -p xtask</code> will walk the grammar module and collect all <code>// test test_name</code> comments into files inside <code>test_data/parser/inline</code> directory.</p>
<p>To update test data, run with <code>UPDATE_EXPECT</code> variable:</p>
<pre class="language-bash"><a-f>env</a-f> UPDATE_EXPECT=1 cargo qt</pre>
<p>After adding a new inline test you need to run <code>cargo test -p xtask</code> and also update the test data as described above.</p>
<p>Note <a href="https://github.com/rust-lang/rust-analyzer/blob/2fb6af89eb794f775de60b82afe56b6f986c2a40/crates/ra_syntax/src/lib.rs#L190-L348"><code>api_walkthrough</code></a>
in particular: it shows off various methods of working with syntax tree.</p>
<p>See <a href="https://github.com/rust-lang/rust-analyzer/pull/93">#93</a> for an example PR which fixes a bug in the grammar.</p>
<p><strong>Architecture Invariant:</strong> <code>syntax</code> crate is completely independent from the rest of rust-analyzer. It knows nothing about salsa or LSP.
This is important because it is possible to make useful tooling using only the syntax tree.
Without semantic information, you don't need to be able to <em>build</em> code, which makes the tooling more robust.
See also https://web.stanford.edu/~mlfbrown/paper.pdf.
You can view the <code>syntax</code> crate as an entry point to rust-analyzer.
<code>syntax</code> crate is an <strong>API Boundary</strong>.</p>
<p><strong>Architecture Invariant:</strong> syntax tree is a value type.
The tree is fully determined by the contents of its syntax nodes, it doesn't need global context (like an interner) and doesn't store semantic info.
Using the tree as a store for semantic info is convenient in traditional compilers, but doesn't work nicely in the IDE.
Specifically, assists and refactors require transforming syntax trees, and that becomes awkward if you need to do something with the semantic info.</p>
<p><strong>Architecture Invariant:</strong> syntax tree is built for a single file.
This is to enable parallel parsing of all files.</p>
<p><strong>Architecture Invariant:</strong>  Syntax trees are by design incomplete and do not enforce well-formedness.
If an AST method returns an <code>Option</code>, it <em>can</em> be <code>None</code> at runtime, even if this is forbidden by the grammar.</p>
<h3><code>crates/base_db</code></h3>
<p>We use the <a href="https://github.com/salsa-rs/salsa">salsa</a> crate for incremental and on-demand computation.
Roughly, you can think of salsa as a key-value store, but it can also compute derived values using specified functions.
The <code>base_db</code> crate provides basic infrastructure for interacting with salsa.
Crucially, it defines most of the "input" queries: facts supplied by the client of the analyzer.
Reading the docs of the <code>base_db::input</code> module should be useful: everything else is strictly derived from those inputs.</p>
<p><strong>Architecture Invariant:</strong> particularities of the build system are <em>not</em> the part of the ground state.
In particular, <code>base_db</code> knows nothing about cargo.
For example, <code>cfg</code> flags are a part of <code>base_db</code>, but <code>feature</code>s are not.
A <code>foo</code> feature is a Cargo-level concept, which is lowered by Cargo to <code>--cfg feature=foo</code> argument on the command line.
The <code>CrateGraph</code> structure is used to represent the dependencies between the crates abstractly.</p>
<p><strong>Architecture Invariant:</strong> <code>base_db</code> doesn't know about file system and file paths.
Files are represented with opaque <code>FileId</code>, there's no operation to get an <code>std::path::Path</code> out of the <code>FileId</code>.</p>
<h3><code>crates/hir_expand</code>, <code>crates/hir_def</code>, <code>crates/hir_ty</code></h3>
<p>These crates are the <em>brain</em> of rust-analyzer.
This is the compiler part of the IDE.</p>
<p><code>hir_xxx</code> crates have a strong <a href="https://en.wikipedia.org/wiki/Entity_component_system">ECS</a> flavor, in that they work with raw ids and directly query the database.
There's little abstraction here.
These crates integrate deeply with salsa and chalk.</p>
<p>Name resolution, macro expansion and type inference all happen here.
These crates also define various intermediate representations of the core.</p>
<p><code>ItemTree</code> condenses a single <code>SyntaxTree</code> into a "summary" data structure, which is stable over modifications to function bodies.</p>
<p><code>DefMap</code> contains the module tree of a crate and stores module scopes.</p>
<p><code>Body</code> stores information about expressions.</p>
<p><strong>Architecture Invariant:</strong> these crates are not, and will never be, an api boundary.</p>
<p><strong>Architecture Invariant:</strong> these crates explicitly care about being incremental.
The core invariant we maintain is "typing inside a function's body never invalidates global derived data".
i.e., if you change the body of <code>foo</code>, all facts about <code>bar</code> should remain intact.</p>
<p><strong>Architecture Invariant:</strong> hir exists only in context of particular crate instance with specific CFG flags.
The same syntax may produce several instances of HIR if the crate participates in the crate graph more than once.</p>
<h3><code>crates/hir</code></h3>
<p>The top-level <code>hir</code> crate is an <strong>API Boundary</strong>.
If you think about "using rust-analyzer as a library", <code>hir</code> crate is most likely the façade you'll be talking to.</p>
<p>It wraps ECS-style internal API into a more OO-flavored API (with an extra <code>db</code> argument for each call).</p>
<p><strong>Architecture Invariant:</strong> <code>hir</code> provides a static, fully resolved view of the code.
While internal <code>hir_*</code> crates <em>compute</em> things, <code>hir</code>, from the outside, looks like an inert data structure.</p>
<p><code>hir</code> also handles the delicate task of going from syntax to the corresponding <code>hir</code>.
Remember that the mapping here is one-to-many.
See <code>Semantics</code> type and <code>source_to_def</code> module.</p>
<p>Note in particular a curious recursive structure in <code>source_to_def</code>.
We first resolve the parent <em>syntax</em> node to the parent <em>hir</em> element.
Then we ask the <em>hir</em> parent what <em>syntax</em> children does it have.
Then we look for our node in the set of children.</p>
<p>This is the heart of many IDE features, like goto definition, which start with figuring out the hir node at the cursor.
This is some kind of (yet unnamed) uber-IDE pattern, as it is present in Roslyn and Kotlin as well.</p>
<h3><code>crates/ide</code></h3>
<p>The <code>ide</code> crate builds on top of <code>hir</code> semantic model to provide high-level IDE features like completion or goto definition.
It is an <strong>API Boundary</strong>.
If you want to use IDE parts of rust-analyzer via LSP, custom flatbuffers-based protocol or just as a library in your text editor, this is the right API.</p>
<p><strong>Architecture Invariant:</strong> <code>ide</code> crate's API is build out of POD types with public fields.
The API uses editor's terminology, it talks about offsets and string labels rather than in terms of definitions or types.
It is effectively the view in MVC and viewmodel in <a href="https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93viewmodel">MVVM</a>.
All arguments and return types are conceptually serializable.
In particular, syntax trees and hir types are generally absent from the API (but are used heavily in the implementation).
Shout outs to LSP developers for popularizing the idea that "UI" is a good place to draw a boundary at.</p>
<p><code>ide</code> is also the first crate which has the notion of change over time.
<code>AnalysisHost</code> is a state to which you can transactionally <code>apply_change</code>.
<code>Analysis</code> is an immutable snapshot of the state.</p>
<p>Internally, <code>ide</code> is split across several crates. <code>ide_assists</code>, <code>ide_completion</code> and <code>ide_ssr</code> implement large isolated features.
<code>ide_db</code> implements common IDE functionality (notably, reference search is implemented here).
The <code>ide</code> contains a public API/façade, as well as implementation for a plethora of smaller features.</p>
<p><strong>Architecture Invariant:</strong> <code>ide</code> crate strives to provide a <em>perfect</em> API.
Although at the moment it has only one consumer, the LSP server, LSP <em>does not</em> influence its API design.
Instead, we keep in mind a hypothetical <em>ideal</em> client -- an IDE tailored specifically for rust, every nook and cranny of which is packed with Rust-specific goodies.</p>
<h3><code>crates/rust-analyzer</code></h3>
<p>This crate defines the <code>rust-analyzer</code> binary, so it is the <strong>entry point</strong>.
It implements the language server.</p>
<p><strong>Architecture Invariant:</strong> <code>rust-analyzer</code> is the only crate that knows about LSP and JSON serialization.
If you want to expose a data structure <code>X</code> from ide to LSP, don't make it serializable.
Instead, create a serializable counterpart in <code>rust-analyzer</code> crate and manually convert between the two.</p>
<p><code>GlobalState</code> is the state of the server.
The <code>main_loop</code> defines the server event loop which accepts requests and sends responses.
Requests that modify the state or might block user's typing are handled on the main thread.
All other requests are processed in background.</p>
<p><strong>Architecture Invariant:</strong> the server is stateless, a-la HTTP.
Sometimes state needs to be preserved between requests.
For example, "what is the <code>edit</code> for the fifth completion item of the last completion edit?".
For this, the second request should include enough info to re-create the context from scratch.
This generally means including all the parameters of the original request.</p>
<p><code>reload</code> module contains the code that handles configuration and Cargo.toml changes.
This is a tricky business.</p>
<p><strong>Architecture Invariant:</strong> <code>rust-analyzer</code> should be partially available even when the build is broken.
Reloading process should not prevent IDE features from working.</p>
<h3><code>crates/toolchain</code>, <code>crates/project_model</code>, <code>crates/flycheck</code></h3>
<p>These crates deal with invoking <code>cargo</code> to learn about project structure and get compiler errors for the "check on save" feature.</p>
<p>They use <code>crates/path</code> heavily instead of <code>std::path</code>.
A single <code>rust-analyzer</code> process can serve many projects, so it is important that server's current directory does not leak.</p>
<h3><code>crates/mbe</code>, <code>crates/tt</code>, <code>crates/proc_macro_api</code>, <code>crates/proc_macro_srv</code></h3>
<p>These crates implement macros as token tree -&gt; token tree transforms.
They are independent from the rest of the code.</p>
<p><code>tt</code> crate defined <code>TokenTree</code>, a single token or a delimited sequence of token trees.
<code>mbe</code> crate contains tools for transforming between syntax trees and token tree.
And it also handles the actual parsing and expansion of declarative macro (a-la "Macros By Example" or mbe).</p>
<p>For proc macros, the client-server model are used.
We pass an argument <code>--proc-macro</code> to <code>rust-analyzer</code> binary to start a separate process  (<code>proc_macro_srv</code>).
And the client (<code>proc_macro_api</code>) provides an interface to talk to that server separately.</p>
<p>And then token trees are passed from client, and the server will load the corresponding dynamic library (which built by <code>cargo</code>).
And due to the fact the api for getting result from proc macro are always unstable in <code>rustc</code>,
we maintain our own copy (and paste) of that part of code to allow us to build the whole thing in stable rust.</p>
<p><strong>Architecture Invariant:</strong>
Bad proc macros may panic or segfault accidentally. So we run it in another process and recover it from fatal error.
And they may be non-deterministic which conflict how <code>salsa</code> works, so special attention is required.</p>
<h3><code>crates/cfg</code></h3>
<p>This crate is responsible for parsing, evaluation and general definition of <code>cfg</code> attributes.</p>
<h3><code>crates/vfs</code>, <code>crates/vfs-notify</code></h3>
<p>These crates implement a virtual file system.
They provide consistent snapshots of the underlying file system and insulate messy OS paths.</p>
<p><strong>Architecture Invariant:</strong> vfs doesn't assume a single unified file system.
i.e., a single rust-analyzer process can act as a remote server for two different machines, where the same <code>/tmp/foo.rs</code> path points to different files.
For this reason, all path APIs generally take some existing path as a "file system witness".</p>
<h3><code>crates/stdx</code></h3>
<p>This crate contains various non-rust-analyzer specific utils, which could have been in std, as well
as copies of unstable std items we would like to make use of already, like <code>std::str::split_once</code>.</p>
<h3><code>crates/profile</code></h3>
<p>This crate contains utilities for CPU and memory profiling.</p>
<h2>Cross-Cutting Concerns</h2>
<p>This sections talks about the things which are everywhere and nowhere in particular.</p>
<h3>Stability Guarantees</h3>
<p>One of the reasons rust-analyzer moves relatively fast is that we don't introduce new stability guarantees.
Instead, as much as possible we leverage existing ones.</p>
<p>Examples:</p>
<ul>
<li>The <code>ide</code> API of rust-analyzer are explicitly unstable, but the LSP interface is stable, and here we just implement a stable API managed by someone else.</li>
<li>Rust language and Cargo are stable, and they are the primary inputs to rust-analyzer.</li>
<li>The <code>rowan</code> library is published to crates.io, but it is deliberately kept under <code>1.0</code> and always makes semver-incompatible upgrades</li>
</ul>
<p>Another important example is that rust-analyzer isn't run on CI, so, unlike <code>rustc</code> and <code>clippy</code>, it is actually ok for us to change runtime behavior.</p>
<p>At some point we might consider opening up APIs or allowing crates.io libraries to include rust-analyzer specific annotations, but that's going to be a big commitment on our side.</p>
<p>Exceptions:</p>
<ul>
<li><code>rust-project.json</code> is a de-facto stable format for non-cargo build systems.
It is probably ok enough, but was definitely stabilized implicitly.
Lesson for the future: when designing API which could become a stability boundary, don't wait for the first users until you stabilize it.
By the time you have first users, it is already de-facto stable.
And the users will first use the thing, and <em>then</em> inform you that now you have users.
The sad thing is that stuff should be stable before someone uses it for the first time, or it should contain explicit opt-in.</li>
<li>We ship some LSP extensions, and we try to keep those somewhat stable.
Here, we need to work with a finite set of editor maintainers, so not providing rock-solid guarantees works.</li>
</ul>
<h3>Code generation</h3>
<p>Some components in this repository are generated through automatic processes.
Generated code is updated automatically on <code>cargo test</code>.
Generated code is generally committed to the git repository.</p>
<p>In particular, we generate:</p>
<ul>
<li>
<p>API for working with syntax trees (<code>syntax::ast</code>, the <a href="https://github.com/rust-analyzer/ungrammar"><code>ungrammar</code></a> crate).</p>
</li>
<li>
<p>Various sections of the manual:</p>
<ul>
<li>features</li>
<li>assists</li>
<li>config</li>
</ul>
</li>
<li>
<p>Documentation tests for assists</p>
</li>
</ul>
<p>See the <code>sourcegen</code> crate for details.</p>
<p><strong>Architecture Invariant:</strong> we avoid bootstrapping.
For codegen we need to parse Rust code.
Using rust-analyzer for that would work and would be fun, but it would also complicate the build process a lot.
For that reason, we use syn and manual string parsing.</p>
<h3>Cancellation</h3>
<p>Let's say that the IDE is in the process of computing syntax highlighting, when the user types <code>foo</code>.
What should happen?
<code>rust-analyzer</code>s answer is that the highlighting process should be cancelled -- its results are now stale, and it also blocks modification of the inputs.</p>
<p>The salsa database maintains a global revision counter.
When applying a change, salsa bumps this counter and waits until all other threads using salsa finish.
If a thread does salsa-based computation and notices that the counter is incremented, it panics with a special value (see <code>Canceled::throw</code>).
That is, rust-analyzer requires unwinding.</p>
<p><code>ide</code> is the boundary where the panic is caught and transformed into a <code>Result&lt;T, Cancelled&gt;</code>.</p>
<h3>Testing</h3>
<p>Rust Analyzer has three interesting <a href="https://www.tedinski.com/2018/04/10/making-tests-a-positive-influence-on-design.html">system boundaries</a> to concentrate tests on.</p>
<p>The outermost boundary is the <code>rust-analyzer</code> crate, which defines an LSP interface in terms of stdio.
We do integration testing of this component, by feeding it with a stream of LSP requests and checking responses.
These tests are known as "heavy", because they interact with Cargo and read real files from disk.
For this reason, we try to avoid writing too many tests on this boundary: in a statically typed language, it's hard to make an error in the protocol itself if messages are themselves typed.
Heavy tests are only run when <code>RUN_SLOW_TESTS</code> env var is set.</p>
<p>The middle, and most important, boundary is <code>ide</code>.
Unlike <code>rust-analyzer</code>, which exposes API, <code>ide</code> uses Rust API and is intended for use by various tools.
A typical test creates an <code>AnalysisHost</code>, calls some <code>Analysis</code> functions and compares the results against expectation.</p>
<p>The innermost and most elaborate boundary is <code>hir</code>.
It has a much richer vocabulary of types than <code>ide</code>, but the basic testing setup is the same: we create a database, run some queries, assert result.</p>
<p>For comparisons, we use the <code>expect</code> crate for snapshot testing.</p>
<p>To test various analysis corner cases and avoid forgetting about old tests, we use so-called marks.
See the <code>marks</code> module in the <code>test_utils</code> crate for more.</p>
<p><strong>Architecture Invariant:</strong> rust-analyzer tests do not use libcore or libstd.
All required library code must be a part of the tests.
This ensures fast test execution.</p>
<p><strong>Architecture Invariant:</strong> tests are data driven and do not test the API.
Tests which directly call various API functions are a liability, because they make refactoring the API significantly more complicated.
So most of the tests look like this:</p>
<pre class="language-rust"><a-at>#</a-at><a-p>[</a-p><a-at>track_caller</a-at><a-p>]</a-p>
<a-k>fn</a-k> <a-f>check</a-f><a-p>(</a-p><a-v>input</a-v><a-p>:</a-p> <a-o>&amp;</a-o><a-t>str</a-t><a-p>,</a-p> <a-v>expect</a-v><a-p>:</a-p> expect_test<a-p>::</a-p><a-t>Expect</a-t><a-p>)</a-p> <a-p>{</a-p>
    <a-c>// The single place that actually exercises a particular API</a-c>
<a-p>}</a-p>

<a-at>#</a-at><a-p>[</a-p><a-at>test</a-at><a-p>]</a-p>
<a-k>fn</a-k> <a-f>foo</a-f><a-p>()</a-p> <a-p>{</a-p>
    <a-f>check</a-f><a-p>(</a-p><a-s>&quot;foo&quot;</a-s><a-p>,</a-p> <a-m>expect!</a-m><a-p>[[</a-p><a-s>&quot;bar&quot;</a-s><a-p>]]);</a-p>
<a-p>}</a-p>

<a-at>#</a-at><a-p>[</a-p><a-at>test</a-at><a-p>]</a-p>
<a-k>fn</a-k> <a-f>spam</a-f><a-p>()</a-p> <a-p>{</a-p>
    <a-f>check</a-f><a-p>(</a-p><a-s>&quot;spam&quot;</a-s><a-p>,</a-p> <a-m>expect!</a-m><a-p>[[</a-p><a-s>&quot;eggs&quot;</a-s><a-p>]]);</a-p>
<a-p>}</a-p>
<a-c>// ...and a hundred more tests that don&#39;t care about the specific API at all.</a-c></pre>
<p>To specify input data, we use a single string literal in a special format, which can describe a set of rust files.
See the <code>Fixture</code> its module for fixture examples and documentation.</p>
<p><strong>Architecture Invariant:</strong> all code invariants are tested by <code>#[test]</code> tests.
There's no additional checks in CI, formatting and tidy tests are run with <code>cargo test</code>.</p>
<p><strong>Architecture Invariant:</strong> tests do not depend on any kind of external resources, they are perfectly reproducible.</p>
<h3>Performance Testing</h3>
<p>TBA, take a look at the <code>metrics</code> xtask and <code>#[test] fn benchmark_xxx()</code> functions.</p>
<h3>Error Handling</h3>
<p><strong>Architecture Invariant:</strong> core parts of rust-analyzer (<code>ide</code>/<code>hir</code>) don't interact with the outside world and thus can't fail.
Only parts touching LSP are allowed to do IO.</p>
<p>Internals of rust-analyzer need to deal with broken code, but this is not an error condition.
rust-analyzer is robust: various analysis compute <code>(T, Vec&lt;Error&gt;)</code> rather than <code>Result&lt;T, Error&gt;</code>.</p>
<p>rust-analyzer is a complex long-running process.
It will always have bugs and panics.
But a panic in an isolated feature should not bring down the whole process.
Each LSP-request is protected by a <code>catch_unwind</code>.
We use <code>always</code> and <code>never</code> macros instead of <code>assert</code> to gracefully recover from impossible conditions.</p>
<h3>Observability</h3>
<p>rust-analyzer is a long-running process, so it is important to understand what's going on inside.
We have several instruments for that.</p>
<p>The event loop that runs rust-analyzer is very explicit.
Rather than spawning futures or scheduling callbacks (open), the event loop accepts an <code>enum</code> of possible events (closed).
It's easy to see all the things that trigger rust-analyzer processing, together with their performance</p>
<p>rust-analyzer includes a simple hierarchical profiler (<code>hprof</code>).
It is enabled with <code>RA_PROFILE='*&gt;50'</code> env var (log all (<code>*</code>) actions which take more than <code>50</code> ms) and produces output like:</p>
<pre>85ms - handle_completion
    68ms - import_on_the_fly
        67ms - import_assets::search_for_relative_paths
             0ms - crate_def_map:wait (804 calls)
             0ms - find_path (16 calls)
             2ms - find_similar_imports (1 calls)
             0ms - generic_params_query (334 calls)
            59ms - trait_solve_query (186 calls)
         0ms - Semantics::analyze_impl (1 calls)
         1ms - render_resolution (8 calls)
     0ms - Semantics::analyze_impl (5 calls)
</pre>
<p>This is cheap enough to enable in production.</p>
<p>Similarly, we save live object counting (<code>RA_COUNT=1</code>).
It is not cheap enough to enable in prod, and this is a bug which should be fixed.</p>
<h3>Configurability</h3>
<p>rust-analyzer strives to be as configurable as possible while offering reasonable defaults where no configuration exists yet.
There will always be features that some people find more annoying than helpful, so giving the users the ability to tweak or disable these is a big part of offering a good user experience.
Mind the code--architecture gap: at the moment, we are using fewer feature flags than we really should.</p>
<h3>Serialization</h3>
<p>In Rust, it is easy (often too easy) to add serialization to any type by adding <code>#[derive(Serialize)]</code>.
This easiness is misleading -- serializable types impose significant backwards compatability constraints.
If a type is serializable, then it is a part of some IPC boundary.
You often don't control the other side of this boundary, so changing serializable types is hard.</p>
<p>For this reason, the types in <code>ide</code>, <code>base_db</code> and below are not serializable by design.
If such types need to cross an IPC boundary, then the client of rust-analyzer needs to provide custom, client-specific serialization format.
This isolates backwards compatibility and migration concerns to a specific client.</p>
<p>For example, <code>rust-project.json</code> is it's own format -- it doesn't include <code>CrateGraph</code> as is.
Instead, it creates a <code>CrateGraph</code> by calling appropriate constructing functions.</p>
</section><div class="code-panel"><pre class="code src language-markdown"><span class="code-line"><a class="ln" id="L1" href="#L1">1</a><span class="code-text"><a-p>#</a-p> <a-tt>Architecture</a-tt></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L2" href="#L2">2</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L3" href="#L3">3</a><span class="code-text">This document describes the high-level architecture of rust-analyzer.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L4" href="#L4">4</a><span class="code-text">If you want to familiarize yourself with the code base, you are just in the right place!</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L5" href="#L5">5</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L6" href="#L6">6</a><span class="code-text">You might also enjoy [&quot;Explaining Rust Analyzer&quot;](https://www.youtube.com/playlist?list=PLhb66M_x9UmrqXhQuIpWC5VgTdrGxMx3y) series on YouTube.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L7" href="#L7">7</a><span class="code-text">It goes deeper than what is covered in this document, but will take some time to watch.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L8" href="#L8">8</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L9" href="#L9">9</a><span class="code-text">See also these implementation-related blog posts:</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L10" href="#L10">10</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L11" href="#L11">11</a><span class="code-text"><a-p>* </a-p>https://rust-analyzer.github.io/blog/2019/11/13/find-usages.html</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L12" href="#L12">12</a><span class="code-text"><a-p>* </a-p>https://rust-analyzer.github.io/blog/2020/07/20/three-architectures-for-responsive-ide.html</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L13" href="#L13">13</a><span class="code-text"><a-p>* </a-p>https://rust-analyzer.github.io/blog/2020/09/16/challeging-LR-parsing.html</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L14" href="#L14">14</a><span class="code-text"><a-p>* </a-p>https://rust-analyzer.github.io/blog/2020/09/28/how-to-make-a-light-bulb.html</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L15" href="#L15">15</a><span class="code-text"><a-p>* </a-p>https://rust-analyzer.github.io/blog/2020/10/24/introducing-ungrammar.html</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L16" href="#L16">16</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L17" href="#L17">17</a><span class="code-text"><a-p>For older, by now mostly outdated stuff, see the [guide](./guide.md) and [another playlist](https://www.youtube.com/playlist?list=PL85XCvVPmGQho7MZkdW-wtPtuJcFpzycE).</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L18" href="#L18">18</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L19" href="#L19">19</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L20" href="#L20">20</a><span class="code-text"><a-p>## </a-p><a-tt>Bird&#39;s Eye View</a-tt></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L21" href="#L21">21</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L22" href="#L22">22</a><span class="code-text"><a-p>![](https://user-images.githubusercontent.com/4789492/107129398-0ab70f00-687a-11eb-9bfc-d4eb023aec06.png)</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L23" href="#L23">23</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L24" href="#L24">24</a><span class="code-text"><a-p>On the highest level, rust-analyzer is a thing which accepts input source code from the client and produces a structured semantic model of the code.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L25" href="#L25">25</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L26" href="#L26">26</a><span class="code-text"><a-p>More specifically, input data consists of a set of test files (`(PathBuf, String)` pairs) and information about project structure, captured in the so called `CrateGraph`.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L27" href="#L27">27</a><span class="code-text"><a-p>The crate graph specifies which files are crate roots, which cfg flags are specified for each crate and what dependencies exist between the crates.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L28" href="#L28">28</a><span class="code-text"><a-p>This is the input (ground) state.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L29" href="#L29">29</a><span class="code-text"><a-p>The analyzer keeps all this input data in memory and never does any IO.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L30" href="#L30">30</a><span class="code-text"><a-p>Because the input data is source code, which typically measures in tens of megabytes at most, keeping everything in memory is OK.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L31" href="#L31">31</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L32" href="#L32">32</a><span class="code-text"><a-p>A &quot;structured semantic model&quot; is basically an object-oriented representation of modules, functions and types which appear in the source code.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L33" href="#L33">33</a><span class="code-text"><a-p>This representation is fully &quot;resolved&quot;: all expressions have types, all references are bound to declarations, etc.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L34" href="#L34">34</a><span class="code-text"><a-p>This is derived state.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L35" href="#L35">35</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L36" href="#L36">36</a><span class="code-text"><a-p>The client can submit a small delta of input data (typically, a change to a single file) and get a fresh code model which accounts for changes.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L37" href="#L37">37</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L38" href="#L38">38</a><span class="code-text"><a-p>The underlying engine makes sure that model is computed lazily (on-demand) and can be quickly updated for small modifications.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L39" href="#L39">39</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L40" href="#L40">40</a><span class="code-text"><a-p>## </a-p><a-tt>Entry Points</a-tt></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L41" href="#L41">41</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L42" href="#L42">42</a><span class="code-text"><a-p>`crates/rust-analyzer/src/bin/main.rs` contains the main function which spawns LSP.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L43" href="#L43">43</a><span class="code-text"><a-p>This is *the* entry point, but it front-loads a lot of complexity, so it&#39;s fine to just skim through it.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L44" href="#L44">44</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L45" href="#L45">45</a><span class="code-text"><a-p>`crates/rust-analyzer/src/handlers.rs` implements all LSP requests and is a great place to start if you are already familiar with LSP.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L46" href="#L46">46</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L47" href="#L47">47</a><span class="code-text"><a-p>`Analysis` and `AnalysisHost` types define the main API for consumers of IDE services.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L48" href="#L48">48</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L49" href="#L49">49</a><span class="code-text"><a-p>## </a-p><a-tt>Code Map</a-tt></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L50" href="#L50">50</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L51" href="#L51">51</a><span class="code-text"><a-p>This section talks briefly about various important directories and data structures.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L52" href="#L52">52</a><span class="code-text"><a-p>Pay attention to the **Architecture Invariant** sections.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L53" href="#L53">53</a><span class="code-text"><a-p>They often talk about things which are deliberately absent in the source code.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L54" href="#L54">54</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L55" href="#L55">55</a><span class="code-text"><a-p>Note also which crates are **API Boundaries**.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L56" href="#L56">56</a><span class="code-text"><a-p>Remember, [rules at the boundary are different](https://www.tedinski.com/2018/02/06/system-boundaries.html).</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L57" href="#L57">57</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L58" href="#L58">58</a><span class="code-text"><a-p>### </a-p><a-tt>`xtask`</a-tt></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L59" href="#L59">59</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L60" href="#L60">60</a><span class="code-text"><a-p>This is rust-analyzer&#39;s &quot;build system&quot;.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L61" href="#L61">61</a><span class="code-text"><a-p>We use cargo to compile rust code, but there are also various other tasks, like release management or local installation.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L62" href="#L62">62</a><span class="code-text"><a-p>They are handled by Rust code in the xtask directory.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L63" href="#L63">63</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L64" href="#L64">64</a><span class="code-text"><a-p>### </a-p><a-tt>`editors/code`</a-tt></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L65" href="#L65">65</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L66" href="#L66">66</a><span class="code-text"><a-p>VS Code plugin.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L67" href="#L67">67</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L68" href="#L68">68</a><span class="code-text"><a-p>### </a-p><a-tt>`lib/`</a-tt></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L69" href="#L69">69</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L70" href="#L70">70</a><span class="code-text"><a-p>rust-analyzer independent libraries which we publish to crates.io.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L71" href="#L71">71</a><span class="code-text"><a-p>It&#39;s not heavily utilized at the moment.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L72" href="#L72">72</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L73" href="#L73">73</a><span class="code-text"><a-p>### </a-p><a-tt>`crates/parser`</a-tt></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L74" href="#L74">74</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L75" href="#L75">75</a><span class="code-text"><a-p>It is a hand-written recursive descent parser, which produces a sequence of events like &quot;start node X&quot;, &quot;finish node Y&quot;.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L76" href="#L76">76</a><span class="code-text"><a-p>It works similarly to</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L77" href="#L77">77</a><span class="code-text"><a-p>[kotlin&#39;s parser](https://github.com/JetBrains/kotlin/blob/4d951de616b20feca92f3e9cc9679b2de9e65195/compiler/frontend/src/org/jetbrains/kotlin/parsing/KotlinParsing.java),</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L78" href="#L78">78</a><span class="code-text"><a-p>which is a good source of inspiration for dealing with syntax errors and incomplete input.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L79" href="#L79">79</a><span class="code-text"><a-p>Original [libsyntax parser](https://github.com/rust-lang/rust/blob/6b99adeb11313197f409b4f7c4083c2ceca8a4fe/src/libsyntax/parse/parser.rs) is what we use for the definition of the Rust language.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L80" href="#L80">80</a><span class="code-text"><a-p>`TreeSink` and `TokenSource` traits bridge the tree-agnostic parser from `grammar` with `rowan` trees.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L81" href="#L81">81</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L82" href="#L82">82</a><span class="code-text"><a-p>**Architecture Invariant:** the parser is independent of the particular tree structure and particular representation of the tokens.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L83" href="#L83">83</a><span class="code-text"><a-p>It transforms one flat stream of events into another flat stream of events.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L84" href="#L84">84</a><span class="code-text"><a-p>Token independence allows us to parse out both text-based source code and `tt`-based macro input.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L85" href="#L85">85</a><span class="code-text"><a-p>Tree independence allows us to more easily vary the syntax tree implementation.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L86" href="#L86">86</a><span class="code-text"><a-p>It should also unlock efficient light-parsing approaches.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L87" href="#L87">87</a><span class="code-text"><a-p>For example, you can extract the set of names defined in a file (for typo correction) without building a syntax tree.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L88" href="#L88">88</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L89" href="#L89">89</a><span class="code-text"><a-p>**Architecture Invariant:** parsing never fails, the parser produces `(T, Vec&lt;Error&gt;)` rather than `Result&lt;T, Error&gt;`.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L90" href="#L90">90</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L91" href="#L91">91</a><span class="code-text"><a-p>### </a-p><a-tt>`crates/syntax`</a-tt></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L92" href="#L92">92</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L93" href="#L93">93</a><span class="code-text"><a-p>Rust syntax tree structure and parser.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L94" href="#L94">94</a><span class="code-text"><a-p>See [RFC](https://github.com/rust-lang/rfcs/pull/2256) and [./syntax.md](./syntax.md) for some design notes.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L95" href="#L95">95</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L96" href="#L96">96</a><span class="code-text"><a-p>- [rowan](https://github.com/rust-analyzer/rowan) library is used for constructing syntax trees.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L97" href="#L97">97</a><span class="code-text"><a-p>- `ast` provides a type safe API on top of the raw `rowan` tree.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L98" href="#L98">98</a><span class="code-text"><a-p>- `ungrammar` description of the grammar, which is used to generate `syntax_kinds` and `ast` modules, using `cargo test -p xtask` command.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L99" href="#L99">99</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L100" href="#L100">100</a><span class="code-text"><a-p>Tests for ra_syntax are mostly data-driven.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L101" href="#L101">101</a><span class="code-text"><a-p>`test_data/parser` contains subdirectories with a bunch of `.rs` (test vectors) and `.txt` files with corresponding syntax trees.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L102" href="#L102">102</a><span class="code-text"><a-p>During testing, we check `.rs` against `.txt`.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L103" href="#L103">103</a><span class="code-text"><a-p>If the `.txt` file is missing, it is created (this is how you update tests).</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L104" href="#L104">104</a><span class="code-text"><a-p>Additionally, running the xtask test suite with `cargo test -p xtask` will walk the grammar module and collect all `// test test_name` comments into files inside `test_data/parser/inline` directory.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L105" href="#L105">105</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L106" href="#L106">106</a><span class="code-text"><a-p>To update test data, run with `UPDATE_EXPECT` variable:</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L107" href="#L107">107</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L108" href="#L108">108</a><span class="code-text"><a-p>```</a-p><a-tl>bash</a-tl></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L109" href="#L109">109</a><span class="code-text"><a-p>env UPDATE_EXPECT=1 cargo qt</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L110" href="#L110">110</a><span class="code-text"><a-p>```</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L111" href="#L111">111</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L112" href="#L112">112</a><span class="code-text"><a-p>After adding a new inline test you need to run `cargo test -p xtask` and also update the test data as described above.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L113" href="#L113">113</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L114" href="#L114">114</a><span class="code-text"><a-p>Note [`api_walkthrough`](https://github.com/rust-lang/rust-analyzer/blob/2fb6af89eb794f775de60b82afe56b6f986c2a40/crates/ra_syntax/src/lib.rs#L190-L348)</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L115" href="#L115">115</a><span class="code-text"><a-p>in particular: it shows off various methods of working with syntax tree.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L116" href="#L116">116</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L117" href="#L117">117</a><span class="code-text"><a-p>See [#93](https://github.com/rust-lang/rust-analyzer/pull/93) for an example PR which fixes a bug in the grammar.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L118" href="#L118">118</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L119" href="#L119">119</a><span class="code-text"><a-p>**Architecture Invariant:** `syntax` crate is completely independent from the rest of rust-analyzer. It knows nothing about salsa or LSP.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L120" href="#L120">120</a><span class="code-text"><a-p>This is important because it is possible to make useful tooling using only the syntax tree.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L121" href="#L121">121</a><span class="code-text"><a-p>Without semantic information, you don&#39;t need to be able to _build_ code, which makes the tooling more robust.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L122" href="#L122">122</a><span class="code-text"><a-p>See also https://web.stanford.edu/~mlfbrown/paper.pdf.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L123" href="#L123">123</a><span class="code-text"><a-p>You can view the `syntax` crate as an entry point to rust-analyzer.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L124" href="#L124">124</a><span class="code-text"><a-p>`syntax` crate is an **API Boundary**.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L125" href="#L125">125</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L126" href="#L126">126</a><span class="code-text"><a-p>**Architecture Invariant:** syntax tree is a value type.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L127" href="#L127">127</a><span class="code-text"><a-p>The tree is fully determined by the contents of its syntax nodes, it doesn&#39;t need global context (like an interner) and doesn&#39;t store semantic info.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L128" href="#L128">128</a><span class="code-text"><a-p>Using the tree as a store for semantic info is convenient in traditional compilers, but doesn&#39;t work nicely in the IDE.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L129" href="#L129">129</a><span class="code-text"><a-p>Specifically, assists and refactors require transforming syntax trees, and that becomes awkward if you need to do something with the semantic info.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L130" href="#L130">130</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L131" href="#L131">131</a><span class="code-text"><a-p>**Architecture Invariant:** syntax tree is built for a single file.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L132" href="#L132">132</a><span class="code-text"><a-p>This is to enable parallel parsing of all files.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L133" href="#L133">133</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L134" href="#L134">134</a><span class="code-text"><a-p>**Architecture Invariant:**  Syntax trees are by design incomplete and do not enforce well-formedness.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L135" href="#L135">135</a><span class="code-text"><a-p>If an AST method returns an `Option`, it *can* be `None` at runtime, even if this is forbidden by the grammar.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L136" href="#L136">136</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L137" href="#L137">137</a><span class="code-text"><a-p>### </a-p><a-tt>`crates/base_db`</a-tt></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L138" href="#L138">138</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L139" href="#L139">139</a><span class="code-text"><a-p>We use the [salsa](https://github.com/salsa-rs/salsa) crate for incremental and on-demand computation.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L140" href="#L140">140</a><span class="code-text"><a-p>Roughly, you can think of salsa as a key-value store, but it can also compute derived values using specified functions.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L141" href="#L141">141</a><span class="code-text"><a-p>The `base_db` crate provides basic infrastructure for interacting with salsa.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L142" href="#L142">142</a><span class="code-text"><a-p>Crucially, it defines most of the &quot;input&quot; queries: facts supplied by the client of the analyzer.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L143" href="#L143">143</a><span class="code-text"><a-p>Reading the docs of the `base_db::input` module should be useful: everything else is strictly derived from those inputs.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L144" href="#L144">144</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L145" href="#L145">145</a><span class="code-text"><a-p>**Architecture Invariant:** particularities of the build system are *not* the part of the ground state.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L146" href="#L146">146</a><span class="code-text"><a-p>In particular, `base_db` knows nothing about cargo.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L147" href="#L147">147</a><span class="code-text"><a-p>For example, `cfg` flags are a part of `base_db`, but `feature`s are not.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L148" href="#L148">148</a><span class="code-text"><a-p>A `foo` feature is a Cargo-level concept, which is lowered by Cargo to `--cfg feature=foo` argument on the command line.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L149" href="#L149">149</a><span class="code-text"><a-p>The `CrateGraph` structure is used to represent the dependencies between the crates abstractly.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L150" href="#L150">150</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L151" href="#L151">151</a><span class="code-text"><a-p>**Architecture Invariant:** `base_db` doesn&#39;t know about file system and file paths.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L152" href="#L152">152</a><span class="code-text"><a-p>Files are represented with opaque `FileId`, there&#39;s no operation to get an `std::path::Path` out of the `FileId`.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L153" href="#L153">153</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L154" href="#L154">154</a><span class="code-text"><a-p>### </a-p><a-tt>`crates/hir_expand`, `crates/hir_def`, `crates/hir_ty`</a-tt></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L155" href="#L155">155</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L156" href="#L156">156</a><span class="code-text"><a-p>These crates are the *brain* of rust-analyzer.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L157" href="#L157">157</a><span class="code-text"><a-p>This is the compiler part of the IDE.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L158" href="#L158">158</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L159" href="#L159">159</a><span class="code-text"><a-p>`hir_xxx` crates have a strong [ECS](https://en.wikipedia.org/wiki/Entity_component_system) flavor, in that they work with raw ids and directly query the database.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L160" href="#L160">160</a><span class="code-text"><a-p>There&#39;s little abstraction here.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L161" href="#L161">161</a><span class="code-text"><a-p>These crates integrate deeply with salsa and chalk.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L162" href="#L162">162</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L163" href="#L163">163</a><span class="code-text"><a-p>Name resolution, macro expansion and type inference all happen here.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L164" href="#L164">164</a><span class="code-text"><a-p>These crates also define various intermediate representations of the core.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L165" href="#L165">165</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L166" href="#L166">166</a><span class="code-text"><a-p>`ItemTree` condenses a single `SyntaxTree` into a &quot;summary&quot; data structure, which is stable over modifications to function bodies.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L167" href="#L167">167</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L168" href="#L168">168</a><span class="code-text"><a-p>`DefMap` contains the module tree of a crate and stores module scopes.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L169" href="#L169">169</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L170" href="#L170">170</a><span class="code-text"><a-p>`Body` stores information about expressions.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L171" href="#L171">171</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L172" href="#L172">172</a><span class="code-text"><a-p>**Architecture Invariant:** these crates are not, and will never be, an api boundary.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L173" href="#L173">173</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L174" href="#L174">174</a><span class="code-text"><a-p>**Architecture Invariant:** these crates explicitly care about being incremental.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L175" href="#L175">175</a><span class="code-text"><a-p>The core invariant we maintain is &quot;typing inside a function&#39;s body never invalidates global derived data&quot;.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L176" href="#L176">176</a><span class="code-text"><a-p>i.e., if you change the body of `foo`, all facts about `bar` should remain intact.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L177" href="#L177">177</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L178" href="#L178">178</a><span class="code-text"><a-p>**Architecture Invariant:** hir exists only in context of particular crate instance with specific CFG flags.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L179" href="#L179">179</a><span class="code-text"><a-p>The same syntax may produce several instances of HIR if the crate participates in the crate graph more than once.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L180" href="#L180">180</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L181" href="#L181">181</a><span class="code-text"><a-p>### </a-p><a-tt>`crates/hir`</a-tt></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L182" href="#L182">182</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L183" href="#L183">183</a><span class="code-text"><a-p>The top-level `hir` crate is an **API Boundary**.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L184" href="#L184">184</a><span class="code-text"><a-p>If you think about &quot;using rust-analyzer as a library&quot;, `hir` crate is most likely the façade you&#39;ll be talking to.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L185" href="#L185">185</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L186" href="#L186">186</a><span class="code-text"><a-p>It wraps ECS-style internal API into a more OO-flavored API (with an extra `db` argument for each call).</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L187" href="#L187">187</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L188" href="#L188">188</a><span class="code-text"><a-p>**Architecture Invariant:** `hir` provides a static, fully resolved view of the code.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L189" href="#L189">189</a><span class="code-text"><a-p>While internal `hir_*` crates _compute_ things, `hir`, from the outside, looks like an inert data structure.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L190" href="#L190">190</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L191" href="#L191">191</a><span class="code-text"><a-p>`hir` also handles the delicate task of going from syntax to the corresponding `hir`.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L192" href="#L192">192</a><span class="code-text"><a-p>Remember that the mapping here is one-to-many.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L193" href="#L193">193</a><span class="code-text"><a-p>See `Semantics` type and `source_to_def` module.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L194" href="#L194">194</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L195" href="#L195">195</a><span class="code-text"><a-p>Note in particular a curious recursive structure in `source_to_def`.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L196" href="#L196">196</a><span class="code-text"><a-p>We first resolve the parent _syntax_ node to the parent _hir_ element.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L197" href="#L197">197</a><span class="code-text"><a-p>Then we ask the _hir_ parent what _syntax_ children does it have.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L198" href="#L198">198</a><span class="code-text"><a-p>Then we look for our node in the set of children.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L199" href="#L199">199</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L200" href="#L200">200</a><span class="code-text"><a-p>This is the heart of many IDE features, like goto definition, which start with figuring out the hir node at the cursor.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L201" href="#L201">201</a><span class="code-text"><a-p>This is some kind of (yet unnamed) uber-IDE pattern, as it is present in Roslyn and Kotlin as well.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L202" href="#L202">202</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L203" href="#L203">203</a><span class="code-text"><a-p>### </a-p><a-tt>`crates/ide`</a-tt></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L204" href="#L204">204</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L205" href="#L205">205</a><span class="code-text"><a-p>The `ide` crate builds on top of `hir` semantic model to provide high-level IDE features like completion or goto definition.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L206" href="#L206">206</a><span class="code-text"><a-p>It is an **API Boundary**.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L207" href="#L207">207</a><span class="code-text"><a-p>If you want to use IDE parts of rust-analyzer via LSP, custom flatbuffers-based protocol or just as a library in your text editor, this is the right API.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L208" href="#L208">208</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L209" href="#L209">209</a><span class="code-text"><a-p>**Architecture Invariant:** `ide` crate&#39;s API is build out of POD types with public fields.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L210" href="#L210">210</a><span class="code-text"><a-p>The API uses editor&#39;s terminology, it talks about offsets and string labels rather than in terms of definitions or types.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L211" href="#L211">211</a><span class="code-text"><a-p>It is effectively the view in MVC and viewmodel in [MVVM](https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93viewmodel).</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L212" href="#L212">212</a><span class="code-text"><a-p>All arguments and return types are conceptually serializable.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L213" href="#L213">213</a><span class="code-text"><a-p>In particular, syntax trees and hir types are generally absent from the API (but are used heavily in the implementation).</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L214" href="#L214">214</a><span class="code-text"><a-p>Shout outs to LSP developers for popularizing the idea that &quot;UI&quot; is a good place to draw a boundary at.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L215" href="#L215">215</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L216" href="#L216">216</a><span class="code-text"><a-p>`ide` is also the first crate which has the notion of change over time.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L217" href="#L217">217</a><span class="code-text"><a-p>`AnalysisHost` is a state to which you can transactionally `apply_change`.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L218" href="#L218">218</a><span class="code-text"><a-p>`Analysis` is an immutable snapshot of the state.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L219" href="#L219">219</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L220" href="#L220">220</a><span class="code-text"><a-p>Internally, `ide` is split across several crates. `ide_assists`, `ide_completion` and `ide_ssr` implement large isolated features.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L221" href="#L221">221</a><span class="code-text"><a-p>`ide_db` implements common IDE functionality (notably, reference search is implemented here).</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L222" href="#L222">222</a><span class="code-text"><a-p>The `ide` contains a public API/façade, as well as implementation for a plethora of smaller features.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L223" href="#L223">223</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L224" href="#L224">224</a><span class="code-text"><a-p>**Architecture Invariant:** `ide` crate strives to provide a _perfect_ API.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L225" href="#L225">225</a><span class="code-text"><a-p>Although at the moment it has only one consumer, the LSP server, LSP *does not* influence its API design.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L226" href="#L226">226</a><span class="code-text"><a-p>Instead, we keep in mind a hypothetical _ideal_ client -- an IDE tailored specifically for rust, every nook and cranny of which is packed with Rust-specific goodies.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L227" href="#L227">227</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L228" href="#L228">228</a><span class="code-text"><a-p>### </a-p><a-tt>`crates/rust-analyzer`</a-tt></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L229" href="#L229">229</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L230" href="#L230">230</a><span class="code-text"><a-p>This crate defines the `rust-analyzer` binary, so it is the **entry point**.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L231" href="#L231">231</a><span class="code-text"><a-p>It implements the language server.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L232" href="#L232">232</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L233" href="#L233">233</a><span class="code-text"><a-p>**Architecture Invariant:** `rust-analyzer` is the only crate that knows about LSP and JSON serialization.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L234" href="#L234">234</a><span class="code-text"><a-p>If you want to expose a data structure `X` from ide to LSP, don&#39;t make it serializable.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L235" href="#L235">235</a><span class="code-text"><a-p>Instead, create a serializable counterpart in `rust-analyzer` crate and manually convert between the two.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L236" href="#L236">236</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L237" href="#L237">237</a><span class="code-text"><a-p>`GlobalState` is the state of the server.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L238" href="#L238">238</a><span class="code-text"><a-p>The `main_loop` defines the server event loop which accepts requests and sends responses.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L239" href="#L239">239</a><span class="code-text"><a-p>Requests that modify the state or might block user&#39;s typing are handled on the main thread.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L240" href="#L240">240</a><span class="code-text"><a-p>All other requests are processed in background.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L241" href="#L241">241</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L242" href="#L242">242</a><span class="code-text"><a-p>**Architecture Invariant:** the server is stateless, a-la HTTP.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L243" href="#L243">243</a><span class="code-text"><a-p>Sometimes state needs to be preserved between requests.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L244" href="#L244">244</a><span class="code-text"><a-p>For example, &quot;what is the `edit` for the fifth completion item of the last completion edit?&quot;.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L245" href="#L245">245</a><span class="code-text"><a-p>For this, the second request should include enough info to re-create the context from scratch.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L246" href="#L246">246</a><span class="code-text"><a-p>This generally means including all the parameters of the original request.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L247" href="#L247">247</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L248" href="#L248">248</a><span class="code-text"><a-p>`reload` module contains the code that handles configuration and Cargo.toml changes.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L249" href="#L249">249</a><span class="code-text"><a-p>This is a tricky business.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L250" href="#L250">250</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L251" href="#L251">251</a><span class="code-text"><a-p>**Architecture Invariant:** `rust-analyzer` should be partially available even when the build is broken.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L252" href="#L252">252</a><span class="code-text"><a-p>Reloading process should not prevent IDE features from working.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L253" href="#L253">253</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L254" href="#L254">254</a><span class="code-text"><a-p>### </a-p><a-tt>`crates/toolchain`, `crates/project_model`, `crates/flycheck`</a-tt></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L255" href="#L255">255</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L256" href="#L256">256</a><span class="code-text"><a-p>These crates deal with invoking `cargo` to learn about project structure and get compiler errors for the &quot;check on save&quot; feature.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L257" href="#L257">257</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L258" href="#L258">258</a><span class="code-text"><a-p>They use `crates/path` heavily instead of `std::path`.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L259" href="#L259">259</a><span class="code-text"><a-p>A single `rust-analyzer` process can serve many projects, so it is important that server&#39;s current directory does not leak.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L260" href="#L260">260</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L261" href="#L261">261</a><span class="code-text"><a-p>### </a-p><a-tt>`crates/mbe`, `crates/tt`, `crates/proc_macro_api`, `crates/proc_macro_srv`</a-tt></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L262" href="#L262">262</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L263" href="#L263">263</a><span class="code-text"><a-p>These crates implement macros as token tree -&gt; token tree transforms.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L264" href="#L264">264</a><span class="code-text"><a-p>They are independent from the rest of the code.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L265" href="#L265">265</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L266" href="#L266">266</a><span class="code-text"><a-p>`tt` crate defined `TokenTree`, a single token or a delimited sequence of token trees.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L267" href="#L267">267</a><span class="code-text"><a-p>`mbe` crate contains tools for transforming between syntax trees and token tree.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L268" href="#L268">268</a><span class="code-text"><a-p>And it also handles the actual parsing and expansion of declarative macro (a-la &quot;Macros By Example&quot; or mbe).</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L269" href="#L269">269</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L270" href="#L270">270</a><span class="code-text"><a-p>For proc macros, the client-server model are used.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L271" href="#L271">271</a><span class="code-text"><a-p>We pass an argument `--proc-macro` to `rust-analyzer` binary to start a separate process  (`proc_macro_srv`).</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L272" href="#L272">272</a><span class="code-text"><a-p>And the client (`proc_macro_api`) provides an interface to talk to that server separately.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L273" href="#L273">273</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L274" href="#L274">274</a><span class="code-text"><a-p>And then token trees are passed from client, and the server will load the corresponding dynamic library (which built by `cargo`).</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L275" href="#L275">275</a><span class="code-text"><a-p>And due to the fact the api for getting result from proc macro are always unstable in `rustc`,</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L276" href="#L276">276</a><span class="code-text"><a-p>we maintain our own copy (and paste) of that part of code to allow us to build the whole thing in stable rust.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L277" href="#L277">277</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L278" href="#L278">278</a><span class="code-text"><a-p> **Architecture Invariant:**</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L279" href="#L279">279</a><span class="code-text"><a-p>Bad proc macros may panic or segfault accidentally. So we run it in another process and recover it from fatal error.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L280" href="#L280">280</a><span class="code-text"><a-p>And they may be non-deterministic which conflict how `salsa` works, so special attention is required.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L281" href="#L281">281</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L282" href="#L282">282</a><span class="code-text"><a-p>### </a-p><a-tt>`crates/cfg`</a-tt></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L283" href="#L283">283</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L284" href="#L284">284</a><span class="code-text"><a-p>This crate is responsible for parsing, evaluation and general definition of `cfg` attributes.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L285" href="#L285">285</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L286" href="#L286">286</a><span class="code-text"><a-p>### </a-p><a-tt>`crates/vfs`, `crates/vfs-notify`</a-tt></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L287" href="#L287">287</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L288" href="#L288">288</a><span class="code-text"><a-p>These crates implement a virtual file system.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L289" href="#L289">289</a><span class="code-text"><a-p>They provide consistent snapshots of the underlying file system and insulate messy OS paths.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L290" href="#L290">290</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L291" href="#L291">291</a><span class="code-text"><a-p>**Architecture Invariant:** vfs doesn&#39;t assume a single unified file system.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L292" href="#L292">292</a><span class="code-text"><a-p>i.e., a single rust-analyzer process can act as a remote server for two different machines, where the same `/tmp/foo.rs` path points to different files.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L293" href="#L293">293</a><span class="code-text"><a-p>For this reason, all path APIs generally take some existing path as a &quot;file system witness&quot;.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L294" href="#L294">294</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L295" href="#L295">295</a><span class="code-text"><a-p>### </a-p><a-tt>`crates/stdx`</a-tt></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L296" href="#L296">296</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L297" href="#L297">297</a><span class="code-text"><a-p>This crate contains various non-rust-analyzer specific utils, which could have been in std, as well</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L298" href="#L298">298</a><span class="code-text"><a-p>as copies of unstable std items we would like to make use of already, like `std::str::split_once`.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L299" href="#L299">299</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L300" href="#L300">300</a><span class="code-text"><a-p>### </a-p><a-tt>`crates/profile`</a-tt></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L301" href="#L301">301</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L302" href="#L302">302</a><span class="code-text"><a-p>This crate contains utilities for CPU and memory profiling.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L303" href="#L303">303</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L304" href="#L304">304</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L305" href="#L305">305</a><span class="code-text"><a-p>## </a-p><a-tt>Cross-Cutting Concerns</a-tt></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L306" href="#L306">306</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L307" href="#L307">307</a><span class="code-text"><a-p>This sections talks about the things which are everywhere and nowhere in particular.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L308" href="#L308">308</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L309" href="#L309">309</a><span class="code-text"><a-p>### </a-p><a-tt>Stability Guarantees</a-tt></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L310" href="#L310">310</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L311" href="#L311">311</a><span class="code-text"><a-p>One of the reasons rust-analyzer moves relatively fast is that we don&#39;t introduce new stability guarantees.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L312" href="#L312">312</a><span class="code-text"><a-p>Instead, as much as possible we leverage existing ones.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L313" href="#L313">313</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L314" href="#L314">314</a><span class="code-text"><a-p>Examples:</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L315" href="#L315">315</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L316" href="#L316">316</a><span class="code-text"><a-p>* The `ide` API of rust-analyzer are explicitly unstable, but the LSP interface is stable, and here we just implement a stable API managed by someone else.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L317" href="#L317">317</a><span class="code-text"><a-p>* Rust language and Cargo are stable, and they are the primary inputs to rust-analyzer.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L318" href="#L318">318</a><span class="code-text"><a-p>* The `rowan` library is published to crates.io, but it is deliberately kept under `1.0` and always makes semver-incompatible upgrades</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L319" href="#L319">319</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L320" href="#L320">320</a><span class="code-text"><a-p>Another important example is that rust-analyzer isn&#39;t run on CI, so, unlike `rustc` and `clippy`, it is actually ok for us to change runtime behavior.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L321" href="#L321">321</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L322" href="#L322">322</a><span class="code-text"><a-p>At some point we might consider opening up APIs or allowing crates.io libraries to include rust-analyzer specific annotations, but that&#39;s going to be a big commitment on our side.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L323" href="#L323">323</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L324" href="#L324">324</a><span class="code-text"><a-p>Exceptions:</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L325" href="#L325">325</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L326" href="#L326">326</a><span class="code-text"><a-p>* `rust-project.json` is a de-facto stable format for non-cargo build systems.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L327" href="#L327">327</a><span class="code-text"><a-p>  It is probably ok enough, but was definitely stabilized implicitly.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L328" href="#L328">328</a><span class="code-text"><a-p>  Lesson for the future: when designing API which could become a stability boundary, don&#39;t wait for the first users until you stabilize it.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L329" href="#L329">329</a><span class="code-text"><a-p>  By the time you have first users, it is already de-facto stable.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L330" href="#L330">330</a><span class="code-text"><a-p>  And the users will first use the thing, and *then* inform you that now you have users.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L331" href="#L331">331</a><span class="code-text"><a-p>  The sad thing is that stuff should be stable before someone uses it for the first time, or it should contain explicit opt-in.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L332" href="#L332">332</a><span class="code-text"><a-p>* We ship some LSP extensions, and we try to keep those somewhat stable.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L333" href="#L333">333</a><span class="code-text"><a-p>  Here, we need to work with a finite set of editor maintainers, so not providing rock-solid guarantees works.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L334" href="#L334">334</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L335" href="#L335">335</a><span class="code-text"><a-p>### </a-p><a-tt>Code generation</a-tt></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L336" href="#L336">336</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L337" href="#L337">337</a><span class="code-text"><a-p>Some components in this repository are generated through automatic processes.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L338" href="#L338">338</a><span class="code-text"><a-p>Generated code is updated automatically on `cargo test`.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L339" href="#L339">339</a><span class="code-text"><a-p>Generated code is generally committed to the git repository.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L340" href="#L340">340</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L341" href="#L341">341</a><span class="code-text"><a-p>In particular, we generate:</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L342" href="#L342">342</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L343" href="#L343">343</a><span class="code-text"><a-p>* API for working with syntax trees (`syntax::ast`, the [`ungrammar`](https://github.com/rust-analyzer/ungrammar) crate).</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L344" href="#L344">344</a><span class="code-text"><a-p>* Various sections of the manual:</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L345" href="#L345">345</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L346" href="#L346">346</a><span class="code-text"><a-p>    * features</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L347" href="#L347">347</a><span class="code-text"><a-p>    * assists</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L348" href="#L348">348</a><span class="code-text"><a-p>    * config</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L349" href="#L349">349</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L350" href="#L350">350</a><span class="code-text"><a-p>* Documentation tests for assists</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L351" href="#L351">351</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L352" href="#L352">352</a><span class="code-text"><a-p>See the `sourcegen` crate for details.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L353" href="#L353">353</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L354" href="#L354">354</a><span class="code-text"><a-p>**Architecture Invariant:** we avoid bootstrapping.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L355" href="#L355">355</a><span class="code-text"><a-p>For codegen we need to parse Rust code.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L356" href="#L356">356</a><span class="code-text"><a-p>Using rust-analyzer for that would work and would be fun, but it would also complicate the build process a lot.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L357" href="#L357">357</a><span class="code-text"><a-p>For that reason, we use syn and manual string parsing.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L358" href="#L358">358</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L359" href="#L359">359</a><span class="code-text"><a-p>### </a-p><a-tt>Cancellation</a-tt></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L360" href="#L360">360</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L361" href="#L361">361</a><span class="code-text"><a-p>Let&#39;s say that the IDE is in the process of computing syntax highlighting, when the user types `foo`.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L362" href="#L362">362</a><span class="code-text"><a-p>What should happen?</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L363" href="#L363">363</a><span class="code-text"><a-p>`rust-analyzer`s answer is that the highlighting process should be cancelled -- its results are now stale, and it also blocks modification of the inputs.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L364" href="#L364">364</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L365" href="#L365">365</a><span class="code-text"><a-p>The salsa database maintains a global revision counter.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L366" href="#L366">366</a><span class="code-text"><a-p>When applying a change, salsa bumps this counter and waits until all other threads using salsa finish.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L367" href="#L367">367</a><span class="code-text"><a-p>If a thread does salsa-based computation and notices that the counter is incremented, it panics with a special value (see `Canceled::throw`).</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L368" href="#L368">368</a><span class="code-text"><a-p>That is, rust-analyzer requires unwinding.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L369" href="#L369">369</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L370" href="#L370">370</a><span class="code-text"><a-p>`ide` is the boundary where the panic is caught and transformed into a `Result&lt;T, Cancelled&gt;`.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L371" href="#L371">371</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L372" href="#L372">372</a><span class="code-text"><a-p>### </a-p><a-tt>Testing</a-tt></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L373" href="#L373">373</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L374" href="#L374">374</a><span class="code-text"><a-p>Rust Analyzer has three interesting [system boundaries](https://www.tedinski.com/2018/04/10/making-tests-a-positive-influence-on-design.html) to concentrate tests on.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L375" href="#L375">375</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L376" href="#L376">376</a><span class="code-text"><a-p>The outermost boundary is the `rust-analyzer` crate, which defines an LSP interface in terms of stdio.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L377" href="#L377">377</a><span class="code-text"><a-p>We do integration testing of this component, by feeding it with a stream of LSP requests and checking responses.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L378" href="#L378">378</a><span class="code-text"><a-p>These tests are known as &quot;heavy&quot;, because they interact with Cargo and read real files from disk.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L379" href="#L379">379</a><span class="code-text"><a-p>For this reason, we try to avoid writing too many tests on this boundary: in a statically typed language, it&#39;s hard to make an error in the protocol itself if messages are themselves typed.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L380" href="#L380">380</a><span class="code-text"><a-p>Heavy tests are only run when `RUN_SLOW_TESTS` env var is set.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L381" href="#L381">381</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L382" href="#L382">382</a><span class="code-text"><a-p>The middle, and most important, boundary is `ide`.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L383" href="#L383">383</a><span class="code-text"><a-p>Unlike `rust-analyzer`, which exposes API, `ide` uses Rust API and is intended for use by various tools.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L384" href="#L384">384</a><span class="code-text"><a-p>A typical test creates an `AnalysisHost`, calls some `Analysis` functions and compares the results against expectation.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L385" href="#L385">385</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L386" href="#L386">386</a><span class="code-text"><a-p>The innermost and most elaborate boundary is `hir`.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L387" href="#L387">387</a><span class="code-text"><a-p>It has a much richer vocabulary of types than `ide`, but the basic testing setup is the same: we create a database, run some queries, assert result.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L388" href="#L388">388</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L389" href="#L389">389</a><span class="code-text"><a-p>For comparisons, we use the `expect` crate for snapshot testing.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L390" href="#L390">390</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L391" href="#L391">391</a><span class="code-text"><a-p>To test various analysis corner cases and avoid forgetting about old tests, we use so-called marks.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L392" href="#L392">392</a><span class="code-text"><a-p>See the `marks` module in the `test_utils` crate for more.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L393" href="#L393">393</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L394" href="#L394">394</a><span class="code-text"><a-p>**Architecture Invariant:** rust-analyzer tests do not use libcore or libstd.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L395" href="#L395">395</a><span class="code-text"><a-p>All required library code must be a part of the tests.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L396" href="#L396">396</a><span class="code-text"><a-p>This ensures fast test execution.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L397" href="#L397">397</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L398" href="#L398">398</a><span class="code-text"><a-p>**Architecture Invariant:** tests are data driven and do not test the API.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L399" href="#L399">399</a><span class="code-text"><a-p>Tests which directly call various API functions are a liability, because they make refactoring the API significantly more complicated.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L400" href="#L400">400</a><span class="code-text"><a-p>So most of the tests look like this:</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L401" href="#L401">401</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L402" href="#L402">402</a><span class="code-text"><a-p>```</a-p><a-tl>rust</a-tl></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L403" href="#L403">403</a><span class="code-text"><a-p>#[track_caller]</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L404" href="#L404">404</a><span class="code-text"><a-p>fn </a-p><a-f>check</a-f><a-p>(</a-p><a-v>input</a-v><a-p>: </a-p><a-o>&amp;</a-o><a-t>str</a-t><a-p>, </a-p><a-v>expect</a-v><a-p>: expect_test::</a-p><a-t>Expect</a-t><a-p>) {</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L405" href="#L405">405</a><span class="code-text"><a-p>    </a-p><a-c>// The single place that actually exercises a particular API</a-c></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L406" href="#L406">406</a><span class="code-text"><a-p>}</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L407" href="#L407">407</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L408" href="#L408">408</a><span class="code-text"><a-p>#[test]</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L409" href="#L409">409</a><span class="code-text"><a-p>fn </a-p><a-f>foo</a-f><a-p>() {</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L410" href="#L410">410</a><span class="code-text"><a-p>    </a-p><a-f>check</a-f><a-p>(</a-p><a-s>&quot;foo&quot;</a-s><a-p>, </a-p><a-m>expect!</a-m><a-p>[[</a-p><a-s>&quot;bar&quot;</a-s><a-p>]]);</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L411" href="#L411">411</a><span class="code-text"><a-p>}</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L412" href="#L412">412</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L413" href="#L413">413</a><span class="code-text"><a-p>#[test]</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L414" href="#L414">414</a><span class="code-text"><a-p>fn </a-p><a-f>spam</a-f><a-p>() {</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L415" href="#L415">415</a><span class="code-text"><a-p>    </a-p><a-f>check</a-f><a-p>(</a-p><a-s>&quot;spam&quot;</a-s><a-p>, </a-p><a-m>expect!</a-m><a-p>[[</a-p><a-s>&quot;eggs&quot;</a-s><a-p>]]);</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L416" href="#L416">416</a><span class="code-text"><a-p>}</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L417" href="#L417">417</a><span class="code-text"><a-p>// ...and a hundred more tests that don&#39;t care about the specific API at all.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L418" href="#L418">418</a><span class="code-text"><a-p>```</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L419" href="#L419">419</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L420" href="#L420">420</a><span class="code-text"><a-p>To specify input data, we use a single string literal in a special format, which can describe a set of rust files.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L421" href="#L421">421</a><span class="code-text"><a-p>See the `Fixture` its module for fixture examples and documentation.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L422" href="#L422">422</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L423" href="#L423">423</a><span class="code-text"><a-p>**Architecture Invariant:** all code invariants are tested by `#[test]` tests.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L424" href="#L424">424</a><span class="code-text"><a-p>There&#39;s no additional checks in CI, formatting and tidy tests are run with `cargo test`.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L425" href="#L425">425</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L426" href="#L426">426</a><span class="code-text"><a-p>**Architecture Invariant:** tests do not depend on any kind of external resources, they are perfectly reproducible.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L427" href="#L427">427</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L428" href="#L428">428</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L429" href="#L429">429</a><span class="code-text"><a-p>### </a-p><a-tt>Performance Testing</a-tt></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L430" href="#L430">430</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L431" href="#L431">431</a><span class="code-text"><a-p>TBA, take a look at the `metrics` xtask and `#[test] fn benchmark_xxx()` functions.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L432" href="#L432">432</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L433" href="#L433">433</a><span class="code-text"><a-p>### </a-p><a-tt>Error Handling</a-tt></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L434" href="#L434">434</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L435" href="#L435">435</a><span class="code-text"><a-p>**Architecture Invariant:** core parts of rust-analyzer (`ide`/`hir`) don&#39;t interact with the outside world and thus can&#39;t fail.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L436" href="#L436">436</a><span class="code-text"><a-p>Only parts touching LSP are allowed to do IO.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L437" href="#L437">437</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L438" href="#L438">438</a><span class="code-text"><a-p>Internals of rust-analyzer need to deal with broken code, but this is not an error condition.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L439" href="#L439">439</a><span class="code-text"><a-p>rust-analyzer is robust: various analysis compute `(T, Vec&lt;Error&gt;)` rather than `Result&lt;T, Error&gt;`.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L440" href="#L440">440</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L441" href="#L441">441</a><span class="code-text"><a-p>rust-analyzer is a complex long-running process.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L442" href="#L442">442</a><span class="code-text"><a-p>It will always have bugs and panics.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L443" href="#L443">443</a><span class="code-text"><a-p>But a panic in an isolated feature should not bring down the whole process.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L444" href="#L444">444</a><span class="code-text"><a-p>Each LSP-request is protected by a `catch_unwind`.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L445" href="#L445">445</a><span class="code-text"><a-p>We use `always` and `never` macros instead of `assert` to gracefully recover from impossible conditions.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L446" href="#L446">446</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L447" href="#L447">447</a><span class="code-text"><a-p>### </a-p><a-tt>Observability</a-tt></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L448" href="#L448">448</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L449" href="#L449">449</a><span class="code-text"><a-p>rust-analyzer is a long-running process, so it is important to understand what&#39;s going on inside.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L450" href="#L450">450</a><span class="code-text"><a-p>We have several instruments for that.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L451" href="#L451">451</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L452" href="#L452">452</a><span class="code-text"><a-p>The event loop that runs rust-analyzer is very explicit.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L453" href="#L453">453</a><span class="code-text"><a-p>Rather than spawning futures or scheduling callbacks (open), the event loop accepts an `enum` of possible events (closed).</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L454" href="#L454">454</a><span class="code-text"><a-p>It&#39;s easy to see all the things that trigger rust-analyzer processing, together with their performance</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L455" href="#L455">455</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L456" href="#L456">456</a><span class="code-text"><a-p>rust-analyzer includes a simple hierarchical profiler (`hprof`).</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L457" href="#L457">457</a><span class="code-text"><a-p>It is enabled with `RA_PROFILE=&#39;*&gt;50&#39;` env var (log all (`*`) actions which take more than `50` ms) and produces output like:</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L458" href="#L458">458</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L459" href="#L459">459</a><span class="code-text"><a-p>```</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L460" href="#L460">460</a><span class="code-text"><a-p>85ms - handle_completion</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L461" href="#L461">461</a><span class="code-text"><a-p>    68ms - import_on_the_fly</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L462" href="#L462">462</a><span class="code-text"><a-p>        67ms - import_assets::search_for_relative_paths</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L463" href="#L463">463</a><span class="code-text"><a-p>             0ms - crate_def_map:wait (804 calls)</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L464" href="#L464">464</a><span class="code-text"><a-p>             0ms - find_path (16 calls)</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L465" href="#L465">465</a><span class="code-text"><a-p>             2ms - find_similar_imports (1 calls)</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L466" href="#L466">466</a><span class="code-text"><a-p>             0ms - generic_params_query (334 calls)</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L467" href="#L467">467</a><span class="code-text"><a-p>            59ms - trait_solve_query (186 calls)</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L468" href="#L468">468</a><span class="code-text"><a-p>         0ms - Semantics::analyze_impl (1 calls)</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L469" href="#L469">469</a><span class="code-text"><a-p>         1ms - render_resolution (8 calls)</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L470" href="#L470">470</a><span class="code-text"><a-p>     0ms - Semantics::analyze_impl (5 calls)</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L471" href="#L471">471</a><span class="code-text"><a-p>```</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L472" href="#L472">472</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L473" href="#L473">473</a><span class="code-text"><a-p>This is cheap enough to enable in production.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L474" href="#L474">474</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L475" href="#L475">475</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L476" href="#L476">476</a><span class="code-text"><a-p>Similarly, we save live object counting (`RA_COUNT=1`).</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L477" href="#L477">477</a><span class="code-text"><a-p>It is not cheap enough to enable in prod, and this is a bug which should be fixed.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L478" href="#L478">478</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L479" href="#L479">479</a><span class="code-text"><a-p>### </a-p><a-tt>Configurability</a-tt></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L480" href="#L480">480</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L481" href="#L481">481</a><span class="code-text"><a-p>rust-analyzer strives to be as configurable as possible while offering reasonable defaults where no configuration exists yet.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L482" href="#L482">482</a><span class="code-text"><a-p>There will always be features that some people find more annoying than helpful, so giving the users the ability to tweak or disable these is a big part of offering a good user experience.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L483" href="#L483">483</a><span class="code-text"><a-p>Mind the code--architecture gap: at the moment, we are using fewer feature flags than we really should.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L484" href="#L484">484</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L485" href="#L485">485</a><span class="code-text"><a-p>### </a-p><a-tt>Serialization</a-tt></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L486" href="#L486">486</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L487" href="#L487">487</a><span class="code-text">In Rust, it is easy (often too easy) to add serialization to any type by adding `#[derive(Serialize)]`.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L488" href="#L488">488</a><span class="code-text">This easiness is misleading -- serializable types impose significant backwards compatability constraints.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L489" href="#L489">489</a><span class="code-text">If a type is serializable, then it is a part of some IPC boundary.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L490" href="#L490">490</a><span class="code-text">You often don&#39;t control the other side of this boundary, so changing serializable types is hard.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L491" href="#L491">491</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L492" href="#L492">492</a><span class="code-text">For this reason, the types in `ide`, `base_db` and below are not serializable by design.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L493" href="#L493">493</a><span class="code-text">If such types need to cross an IPC boundary, then the client of rust-analyzer needs to provide custom, client-specific serialization format.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L494" href="#L494">494</a><span class="code-text">This isolates backwards compatibility and migration concerns to a specific client.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L495" href="#L495">495</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L496" href="#L496">496</a><span class="code-text">For example, `rust-project.json` is it&#39;s own format -- it doesn&#39;t include `CrateGraph` as is.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L497" href="#L497">497</a><span class="code-text">Instead, it creates a `CrateGraph` by calling appropriate constructing functions.</span></span></pre></div></div>
</main>
</body>
</html>
