]> git.proxmox.com Git - rustc.git/blob - src/doc/rustc-dev-guide/src/type-checking.md
New upstream version 1.49.0+dfsg1
[rustc.git] / src / doc / rustc-dev-guide / src / type-checking.md
1 # Type checking
2
3 The [`rustc_typeck`][typeck] crate contains the source for "type collection"
4 and "type checking", as well as a few other bits of related functionality. (It
5 draws heavily on the [type inference] and [trait solving].)
6
7 [typeck]: https://github.com/rust-lang/rust/tree/master/src/librustc_typeck
8 [type inference]: type-inference.html
9 [trait solving]: traits/resolution.html
10
11 ## Type collection
12
13 Type "collection" is the process of converting the types found in the HIR
14 (`hir::Ty`), which represent the syntactic things that the user wrote, into the
15 **internal representation** used by the compiler (`Ty<'tcx>`) – we also do
16 similar conversions for where-clauses and other bits of the function signature.
17
18 To try and get a sense for the difference, consider this function:
19
20 ```rust,ignore
21 struct Foo { }
22 fn foo(x: Foo, y: self::Foo) { ... }
23 // ^^^ ^^^^^^^^^
24 ```
25
26 Those two parameters `x` and `y` each have the same type: but they will have
27 distinct `hir::Ty` nodes. Those nodes will have different spans, and of course
28 they encode the path somewhat differently. But once they are "collected" into
29 `Ty<'tcx>` nodes, they will be represented by the exact same internal type.
30
31 Collection is defined as a bundle of [queries] for computing information about
32 the various functions, traits, and other items in the crate being compiled.
33 Note that each of these queries is concerned with *interprocedural* things –
34 for example, for a function definition, collection will figure out the type and
35 signature of the function, but it will not visit the *body* of the function in
36 any way, nor examine type annotations on local variables (that's the job of
37 type *checking*).
38
39 For more details, see the [`collect`][collect] module.
40
41 [queries]: query.html
42 [collect]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_typeck/collect/
43
44 **TODO**: actually talk about type checking...