Skip to content

Commit 35a3fa0

Browse files
committed
add some comments explaining how the tables work
1 parent 2b45da8 commit 35a3fa0

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

src/rustc/middle/ty.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,18 @@ type ctxt =
194194
sess: session::session,
195195
def_map: resolve::def_map,
196196
region_map: @middle::region::region_map,
197+
198+
// Stores the types for various nodes in the AST. Note that this table
199+
// is not guaranteed to be populated until after typeck. See
200+
// typeck::fn_ctxt for details.
197201
node_types: node_type_table,
202+
203+
// Stores the type parameters which were substituted to obtain the type
204+
// of this node. This only applies to nodes that refer to entities
205+
// parameterized by type parameters, such as generic fns, types, or
206+
// other items.
198207
node_type_substs: hashmap<node_id, [t]>,
208+
199209
items: ast_map::map,
200210
freevars: freevars::freevar_map,
201211
tcache: type_cache,

src/rustc/middle/typeck.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,33 @@ type fn_ctxt =
8282
locals: hashmap<ast::node_id, int>,
8383
next_var_id: @mut int,
8484
next_region_var_id: @mut int,
85+
86+
// While type checking a function, the intermediate types for the
87+
// expressions, blocks, and so forth contained within the function are
88+
// stored in these tables. These types may contain unresolved type
89+
// variables. After type checking is complete, the functions in the
90+
// writeback module are used to take the types from this table, resolve
91+
// them, and then write them into their permanent home in the type
92+
// context `ccx.tcx`.
93+
//
94+
// This means that during inferencing you should use `fcx.write_ty()`
95+
// and `fcx.expr_ty()` / `fcx.node_ty()` to write/obtain the types of
96+
// nodes within the function.
97+
//
98+
// The types of top-level items, which never contain unbound type
99+
// variables, are stored directly into the `tcx` tables.
100+
//
101+
// n.b.: A type variable is not the same thing as a type parameter. A
102+
// type variable is rather an "instance" of a type parameter: that is,
103+
// given a generic function `fn foo<T>(t: T)`: while checking the
104+
// function `foo`, the type `ty_param(0)` refers to the type `T`, which
105+
// is treated in abstract. When `foo()` is called, however, `T` will be
106+
// substituted for a fresh type variable `ty_var(N)`. This variable will
107+
// eventually be resolved to some concrete type (which might itself be
108+
// type parameter).
85109
node_types: smallintmap::smallintmap<ty::t>,
86110
node_type_substs: hashmap<ast::node_id, [ty::t]>,
111+
87112
ccx: @crate_ctxt};
88113

89114
// Determines whether the given node ID is a use of the def of

0 commit comments

Comments
 (0)