Skip to content

Commit ea94298

Browse files
Mark-Simulacrummark-i-m
authored andcommitted
Add some docs around the lint store (rust-lang#476)
* Add some docs around the lint store * Update src/diagnostics.md Co-Authored-By: Niko Matsakis <[email protected]> * restructure
1 parent 017abbd commit ea94298

File tree

3 files changed

+144
-13
lines changed

3 files changed

+144
-13
lines changed

Diff for: src/SUMMARY.md

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
- [Coding conventions](./conventions.md)
2424
- [crates.io Dependencies](./crates-io.md)
2525
- [Emitting Errors and other Diagnostics](diagnostics.md)
26+
- [`LintStore`](./diagnostics/lintstore.md)
2627
- [ICE-breaker teams](ice-breaker/about.md)
2728
- [LLVM ICE-breakers](ice-breaker/llvm.md)
2829
- [Part 2: How rustc works](./part-2-intro.md)

Diff for: src/diagnostics.md

+38-13
Original file line numberDiff line numberDiff line change
@@ -171,11 +171,17 @@ crate.
171171

172172
[builtin]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_lint/index.html
173173

174-
Each lint is defined as a `struct` that implements the `LintPass` `trait`. The
175-
trait implementation allows you to check certain syntactic constructs the
176-
linter walks the source code. You can then choose to emit lints in a very
177-
similar way to compile errors. Finally, you register the lint to actually get
178-
it to be run by the compiler by using the `declare_lint!` macro.
174+
Every lint is implemented via a `struct` that implements the `LintPass` `trait`
175+
(you also implement one of the more specific lint pass traits, either
176+
`EarlyLintPass` or `LateLintPass`). The trait implementation allows you to
177+
check certain syntactic constructs as the linter walks the source code. You can
178+
then choose to emit lints in a very similar way to compile errors.
179+
180+
You also declare the metadata of a particular lint via the `declare_lint!`
181+
macro. This includes the name, the default level, a short description, and some
182+
more details.
183+
184+
Note that the lint and the lint pass must be registered with the compiler.
179185

180186
For example, the following lint checks for uses
181187
of `while true { ... }` and suggests using `loop { ... }` instead.
@@ -196,11 +202,15 @@ declare_lint! {
196202
#[derive(Copy, Clone)]
197203
pub struct WhileTrue;
198204
199-
impl LintPass for WhileTrue {
200-
fn get_lints(&self) -> LintArray {
201-
lint_array!(WHILE_TRUE)
202-
}
203-
}
205+
// This declares a lint pass, providing a list of associated lints. The
206+
// compiler currently doesn't use the associated lints directly (e.g., to not
207+
// run the pass or otherwise check that the pass emits the appropriate set of
208+
// lints). However, it's good to be accurate here as it's possible that we're
209+
// going to register the lints via the get_lints method on our lint pass (that
210+
// this macro generates).
211+
impl_lint_pass!(
212+
WhileTrue => [WHILE_TRUE],
213+
);
204214
205215
// LateLintPass has lots of methods. We only override the definition of
206216
// `check_expr` for this lint because that's all we need, but you could
@@ -242,9 +252,24 @@ declare_lint! {
242252
This makes the `ANONYMOUS_PARAMETERS` lint allow-by-default in the 2015 edition
243253
but warn-by-default in the 2018 edition.
244254

245-
Lints that represent an incompatibility (i.e. error) in the upcoming edition
246-
should also be registered as `FutureIncompatibilityLint`s in
247-
[`register_builtins`][rbuiltins] function in [`rustc_lint::lib`][builtin].
255+
A future-incompatible lint should be declared with the `@future_incompatible`
256+
additional "field":
257+
258+
```rust,ignore
259+
declare_lint! {
260+
pub ANONYMOUS_PARAMETERS,
261+
Allow,
262+
"detects anonymous parameters",
263+
@future_incompatible = FutureIncompatibleInfo {
264+
reference: "issue #41686 <https://github.com/rust-lang/rust/issues/41686>",
265+
edition: Some(Edition::Edition2018),
266+
};
267+
}
268+
```
269+
270+
If you need a combination of options that's not supported by the
271+
`declare_lint!` macro, you can always define your own static with a type of
272+
`&Lint` but this is currently linted against in the compiler tree.
248273

249274
### Lint Groups
250275

Diff for: src/diagnostics/lintstore.md

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
# Lints
2+
3+
This page documents some of the machinery around lint registration and how we
4+
run lints in the compiler.
5+
6+
The `LintStore` is the central piece of infrastructure, around which everything
7+
rotates. It's not available during the early parts of compilation (i.e., before
8+
TyCtxt) in most code, as we need to fill it in with all of the lints, which can only happen after
9+
plugin registration.
10+
11+
## Lints vs. lint passes
12+
13+
There are two parts to the linting mechanism within the compiler: lints and lint passes.
14+
Unfortunately, a lot of the documentation we have refers to both of these as just "lints."
15+
16+
First, we have the lint declarations themselves: this is where the name and default lint level and
17+
other metadata come from. These are normally defined by way of the [`declare_lint!`] macro, which
18+
boils down to a static with type `&rustc::lint::Lint`. We lint against direct declarations without
19+
the use of the macro today (though this may change in the future, as the macro is somewhat unwieldy
20+
to add new fields to, like all macros by example).
21+
22+
Lint declarations don't carry any "state" - they are merely global identifers and descriptions of
23+
lints. We assert at runtime that they are not registered twice (by lint name).
24+
25+
Lint passes are the meat of any lint. Notably, there is not a one-to-one relationship between
26+
lints and lint passes; a lint might not have any lint pass that emits it, it could have many, or
27+
just one -- the compiler doesn't track whether a pass is in any way associated with a particular
28+
lint, and frequently lints are emitted as part of other work (e.g., type checking, etc.).
29+
30+
## Registration
31+
32+
### High-level overview
33+
34+
The lint store is created and all lints are registered during plugin registration, in
35+
[`rustc_interface::register_plugins`]. There are three 'sources' of lint: the internal lints, plugin
36+
lints, and `rustc_interface::Config` `register_lints`. All are registered here, in
37+
`register_plugins`.
38+
39+
Once the registration is complete, we "freeze" the lint store by placing it in an `Lrc`. Later in
40+
the driver, it's passed into the `GlobalCtxt` constructor where it lives in an immutable form from
41+
then on.
42+
43+
Lints are registered via the [`LintStore::register_lint`] function. This should
44+
happen just once for any lint, or an ICE will occur.
45+
46+
Lint passes are registered separately into one of the categories (pre-expansion,
47+
early, late, late module). Passes are registered as a closure -- i.e., `impl
48+
Fn() -> Box<dyn X>`, where `dyn X` is either an early or late lint pass trait
49+
object. When we run the lint passes, we run the closure and then invoke the lint
50+
pass methods, which take `&mut self` -- lint passes can keep track of state
51+
internally.
52+
53+
#### Internal lints
54+
55+
Note, these include both rustc-internal lints, and the traditional lints, like, for example the dead
56+
code lint.
57+
58+
These are primarily described in two places: `rustc::lint::builtin` and `rustc_lint::builtin`. The
59+
first provides the definitions for the lints themselves, and the latter provides the lint pass
60+
definitions (and implementations).
61+
62+
The internal lint registration happens in the [`rustc_lint::register_builtins`] function, along with
63+
the [`rustc_lint::register_internals`] function. More generally, the LintStore "constructor"
64+
function which is *the* way to get a `LintStore` in the compiler (you should not construct it
65+
directly) is [`rustc_lint::new_lint_store`]; it calls the registration functions.
66+
67+
#### Plugin lints
68+
69+
This is one of the primary use cases remaining for plugins/drivers. Plugins are given access to the
70+
mutable `LintStore` during registration to call any functions they need on the `LintStore`, just
71+
like rustc code. Plugins are intended to declare lints with the `plugin` field set to true (e.g., by
72+
way of the [`declare_tool_lint!`] macro), but this is purely for diagnostics and help text;
73+
otherwise plugin lints are mostly just as first class as rustc builtin lints.
74+
75+
#### Driver lints
76+
77+
These are the lints provided by drivers via the `rustc_interface::Config` [`register_lints`] field,
78+
which is a callback. Drivers should, if finding it already set, call the function currently set
79+
within the callback they add. The best way for drivers to get access to this is by overriding the
80+
`Callbacks::config` function which gives them direct access to the `Config` structure.
81+
82+
## Compiler lint passes are combined into one pass
83+
84+
Within the compiler, for performance reasons, we usually do not register dozens
85+
of lint passes. Instead, we have a single lint pass of each variety
86+
(e.g. `BuiltinCombinedModuleLateLintPass`) which will internally call all of the
87+
individual lint passes; this is because then we get the benefits of static over
88+
dynamic dispatch for each of the (often empty) trait methods.
89+
90+
Ideally, we'd not have to do this, since it certainly adds to the complexity of
91+
understanding the code. However, with the current type-erased lint store
92+
approach, it is beneficial to do so for performance reasons.
93+
94+
New lints being added likely want to join one of the existing declarations like
95+
`late_lint_mod_passes` in `librustc_lint/lib.rs`, which would then
96+
auto-propagate into the other.
97+
98+
[`LintStore::register_lint`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc/lint/struct.LintStore.html#method.register_lints
99+
[`rustc_interface::register_plugins`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_interface/passes/fn.register_plugins.html)
100+
[`rustc_lint::register_builtins`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_lint/fn.register_builtins.html
101+
[`rustc_lint::register_internals`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_lint/fn.register_internals.html
102+
[`rustc_lint::new_lint_store`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_lint/fn.new_lint_store.html
103+
[`rustc::declare_lint!`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc/macro.declare_lint.html
104+
[`rustc::declare_tool_lint!`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc/macro.declare_tool_lint.html
105+
[`register_lints`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_interface/interface/struct.Config.html#structfield.register_lints

0 commit comments

Comments
 (0)