Skip to content

Commit a34e75b

Browse files
committed
Auto merge of #10653 - flip1995:book-type_passes_corrections, r=llogiq
Book: Improve type checking and lint passes chapters - Adds the lint passes chapter to the index. - Moves additional reading material for beginners to the front page of the development section. - Clarify some details in `hir::Ty` vs `ty::Ty`. `@blyxyas` I started reviewing the chapters that were already merged. Great work. I liked the additional reading chapter so much, that I moved it front and center. Other than that I only had some minor corrections to make. Please review. r? `@llogiq` changelog: none
2 parents 0049816 + b842e99 commit a34e75b

File tree

4 files changed

+40
-32
lines changed

4 files changed

+40
-32
lines changed

book/src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
- [Development](development/README.md)
1414
- [Basics](development/basics.md)
1515
- [Adding Lints](development/adding_lints.md)
16+
- [Lint Passes](development/lint_passes.md)
1617
- [Type Checking](development/type_checking.md)
1718
- [Common Tools](development/common_tools_writing_lints.md)
1819
- [Infrastructure](development/infrastructure/README.md)

book/src/development/README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,24 @@ If this is your first time contributing to Clippy, you should first read the
1313
[Basics docs](basics.md). This will explain the basics on how to get the source
1414
code and how to compile and test the code.
1515

16+
## Additional Readings for Beginners
17+
18+
If a dear reader of this documentation has never taken a class on compilers
19+
and interpreters, it might be confusing as to why AST level deals with only
20+
the language's syntax. And some readers might not even understand what lexing,
21+
parsing, and AST mean.
22+
23+
This documentation serves by no means as a crash course on compilers or language design.
24+
And for details specifically related to Rust, the [Rustc Development Guide][rustc_dev_guide]
25+
is a far better choice to peruse.
26+
27+
The [Syntax and AST][ast] chapter and the [High-Level IR][hir] chapter are
28+
great introduction to the concepts mentioned in this chapter.
29+
30+
Some readers might also find the [introductory chapter][map_of_territory] of
31+
Robert Nystrom's _Crafting Interpreters_ a helpful overview of compiled and
32+
interpreted languages before jumping back to the Rustc guide.
33+
1634
## Writing code
1735

1836
If you have done the basic setup, it's time to start hacking.
@@ -37,6 +55,10 @@ book](../lints.md).
3755
> - Triage procedure
3856
> - Bors and Homu
3957
58+
[ast]: https://rustc-dev-guide.rust-lang.org/syntax-intro.html
59+
[hir]: https://rustc-dev-guide.rust-lang.org/hir.html
60+
[rustc_dev_guide]: https://rustc-dev-guide.rust-lang.org/
61+
[map_of_territory]: https://craftinginterpreters.com/a-map-of-the-territory.html
4062
[clippy_rfc]: https://github.com/rust-lang/rfcs/blob/master/text/2476-clippy-uno.md
4163
[rfc_stability]: https://github.com/rust-lang/rfcs/blob/master/text/2476-clippy-uno.md#stability-guarantees
4264
[rfc_lint_cats]: https://github.com/rust-lang/rfcs/blob/master/text/2476-clippy-uno.md#lint-audit-and-categories

book/src/development/lint_passes.md

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Lint passes
22

33
Before working on the logic of a new lint, there is an important decision
4-
that every Clippy developers must make: to use
4+
that every Clippy developer must make: to use
55
[`EarlyLintPass`][early_lint_pass] or [`LateLintPass`][late_lint_pass].
66

