Skip to content

Commit c72d6a3

Browse files
committed
Merge remote branch 'espindola/params'
2 parents 51542b2 + 7672813 commit c72d6a3

File tree

1 file changed

+40
-10
lines changed

1 file changed

+40
-10
lines changed

src/comp/middle/typeck.rs

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,11 @@ import std.option.none;
3131
import std.option.some;
3232

3333
type ty_table = hashmap[ast.def_id, @ty.t];
34+
type ty_item_table = hashmap[ast.def_id,@ast.item];
35+
3436
type crate_ctxt = rec(session.session sess,
3537
@ty_table item_types,
38+
@ty_item_table item_items,
3639
vec[ast.obj_field] obj_fields,
3740
mutable int next_var_id);
3841

@@ -41,7 +44,8 @@ type fn_ctxt = rec(@ty.t ret_ty,
4144
@crate_ctxt ccx);
4245

4346
// Used for ast_ty_to_ty() below.
44-
type ty_getter = fn(ast.def_id) -> @ty.t;
47+
type ty_and_params = rec(vec[ast.ty_param] params, @ty.t ty);
48+
type ty_getter = fn(ast.def_id) -> ty_and_params;
4549

4650
// Replaces parameter types inside a type with type variables.
4751
fn generalize_ty(@crate_ctxt cx, @ty.t t) -> @ty.t {
@@ -118,7 +122,7 @@ fn ast_ty_to_ty(ty_getter getter, &@ast.ty ast_ty) -> @ty.t {
118122
case (ast.def_ty(?id)) {
119123
// TODO: maybe record cname chains so we can do
120124
// "foo = int" like OCaml?
121-
sty = getter(id).struct;
125+
sty = getter(id).ty.struct;
122126
}
123127
case (ast.def_ty_arg(?id)) { sty = ty.ty_param(id); }
124128
case (_) { fail; }
@@ -155,14 +159,39 @@ fn ast_ty_to_ty(ty_getter getter, &@ast.ty ast_ty) -> @ty.t {
155159
// A convenience function to use a crate_ctxt to resolve names for
156160
// ast_ty_to_ty.
157161
fn ast_ty_to_ty_crate(@crate_ctxt ccx, &@ast.ty ast_ty) -> @ty.t {
158-
fn getter(@crate_ctxt ccx, ast.def_id id) -> @ty.t {
162+
fn getter(@crate_ctxt ccx, ast.def_id id) -> ty_and_params {
159163
check (ccx.item_types.contains_key(id));
160-
ret ccx.item_types.get(id);
164+
check (ccx.item_items.contains_key(id));
165+
auto ty = ccx.item_types.get(id);
166+
auto item = ccx.item_items.get(id);
167+
auto params = ty_params_of_item(item);
168+
ret rec(params = params, ty = ty);
161169
}
162170
auto f = bind getter(ccx, _);
163171
ret ast_ty_to_ty(f, ast_ty);
164172
}
165173

174+
fn ty_params_of_item(@ast.item item) -> vec[ast.ty_param] {
175+
alt (item.node) {
176+
case (ast.item_fn(_, _, ?p, _, _)) {
177+
ret p;
178+
}
179+
case (ast.item_ty(_, _, ?p, _, _)) {
180+
ret p;
181+
}
182+
case (ast.item_tag(_, _, ?p, _)) {
183+
ret p;
184+
}
185+
case (ast.item_obj(_, _, ?p, _, _)) {
186+
ret p;
187+
}
188+
case (_) {
189+
let vec[ast.ty_param] r = vec();
190+
ret r;
191+
}
192+
}
193+
}
194+
166195
// Item collection - a pair of bootstrap passes:
167196
//
168197
// 1. Collect the IDs of all type items (typedefs) and store them in a table.
@@ -175,16 +204,16 @@ fn ast_ty_to_ty_crate(@crate_ctxt ccx, &@ast.ty ast_ty) -> @ty.t {
175204
// AST, along with a table mapping item IDs to their types.
176205

177206
fn collect_item_types(session.session sess, @ast.crate crate)
178-
-> tup(@ast.crate, @ty_table) {
179-
180-
type ty_item_table = hashmap[ast.def_id,@ast.item];
207+
-> tup(@ast.crate, @ty_table, @ty_item_table) {
181208

182209
fn getter(@ty_item_table id_to_ty_item,
183210
@ty_table item_to_ty,
184-
ast.def_id id) -> @ty.t {
211+
ast.def_id id) -> ty_and_params {
185212
check (id_to_ty_item.contains_key(id));
186213
auto item = id_to_ty_item.get(id);
187-
ret ty_of_item(id_to_ty_item, item_to_ty, item);
214+
auto ty = ty_of_item(id_to_ty_item, item_to_ty, item);
215+
auto params = ty_params_of_item(item);
216+
ret rec(params = params, ty = ty);
188217
}
189218

190219
fn ty_of_arg(@ty_item_table id_to_ty_item,
@@ -485,7 +514,7 @@ fn collect_item_types(session.session sess, @ast.crate crate)
485514
fold_item_tag = bind fold_item_tag(_,_,_,_,_,_)
486515
with *fld_2);
487516
auto crate_ = fold.fold_crate[@env](e, fld_2, crate);
488-
ret tup(crate_, item_to_ty);
517+
ret tup(crate_, item_to_ty, id_to_ty_item);
489518
}
490519

491520
fn unify(&@fn_ctxt fcx, @ty.t expected, @ty.t actual) -> ty.unify_result {
@@ -1634,6 +1663,7 @@ fn check_crate(session.session sess, @ast.crate crate) -> @ast.crate {
16341663

16351664
auto ccx = @rec(sess=sess,
16361665
item_types=result._1,
1666+
item_items=result._2,
16371667
obj_fields=fields,
16381668
mutable next_var_id=0);
16391669

0 commit comments

Comments
 (0)