Skip to content

Commit fbdcb26

Browse files
committed
---
yaml --- r: 1295 b: refs/heads/master c: 596a97c h: refs/heads/master i: 1293: 6e2d5f2 1291: a0ccfec 1287: cdab115 1279: 74f4be3 v: v3
1 parent 46b9cb9 commit fbdcb26

File tree

2 files changed

+48
-19
lines changed

2 files changed

+48
-19
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
---
2-
refs/heads/master: 70c129f79b42f6020b2342e81bf96a0c701ee9e2
2+
refs/heads/master: 596a97cf70b5329fb6bfe779ab1425a0e37a61d2

trunk/src/comp/middle/typeck.rs

Lines changed: 47 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,24 @@ fn ast_ty_to_ty(ty_getter getter, &@ast.ty ast_ty) -> @ty.t {
9898
ret ty.fold_ty(replacer, t);
9999
}
100100

101+
fn instantiate(ty_getter getter, ast.def_id id,
102+
vec[@ast.ty] args) -> @ty.t {
103+
// TODO: maybe record cname chains so we can do
104+
// "foo = int" like OCaml?
105+
auto ty_and_params = getter(id);
106+
auto params = ty_and_params.params;
107+
auto num_type_args = _vec.len[@ast.ty](args);
108+
check(num_type_args == _vec.len[ast.ty_param](params));
109+
110+
auto param_map = common.new_def_hash[@ty.t]();
111+
for each (uint i in _uint.range(0u, num_type_args)) {
112+
auto arg = args.(i);
113+
auto param = params.(i);
114+
param_map.insert(param.id, ast_ty_to_ty(getter, arg));
115+
}
116+
ret replace_type_params(ty_and_params.ty, param_map);
117+
}
118+
101119
auto mut = ast.imm;
102120
auto sty;
103121
auto cname = none[str];
@@ -137,22 +155,10 @@ fn ast_ty_to_ty(ty_getter getter, &@ast.ty ast_ty) -> @ty.t {
137155
check (def != none[ast.def]);
138156
alt (option.get[ast.def](def)) {
139157
case (ast.def_ty(?id)) {
140-
// TODO: maybe record cname chains so we can do
141-
// "foo = int" like OCaml?
142-
auto ty_and_params = getter(id);
143-
auto params = ty_and_params.params;
144-
auto num_type_params = _vec.len[@ast.ty](path.node.types);
145-
check(num_type_params == _vec.len[ast.ty_param](params));
146-
147-
auto param_map = common.new_def_hash[@ty.t]();
148-
for each (uint i in _uint.range(0u, num_type_params)) {
149-
auto x = path.node.types.(i);
150-
auto y = params.(i);
151-
param_map.insert(y.id, ast_ty_to_ty(getter, x));
152-
}
153-
154-
sty = replace_type_params(ty_and_params.ty,
155-
param_map).struct;
158+
sty = instantiate(getter, id, path.node.types).struct;
159+
}
160+
case (ast.def_obj(?id)) {
161+
sty = instantiate(getter, id, path.node.types).struct;
156162
}
157163
case (ast.def_ty_arg(?id)) { sty = ty.ty_param(id); }
158164
case (_) { fail; }
@@ -190,11 +196,21 @@ fn ast_ty_to_ty(ty_getter getter, &@ast.ty ast_ty) -> @ty.t {
190196
// ast_ty_to_ty.
191197
fn ast_ty_to_ty_crate(@crate_ctxt ccx, &@ast.ty ast_ty) -> @ty.t {
192198
fn getter(@crate_ctxt ccx, ast.def_id id) -> ty_and_params {
193-
check (ccx.item_types.contains_key(id));
194199
check (ccx.item_items.contains_key(id));
195-
auto ty = ccx.item_types.get(id);
200+
check (ccx.item_types.contains_key(id));
196201
auto item = ccx.item_items.get(id);
202+
auto ty = ccx.item_types.get(id);
197203
auto params = ty_params_of_item(item);
204+
205+
alt (item.node) {
206+
case (ast.item_obj(_,_,_,_,_)) {
207+
// An obj used as a type name refers to the output type of the
208+
// item (constructor).
209+
ty = middle.ty.ty_fn_ret(ty);
210+
}
211+
case (_) { }
212+
}
213+
198214
ret rec(params = params, ty = ty);
199215
}
200216
auto f = bind getter(ccx, _);
@@ -243,6 +259,16 @@ fn collect_item_types(session.session sess, @ast.crate crate)
243259
auto item = id_to_ty_item.get(id);
244260
auto ty = ty_of_item(id_to_ty_item, item_to_ty, item);
245261
auto params = ty_params_of_item(item);
262+
263+
alt (item.node) {
264+
case (ast.item_obj(_,_,_,_,_)) {
265+
// An obj used as a type name refers to the output type of the
266+
// item (constructor).
267+
ty = middle.ty.ty_fn_ret(ty);
268+
}
269+
case (_) { }
270+
}
271+
246272
ret rec(params = params, ty = ty);
247273
}
248274

@@ -400,6 +426,9 @@ fn collect_item_types(session.session sess, @ast.crate crate)
400426
case (ast.item_tag(_, _, _, ?def_id)) {
401427
id_to_ty_item.insert(def_id, i);
402428
}
429+
case (ast.item_obj(_, _, _, ?def_id, _)) {
430+
id_to_ty_item.insert(def_id, i);
431+
}
403432
case (_) { /* empty */ }
404433
}
405434
ret id_to_ty_item;

0 commit comments

Comments
 (0)