|
1 | 1 | # AST Validation
|
2 | 2 |
|
3 |
| -AST validation is the process of checking various correctness properties about |
4 |
| -the AST after macro expansion. |
| 3 | +* [About](#about) |
| 4 | +* [Validation](#validation) |
5 | 5 |
|
6 |
| -**TODO**: write this chapter. [#656](https://github.com/rust-lang/rustc-dev-guide/issues/656) |
| 6 | +## About |
| 7 | + |
| 8 | +_AST validation_ is a separate AST pass that visits each |
| 9 | +item in the tree and performs simple checks. This pass |
| 10 | +doesn't perform any complex analysis, type checking or |
| 11 | +name resolution. |
| 12 | + |
| 13 | +Before performing any validation, the compiler first expands |
| 14 | +the macros. Then this pass performs validations to check |
| 15 | +that each AST item is in the correct state. And when this pass |
| 16 | +is done, the compiler runs the crate resolution pass. |
| 17 | + |
| 18 | +## Validations |
| 19 | + |
| 20 | +Validations are defined in `AstValidator` class, which |
| 21 | +itself is located in `rustc_ast_passes` crate. This |
| 22 | +class implements various simple checks which emit errors |
| 23 | +when certain language rules are broken. |
| 24 | + |
| 25 | +In addition, `AstValidator` implements `Visitor` trait |
| 26 | +that defines how to visit AST items (which can be functions, |
| 27 | +traits, enums, etc). |
| 28 | + |
| 29 | +For each item, visitor performs specific checks. For |
| 30 | +example, when visiting a function declaration, |
| 31 | +`AstValidator` checks that the function has: |
| 32 | + |
| 33 | +* no more than `u16::MAX` parameters; |
| 34 | +* c-variadic functions are declared with at least one named argument; |
| 35 | +* c-variadic argument goes the last in the declaration; |
| 36 | +* documentation comments aren't applied to function parameters; |
| 37 | +* and other validations. |
0 commit comments