Skip to content

Commit 38813cf

Browse files
committed
start writing some typeck docs (incomplete)
1 parent 032fdef commit 38813cf

File tree

2 files changed

+63
-44
lines changed

2 files changed

+63
-44
lines changed

src/librustc_typeck/README.md

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
NB: This crate is part of the Rust compiler. For an overview of the
2+
compiler as a whole, see
3+
[the README.md file found in `librustc`](../librustc/README.md).
4+
5+
The `rustc_typeck` crate contains the source for "type collection" and
6+
"type checking", as well as a few other bits of related functionality.
7+
(It draws heavily on the [type inferencing][infer] and
8+
[trait solving][traits] code found in librustc.)
9+
10+
[infer]: ../librustc/infer/README.md
11+
[traits]: ../librustc/traits/README.md
12+
13+
## Type collection
14+
15+
Type "collection" is the process of convering the types found in the
16+
HIR (`hir::Ty`), which represent the syntactic things that the user
17+
wrote, into the **internal representation** used by the compiler
18+
(`Ty<'tcx>`) -- we also do similar conversions for where-clauses and
19+
other bits of the function signature.
20+
21+
To try and get a sense for the difference, consider this function:
22+
23+
```rust
24+
struct Foo { }
25+
fn foo(x: Foo, y: self::Foo) { .. }
26+
// ^^^ ^^^^^^^^^
27+
```
28+
29+
Those two parameters `x` and `y` each have the same type: but they
30+
will have distinct `hir::Ty` nodes. Those nodes will have different
31+
spans, and of course they encode the path somewhat differently. But
32+
once they are "collected" into `Ty<'tcx>` nodes, they will be
33+
represented by the exact same internal type.
34+
35+
Collection is defined as a bundle of queries (e.g., `type_of`) for
36+
computing information about the various functions, traits, and other
37+
items in the crate being compiled. Note that each of these queries is
38+
concerned with *interprocedural* things -- for example, for a function
39+
definition, collection will figure out the type and signature of the
40+
function, but it will not visit the *body* of the function in any way,
41+
nor examine type annotations on local variables (that's the job of
42+
type *checking*).
43+
44+
For more details, see the `collect` module.
45+
46+
## Type checking
47+
48+
TODO

src/librustc_typeck/collect.rs

+15-44
Original file line numberDiff line numberDiff line change
@@ -8,50 +8,21 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
/*
12-
13-
# Collect phase
14-
15-
The collect phase of type check has the job of visiting all items,
16-
determining their type, and writing that type into the `tcx.types`
17-
table. Despite its name, this table does not really operate as a
18-
*cache*, at least not for the types of items defined within the
19-
current crate: we assume that after the collect phase, the types of
20-
all local items will be present in the table.
21-
22-
Unlike most of the types that are present in Rust, the types computed
23-
for each item are in fact type schemes. This means that they are
24-
generic types that may have type parameters. TypeSchemes are
25-
represented by a pair of `Generics` and `Ty`. Type
26-
parameters themselves are represented as `ty_param()` instances.
27-
28-
The phasing of type conversion is somewhat complicated. There is no
29-
clear set of phases we can enforce (e.g., converting traits first,
30-
then types, or something like that) because the user can introduce
31-
arbitrary interdependencies. So instead we generally convert things
32-
lazilly and on demand, and include logic that checks for cycles.
33-
Demand is driven by calls to `AstConv::get_item_type_scheme` or
34-
`AstConv::trait_def`.
35-
36-
Currently, we "convert" types and traits in two phases (note that
37-
conversion only affects the types of items / enum variants / methods;
38-
it does not e.g. compute the types of individual expressions):
39-
40-
0. Intrinsics
41-
1. Trait/Type definitions
42-
43-
Conversion itself is done by simply walking each of the items in turn
44-
and invoking an appropriate function (e.g., `trait_def_of_item` or
45-
`convert_item`). However, it is possible that while converting an
46-
item, we may need to compute the *type scheme* or *trait definition*
47-
for other items.
48-
49-
There are some shortcomings in this design:
50-
- Because the item generics include defaults, cycles through type
51-
parameter defaults are illegal even if those defaults are never
52-
employed. This is not necessarily a bug.
53-
54-
*/
11+
//! "Collection" is the process of determining the type and other external
12+
//! details of each item in Rust. Collection is specifically concerned
13+
//! with *interprocedural* things -- for example, for a function
14+
//! definition, collection will figure out the type and signature of the
15+
//! function, but it will not visit the *body* of the function in any way,
16+
//! nor examine type annotations on local variables (that's the job of
17+
//! type *checking*).
18+
//!
19+
//! Collecting is ultimately defined by a bundle of queries that
20+
//! inquire after various facts about the items in the crate (e.g.,
21+
//! `type_of`, `generics_of`, `predicates_of`, etc). See the `provide` function
22+
//! for the full set.
23+
//!
24+
//! At present, however, we do run collection across all items in the
25+
//! crate as a kind of pass. This should eventually be factored away.
5526
5627
use astconv::{AstConv, Bounds};
5728
use lint;

0 commit comments

Comments
 (0)