Skip to content

Commit 5c20a8a

Browse files
committed
Make "cannot determine a type for this expression" non-fatal. Closes #621
1 parent c31472e commit 5c20a8a

File tree

1 file changed

+37
-17
lines changed

1 file changed

+37
-17
lines changed

src/comp/middle/typeck.rs

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,28 +1027,45 @@ mod writeback {
10271027
export resolve_type_vars_in_block;
10281028

10291029
fn resolve_type_vars_in_type(&@fn_ctxt fcx, &span sp, ty::t typ) ->
1030-
ty::t {
1031-
if (!ty::type_contains_vars(fcx.ccx.tcx, typ)) { ret typ; }
1030+
option::t[ty::t] {
1031+
if (!ty::type_contains_vars(fcx.ccx.tcx, typ)) { ret some(typ); }
10321032
alt (ty::unify::fixup_vars(fcx.ccx.tcx, fcx.var_bindings, typ)) {
1033-
case (fix_ok(?new_type)) { ret new_type; }
1033+
case (fix_ok(?new_type)) { ret some(new_type); }
10341034
case (fix_err(?vid)) {
1035-
fcx.ccx.tcx.sess.span_fatal(sp,
1035+
fcx.ccx.tcx.sess.span_err(sp,
10361036
"cannot determine a type \
10371037
for this expression");
1038+
ret none;
10381039
}
10391040
}
10401041
}
1041-
fn resolve_type_vars_for_node(&@fn_ctxt fcx, &span sp, ast::node_id id) {
1042+
fn resolve_type_vars_for_node(&@wb_ctxt wbcx,
1043+
&span sp, ast::node_id id) {
1044+
auto fcx = wbcx.fcx;
10421045
auto tpot = ty::node_id_to_ty_param_substs_opt_and_ty
10431046
(fcx.ccx.tcx, id);
1044-
auto new_ty = resolve_type_vars_in_type(fcx, sp, tpot._1);
1047+
auto new_ty = alt (resolve_type_vars_in_type(fcx, sp, tpot._1)) {
1048+
case (some(?t)) { t }
1049+
case (none) {
1050+
wbcx.success = false;
1051+
ret
1052+
}
1053+
};
10451054
auto new_substs_opt;
10461055
alt (tpot._0) {
10471056
case (none[vec[ty::t]]) { new_substs_opt = none[vec[ty::t]]; }
10481057
case (some[vec[ty::t]](?substs)) {
10491058
let vec[ty::t] new_substs = [];
10501059
for (ty::t subst in substs) {
1051-
new_substs += [resolve_type_vars_in_type(fcx, sp, subst)];
1060+
alt (resolve_type_vars_in_type(fcx, sp, subst)) {
1061+
case (some(?t)) {
1062+
new_substs += [t];
1063+
}
1064+
case (none) {
1065+
wbcx.success = false;
1066+
ret;
1067+
}
1068+
}
10521069
}
10531070
new_substs_opt = some[vec[ty::t]](new_substs);
10541071
}
@@ -1058,19 +1075,20 @@ mod writeback {
10581075

10591076
type wb_ctxt = rec(@fn_ctxt fcx,
10601077
// A flag to ignore contained items and lambdas
1061-
mutable bool ignore);
1078+
mutable bool ignore,
1079+
mutable bool success);
10621080

10631081
fn visit_stmt_pre(@wb_ctxt wbcx, &@ast::stmt s) {
1064-
resolve_type_vars_for_node(wbcx.fcx, s.span, ty::stmt_node_id(s));
1082+
resolve_type_vars_for_node(wbcx, s.span, ty::stmt_node_id(s));
10651083
}
10661084
fn visit_expr_pre(@wb_ctxt wbcx, &@ast::expr e) {
1067-
resolve_type_vars_for_node(wbcx.fcx, e.span, e.id);
1085+
resolve_type_vars_for_node(wbcx, e.span, e.id);
10681086
}
10691087
fn visit_block_pre(@wb_ctxt wbcx, &ast::block b) {
1070-
resolve_type_vars_for_node(wbcx.fcx, b.span, b.node.id);
1088+
resolve_type_vars_for_node(wbcx, b.span, b.node.id);
10711089
}
10721090
fn visit_pat_pre(@wb_ctxt wbcx, &@ast::pat p) {
1073-
resolve_type_vars_for_node(wbcx.fcx, p.span, p.id);
1091+
resolve_type_vars_for_node(wbcx, p.span, p.id);
10741092
}
10751093
fn visit_local_pre(@wb_ctxt wbcx, &@ast::local l) {
10761094
auto var_id = lookup_local(wbcx.fcx, l.span, l.node.id);
@@ -1105,11 +1123,12 @@ mod writeback {
11051123
&ast::fn_ident i, ast::node_id d) {
11061124
wbcx.ignore = false;
11071125
}
1108-
fn keep_going(@wb_ctxt wbcx) -> bool { ret !wbcx.ignore; }
1126+
fn keep_going(@wb_ctxt wbcx) -> bool { !wbcx.ignore && wbcx.success }
11091127

1110-
fn resolve_type_vars_in_block(&@fn_ctxt fcx, &ast::block block) {
1128+
fn resolve_type_vars_in_block(&@fn_ctxt fcx, &ast::block block) -> bool {
11111129
auto wbcx = @rec(fcx = fcx,
1112-
mutable ignore = false);
1130+
mutable ignore = false,
1131+
mutable success = true);
11131132
auto visit =
11141133
rec(keep_going=bind keep_going(wbcx),
11151134
visit_item_pre=bind visit_item_pre(wbcx, _),
@@ -1123,6 +1142,7 @@ mod writeback {
11231142
visit_local_pre=bind visit_local_pre(wbcx, _)
11241143
with walk::default_visitor());
11251144
walk::walk_block(visit, block);
1145+
ret wbcx.success;
11261146
}
11271147
}
11281148

@@ -2482,9 +2502,9 @@ fn check_fn(&@crate_ctxt ccx, &ast::fn_decl decl, ast::proto proto,
24822502
case (_) { }
24832503
}
24842504

2485-
writeback::resolve_type_vars_in_block(fcx, body);
2505+
auto success = writeback::resolve_type_vars_in_block(fcx, body);
24862506

2487-
if (option::is_some(body.node.expr)) {
2507+
if (success && option::is_some(body.node.expr)) {
24882508
auto tail_expr = option::get(body.node.expr);
24892509
auto tail_expr_ty = expr_ty(ccx.tcx, tail_expr);
24902510
// Have to exclude ty_nil to allow functions to end in

0 commit comments

Comments
 (0)