new cap clause syntax
This commit is contained in:
parent
8affc78e8a
commit
50ec6bd2c3
31 changed files with 447 additions and 360 deletions
|
|
@ -182,8 +182,8 @@ fn cant_copy(cx: ctx, b: binding) -> bool {
|
|||
fn local_id_for_args(cx: ctx, args: [@ast::expr]) -> uint {
|
||||
for vec::each(args) {|arg|
|
||||
alt arg.node {
|
||||
ast::expr_fn_block(decl, _) | ast::expr_fn(_, decl, _, _) |
|
||||
ast::expr_loop_body(@{node: ast::expr_fn_block(decl, _), _}) {
|
||||
ast::expr_fn_block(decl, _, _) | ast::expr_fn(_, decl, _, _) |
|
||||
ast::expr_loop_body(@{node: ast::expr_fn_block(decl, _, _), _}) {
|
||||
if decl.inputs.len() > 0u {
|
||||
ret local_id_of_node(cx, decl.inputs[0].id);
|
||||
}
|
||||
|
|
@ -216,7 +216,7 @@ fn check_call(cx: @ctx, sc: scope, f: @ast::expr, args: [@ast::expr],
|
|||
ast::by_ref | ast::by_val | ast::by_move | ast::by_copy {}
|
||||
}
|
||||
alt arg.node {
|
||||
ast::expr_fn_block(_, _) { blocks += [arg]; }
|
||||
ast::expr_fn_block(*) { blocks += [arg]; }
|
||||
ast::expr_loop_body(b) { blocks += [b]; }
|
||||
_ {
|
||||
let root_var = path_def_id(*cx, root.ex);
|
||||
|
|
@ -267,7 +267,7 @@ fn check_call(cx: @ctx, sc: scope, f: @ast::expr, args: [@ast::expr],
|
|||
let mut_alias =
|
||||
(ast::by_mutbl_ref == ty::arg_mode(cx.tcx, arg_t));
|
||||
alt args[i].node {
|
||||
ast::expr_fn_block(_, _) | ast::expr_loop_body(_) {}
|
||||
ast::expr_fn_block(*) | ast::expr_loop_body(_) {}
|
||||
_ {
|
||||
if i != j && ty_can_unsafely_include(
|
||||
*cx, unsafe_ty, arg_t.ty, mut_alias) &&
|
||||
|
|
@ -306,7 +306,7 @@ fn check_call(cx: @ctx, sc: scope, f: @ast::expr, args: [@ast::expr],
|
|||
let inner_sc = {bs: bindings + sc.bs, invalid: sc.invalid};
|
||||
for blocks.each {|blk|
|
||||
alt check blk.node {
|
||||
ast::expr_fn_block(_, body) {
|
||||
ast::expr_fn_block(_, body, _) {
|
||||
v.visit_block(body, inner_sc, v);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -36,7 +36,7 @@ fn check_capture_clause(tcx: ty::ctxt,
|
|||
let freevars = freevars::get_freevars(tcx, fn_expr_id);
|
||||
let seen_defs = map::int_hash();
|
||||
|
||||
let check_capture_item = fn@(&&cap_item: @ast::capture_item) {
|
||||
let check_capture_item = fn@(cap_item: ast::capture_item) {
|
||||
let cap_def = tcx.def_map.get(cap_item.id);
|
||||
if !vec::any(*freevars, {|fv| fv.def == cap_def}) {
|
||||
tcx.sess.span_warn(
|
||||
|
|
@ -54,36 +54,19 @@ fn check_capture_clause(tcx: ty::ctxt,
|
|||
}
|
||||
};
|
||||
|
||||
let check_not_upvar = fn@(&&cap_item: @ast::capture_item) {
|
||||
alt tcx.def_map.get(cap_item.id) {
|
||||
ast::def_upvar(_, _, _) {
|
||||
tcx.sess.span_err(
|
||||
cap_item.span,
|
||||
#fmt("upvars (like '%s') cannot be moved into a closure",
|
||||
cap_item.name));
|
||||
}
|
||||
_ {}
|
||||
}
|
||||
};
|
||||
|
||||
let check_block_captures = fn@(v: [@ast::capture_item]) {
|
||||
if check vec::is_not_empty(v) {
|
||||
let cap_item0 = vec::head(v);
|
||||
alt fn_proto {
|
||||
ast::proto_any | ast::proto_block {
|
||||
if vec::is_not_empty(cap_clause) {
|
||||
let cap_item0 = vec::head(cap_clause);
|
||||
tcx.sess.span_err(
|
||||
cap_item0.span,
|
||||
"cannot capture values explicitly with a block closure");
|
||||
}
|
||||
};
|
||||
|
||||
alt fn_proto {
|
||||
ast::proto_any | ast::proto_block {
|
||||
check_block_captures(cap_clause.copies);
|
||||
check_block_captures(cap_clause.moves);
|
||||
}
|
||||
ast::proto_bare | ast::proto_box | ast::proto_uniq {
|
||||
vec::iter(cap_clause.copies, check_capture_item);
|
||||
vec::iter(cap_clause.moves, check_capture_item);
|
||||
vec::iter(cap_clause.moves, check_not_upvar);
|
||||
for cap_clause.each { |cap_item|
|
||||
check_capture_item(cap_item);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -95,23 +78,30 @@ fn compute_capture_vars(tcx: ty::ctxt,
|
|||
let freevars = freevars::get_freevars(tcx, fn_expr_id);
|
||||
let cap_map = map::int_hash();
|
||||
|
||||
vec::iter(cap_clause.copies) { |cap_item|
|
||||
// first add entries for anything explicitly named in the cap clause
|
||||
|
||||
for cap_clause.each { |cap_item|
|
||||
let cap_def = tcx.def_map.get(cap_item.id);
|
||||
let cap_def_id = ast_util::def_id_of_def(cap_def).node;
|
||||
if vec::any(*freevars, {|fv| fv.def == cap_def}) {
|
||||
cap_map.insert(cap_def_id, { def:cap_def, mode:cap_copy });
|
||||
if cap_item.is_move {
|
||||
// if we are moving the value in, but it's not actually used,
|
||||
// must drop it.
|
||||
if vec::any(*freevars, {|fv| fv.def == cap_def}) {
|
||||
cap_map.insert(cap_def_id, { def:cap_def, mode:cap_move });
|
||||
} else {
|
||||
cap_map.insert(cap_def_id, { def:cap_def, mode:cap_drop });
|
||||
}
|
||||
} else {
|
||||
// if we are copying the value in, but it's not actually used,
|
||||
// just ignore it.
|
||||
if vec::any(*freevars, {|fv| fv.def == cap_def}) {
|
||||
cap_map.insert(cap_def_id, { def:cap_def, mode:cap_copy });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
vec::iter(cap_clause.moves) { |cap_item|
|
||||
let cap_def = tcx.def_map.get(cap_item.id);
|
||||
let cap_def_id = ast_util::def_id_of_def(cap_def).node;
|
||||
if vec::any(*freevars, {|fv| fv.def == cap_def}) {
|
||||
cap_map.insert(cap_def_id, { def:cap_def, mode:cap_move });
|
||||
} else {
|
||||
cap_map.insert(cap_def_id, { def:cap_def, mode:cap_drop });
|
||||
}
|
||||
}
|
||||
// now go through anything that is referenced but was not explicitly
|
||||
// named and add that
|
||||
|
||||
let implicit_mode = alt fn_proto {
|
||||
ast::proto_any | ast::proto_block { cap_ref }
|
||||
|
|
|
|||
|
|
@ -21,10 +21,10 @@ fn check_crate(tcx: ty::ctxt, crate: @crate) {
|
|||
expr_fn(_, _, _, _) {
|
||||
visit::visit_expr(e, {in_loop: false, can_ret: true}, v);
|
||||
}
|
||||
expr_fn_block(_, b) {
|
||||
expr_fn_block(_, b, _) {
|
||||
v.visit_block(b, {in_loop: false, can_ret: false}, v);
|
||||
}
|
||||
expr_loop_body(@{node: expr_fn_block(_, b), _}) {
|
||||
expr_loop_body(@{node: expr_fn_block(_, b, _), _}) {
|
||||
let blk = is_blockish(ty::ty_fn_proto(ty::expr_ty(tcx, e)));
|
||||
v.visit_block(b, {in_loop: true, can_ret: blk}, v);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -40,12 +40,12 @@ fn collect_freevars(def_map: resolve::def_map, blk: ast::blk)
|
|||
|
||||
let walk_expr = fn@(expr: @ast::expr, &&depth: int, v: visit::vt<int>) {
|
||||
alt expr.node {
|
||||
ast::expr_fn(proto, decl, _, captures) {
|
||||
ast::expr_fn(proto, decl, _, _) {
|
||||
if proto != ast::proto_bare {
|
||||
visit::visit_expr(expr, depth + 1, v);
|
||||
}
|
||||
}
|
||||
ast::expr_fn_block(_, _) {
|
||||
ast::expr_fn_block(_, _, _) {
|
||||
visit::visit_expr(expr, depth + 1, v);
|
||||
}
|
||||
ast::expr_path(path) {
|
||||
|
|
|
|||
|
|
@ -117,16 +117,14 @@ fn check_fn_cap_clause(cx: ctx,
|
|||
});
|
||||
//log("freevar_ids", freevar_ids);
|
||||
with_appropriate_checker(cx, id) { |checker|
|
||||
let check_var = fn@(&&cap_item: @capture_item) {
|
||||
for cap_clause.each { |cap_item|
|
||||
let cap_def = cx.tcx.def_map.get(cap_item.id);
|
||||
let cap_def_id = ast_util::def_id_of_def(cap_def).node;
|
||||
if !vec::contains(freevar_ids, cap_def_id) {
|
||||
let ty = ty::node_id_to_type(cx.tcx, cap_def_id);
|
||||
checker(cx, ty, cap_item.span);
|
||||
}
|
||||
};
|
||||
vec::iter(cap_clause.copies, check_var);
|
||||
vec::iter(cap_clause.moves, check_var);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -227,8 +225,8 @@ fn check_expr(e: @expr, cx: ctx, v: visit::vt<ctx>) {
|
|||
}
|
||||
}
|
||||
}
|
||||
expr_fn(_, _, _, cap_clause) {
|
||||
check_fn_cap_clause(cx, e.id, *cap_clause);
|
||||
expr_fn(_, _, _, cap_clause) | expr_fn_block(_, _, cap_clause) {
|
||||
check_fn_cap_clause(cx, e.id, cap_clause);
|
||||
}
|
||||
_ { }
|
||||
}
|
||||
|
|
|
|||
|
|
@ -154,12 +154,15 @@ fn visit_expr(ex: @expr, cx: ctx, v: visit::vt<ctx>) {
|
|||
v.visit_expr(dest, cx, v);
|
||||
clear_if_path(cx, dest, v, true);
|
||||
}
|
||||
expr_fn_block(_, _, cap_clause) |
|
||||
expr_fn(_, _, _, cap_clause) {
|
||||
// n.b.: safe to ignore copies, as if they are unused
|
||||
// then they are ignored, otherwise they will show up
|
||||
// as freevars in the body.
|
||||
vec::iter(cap_clause.moves) {|ci|
|
||||
clear_def_if_local(cx, cx.def_map.get(ci.id), false);
|
||||
for cap_clause.each { |ci|
|
||||
if ci.is_move {
|
||||
clear_def_if_local(cx, cx.def_map.get(ci.id), false);
|
||||
}
|
||||
}
|
||||
visit::visit_expr(ex, cx, v);
|
||||
}
|
||||
|
|
@ -169,7 +172,7 @@ fn visit_expr(ex: @expr, cx: ctx, v: visit::vt<ctx>) {
|
|||
let arg_ts = ty::ty_fn_args(ty::expr_ty(cx.tcx, f));
|
||||
vec::iter2(args, arg_ts) {|arg, arg_t|
|
||||
alt arg.node {
|
||||
expr_fn(_, _, _, _) | expr_fn_block(_, _)
|
||||
expr_fn(*) | expr_fn_block(*)
|
||||
if is_blockish(ty::ty_fn_proto(arg_t.ty)) {
|
||||
fns += [arg];
|
||||
}
|
||||
|
|
|
|||
|
|
@ -199,14 +199,17 @@ fn visit_expr(ex: @expr, &&cx: @ctx, v: visit::vt<@ctx>) {
|
|||
expr_assign(dest, src) | expr_assign_op(_, dest, src) {
|
||||
check_lval(cx, dest, msg_assign);
|
||||
}
|
||||
expr_fn(_, _, _, cap) {
|
||||
for cap.moves.each {|moved|
|
||||
let def = cx.tcx.def_map.get(moved.id);
|
||||
alt is_illegal_to_modify_def(cx, def, msg_move_out) {
|
||||
some(name) { mk_err(cx, moved.span, msg_move_out, moved.name); }
|
||||
_ { }
|
||||
expr_fn(_, _, _, cap_clause) | expr_fn_block(_, _, cap_clause) {
|
||||
for cap_clause.each { |cap_item|
|
||||
if cap_item.is_move {
|
||||
let def = cx.tcx.def_map.get(cap_item.id);
|
||||
alt is_illegal_to_modify_def(cx, def, msg_move_out) {
|
||||
some(name) { mk_err(cx, cap_item.span,
|
||||
msg_move_out, name); }
|
||||
_ { }
|
||||
}
|
||||
cx.mutbl_map.insert(ast_util::def_id_of_def(def).node, ());
|
||||
}
|
||||
cx.mutbl_map.insert(ast_util::def_id_of_def(def).node, ());
|
||||
}
|
||||
}
|
||||
_ { }
|
||||
|
|
|
|||
|
|
@ -286,7 +286,7 @@ fn resolve_pat(pat: @ast::pat, cx: ctxt, visitor: visit::vt<ctxt>) {
|
|||
fn resolve_expr(expr: @ast::expr, cx: ctxt, visitor: visit::vt<ctxt>) {
|
||||
record_parent(cx, expr.id);
|
||||
alt expr.node {
|
||||
ast::expr_fn(_, _, _, _) | ast::expr_fn_block(_, _) {
|
||||
ast::expr_fn(*) | ast::expr_fn_block(*) {
|
||||
let new_cx = {parent: some(expr.id) with cx};
|
||||
visit::visit_expr(expr, new_cx, visitor);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -390,7 +390,7 @@ fn check_unused_imports(e: @env, level: lint::level) {
|
|||
};
|
||||
}
|
||||
|
||||
fn resolve_capture_item(e: @env, sc: scopes, &&cap_item: @ast::capture_item) {
|
||||
fn resolve_capture_item(e: @env, sc: scopes, cap_item: ast::capture_item) {
|
||||
let dcur = lookup_in_scope_strict(
|
||||
*e, sc, cap_item.span, cap_item.name, ns_val);
|
||||
maybe_insert(e, cap_item.id, dcur);
|
||||
|
|
@ -453,10 +453,11 @@ fn resolve_names(e: @env, c: @ast::crate) {
|
|||
maybe_insert(e, exp.id,
|
||||
lookup_path_strict(*e, sc, exp.span, p, ns_val));
|
||||
}
|
||||
ast::expr_fn(_, _, _, cap_clause) {
|
||||
let rci = bind resolve_capture_item(e, sc, _);
|
||||
vec::iter(cap_clause.copies, rci);
|
||||
vec::iter(cap_clause.moves, rci);
|
||||
ast::expr_fn(_, _, _, cap_clause) |
|
||||
ast::expr_fn_block(_, _, cap_clause) {
|
||||
for cap_clause.each { |ci|
|
||||
resolve_capture_item(e, sc, ci);
|
||||
}
|
||||
}
|
||||
_ { }
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2544,11 +2544,11 @@ fn trans_cast(cx: block, e: @ast::expr, id: ast::node_id,
|
|||
fn trans_loop_body(bcx: block, e: @ast::expr, ret_flag: option<ValueRef>,
|
||||
dest: dest) -> block {
|
||||
alt check e.node {
|
||||
ast::expr_loop_body(b@@{node: ast::expr_fn_block(decl, body), _}) {
|
||||
ast::expr_loop_body(b@@{node: ast::expr_fn_block(decl, body, cap), _}) {
|
||||
alt check ty::get(expr_ty(bcx, e)).struct {
|
||||
ty::ty_fn({proto, _}) {
|
||||
closure::trans_expr_fn(bcx, proto, decl, body, e.span, b.id,
|
||||
{copies: [], moves: []}, some(ret_flag),
|
||||
cap, some(ret_flag),
|
||||
dest)
|
||||
}
|
||||
}
|
||||
|
|
@ -2800,7 +2800,7 @@ fn trans_call_inner(in_cx: block, fn_expr_ty: ty::t, ret_ty: ty::t,
|
|||
-> block {
|
||||
let ret_in_loop = alt args {
|
||||
arg_exprs(args) { args.len() > 0u && alt vec::last(args).node {
|
||||
ast::expr_loop_body(@{node: ast::expr_fn_block(_, body), _}) {
|
||||
ast::expr_loop_body(@{node: ast::expr_fn_block(_, body, _), _}) {
|
||||
body_contains_ret(body)
|
||||
}
|
||||
_ { false }
|
||||
|
|
@ -3166,15 +3166,15 @@ fn trans_expr(bcx: block, e: @ast::expr, dest: dest) -> block {
|
|||
ast::expr_addr_of(_, x) { ret trans_addr_of(bcx, x, dest); }
|
||||
ast::expr_fn(proto, decl, body, cap_clause) {
|
||||
ret closure::trans_expr_fn(bcx, proto, decl, body, e.span, e.id,
|
||||
*cap_clause, none, dest);
|
||||
cap_clause, none, dest);
|
||||
}
|
||||
ast::expr_fn_block(decl, body) {
|
||||
ast::expr_fn_block(decl, body, cap_clause) {
|
||||
alt check ty::get(expr_ty(bcx, e)).struct {
|
||||
ty::ty_fn({proto, _}) {
|
||||
#debug("translating fn_block %s with type %s",
|
||||
expr_to_str(e), ty_to_str(tcx, expr_ty(bcx, e)));
|
||||
ret closure::trans_expr_fn(bcx, proto, decl, body, e.span, e.id,
|
||||
{copies: [], moves: []}, none, dest);
|
||||
cap_clause, none, dest);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -772,7 +772,7 @@ fn create_function(fcx: fn_ctxt) -> @metadata<subprogram_md> {
|
|||
ast::expr_fn(_, decl, _, _) {
|
||||
(dbg_cx.names("fn"), decl.output, expr.id)
|
||||
}
|
||||
ast::expr_fn_block(decl, _) {
|
||||
ast::expr_fn_block(decl, _, _) {
|
||||
(dbg_cx.names("fn"), decl.output, expr.id)
|
||||
}
|
||||
_ { fcx.ccx.sess.span_bug(expr.span, "create_function: \
|
||||
|
|
|
|||
|
|
@ -160,7 +160,7 @@ fn mark_for_expr(cx: ctx, e: @expr) {
|
|||
}
|
||||
}
|
||||
}
|
||||
expr_fn(_, _, _, _) | expr_fn_block(_, _) {
|
||||
expr_fn(*) | expr_fn_block(*) {
|
||||
alt ty::ty_fn_proto(ty::expr_ty(cx.ccx.tcx, e)) {
|
||||
proto_bare | proto_any | proto_uniq {}
|
||||
proto_box | proto_block {
|
||||
|
|
|
|||
|
|
@ -340,23 +340,20 @@ fn find_pre_post_expr(fcx: fn_ctxt, e: @expr) {
|
|||
expr_log(_, lvl, arg) {
|
||||
find_pre_post_exprs(fcx, [lvl, arg], e.id);
|
||||
}
|
||||
expr_fn(_, _, _, cap_clause) {
|
||||
expr_fn(_, _, _, cap_clause) | expr_fn_block(_, _, cap_clause) {
|
||||
find_pre_post_expr_fn_upvars(fcx, e);
|
||||
|
||||
let use_cap_item = fn@(&&cap_item: @capture_item) {
|
||||
for cap_clause.each { |cap_item|
|
||||
let d = local_node_id_to_local_def_id(fcx, cap_item.id);
|
||||
option::iter(d, { |id| use_var(fcx, id) });
|
||||
};
|
||||
vec::iter(cap_clause.copies, use_cap_item);
|
||||
vec::iter(cap_clause.moves, use_cap_item);
|
||||
|
||||
vec::iter(cap_clause.moves) { |cap_item|
|
||||
log(debug, ("forget_in_postcond: ", cap_item));
|
||||
forget_in_postcond(fcx, e.id, cap_item.id);
|
||||
}
|
||||
}
|
||||
expr_fn_block(_, _) {
|
||||
find_pre_post_expr_fn_upvars(fcx, e);
|
||||
|
||||
for cap_clause.each { |cap_item|
|
||||
if cap_item.is_move {
|
||||
log(debug, ("forget_in_postcond: ", cap_item));
|
||||
forget_in_postcond(fcx, e.id, cap_item.id);
|
||||
}
|
||||
}
|
||||
}
|
||||
expr_block(b) {
|
||||
find_pre_post_block(fcx, b);
|
||||
|
|
|
|||
|
|
@ -363,8 +363,10 @@ fn find_pre_post_state_cap_clause(fcx: fn_ctxt, e_id: node_id,
|
|||
let ccx = fcx.ccx;
|
||||
let pres_changed = set_prestate_ann(ccx, e_id, pres);
|
||||
let post = tritv_clone(pres);
|
||||
vec::iter(cap_clause.moves) { |cap_item|
|
||||
forget_in_poststate(fcx, post, cap_item.id);
|
||||
for cap_clause.each { |cap_item|
|
||||
if cap_item.is_move {
|
||||
forget_in_poststate(fcx, post, cap_item.id);
|
||||
}
|
||||
}
|
||||
ret set_poststate_ann(ccx, e_id, post) || pres_changed;
|
||||
}
|
||||
|
|
@ -418,9 +420,11 @@ fn find_pre_post_state_expr(fcx: fn_ctxt, pres: prestate, e: @expr) -> bool {
|
|||
expr_mac(_) { fcx.ccx.tcx.sess.bug("unexpanded macro"); }
|
||||
expr_lit(l) { ret pure_exp(fcx.ccx, e.id, pres); }
|
||||
expr_fn(_, _, _, cap_clause) {
|
||||
ret find_pre_post_state_cap_clause(fcx, e.id, pres, *cap_clause);
|
||||
ret find_pre_post_state_cap_clause(fcx, e.id, pres, cap_clause);
|
||||
}
|
||||
expr_fn_block(_, _, cap_clause) {
|
||||
ret find_pre_post_state_cap_clause(fcx, e.id, pres, cap_clause);
|
||||
}
|
||||
expr_fn_block(_, _) { ret pure_exp(fcx.ccx, e.id, pres); }
|
||||
expr_block(b) {
|
||||
ret find_pre_post_state_block(fcx, pres, b) |
|
||||
set_prestate_ann(fcx.ccx, e.id, pres) |
|
||||
|
|
|
|||
|
|
@ -275,7 +275,8 @@ fn instantiate_path(fcx: @fn_ctxt,
|
|||
fcx.write_ty_substs(id, tpt.ty, substs);
|
||||
}
|
||||
|
||||
// Type tests
|
||||
// Resolves `typ` by a single level if `typ` is a type variable. If no
|
||||
// resolution is possible, then an error is reported.
|
||||
fn structurally_resolved_type(fcx: @fn_ctxt, sp: span, tp: ty::t) -> ty::t {
|
||||
alt infer::resolve_shallow(fcx.infcx, tp, false) {
|
||||
result::ok(t_s) if !ty::type_is_var(t_s) { ret t_s; }
|
||||
|
|
@ -286,7 +287,6 @@ fn structurally_resolved_type(fcx: @fn_ctxt, sp: span, tp: ty::t) -> ty::t {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
// Returns the one-level-deep structure of the given type.
|
||||
fn structure_of(fcx: @fn_ctxt, sp: span, typ: ty::t) -> ty::sty {
|
||||
ty::get(structurally_resolved_type(fcx, sp, typ)).struct
|
||||
|
|
@ -689,7 +689,7 @@ fn ast_ty_to_ty<AC: ast_conv, RS: region_scope copy>(
|
|||
ty::mk_rec(tcx, flds)
|
||||
}
|
||||
ast::ty_fn(proto, decl) {
|
||||
ty::mk_fn(tcx, ty_of_fn_decl(self, rscope, proto, decl))
|
||||
ty::mk_fn(tcx, ty_of_fn_decl(self, rscope, proto, decl, none))
|
||||
}
|
||||
ast::ty_path(path, id) {
|
||||
let a_def = alt tcx.def_map.find(id) {
|
||||
|
|
@ -777,7 +777,13 @@ fn ast_ty_to_ty<AC: ast_conv, RS: region_scope copy>(
|
|||
ty::mk_constr(tcx, ast_ty_to_ty(self, rscope, t), out_cs)
|
||||
}
|
||||
ast::ty_infer {
|
||||
self.ty_infer(ast_ty.span)
|
||||
// ty_infer should only appear as the type of arguments or return
|
||||
// values in a fn_expr, or as the type of local variables. Both of
|
||||
// these cases are handled specially and should not descend into this
|
||||
// routine.
|
||||
self.tcx().sess.span_bug(
|
||||
ast_ty.span,
|
||||
"found `ty_infer` in unexpected place");
|
||||
}
|
||||
ast::ty_mac(_) {
|
||||
tcx.sess.span_bug(ast_ty.span,
|
||||
|
|
@ -850,7 +856,8 @@ fn ty_of_item(ccx: @crate_ctxt, it: @ast::item)
|
|||
}
|
||||
ast::item_fn(decl, tps, _) {
|
||||
let bounds = ty_param_bounds(ccx, tps);
|
||||
let tofd = ty_of_fn_decl(ccx, empty_rscope, ast::proto_bare, decl);
|
||||
let tofd = ty_of_fn_decl(ccx, empty_rscope, ast::proto_bare,
|
||||
decl, none);
|
||||
let tpt = {bounds: bounds,
|
||||
rp: ast::rp_none, // functions do not have a self
|
||||
ty: ty::mk_fn(ccx.tcx, tofd)};
|
||||
|
|
@ -884,7 +891,8 @@ fn ty_of_item(ccx: @crate_ctxt, it: @ast::item)
|
|||
}
|
||||
ast::item_res(decl, tps, _, _, _, rp) {
|
||||
let {bounds, substs} = mk_substs(ccx, tps, rp);
|
||||
let t_arg = ty_of_arg(ccx, type_rscope(rp), decl.inputs[0]);
|
||||
let t_arg = ty_of_arg(ccx, type_rscope(rp),
|
||||
decl.inputs[0], none);
|
||||
let t = ty::mk_res(tcx, local_def(it.id), t_arg.ty, substs);
|
||||
let t_res = {bounds: bounds, rp: rp, ty: t};
|
||||
tcx.tcache.insert(local_def(it.id), t_res);
|
||||
|
|
@ -1027,16 +1035,27 @@ fn replace_bound_regions(
|
|||
}
|
||||
|
||||
fn ty_of_arg<AC: ast_conv, RS: region_scope copy>(
|
||||
self: AC, rscope: RS, a: ast::arg) -> ty::arg {
|
||||
self: AC, rscope: RS, a: ast::arg,
|
||||
expected_ty: option<ty::arg>) -> ty::arg {
|
||||
|
||||
fn arg_mode(tcx: ty::ctxt, m: ast::mode, ty: ty::t) -> ast::mode {
|
||||
alt m {
|
||||
let ty = alt a.ty.node {
|
||||
ast::ty_infer if expected_ty.is_some() {expected_ty.get().ty}
|
||||
ast::ty_infer {self.ty_infer(a.ty.span)}
|
||||
_ {ast_ty_to_ty(self, rscope, a.ty)}
|
||||
};
|
||||
|
||||
let mode = {
|
||||
alt a.mode {
|
||||
ast::infer(_) if expected_ty.is_some() {
|
||||
result::get(ty::unify_mode(self.tcx(), a.mode,
|
||||
expected_ty.get().mode))
|
||||
}
|
||||
ast::infer(_) {
|
||||
alt ty::get(ty).struct {
|
||||
// If the type is not specified, then this must be a fn expr.
|
||||
// Leave the mode as infer(_), it will get inferred based
|
||||
// on constraints elsewhere.
|
||||
ty::ty_var(_) { m }
|
||||
ty::ty_var(_) {a.mode}
|
||||
|
||||
// If the type is known, then use the default for that type.
|
||||
// Here we unify m and the default. This should update the
|
||||
|
|
@ -1044,30 +1063,48 @@ fn ty_of_arg<AC: ast_conv, RS: region_scope copy>(
|
|||
// will have been unified with m yet:
|
||||
_ {
|
||||
let m1 = ast::expl(ty::default_arg_mode_for_ty(ty));
|
||||
result::get(ty::unify_mode(tcx, m, m1))
|
||||
result::get(ty::unify_mode(self.tcx(), a.mode, m1))
|
||||
}
|
||||
}
|
||||
}
|
||||
ast::expl(_) { m }
|
||||
ast::expl(_) {a.mode}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
let ty = ast_ty_to_ty(self, rscope, a.ty);
|
||||
let mode = arg_mode(self.tcx(), a.mode, ty);
|
||||
{mode: mode, ty: ty}
|
||||
}
|
||||
|
||||
type expected_tys = option<{inputs: [ty::arg],
|
||||
output: ty::t}>;
|
||||
|
||||
fn ty_of_fn_decl<AC: ast_conv, RS: region_scope copy>(
|
||||
self: AC, rscope: RS,
|
||||
proto: ast::proto,
|
||||
decl: ast::fn_decl) -> ty::fn_ty {
|
||||
decl: ast::fn_decl,
|
||||
expected_tys: expected_tys) -> ty::fn_ty {
|
||||
|
||||
#debug["ty_of_fn_decl"];
|
||||
indent {||
|
||||
// new region names that appear inside of the fn decl are bound to
|
||||
// that function type
|
||||
let rb = in_binding_rscope(rscope);
|
||||
let input_tys = vec::map(decl.inputs) { |a| ty_of_arg(self, rb, a) };
|
||||
let output_ty = ast_ty_to_ty(self, rb, decl.output);
|
||||
|
||||
let input_tys = decl.inputs.mapi { |i, a|
|
||||
let expected_arg_ty = expected_tys.chain { |e|
|
||||
// no guarantee that the correct number of expected args
|
||||
// were supplied
|
||||
if i < e.inputs.len() {some(e.inputs[i])} else {none}
|
||||
};
|
||||
ty_of_arg(self, rb, a, expected_arg_ty)
|
||||
};
|
||||
|
||||
let expected_ret_ty = expected_tys.map { |e| e.output };
|
||||
let output_ty = alt decl.output.node {
|
||||
ast::ty_infer if expected_ret_ty.is_some() {expected_ret_ty.get()}
|
||||
ast::ty_infer {self.ty_infer(decl.output.span)}
|
||||
_ {ast_ty_to_ty(self, rb, decl.output)}
|
||||
};
|
||||
|
||||
let out_constrs = vec::map(decl.constraints) {|constr|
|
||||
ty::ast_constr_to_constr(self.tcx(), constr)
|
||||
};
|
||||
|
|
@ -1083,7 +1120,7 @@ fn ty_of_native_fn_decl(ccx: @crate_ctxt,
|
|||
|
||||
let bounds = ty_param_bounds(ccx, ty_params);
|
||||
let rb = in_binding_rscope(empty_rscope);
|
||||
let input_tys = vec::map(decl.inputs) { |a| ty_of_arg(ccx, rb, a) };
|
||||
let input_tys = decl.inputs.map { |a| ty_of_arg(ccx, rb, a, none) };
|
||||
let output_ty = ast_ty_to_ty(ccx, rb, decl.output);
|
||||
|
||||
let t_fn = ty::mk_fn(ccx.tcx, {proto: ast::proto_bare,
|
||||
|
|
@ -1135,7 +1172,8 @@ fn ty_of_method(ccx: @crate_ctxt,
|
|||
rp: ast::region_param) -> ty::method {
|
||||
{ident: m.ident,
|
||||
tps: ty_param_bounds(ccx, m.tps),
|
||||
fty: ty_of_fn_decl(ccx, type_rscope(rp), ast::proto_bare, m.decl),
|
||||
fty: ty_of_fn_decl(ccx, type_rscope(rp), ast::proto_bare,
|
||||
m.decl, none),
|
||||
purity: m.decl.purity,
|
||||
privacy: m.privacy}
|
||||
}
|
||||
|
|
@ -1145,7 +1183,8 @@ fn ty_of_ty_method(self: @crate_ctxt,
|
|||
rp: ast::region_param) -> ty::method {
|
||||
{ident: m.ident,
|
||||
tps: ty_param_bounds(self, m.tps),
|
||||
fty: ty_of_fn_decl(self, type_rscope(rp), ast::proto_bare, m.decl),
|
||||
fty: ty_of_fn_decl(self, type_rscope(rp), ast::proto_bare,
|
||||
m.decl, none),
|
||||
// assume public, because this is only invoked on iface methods
|
||||
purity: m.decl.purity, privacy: ast::pub}
|
||||
}
|
||||
|
|
@ -1649,7 +1688,8 @@ mod collect {
|
|||
ast::item_res(decl, tps, _, dtor_id, ctor_id, rp) {
|
||||
let {bounds, substs} = mk_substs(ccx, tps, rp);
|
||||
let def_id = local_def(it.id);
|
||||
let t_arg = ty_of_arg(ccx, type_rscope(rp), decl.inputs[0]);
|
||||
let t_arg = ty_of_arg(ccx, type_rscope(rp),
|
||||
decl.inputs[0], none);
|
||||
let t_res = ty::mk_res(tcx, def_id, t_arg.ty, substs);
|
||||
|
||||
let t_ctor = ty::mk_fn(tcx, {
|
||||
|
|
@ -1691,7 +1731,8 @@ mod collect {
|
|||
ty_of_fn_decl(ccx,
|
||||
empty_rscope,
|
||||
ast::proto_any,
|
||||
ctor.node.dec));
|
||||
ctor.node.dec,
|
||||
none));
|
||||
write_ty_to_tcx(tcx, ctor.node.id, t_ctor);
|
||||
tcx.tcache.insert(local_def(ctor.node.id),
|
||||
{bounds: tpt.bounds,
|
||||
|
|
@ -1961,7 +2002,7 @@ mod writeback {
|
|||
resolve_type_vars_for_node(wbcx, e.span, e.id);
|
||||
alt e.node {
|
||||
ast::expr_fn(_, decl, _, _) |
|
||||
ast::expr_fn_block(decl, _) {
|
||||
ast::expr_fn_block(decl, _, _) {
|
||||
vec::iter(decl.inputs) {|input|
|
||||
let r_ty = resolve_type_vars_for_node(wbcx, e.span, input.id);
|
||||
|
||||
|
|
@ -2902,35 +2943,6 @@ fn region_of(fcx: @fn_ctxt, expr: @ast::expr) -> ty::region {
|
|||
}
|
||||
}
|
||||
|
||||
fn check_expr_fn_with_unifier(fcx: @fn_ctxt,
|
||||
expr: @ast::expr,
|
||||
proto: ast::proto,
|
||||
decl: ast::fn_decl,
|
||||
body: ast::blk,
|
||||
is_loop_body: bool,
|
||||
unifier: fn()) {
|
||||
let tcx = fcx.ccx.tcx;
|
||||
let fty = ty::mk_fn(tcx, ty_of_fn_decl(fcx, fcx, proto, decl));
|
||||
|
||||
#debug("check_expr_fn_with_unifier %s fty=%s",
|
||||
expr_to_str(expr), fcx.ty_to_str(fty));
|
||||
|
||||
fcx.write_ty(expr.id, fty);
|
||||
|
||||
// Unify the type of the function with the expected type before we
|
||||
// typecheck the body so that we have more information about the
|
||||
// argument types in the body. This is needed to make binops and
|
||||
// record projection work on type inferred arguments.
|
||||
unifier();
|
||||
|
||||
let ret_ty = ty::ty_fn_ret(fty);
|
||||
let arg_tys = vec::map(ty::ty_fn_args(fty)) {|a| a.ty };
|
||||
|
||||
check_fn(fcx.ccx, proto, decl, body, expr.id,
|
||||
ret_ty, arg_tys, is_loop_body, some(fcx),
|
||||
fcx.self_ty);
|
||||
}
|
||||
|
||||
fn check_expr_with_unifier(fcx: @fn_ctxt,
|
||||
expr: @ast::expr,
|
||||
expected: option<ty::t>,
|
||||
|
|
@ -2999,7 +3011,7 @@ fn check_expr_with_unifier(fcx: @fn_ctxt,
|
|||
alt a_opt {
|
||||
some(a) {
|
||||
let is_block = alt a.node {
|
||||
ast::expr_fn_block(_, _) { true }
|
||||
ast::expr_fn_block(*) { true }
|
||||
_ { false }
|
||||
};
|
||||
if is_block == check_blocks {
|
||||
|
|
@ -3227,6 +3239,11 @@ fn check_expr_with_unifier(fcx: @fn_ctxt,
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Resolves `expected` by a single level if it is a variable and passes it
|
||||
// through the `unpack` function. It there is no expected type or
|
||||
// resolution is not possible (e.g., no constraints yet present), just
|
||||
// returns `none`.
|
||||
fn unpack_expected<O: copy>(fcx: @fn_ctxt, expected: option<ty::t>,
|
||||
unpack: fn(ty::sty) -> option<O>)
|
||||
-> option<O> {
|
||||
|
|
@ -3241,6 +3258,42 @@ fn check_expr_with_unifier(fcx: @fn_ctxt,
|
|||
}
|
||||
}
|
||||
|
||||
fn check_expr_fn(fcx: @fn_ctxt,
|
||||
expr: @ast::expr,
|
||||
proto: ast::proto,
|
||||
decl: ast::fn_decl,
|
||||
body: ast::blk,
|
||||
is_loop_body: bool,
|
||||
expected: option<ty::t>) {
|
||||
let tcx = fcx.ccx.tcx;
|
||||
|
||||
let expected_tys = unpack_expected(fcx, expected) { |sty|
|
||||
alt sty {
|
||||
ty::ty_fn(fn_ty) {some({inputs:fn_ty.inputs,
|
||||
output:fn_ty.output})}
|
||||
_ {none}
|
||||
}
|
||||
};
|
||||
|
||||
// construct the function type
|
||||
let fty = ty::mk_fn(tcx,
|
||||
ty_of_fn_decl(fcx, fcx, proto, decl,
|
||||
expected_tys));
|
||||
|
||||
#debug("check_expr_fn_with_unifier %s fty=%s",
|
||||
expr_to_str(expr), fcx.ty_to_str(fty));
|
||||
|
||||
fcx.write_ty(expr.id, fty);
|
||||
|
||||
let ret_ty = ty::ty_fn_ret(fty);
|
||||
let arg_tys = vec::map(ty::ty_fn_args(fty)) {|a| a.ty };
|
||||
|
||||
check_fn(fcx.ccx, proto, decl, body, expr.id,
|
||||
ret_ty, arg_tys, is_loop_body, some(fcx),
|
||||
fcx.self_ty);
|
||||
}
|
||||
|
||||
|
||||
let tcx = fcx.ccx.tcx;
|
||||
let id = expr.id;
|
||||
let mut bot = false;
|
||||
|
|
@ -3510,20 +3563,25 @@ fn check_expr_with_unifier(fcx: @fn_ctxt,
|
|||
if !arm_non_bot { result_ty = ty::mk_bot(tcx); }
|
||||
fcx.write_ty(id, result_ty);
|
||||
}
|
||||
ast::expr_fn(proto, decl, body, captures) {
|
||||
check_expr_fn_with_unifier(fcx, expr, proto, decl, body,
|
||||
false, unifier);
|
||||
capture::check_capture_clause(tcx, expr.id, proto, *captures);
|
||||
ast::expr_fn(proto, decl, body, cap_clause) {
|
||||
check_expr_fn(fcx, expr, proto, decl, body, false, expected);
|
||||
capture::check_capture_clause(tcx, expr.id, proto, cap_clause);
|
||||
}
|
||||
ast::expr_fn_block(decl, body) {
|
||||
ast::expr_fn_block(decl, body, cap_clause) {
|
||||
// Take the prototype from the expected type, but default to block:
|
||||
let proto = unpack_expected(fcx, expected, {|sty|
|
||||
alt sty { ty::ty_fn({proto, _}) { some(proto) } _ { none } }
|
||||
}).get_default(ast::proto_box);
|
||||
check_expr_fn_with_unifier(fcx, expr, proto, decl, body,
|
||||
false, unifier);
|
||||
check_expr_fn(fcx, expr, proto, decl, body, false, expected);
|
||||
capture::check_capture_clause(tcx, expr.id, proto, cap_clause);
|
||||
}
|
||||
ast::expr_loop_body(b) {
|
||||
// a loop body is the special argument to a `for` loop. We know that
|
||||
// there will be an expected type in this context because it can only
|
||||
// appear in the context of a call, so we get the expected type of the
|
||||
// parameter. The catch here is that we need to validate two things:
|
||||
// 1. a closure that returns a bool is expected
|
||||
// 2. the cloure that was given returns unit
|
||||
let expected_sty = unpack_expected(fcx, expected, {|x|some(x)}).get();
|
||||
let (inner_ty, proto) = alt expected_sty {
|
||||
ty::ty_fn(fty) {
|
||||
|
|
@ -3544,10 +3602,10 @@ fn check_expr_with_unifier(fcx: @fn_ctxt,
|
|||
}
|
||||
};
|
||||
alt check b.node {
|
||||
ast::expr_fn_block(decl, body) {
|
||||
check_expr_fn_with_unifier(fcx, b, proto, decl, body, true) {||
|
||||
demand::suptype(fcx, b.span, inner_ty, fcx.expr_ty(b));
|
||||
}
|
||||
ast::expr_fn_block(decl, body, cap_clause) {
|
||||
check_expr_fn(fcx, b, proto, decl, body, true, some(inner_ty));
|
||||
demand::suptype(fcx, b.span, inner_ty, fcx.expr_ty(b));
|
||||
capture::check_capture_clause(tcx, b.id, proto, cap_clause);
|
||||
}
|
||||
}
|
||||
let block_ty = structurally_resolved_type(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue