Skip to content

Commit 67e361a

Browse files
committed
Introduce a ty_infer ast node and use it instead of option::t[ty].
This actually basically makes things worse, since we get less nice type system guarentees but it will make doing type inferred blocks a fair deal less painful. I'm not /really/ happy about this...
1 parent 1e6074c commit 67e361a

File tree

6 files changed

+55
-49
lines changed

6 files changed

+55
-49
lines changed

src/comp/middle/typeck.rs

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,10 @@ fn ast_ty_to_ty(tcx: &ty::ctxt, getter: &ty_getter, ast_ty: &@ast::ty) ->
408408
}
409409
typ = ty::mk_constr(tcx, ast_ty_to_ty(tcx, getter, t), out_cs);
410410
}
411+
ast::ty_infer. {
412+
tcx.sess.span_bug(ast_ty.span,
413+
"found ty_infer in unexpected place");
414+
}
411415
}
412416
alt cname {
413417
none. {/* no-op */ }
@@ -429,6 +433,23 @@ fn ast_ty_to_ty_crate(ccx: @crate_ctxt, ast_ty: &@ast::ty) -> ty::t {
429433
ret ast_ty_to_ty(ccx.tcx, f, ast_ty);
430434
}
431435

436+
// A wrapper around ast_ty_to_ty_crate that handles ty_infer.
437+
fn ast_ty_to_ty_crate_infer(ccx: @crate_ctxt, ast_ty: &@ast::ty)
438+
-> option::t[ty::t] {
439+
alt ast_ty.node {
440+
ast::ty_infer. { none }
441+
_ { some(ast_ty_to_ty_crate(ccx, ast_ty)) }
442+
}
443+
}
444+
// A wrapper around ast_ty_to_ty_infer that generates a new type variable if
445+
// there isn't a fixed type.
446+
fn ast_ty_to_ty_crate_tyvar(fcx: &@fn_ctxt, ast_ty: &@ast::ty) -> ty::t {
447+
alt ast_ty_to_ty_crate_infer(fcx.ccx, ast_ty) {
448+
some(ty) { ty }
449+
none. { next_ty_var(fcx) }
450+
}
451+
}
452+
432453

433454
// Functions that write types into the node type table.
434455
mod write {
@@ -1269,17 +1290,8 @@ fn gather_locals(ccx: &@crate_ctxt, f: &ast::_fn, id: &ast::node_id,
12691290

12701291
// Add explicitly-declared locals.
12711292
let visit_local = lambda(local: &@ast::local, e: &(), v: &visit::vt[()]) {
1272-
alt local.node.ty {
1273-
none. {
1274-
// Auto slot.
1275-
assign(local.node.id, ident_for_local(local), none);
1276-
}
1277-
some(ast_ty) {
1278-
// Explicitly typed slot.
1279-
let local_ty = ast_ty_to_ty_crate(ccx, ast_ty);
1280-
assign(local.node.id, ident_for_local(local), some(local_ty));
1281-
}
1282-
}
1293+
let local_ty = ast_ty_to_ty_crate_infer(ccx, local.node.ty);
1294+
assign(local.node.id, ident_for_local(local), local_ty);
12831295
visit::visit_local(local, e, v);
12841296
};
12851297

@@ -2339,10 +2351,9 @@ fn check_expr(fcx: &@fn_ctxt, expr: &@ast::expr) -> bool {
23392351
}
23402352
ast::expr_port(typ) {
23412353
let t = next_ty_var(fcx);
2342-
alt typ {
2354+
alt ast_ty_to_ty_crate_infer(fcx.ccx, typ) {
23432355
some(_t) {
2344-
demand::simple(fcx, expr.span, ast_ty_to_ty_crate(fcx.ccx, _t),
2345-
t);
2356+
demand::simple(fcx, expr.span, _t, t);
23462357
}
23472358
none. { }
23482359
}

src/comp/syntax/ast.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ tag init_op { init_assign; init_move; }
260260
261261
type initializer = {op: init_op, expr: @expr};
262262
263-
type local_ = {ty: option::t[@ty],
263+
type local_ = {ty: @ty,
264264
pat: @pat,
265265
init: option::t[initializer],
266266
id: node_id};
@@ -335,7 +335,7 @@ tag expr_ {
335335
/* FIXME Would be nice if expr_check desugared
336336
to expr_if_check. */
337337
expr_if_check(@expr, blk, option::t[@expr]);
338-
expr_port(option::t[@ty]);
338+
expr_port(@ty);
339339
expr_chan(@expr);
340340
expr_anon_obj(anon_obj);
341341
expr_mac(mac);
@@ -451,6 +451,10 @@ tag ty_ {
451451
ty_type;
452452
ty_constr(@ty, [@ty_constr]);
453453
ty_mac(mac);
454+
// ty_infer means the type should be inferred instead of it having been
455+
// specified. This should only appear at the "top level" of a type and not
456+
// nested in one.
457+
ty_infer;
454458
}
455459

456460

src/comp/syntax/fold.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -421,11 +421,8 @@ fn noop_fold_expr(e: &expr_, fld: ast_fold) -> expr_ {
421421
expr_if_check(fld.fold_expr(cond), fld.fold_block(tr),
422422
option::map(fld.fold_expr, fl))
423423
}
424-
expr_port(ot) {
425-
expr_port(alt ot {
426-
option::some(t) { option::some(fld.fold_ty(t)) }
427-
option::none. { option::none }
428-
})
424+
expr_port(t) {
425+
expr_port(fld.fold_ty(t))
429426
}
430427
expr_chan(e) { expr_chan(fld.fold_expr(e)) }
431428
expr_anon_obj(ao) { expr_anon_obj(fold_anon_obj(ao)) }
@@ -487,7 +484,7 @@ fn noop_fold_path(p: &path_, fld: ast_fold) -> path_ {
487484
}
488485

489486
fn noop_fold_local(l: &local_, fld: ast_fold) -> local_ {
490-
ret {ty: option::map(fld.fold_ty, l.ty),
487+
ret {ty: fld.fold_ty(l.ty),
491488
pat: fld.fold_pat(l.pat),
492489
init: alt l.init {
493490
option::none[initializer]. { l.init }

src/comp/syntax/parse/parser.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -952,10 +952,10 @@ fn parse_bottom_expr(p: &parser) -> @ast::expr {
952952
ex = ast::expr_be(e);
953953
} else { p.fatal("Non-call expression in tail call"); }
954954
} else if (eat_word(p, "port")) {
955-
let ty = none;
955+
let ty = @spanned(lo, hi, ast::ty_infer);
956956
if token::LBRACKET == p.peek() {
957957
expect(p, token::LBRACKET);
958-
ty = some(parse_ty(p));
958+
ty = parse_ty(p);
959959
expect(p, token::RBRACKET);
960960
}
961961
expect(p, token::LPAREN);
@@ -1481,8 +1481,8 @@ fn parse_pat(p: &parser) -> @ast::pat {
14811481
fn parse_local(p: &parser, allow_init: bool) -> @ast::local {
14821482
let lo = p.get_lo_pos();
14831483
let pat = parse_pat(p);
1484-
let ty = none;
1485-
if eat(p, token::COLON) { ty = some(parse_ty(p)); }
1484+
let ty = @spanned(lo, lo, ast::ty_infer);
1485+
if eat(p, token::COLON) { ty = parse_ty(p); }
14861486
let init = if allow_init { parse_initializer(p) } else { none };
14871487
ret @spanned(lo, p.get_last_hi_pos(),
14881488
{ty: ty,

src/comp/syntax/print/pprust.rs

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -966,11 +966,11 @@ fn print_expr(s: &ps, expr: &@ast::expr) {
966966
pclose(s);
967967
}
968968
ast::expr_mac(m) { print_mac(s, m); }
969-
ast::expr_port(ot) {
969+
ast::expr_port(t) {
970970
word(s.s, "port");
971-
alt ot {
972-
some(t) { word(s.s, "["); print_type(s, *t); word(s.s, "]"); }
973-
none. { }
971+
alt t.node {
972+
ast::ty_infer. { }
973+
_ { word(s.s, "["); print_type(s, *t); word(s.s, "]"); }
974974
}
975975
popen(s);
976976
pclose(s);
@@ -1040,6 +1040,14 @@ fn print_expr_parens_if_unary(s: &ps, ex: &@ast::expr) {
10401040
if parens { pclose(s); }
10411041
}
10421042

1043+
fn print_local_decl(s: &ps, loc: &@ast::local) {
1044+
print_pat(s, loc.node.pat);
1045+
alt loc.node.ty.node {
1046+
ast::ty_infer. { }
1047+
_ { word_space(s, ":"); print_type(s, *loc.node.ty); }
1048+
}
1049+
}
1050+
10431051
fn print_decl(s: &ps, decl: &@ast::decl) {
10441052
maybe_print_comment(s, decl.span.lo);
10451053
alt decl.node {
@@ -1049,14 +1057,7 @@ fn print_decl(s: &ps, decl: &@ast::decl) {
10491057
word_nbsp(s, "let");
10501058
fn print_local(s: &ps, loc: &@ast::local) {
10511059
ibox(s, indent_unit);
1052-
print_pat(s, loc.node.pat);
1053-
alt loc.node.ty {
1054-
some(ty) {
1055-
word_space(s, ":");
1056-
print_type(s, *ty);
1057-
}
1058-
_ { }
1059-
}
1060+
print_local_decl(s, loc);
10601061
end(s);
10611062
alt loc.node.init {
10621063
some(init) {
@@ -1080,11 +1081,7 @@ fn print_decl(s: &ps, decl: &@ast::decl) {
10801081
fn print_ident(s: &ps, ident: &ast::ident) { word(s.s, ident); }
10811082

10821083
fn print_for_decl(s: &ps, loc: &@ast::local, coll: &@ast::expr) {
1083-
print_pat(s, loc.node.pat);
1084-
alt loc.node.ty {
1085-
some(t) { word_space(s, ":"); print_type(s, *t); }
1086-
none. { }
1087-
}
1084+
print_local_decl(s, loc);
10881085
space(s.s);
10891086
word_space(s, "in");
10901087
print_expr(s, coll);

src/comp/syntax/visit.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ fn visit_view_item[E](vi: &@view_item, e: &E, v: &vt[E]) { }
7979

8080
fn visit_local[E](loc: &@local, e: &E, v: &vt[E]) {
8181
v.visit_pat(loc.node.pat, e, v);
82-
alt loc.node.ty { none. { } some(t) { v.visit_ty(t, e, v); } }
82+
v.visit_ty(loc.node.ty, e, v);
8383
alt loc.node.init { none. { } some(i) { v.visit_expr(i.expr, e, v); } }
8484
}
8585

@@ -154,13 +154,10 @@ fn visit_ty[E](t: &@ty, e: &E, v: &vt[E]) {
154154
v.visit_constr(tc.node.path, tc.span, tc.node.id, e, v);
155155
}
156156
}
157+
ty_infer. {/* no-op */ }
157158
}
158159
}
159160

160-
fn visit_ty_opt[E](ot: &option::t[@ty], e: &E, v: &vt[E]) {
161-
alt ot { none. { } some(t) { v.visit_ty(t, e, v); } }
162-
}
163-
164161
fn visit_constr[E](operator: &path, sp: &span, id: node_id, e: &E,
165162
v: &vt[E]) {
166163
// default
@@ -313,7 +310,7 @@ fn visit_expr[E](ex: &@expr, e: &E, v: &vt[E]) {
313310
expr_log(_, x) { v.visit_expr(x, e, v); }
314311
expr_check(_, x) { v.visit_expr(x, e, v); }
315312
expr_assert(x) { v.visit_expr(x, e, v); }
316-
expr_port(t) { visit_ty_opt(t, e, v); }
313+
expr_port(t) { v.visit_ty(t, e, v); }
317314
expr_chan(x) { v.visit_expr(x, e, v); }
318315
expr_anon_obj(anon_obj) {
319316
alt anon_obj.fields {

0 commit comments

Comments
 (0)