77
In short, the `LateLintPass` has access to type and symbol information while the
@@ -107,30 +107,8 @@ that use `LateLintPass`:
107107
$ cargo dev new_lint --name=<your_new_lint> --pass=late --category=<your_category_choice>
108108
```
109109

110-
## Additional Readings for Beginners
111-
112-
If a dear reader of this documentation has never taken a class on compilers
113-
and interpreters, it might be confusing as to why AST level deals with only
114-
the language's syntax. And some readers might not even understand what lexing,
115-
parsing, and AST mean.
116-
117-
This documentation serves by no means as a crash course on compilers or language design.
118-
And for details specifically related to Rust, the [Rustc Development Guide][rustc_dev_guide]
119-
is a far better choice to peruse.
120-
121-
The [Syntax and AST][ast] chapter and the [High-Level IR][hir] chapter are
122-
great introduction to the concepts mentioned in this chapter.
123-
124-
Some readers might also find the [introductory chapter][map_of_territory] of
125-
Robert Nystrom's _Crafting Interpreters_ a helpful overview of compiled and
126-
interpreted languages before jumping back to the Rustc guide.
127-
128-
[ast]: https://rustc-dev-guide.rust-lang.org/syntax-intro.html
129110
[early_context]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_lint/context/struct.EarlyContext.html
130111
[early_lint_pass]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_lint/trait.EarlyLintPass.html
131-
[hir]: https://rustc-dev-guide.rust-lang.org/hir.html
132112
[late_context]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_lint/context/struct.LateContext.html
133113
[late_lint_pass]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_lint/trait.LateLintPass.html
134114
[lexing_and_parsing]: https://rustc-dev-guide.rust-lang.org/overview.html#lexing-and-parsing
135-
[rustc_dev_guide]: https://rustc-dev-guide.rust-lang.org/
136-
[map_of_territory]: https://craftinginterpreters.com/a-map-of-the-territory.html

book/src/development/type_checking.md

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ impl LateLintPass<'_> for MyStructLint {
5151
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
5252
// Get type of `expr`
5353
let ty = cx.typeck_results().expr_ty(expr);
54-
54+
5555
// Check if the `Ty` of this expression is of character type
5656
if ty.is_char() {
5757
println!("Our expression is a char!");
@@ -70,18 +70,18 @@ pub fn is_char(self) -> bool {
7070
}
7171
```
7272

73-
Indeed, we just discovered `Ty`'s [`kind` method][kind], which provides us
73+
Indeed, we just discovered `Ty`'s [`kind()` method][kind], which provides us
7474
with [`TyKind`][TyKind] of a `Ty`.
7575

7676
## `TyKind`
7777

7878
`TyKind` defines the kinds of types in Rust's type system.
7979
Peeking into [`TyKind` documentation][TyKind], we will see that it is an
80-
enum of 27 variants, including items such as `Bool`, `Int`, `Ref`, etc.
80+
enum of over 25 variants, including items such as `Bool`, `Int`, `Ref`, etc.
8181

8282
### `kind` Usage
8383

84-
The `TyKind` of `Ty` can be returned by calling [`Ty.kind` method][kind].
84+
The `TyKind` of `Ty` can be returned by calling [`Ty.kind()` method][kind].
8585
We often use this method to perform pattern matching in Clippy.
8686

8787
For instance, if we want to check for a `struct`, we could examine if the
@@ -107,15 +107,21 @@ impl LateLintPass<'_> for MyStructLint {
107107
We've been talking about [`ty::Ty`][middle_ty] this whole time without addressing [`hir::Ty`][hir_ty], but the latter
108108
is also important to understand.
109109

110-
`hir::Ty` would represent *what* an user wrote, while `ty::Ty` would understand the meaning of it (because it has more
111-
information).
110+
`hir::Ty` would represent *what* the user wrote, while `ty::Ty` is how the compiler sees the type and has more
111+
information. Example:
112112

113-
**Example: `fn foo(x: u32) -> u32 { x }`**
113+
```rust
114+
fn foo(x: u32) -> u32 { x }
115+
```
114116

115117
Here the HIR sees the types without "thinking" about them, it knows that the function takes an `u32` and returns
116-
an `u32`. But at the `ty::Ty` level the compiler understands that they're the same type, in-depth lifetimes, etc...
118+
an `u32`. As far as `hir::Ty` is concerned those might be different types. But at the `ty::Ty` level the compiler
119+
understands that they're the same type, in-depth lifetimes, etc...
120+
121+
To get from a `hir::Ty` to a `ty::Ty`, you can use the [`hir_ty_to_ty`][hir_ty_to_ty] function outside of bodies or
122+
outside of bodies the [`TypeckResults::node_type()`][node_type] method.
117123

118-
you can use the [`hir_ty_to_ty`][hir_ty_to_ty] function to convert from a `hir::Ty` to a `ty::Ty`
124+
> **Warning**: Don't use `hir_ty_to_ty` inside of bodies, because this can cause ICEs.
119125
120126
## Useful Links
121127

@@ -130,6 +136,7 @@ in this chapter:
130136
[Adt]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/enum.TyKind.html#variant.Adt
131137
[AdtDef]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/adt/struct.AdtDef.html
132138
[expr_ty]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/struct.TypeckResults.html#method.expr_ty
139+
[node_type]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/struct.TypeckResults.html#method.node_type
133140
[is_char]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/struct.Ty.html#method.is_char
134141
[is_char_source]: https://doc.rust-lang.org/nightly/nightly-rustc/src/rustc_middle/ty/sty.rs.html#1831-1834
135142
[kind]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/struct.Ty.html#method.kind

0 commit comments

Comments
 (0)