Skip to content

Commit 1b0f1f0

Browse files
committed
Remove the environment argument from bare functions
Issue #1022
1 parent 84e98f4 commit 1b0f1f0

File tree

1 file changed

+35
-7
lines changed

1 file changed

+35
-7
lines changed

src/comp/middle/trans.rs

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,16 @@ fn type_of_fn(cx: @crate_ctxt, sp: span, proto: ast::proto,
9898
// Arg 2: Env (closure-bindings / self-obj)
9999
if is_method {
100100
atys += [T_ptr(cx.rust_object_type)];
101-
} else { atys += [T_opaque_closure_ptr(*cx)]; }
101+
} else {
102+
alt proto {
103+
ast::proto_bare. {
104+
// Bare functions have no environment
105+
}
106+
_ {
107+
atys += [T_opaque_closure_ptr(*cx)];
108+
}
109+
}
110+
}
102111

103112
// Args >3: ty params, if not acquired via capture...
104113
if !is_method {
@@ -3559,7 +3568,15 @@ fn trans_bind_thunk(cx: @local_ctxt, sp: span, incoming_fty: ty::t,
35593568
}
35603569

35613570
// Set up the three implicit arguments to the thunk.
3562-
let llargs: [ValueRef] = [llretptr, fcx.lltaskptr, lltargetenv];
3571+
let llargs: [ValueRef] = alt ty::ty_fn_proto(ccx.tcx, outgoing_fty) {
3572+
ast::proto_bare. {
3573+
// Bare functions don't take an environment
3574+
[llretptr, fcx.lltaskptr]
3575+
}
3576+
_ {
3577+
[llretptr, fcx.lltaskptr, lltargetenv]
3578+
}
3579+
};
35633580

35643581
// Copy in the type parameters.
35653582
let i: uint = 0u;
@@ -3845,7 +3862,12 @@ fn trans_args(cx: @block_ctxt, outer_cx: @block_ctxt, llenv: ValueRef,
38453862
llargs += [bcx.fcx.lltaskptr];
38463863

38473864
// Arg 2: Env (closure-bindings / self-obj)
3848-
llargs += [llenv];
3865+
alt ty::ty_fn_proto(tcx, fn_ty) {
3866+
ast::proto_bare. { }
3867+
_ {
3868+
llargs += [llenv];
3869+
}
3870+
}
38493871

38503872
// Args >3: ty_params ...
38513873
llargs += lltydescs;
@@ -5098,6 +5120,7 @@ fn new_fn_ctxt_w_id(cx: @local_ctxt, sp: span, llfndecl: ValueRef,
50985120
id: ast::node_id, rstyle: ast::ret_style)
50995121
-> @fn_ctxt {
51005122
let llbbs = mk_standard_basic_blocks(llfndecl);
5123+
// FIXME: llenv is not correct for bare functions
51015124
ret @{llfn: llfndecl,
51025125
lltaskptr: llvm::LLVMGetParam(llfndecl, 1u),
51035126
llenv: llvm::LLVMGetParam(llfndecl, 2u),
@@ -5128,6 +5151,13 @@ fn new_fn_ctxt(cx: @local_ctxt, sp: span, llfndecl: ValueRef) -> @fn_ctxt {
51285151
ret new_fn_ctxt_w_id(cx, sp, llfndecl, -1, ast::return_val);
51295152
}
51305153

5154+
fn implicit_args_for_fn(proto: ast::proto) -> uint {
5155+
alt proto {
5156+
ast::proto_bare. { 2u }
5157+
_ { 3u }
5158+
}
5159+
}
5160+
51315161
// NB: must keep 4 fns in sync:
51325162
//
51335163
// - type_of_fn
@@ -5145,10 +5175,8 @@ fn new_fn_ctxt(cx: @local_ctxt, sp: span, llfndecl: ValueRef) -> @fn_ctxt {
51455175
fn create_llargs_for_fn_args(cx: @fn_ctxt, proto: ast::proto,
51465176
ty_self: option::t<ty::t>, ret_ty: ty::t,
51475177
args: [ast::arg], ty_params: [ast::ty_param]) {
5148-
// Skip the implicit arguments 0, 1, and 2. TODO: Pull out 3u and define
5149-
// it as a constant, since we're using it in several places in trans this
5150-
// way.
5151-
let arg_n = 3u;
5178+
// Skip the implicit arguments
5179+
let arg_n = implicit_args_for_fn(proto);
51525180
alt ty_self {
51535181
some(tt) { cx.llself = some::<val_self_pair>({v: cx.llenv, t: tt}); }
51545182
none. {

0 commit comments

Comments
 (0)