Skip to content

Commit b6197c8

Browse files
committed
---
yaml --- r: 986 b: refs/heads/master c: cd97b9d h: refs/heads/master v: v3
1 parent 9ed5f82 commit b6197c8

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
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: 67a616340d8e3a3af8cb2524736f824ea640b456
2+
refs/heads/master: cd97b9d965ada49ed07af7ee651b2b2266df4cec

trunk/src/comp/middle/typeck.rs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ tag sty {
3939
ty_machine(util.common.ty_mach);
4040
ty_char;
4141
ty_str;
42+
ty_tag(ast.def_id);
4243
ty_box(@ty);
4344
ty_vec(@ty);
4445
ty_tup(vec[@ty]);
@@ -401,6 +402,34 @@ fn collect_item_types(@ast.crate crate) -> tup(@ast.crate, @ty_table) {
401402
}
402403
}
403404

405+
fn add_tag_variant_types(@hashmap[ast.def_id,@ast.item] id_to_ty_item,
406+
@ty_table item_to_ty,
407+
&ast.def_id tag_id,
408+
&vec[ast.variant] variants) {
409+
for (ast.variant variant in variants) {
410+
// Nullary tag constructors get turned into constants; n-ary tag
411+
// constructors get turned into functions.
412+
auto result_ty;
413+
if (_vec.len[@ast.ty](variant.args) == 0u) {
414+
result_ty = plain_ty(ty_tag(tag_id));
415+
} else {
416+
// As above, tell ast_ty_to_ty() that trans_ty_item_to_ty()
417+
// should be called to resolve named types.
418+
auto f = bind trans_ty_item_id_to_ty(id_to_ty_item,
419+
item_to_ty, _);
420+
421+
let vec[arg] args = vec();
422+
for (@ast.ty arg_ast_ty in variant.args) {
423+
auto arg_ty = ast_ty_to_ty(f, arg_ast_ty);
424+
args += vec(rec(mode=ast.alias, ty=arg_ty));
425+
}
426+
result_ty = plain_ty(ty_fn(args, plain_ty(ty_tag(tag_id))));
427+
}
428+
429+
item_to_ty.insert(variant.id, result_ty);
430+
}
431+
}
432+
404433
// First pass: collect all type item IDs.
405434
auto module = crate.node.module;
406435
auto id_to_ty_item = @common.new_def_hash[@ast.item]();
@@ -434,7 +463,9 @@ fn collect_item_types(@ast.crate crate) -> tup(@ast.crate, @ty_table) {
434463
case (ast.item_mod(_, _, _)) {
435464
result = it.node;
436465
}
437-
case (ast.item_tag(_, _, _, _)) {
466+
case (ast.item_tag(_, ?variants, _, ?tag_id)) {
467+
add_tag_variant_types(id_to_ty_item, item_to_ty, tag_id,
468+
variants);
438469
result = it.node;
439470
}
440471
}
@@ -1195,6 +1226,10 @@ fn check_expr(&fn_ctxt fcx, @ast.expr expr) -> @ast.expr {
11951226
check (fcx.ccx.item_types.contains_key(id));
11961227
t = fcx.ccx.item_types.get(id);
11971228
}
1229+
case (ast.def_variant(_, ?variant_id)) {
1230+
check (fcx.ccx.item_types.contains_key(variant_id));
1231+
t = fcx.ccx.item_types.get(variant_id);
1232+
}
11981233
case (_) {
11991234
// FIXME: handle other names.
12001235
fcx.ccx.sess.unimpl("definition variant for: "

0 commit comments

Comments
 (0)