aboutsummaryrefslogtreecommitdiff
path: root/docs/dev/architecture.md
diff options
context:
space:
mode:
Diffstat (limited to 'docs/dev/architecture.md')
-rw-r--r--docs/dev/architecture.md503
1 files changed, 389 insertions, 114 deletions
diff --git a/docs/dev/architecture.md b/docs/dev/architecture.md
index b5831f47c..ead12616e 100644
--- a/docs/dev/architecture.md
+++ b/docs/dev/architecture.md
@@ -1,174 +1,449 @@
1# Architecture 1# Architecture
2 2
3This document describes the high-level architecture of rust-analyzer. 3This document describes the high-level architecture of rust-analyzer.
4If you want to familiarize yourself with the code base, you are just 4If you want to familiarize yourself with the code base, you are just in the right place!
5in the right place!
6 5
7See also the [guide](./guide.md), which walks through a particular snapshot of 6See also the [guide](./guide.md), which walks through a particular snapshot of rust-analyzer code base.
8rust-analyzer code base.
9 7
10Yet another resource is this playlist with videos about various parts of the 8Yet another resource is this playlist with videos about various parts of the analyzer:
11analyzer:
12 9
13https://www.youtube.com/playlist?list=PL85XCvVPmGQho7MZkdW-wtPtuJcFpzycE 10https://www.youtube.com/playlist?list=PL85XCvVPmGQho7MZkdW-wtPtuJcFpzycE
14 11
15Note that the guide and videos are pretty dated, this document should be in 12Note that the guide and videos are pretty dated, this document should be, in general, fresher.
16generally fresher.
17 13
18## The Big Picture 14See also these implementation-related blog posts:
19 15
20![](https://user-images.githubusercontent.com/1711539/50114578-e8a34280-0255-11e9-902c-7cfc70747966.png) 16* https://rust-analyzer.github.io/blog/2019/11/13/find-usages.html
17* https://rust-analyzer.github.io/blog/2020/07/20/three-architectures-for-responsive-ide.html
18* https://rust-analyzer.github.io/blog/2020/09/16/challeging-LR-parsing.html
19* https://rust-analyzer.github.io/blog/2020/09/28/how-to-make-a-light-bulb.html
20* https://rust-analyzer.github.io/blog/2020/10/24/introducing-ungrammar.html
21 21
22On the highest level, rust-analyzer is a thing which accepts input source code 22## Bird's Eye View
23from the client and produces a structured semantic model of the code.
24 23
25More specifically, input data consists of a set of test files (`(PathBuf, 24![](https://user-images.githubusercontent.com/4789492/107129398-0ab70f00-687a-11eb-9bfc-d4eb023aec06.png)
26String)` pairs) and information about project structure, captured in the so
27called `CrateGraph`. The crate graph specifies which files are crate roots,
28which cfg flags are specified for each crate and what dependencies exist between
29the crates. The analyzer keeps all this input data in memory and never does any
30IO. Because the input data are source code, which typically measures in tens of
31megabytes at most, keeping everything in memory is OK.
32 25
33A "structured semantic model" is basically an object-oriented representation of 26On 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.
34modules, functions and types which appear in the source code. This representation
35is fully "resolved": all expressions have types, all references are bound to
36declarations, etc.
37 27
38The client can submit a small delta of input data (typically, a change to a 28More specifically, input data consists of a set of test files (`(PathBuf, String)` pairs) and information about project structure, captured in the so called `CrateGraph`.
39single file) and get a fresh code model which accounts for changes. 29The crate graph specifies which files are crate roots, which cfg flags are specified for each crate and what dependencies exist between the crates.
30This is the input (ground) state.
31The analyzer keeps all this input data in memory and never does any IO.
32Because the input data is source code, which typically measures in tens of megabytes at most, keeping everything in memory is OK.
40 33
41The underlying engine makes sure that model is computed lazily (on-demand) and 34A "structured semantic model" is basically an object-oriented representation of modules, functions and types which appear in the source code.
42can be quickly updated for small modifications. 35This representation is fully "resolved": all expressions have types, all references are bound to declarations, etc.
36This is derived state.
43 37
38The 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.
44 39
45## Code generation 40The underlying engine makes sure that model is computed lazily (on-demand) and can be quickly updated for small modifications.
46 41
47Some of the components of this repository are generated through automatic 42## Entry Points
48processes. `cargo xtask codegen` runs all generation tasks. Generated code is
49committed to the git repository.
50 43
51In particular, `cargo xtask codegen` generates: 44`crates/rust-analyzer/src/bin/main.rs` contains the main function which spawns LSP.
45This is *the* entry point, but it front-loads a lot of complexity, so its fine to just skim through it.
52 46
531. [`syntax_kind/generated`](https://github.com/rust-analyzer/rust-analyzer/blob/a0be39296d2925972cacd9fbf8b5fb258fad6947/crates/ra_parser/src/syntax_kind/generated.rs) 47`crates/rust-analyzer/src/handlers.rs` implements all LSP requests and is a great place to start if you are already familiar with LSP.
54 -- the set of terminals and non-terminals of rust grammar.
55 48
562. [`ast/generated`](https://github.com/rust-analyzer/rust-analyzer/blob/a0be39296d2925972cacd9fbf8b5fb258fad6947/crates/ra_syntax/src/ast/generated.rs) 49`Analysis` and `AnalysisHost` types define the main API.
57 -- AST data structure.
58 50
593. [`doc_tests/generated`](https://github.com/rust-analyzer/rust-analyzer/blob/a0be39296d2925972cacd9fbf8b5fb258fad6947/crates/assists/src/doc_tests/generated.rs), 51## Code Map
60 [`test_data/parser/inline`](https://github.com/rust-analyzer/rust-analyzer/tree/a0be39296d2925972cacd9fbf8b5fb258fad6947/crates/ra_syntax/test_data/parser/inline)
61 -- tests for assists and the parser.
62 52
63The source for 1 and 2 is in [`ast_src.rs`](https://github.com/rust-analyzer/rust-analyzer/blob/a0be39296d2925972cacd9fbf8b5fb258fad6947/xtask/src/ast_src.rs). 53This section talks briefly about various important directories and data structures.
54Pay attention to the **Architecture Invariant** sections.
55They often talk about things which are deliberately absent in the source code.
64 56
65## Code Walk-Through 57Note also which crates are **API Boundaries**.
58Remember, [rules at the boundary are different](https://www.tedinski.com/2018/02/06/system-boundaries.html).
66 59
67### `crates/ra_syntax`, `crates/parser` 60### `xtask`
68 61
69Rust syntax tree structure and parser. See 62This is rust-analyzer's "build system".
70[RFC](https://github.com/rust-lang/rfcs/pull/2256) and [./syntax.md](./syntax.md) for some design notes. 63We use cargo to compile rust code, but there are also various other tasks, like release management or local installation.
64They are handled by Rust code in the xtask directory.
65
66### `editors/code`
67
68VS Code plugin.
69
70### `libs/`
71
72rust-analyzer independent libraries which we publish to crates.io.
73It's not heavily utilized at the moment.
74
75### `crates/parser`
76
77It is a hand-written recursive descent parser, which produces a sequence of events like "start node X", "finish node Y".
78It works similarly to
79[kotlin's parser](https://github.com/JetBrains/kotlin/blob/4d951de616b20feca92f3e9cc9679b2de9e65195/compiler/frontend/src/org/jetbrains/kotlin/parsing/KotlinParsing.java),
80which is a good source of inspiration for dealing with syntax errors and incomplete input.
81Original [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.
82`TreeSink` and `TokenSource` traits bridge the tree-agnostic parser from `grammar` with `rowan` trees.
83
84**Architecture Invariant:** the parser is independent of the particular tree structure and particular representation of the tokens.
85It transforms one flat stream of events into another flat stream of events.
86Token independence allows us to parse out both text-based source code and `tt`-based macro input.
87Tree independence allows us to more easily vary the syntax tree implementation.
88It should also unlock efficient light-parsing approaches.
89For example, you can extract the set of names defined in a file (for typo correction) without building a syntax tree.
90
91**Architecture Invariant:** parsing never fails, the parser produces `(T, Vec<Error>)` rather than `Result<T, Error>`.
92
93### `crates/syntax`
94
95Rust syntax tree structure and parser.
96See [RFC](https://github.com/rust-lang/rfcs/pull/2256) and [./syntax.md](./syntax.md) for some design notes.
71 97
72- [rowan](https://github.com/rust-analyzer/rowan) library is used for constructing syntax trees. 98- [rowan](https://github.com/rust-analyzer/rowan) library is used for constructing syntax trees.
73- `grammar` module is the actual parser. It is a hand-written recursive descent parser, which
74 produces a sequence of events like "start node X", "finish node Y". It works similarly to [kotlin's parser](https://github.com/JetBrains/kotlin/blob/4d951de616b20feca92f3e9cc9679b2de9e65195/compiler/frontend/src/org/jetbrains/kotlin/parsing/KotlinParsing.java),
75 which is a good source of inspiration for dealing with syntax errors and incomplete input. Original [libsyntax parser](https://github.com/rust-lang/rust/blob/6b99adeb11313197f409b4f7c4083c2ceca8a4fe/src/libsyntax/parse/parser.rs)
76 is what we use for the definition of the Rust language.
77- `TreeSink` and `TokenSource` traits bridge the tree-agnostic parser from `grammar` with `rowan` trees.
78- `ast` provides a type safe API on top of the raw `rowan` tree. 99- `ast` provides a type safe API on top of the raw `rowan` tree.
79- `ast_src` description of the grammar, which is used to generate `syntax_kinds` 100- `ungrammar` description of the grammar, which is used to generate `syntax_kinds` and `ast` modules, using `cargo xtask codegen` command.
80 and `ast` modules, using `cargo xtask codegen` command. 101
102Tests for ra_syntax are mostly data-driven.
103`test_data/parser` contains subdirectories with a bunch of `.rs` (test vectors) and `.txt` files with corresponding syntax trees.
104During testing, we check `.rs` against `.txt`.
105If the `.txt` file is missing, it is created (this is how you update tests).
106Additionally, running `cargo xtask codegen` will walk the grammar module and collect all `// test test_name` comments into files inside `test_data/parser/inline` directory.
107
108To update test data, run with `UPDATE_EXPECT` variable:
81 109
82Tests for ra_syntax are mostly data-driven: `test_data/parser` contains subdirectories with a bunch of `.rs` 110```bash
83(test vectors) and `.txt` files with corresponding syntax trees. During testing, we check 111env UPDATE_EXPECT=1 cargo qt
84`.rs` against `.txt`. If the `.txt` file is missing, it is created (this is how you update 112```
85tests). Additionally, running `cargo xtask codegen` will walk the grammar module and collect
86all `// test test_name` comments into files inside `test_data/parser/inline` directory.
87 113
88Note 114After adding a new inline test you need to run `cargo xtest codegen` and also update the test data as described above.
89[`api_walkthrough`](https://github.com/rust-analyzer/rust-analyzer/blob/2fb6af89eb794f775de60b82afe56b6f986c2a40/crates/ra_syntax/src/lib.rs#L190-L348) 115
116Note [`api_walkthrough`](https://github.com/rust-analyzer/rust-analyzer/blob/2fb6af89eb794f775de60b82afe56b6f986c2a40/crates/ra_syntax/src/lib.rs#L190-L348)
90in particular: it shows off various methods of working with syntax tree. 117in particular: it shows off various methods of working with syntax tree.
91 118
92See [#93](https://github.com/rust-analyzer/rust-analyzer/pull/93) for an example PR which 119See [#93](https://github.com/rust-analyzer/rust-analyzer/pull/93) for an example PR which fixes a bug in the grammar.
93fixes a bug in the grammar. 120
121**Architecture Invariant:** `syntax` crate is completely independent from the rest of rust-analyzer. It knows nothing about salsa or LSP.
122This is important because it is possible to make useful tooling using only the syntax tree.
123Without semantic information, you don't need to be able to _build_ code, which makes the tooling more robust.
124See also https://web.stanford.edu/~mlfbrown/paper.pdf.
125You can view the `syntax` crate as an entry point to rust-analyzer.
126`syntax` crate is an **API Boundary**.
127
128**Architecture Invariant:** syntax tree is a value type.
129The 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.
130Using the tree as a store for semantic info is convenient in traditional compilers, but doesn't work nicely in the IDE.
131Specifically, assists and refactors require transforming syntax trees, and that becomes awkward if you need to do something with the semantic info.
132
133**Architecture Invariant:** syntax tree is built for a single file.
134This is to enable parallel parsing of all files.
135
136**Architecture Invariant:** Syntax trees are by design incomplete and do not enforce well-formedness.
137If an AST method returns an `Option`, it *can* be `None` at runtime, even if this is forbidden by the grammar.
94 138
95### `crates/base_db` 139### `crates/base_db`
96 140
97We use the [salsa](https://github.com/salsa-rs/salsa) crate for incremental and 141We use the [salsa](https://github.com/salsa-rs/salsa) crate for incremental and on-demand computation.
98on-demand computation. Roughly, you can think of salsa as a key-value store, but 142Roughly, you can think of salsa as a key-value store, but it can also compute derived values using specified functions. The `base_db` crate provides basic infrastructure for interacting with salsa.
99it also can compute derived values using specified functions. The `base_db` crate 143Crucially, it defines most of the "input" queries: facts supplied by the client of the analyzer.
100provides basic infrastructure for interacting with salsa. Crucially, it 144Reading the docs of the `base_db::input` module should be useful: everything else is strictly derived from those inputs.
101defines most of the "input" queries: facts supplied by the client of the 145
102analyzer. Reading the docs of the `base_db::input` module should be useful: 146**Architecture Invariant:** particularities of the build system are *not* the part of the ground state.
103everything else is strictly derived from those inputs. 147In particular, `base_db` knows nothing about cargo.
148The `CrateGraph` structure is used to represent the dependencies between the crates abstractly.
149
150**Architecture Invariant:** `base_db` doesn't know about file system and file paths.
151Files are represented with opaque `FileId`, there's no operation to get an `std::path::Path` out of the `FileId`.
152
153### `crates/hir_expand`, `crates/hir_def`, `crates/hir_ty`
154
155These crates are the *brain* of rust-analyzer.
156This is the compiler part of the IDE.
157
158`hir_xxx` crates have a strong ECS flavor, in that they work with raw ids and directly query the database.
159There's little abstraction here.
160These crates integrate deeply with salsa and chalk.
161
162Name resolution, macro expansion and type inference all happen here.
163These crates also define various intermediate representations of the core.
104 164
105### `crates/hir*` crates 165`ItemTree` condenses a single `SyntaxTree` into a "summary" data structure, which is stable over modifications to function bodies.
106 166
107HIR provides high-level "object oriented" access to Rust code. 167`DefMap` contains the module tree of a crate and stores module scopes.
108 168
109The principal difference between HIR and syntax trees is that HIR is bound to a 169`Body` stores information about expressions.
110particular crate instance. That is, it has cfg flags and features applied. So,
111the relation between syntax and HIR is many-to-one. The `source_binder` module
112is responsible for guessing a HIR for a particular source position.
113 170
114Underneath, HIR works on top of salsa, using a `HirDatabase` trait. 171**Architecture Invariant:** these crates are not, and will never be, an api boundary.
115 172
116`hir_xxx` crates have a strong ECS flavor, in that they work with raw ids and 173**Architecture Invariant:** these crates explicitly care about being incremental.
117directly query the database. 174The core invariant we maintain is "typing inside a function's body never invalidates global derived data".
175i.e., if you change the body of `foo`, all facts about `bar` should remain intact.
118 176
119The top-level `hir` façade crate wraps ids into a more OO-flavored API. 177**Architecture Invariant:** hir exists only in context of particular crate instance with specific CFG flags.
178The same syntax may produce several instances of HIR if the crate participates in the crate graph more than once.
179
180### `crates/hir`
181
182The top-level `hir` crate is an **API Boundary**.
183If you think about "using rust-analyzer as a library", `hir` crate is most likely the façade you'll be talking to.
184
185It wraps ECS-style internal API into a more OO-flavored API (with an extra `db` argument for each call).
186
187**Architecture Invariant:** `hir` provides a static, fully resolved view of the code.
188While internal `hir_*` crates _compute_ things, `hir`, from the outside, looks like an inert data structure.
189
190`hir` also handles the delicate task of going from syntax to the corresponding `hir`.
191Remember that the mapping here is one-to-many.
192See `Semantics` type and `source_to_def` module.
193
194Note in particular a curious recursive structure in `source_to_def`.
195We first resolve the parent _syntax_ node to the parent _hir_ element.
196Then we ask the _hir_ parent what _syntax_ children does it have.
197Then we look for our node in the set of children.
198
199This is the heart of many IDE features, like goto definition, which start with figuring out the hir node at the cursor.
200This is some kind of (yet unnamed) uber-IDE pattern, as it is present in Roslyn and Kotlin as well.
120 201
121### `crates/ide` 202### `crates/ide`
122 203
123A stateful library for analyzing many Rust files as they change. `AnalysisHost` 204The `ide` crate builds on top of `hir` semantic model to provide high-level IDE features like completion or goto definition.
124is a mutable entity (clojure's atom) which holds the current state, incorporates 205It is an **API Boundary**.
125changes and hands out `Analysis` --- an immutable and consistent snapshot of 206If 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.
126the world state at a point in time, which actually powers analysis. 207
208**Architecture Invariant:** `ide` crate's API is build out of POD types with public fields.
209The API uses editor's terminology, it talks about offsets and string labels rather than in terms of definitions or types.
210It is effectively the view in MVC and viewmodel in [MVVM](https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93viewmodel).
211All arguments and return types are conceptually serializable.
212In particular, syntax tress and hir types are generally absent from the API (but are used heavily in the implementation).
213Shout outs to LSP developers for popularizing the idea that "UI" is a good place to draw a boundary at.
214
215`ide` is also the first crate which has the notion of change over time.
216`AnalysisHost` is a state to which you can transactionally `apply_change`.
217`Analysis` is an immutable snapshot of the state.
127 218
128One interesting aspect of analysis is its support for cancellation. When a 219Internally, `ide` is split across several crates. `ide_assists`, `ide_completion` and `ide_ssr` implement large isolated features.
129change is applied to `AnalysisHost`, first all currently active snapshots are 220`ide_db` implements common IDE functionality (notably, reference search is implemented here).
130canceled. Only after all snapshots are dropped the change actually affects the 221The `ide` contains a public API/façade, as well as implementation for a plethora of smaller features.
131database.
132 222
133APIs in this crate are IDE centric: they take text offsets as input and produce 223**Architecture Invariant:** `ide` crate strives to provide a _perfect_ API.
134offsets and strings as output. This works on top of rich code model powered by 224Although at the moment it has only one consumer, the LSP server, LSP *does not* influence it's API design.
135`hir`. 225Instead, 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.
136 226
137### `crates/rust-analyzer` 227### `crates/rust-analyzer`
138 228
139An LSP implementation which wraps `ide` into a language server protocol. 229This crate defines the `rust-analyzer` binary, so it is the **entry point**.
230It implements the language server.
231
232**Architecture Invariant:** `rust-analyzer` is the only crate that knows about LSP and JSON serialization.
233If you want to expose a data structure `X` from ide to LSP, don't make it serializable.
234Instead, create a serializable counterpart in `rust-analyzer` crate and manually convert between the two.
235
236`GlobalState` is the state of the server.
237The `main_loop` defines the server event loop which accepts requests and sends responses.
238Requests that modify the state or might block user's typing are handled on the main thread.
239All other requests are processed in background.
240
241**Architecture Invariant:** the server is stateless, a-la HTTP.
242Sometimes state needs to be preserved between requests.
243For example, "what is the `edit` for the fifth completion item of the last completion edit?".
244For this, the second request should include enough info to re-create the context from scratch.
245This generally means including all the parameters of the original request.
246
247`reload` module contains the code that handles configuration and Cargo.toml changes.
248This is a tricky business.
249
250**Architecture Invariant:** `rust-analyzer` should be partially available even when the build is broken.
251Reloading process should not prevent IDE features from working.
252
253### `crates/toolchain`, `crates/project_model`, `crates/flycheck`
254
255These crates deal with invoking `cargo` to learn about project structure and get compiler errors for the "check on save" feature.
256
257They use `crates/path` heavily instead of `std::path`.
258A single `rust-analyzer` process can serve many projects, so it is important that server's current directory does not leak.
259
260### `crates/mbe`, `crates/tt`, `crates/proc_macro_api`, `crates/proc_macro_srv`
261
262These crates implement macros as token tree -> token tree transforms.
263They are independent from the rest of the code.
264
265`tt` crate defined `TokenTree`, a single token or a delimited sequence of token trees.
266`mbe` crate contains tools for transforming between syntax trees and token tree.
267And it also handles the actual parsing and expansion of declarative macro (a-la "Macros By Example" or mbe).
268
269For proc macros, the client-server model are used.
270We pass an argument `--proc-macro` to `rust-analyzer` binary to start a separate process (`proc_macro_srv`).
271And the client (`proc_macro_api`) provides an interface to talk to that server separately.
272
273And then token trees are passed from client, and the server will load the corresponding dynamic library (which built by `cargo`).
274And due to the fact the api for getting result from proc macro are always unstable in `rustc`,
275we maintain our own copy (and paste) of that part of code to allow us to build the whole thing in stable rust.
140 276
141### `crates/vfs` 277 **Architecture Invariant:**
278Bad proc macros may panic or segfault accidentally. So we run it in another process and recover it from fatal error.
279And they may be non-deterministic which conflict how `salsa` works, so special attention is required.
142 280
143Although `hir` and `ide` don't do any IO, we need to be able to read 281### `crates/cfg`
144files from disk at the end of the day. This is what `vfs` does. It also
145manages overlays: "dirty" files in the editor, whose "true" contents is
146different from data on disk.
147 282
148## Testing Infrastructure 283This crate is responsible for parsing, evaluation and general definition of `cfg` attributes.
149 284
150Rust Analyzer has three interesting [systems 285### `crates/vfs`, `crates/vfs-notify`
151boundaries](https://www.tedinski.com/2018/04/10/making-tests-a-positive-influence-on-design.html)
152to concentrate tests on.
153 286
154The outermost boundary is the `rust-analyzer` crate, which defines an LSP 287These crates implement a virtual file system.
155interface in terms of stdio. We do integration testing of this component, by 288They provide consistent snapshots of the underlying file system and insulate messy OS paths.
156feeding it with a stream of LSP requests and checking responses. These tests are
157known as "heavy", because they interact with Cargo and read real files from
158disk. For this reason, we try to avoid writing too many tests on this boundary:
159in a statically typed language, it's hard to make an error in the protocol
160itself if messages are themselves typed.
161 289
162The middle, and most important, boundary is `ide`. Unlike 290**Architecture Invariant:** vfs doesn't assume a single unified file system.
163`rust-analyzer`, which exposes API, `ide` uses Rust API and is intended to 291i.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.
164use by various tools. Typical test creates an `AnalysisHost`, calls some 292For this reason, all path APIs generally take some existing path as a "file system witness".
165`Analysis` functions and compares the results against expectation.
166 293
167The innermost and most elaborate boundary is `hir`. It has a much richer 294### `crates/stdx`
168vocabulary of types than `ide`, but the basic testing setup is the same: we 295
169create a database, run some queries, assert result. 296This crate contains various non-rust-analyzer specific utils, which could have been in std, as well
297as copies of unstable std items we would like to make use of already, like `std::str::split_once`.
298
299### `crates/profile`
300
301This crate contains utilities for CPU and memory profiling.
302
303
304## Cross-Cutting Concerns
305
306This sections talks about the things which are everywhere and nowhere in particular.
307
308### Code generation
309
310Some of the components of this repository are generated through automatic processes.
311`cargo xtask codegen` runs all generation tasks.
312Generated code is generally committed to the git repository.
313There are tests to check that the generated code is fresh.
314
315In particular, we generate:
316
317* API for working with syntax trees (`syntax::ast`, the [`ungrammar`](https://github.com/rust-analyzer/ungrammar) crate).
318* Various sections of the manual:
319
320 * features
321 * assists
322 * config
323
324* Documentation tests for assists
325
326**Architecture Invariant:** we avoid bootstrapping.
327For codegen we need to parse Rust code.
328Using rust-analyzer for that would work and would be fun, but it would also complicate the build process a lot.
329For that reason, we use syn and manual string parsing.
330
331### Cancellation
332
333Let's say that the IDE is in the process of computing syntax highlighting, when the user types `foo`.
334What should happen?
335`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.
336
337The salsa database maintains a global revision counter.
338When applying a change, salsa bumps this counter and waits until all other threads using salsa finish.
339If a thread does salsa-based computation and notices that the counter is incremented, it panics with a special value (see `Canceled::throw`).
340That is, rust-analyzer requires unwinding.
341
342`ide` is the boundary where the panic is caught and transformed into a `Result<T, Cancelled>`.
343
344### Testing
345
346Rust 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.
347
348The outermost boundary is the `rust-analyzer` crate, which defines an LSP interface in terms of stdio.
349We do integration testing of this component, by feeding it with a stream of LSP requests and checking responses.
350These tests are known as "heavy", because they interact with Cargo and read real files from disk.
351For 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.
352Heavy tests are only run when `RUN_SLOW_TESTS` env var is set.
353
354The middle, and most important, boundary is `ide`.
355Unlike `rust-analyzer`, which exposes API, `ide` uses Rust API and is intended for use by various tools.
356A typical test creates an `AnalysisHost`, calls some `Analysis` functions and compares the results against expectation.
357
358The innermost and most elaborate boundary is `hir`.
359It 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.
170 360
171For comparisons, we use the `expect` crate for snapshot testing. 361For comparisons, we use the `expect` crate for snapshot testing.
172 362
173To test various analysis corner cases and avoid forgetting about old tests, we 363To test various analysis corner cases and avoid forgetting about old tests, we use so-called marks.
174use so-called marks. See the `marks` module in the `test_utils` crate for more. 364See the `marks` module in the `test_utils` crate for more.
365
366**Architecture Invariant:** rust-analyzer tests do not use libcore or libstd.
367All required library code must be a part of the tests.
368This ensures fast test execution.
369
370**Architecture Invariant:** tests are data driven and do not test the API.
371Tests which directly call various API functions are a liability, because they make refactoring the API significantly more complicated.
372So most of the tests look like this:
373
374```rust
375#[track_caller]
376fn check(input: &str, expect: expect_test::Expect) {
377 // The single place that actually exercises a particular API
378}
379
380#[test]
381fn foo() {
382 check("foo", expect![["bar"]]);
383}
384
385#[test]
386fn spam() {
387 check("spam", expect![["eggs"]]);
388}
389// ...and a hundred more tests that don't care about the specific API at all.
390```
391
392To specify input data, we use a single string literal in a special format, which can describe a set of rust files.
393See the `Fixture` type.
394
395**Architecture Invariant:** all code invariants are tested by `#[test]` tests.
396There's no additional checks in CI, formatting and tidy tests are run with `cargo test`.
397
398**Architecture Invariant:** tests do not depend on any kind of external resources, they are perfectly reproducible.
399
400
401### Performance Testing
402
403TBA, take a look at the `metrics` xtask and `#[test] fn benchmark_xxx()` functions.
404
405### Error Handling
406
407**Architecture Invariant:** core parts of rust-analyzer (`ide`/`hir`) don't interact with the outside world and thus can't fail.
408Only parts touching LSP are allowed to do IO.
409
410Internals of rust-analyzer need to deal with broken code, but this is not an error condition.
411rust-analyzer is robust: various analysis compute `(T, Vec<Error>)` rather than `Result<T, Error>`.
412
413rust-analyzer is a complex long-running process.
414It will always have bugs and panics.
415But a panic in an isolated feature should not bring down the whole process.
416Each LSP-request is protected by a `catch_unwind`.
417We use `always` and `never` macros instead of `assert` to gracefully recover from impossible conditions.
418
419### Observability
420
421rust-analyzer is a long-running process, so it is important to understand what's going on inside.
422We have several instruments for that.
423
424The event loop that runs rust-analyzer is very explicit.
425Rather than spawning futures or scheduling callbacks (open), the event loop accepts an `enum` of possible events (closed).
426It's easy to see all the things that trigger rust-analyzer processing, together with their performance
427
428rust-analyzer includes a simple hierarchical profiler (`hprof`).
429It is enabled with `RA_PROFILE='*>50` env var (log all (`*`) actions which take more than `50` ms) and produces output like:
430
431```
43285ms - handle_completion
433 68ms - import_on_the_fly
434 67ms - import_assets::search_for_relative_paths
435 0ms - crate_def_map:wait (804 calls)
436 0ms - find_path (16 calls)
437 2ms - find_similar_imports (1 calls)
438 0ms - generic_params_query (334 calls)
439 59ms - trait_solve_query (186 calls)
440 0ms - Semantics::analyze_impl (1 calls)
441 1ms - render_resolution (8 calls)
442 0ms - Semantics::analyze_impl (5 calls)
443```
444
445This is cheap enough to enable in production.
446
447
448Similarly, we save live object counting (`RA_COUNT=1`).
449It is not cheap enough to enable in prod, and this is a bug which should be fixed.