Skip to content

Commit f3edf8d

Browse files
committed
Make it possible to take the value of (and bind) native fns
Closes #820
1 parent 8ab02f7 commit f3edf8d

File tree

3 files changed

+28
-33
lines changed

3 files changed

+28
-33
lines changed

src/comp/middle/trans.rs

+3-8
Original file line numberDiff line numberDiff line change
@@ -1737,7 +1737,7 @@ fn iter_structural_ty(cx: @block_ctxt, av: ValueRef, t: ty::t,
17371737
}
17381738
ret next_cx;
17391739
}
1740-
ty::ty_fn(_, _, _, _, _) {
1740+
ty::ty_fn(_, _, _, _, _) | ty::ty_native_fn(_, _, _) {
17411741
let box_cell_a = GEP(cx, av, [C_int(0), C_int(abi::fn_field_box)]);
17421742
ret iter_boxpp(cx, box_cell_a, f);
17431743
}
@@ -3047,7 +3047,7 @@ fn trans_var(cx: &@block_ctxt, sp: &span, def: &ast::def, id: ast::node_id) ->
30473047
lval_result {
30483048
let ccx = bcx_ccx(cx);
30493049
alt def {
3050-
ast::def_fn(did, _) {
3050+
ast::def_fn(did, _) | ast::def_native_fn(did) {
30513051
let tyt = ty::lookup_item_type(ccx.tcx, did);
30523052
ret lval_generic_fn(cx, tyt, did, id);
30533053
}
@@ -3092,10 +3092,6 @@ fn trans_var(cx: &@block_ctxt, sp: &span, def: &ast::def, id: ast::node_id) ->
30923092
tp));
30933093
}
30943094
}
3095-
ast::def_native_fn(did) {
3096-
let tyt = ty::lookup_item_type(ccx.tcx, did);
3097-
ret lval_generic_fn(cx, tyt, did, id);
3098-
}
30993095
_ { ret trans_local_var(cx, def); }
31003096
}
31013097
}
@@ -5743,8 +5739,7 @@ fn decl_native_fn_and_pair(ccx: &@crate_ctxt, sp: &span, path: &[str],
57435739
rptr = result.rptr;
57445740
}
57455741
_ {
5746-
r =
5747-
trans_native_call(new_raw_block_ctxt(bcx.fcx, bcx.llbb),
5742+
r = trans_native_call(new_raw_block_ctxt(bcx.fcx, bcx.llbb),
57485743
ccx.externs, ccx.llmod, name, call_args);
57495744
rptr = BitCast(bcx, fcx.llretptr, T_ptr(T_i32()));
57505745
}

src/comp/middle/ty.rs

+1
Original file line numberDiff line numberDiff line change
@@ -801,6 +801,7 @@ fn type_is_structural(cx: &ctxt, ty: t) -> bool {
801801
ty_tup(_) { ret true; }
802802
ty_tag(_, _) { ret true; }
803803
ty_fn(_, _, _, _, _) { ret true; }
804+
ty_native_fn(_, _, _) { ret true; }
804805
ty_obj(_) { ret true; }
805806
ty_res(_, _, _) { ret true; }
806807
_ { ret false; }

src/comp/middle/typeck.rs

+24-25
Original file line numberDiff line numberDiff line change
@@ -2055,34 +2055,33 @@ fn check_expr_with_unifier(fcx: &@fn_ctxt, expr: &@ast::expr, unify: &unifier,
20552055
bot = check_call_or_bind(fcx, expr.span, f, args, kind_bind);
20562056

20572057
// Pull the argument and return types out.
2058-
let proto_1;
2059-
let arg_tys_1: [ty::arg] = [];
2060-
let rt_1;
2061-
let fty = expr_ty(tcx, f);
2062-
let t_1;
2063-
alt structure_of(fcx, expr.span, fty) {
2064-
ty::ty_fn(proto, arg_tys, rt, cf, constrs) {
2065-
proto_1 = proto;
2066-
rt_1 = rt;
2067-
// FIXME:
2068-
// probably need to munge the constrs to drop constraints
2069-
// for any bound args
2058+
let (proto, arg_tys, rt, cf, constrs) =
2059+
alt structure_of(fcx, expr.span, expr_ty(tcx, f)) {
2060+
// FIXME:
2061+
// probably need to munge the constrs to drop constraints
2062+
// for any bound args
2063+
ty::ty_fn(proto, arg_tys, rt, cf, constrs) {
2064+
(proto, arg_tys, rt, cf, constrs)
2065+
}
2066+
ty::ty_native_fn(_, arg_tys, rt) {
2067+
(ast::proto_fn, arg_tys, rt, ast::return, [])
2068+
}
2069+
_ { fail "LHS of bind expr didn't have a function type?!"; }
2070+
};
20702071

2071-
// For each blank argument, add the type of that argument
2072-
// to the resulting function type.
2073-
let i = 0u;
2074-
while i < vec::len::<option::t<@ast::expr>>(args) {
2075-
alt args[i] {
2076-
some(_) {/* no-op */ }
2077-
none. { arg_tys_1 += [arg_tys[i]]; }
2078-
}
2079-
i += 1u;
2072+
// For each blank argument, add the type of that argument
2073+
// to the resulting function type.
2074+
let out_args = [];
2075+
let i = 0u;
2076+
while i < vec::len(args) {
2077+
alt args[i] {
2078+
some(_) {/* no-op */ }
2079+
none. { out_args += [arg_tys[i]]; }
20802080
}
2081-
t_1 = ty::mk_fn(tcx, proto_1, arg_tys_1, rt_1, cf, constrs);
2082-
}
2083-
_ { fail "LHS of bind expr didn't have a function type?!"; }
2081+
i += 1u;
20842082
}
2085-
write::ty_only_fixup(fcx, id, t_1);
2083+
let ft = ty::mk_fn(tcx, proto, out_args, rt, cf, constrs);
2084+
write::ty_only_fixup(fcx, id, ft);
20862085
}
20872086
ast::expr_call(f, args) {
20882087
bot = check_call_full(fcx, expr.span, f, args, kind_call, expr.id);

0 commit comments

Comments
 (0)