diff options
| author | Marijn Haverbeke <marijnh@gmail.com> | 2011-12-07 15:28:57 +0100 |
|---|---|---|
| committer | Marijn Haverbeke <marijnh@gmail.com> | 2011-12-07 15:28:57 +0100 |
| commit | 03a6e542126b755a9cd2f4f56144107ba0f4d1cd (patch) | |
| tree | 3e939cd30c0c905760fc1827f8519bbc8acd976e /src/comp | |
| parent | d28e0c0c0ae329705a063a025b853b292ff033a7 (diff) | |
| download | rust-03a6e542126b755a9cd2f4f56144107ba0f4d1cd.tar.gz rust-03a6e542126b755a9cd2f4f56144107ba0f4d1cd.zip | |
Disallow binding by-mut-ref and by-move arguments
Fix bug in bound by-copy arguments. Closes #1261
Diffstat (limited to 'src/comp')
| -rw-r--r-- | src/comp/middle/mut.rs | 25 | ||||
| -rw-r--r-- | src/comp/middle/trans.rs | 6 |
2 files changed, 31 insertions, 0 deletions
diff --git a/src/comp/middle/mut.rs b/src/comp/middle/mut.rs index 12598f3729b..63dadd347e1 100644 --- a/src/comp/middle/mut.rs +++ b/src/comp/middle/mut.rs @@ -150,6 +150,7 @@ fn visit_decl(cx: @ctx, d: @decl, &&e: (), v: visit::vt<()>) { fn visit_expr(cx: @ctx, ex: @expr, &&e: (), v: visit::vt<()>) { alt ex.node { expr_call(f, args, _) { check_call(cx, f, args); } + expr_bind(f, args) { check_bind(cx, f, args); } expr_swap(lhs, rhs) { check_lval(cx, lhs, msg_assign); check_lval(cx, rhs, msg_assign); @@ -230,6 +231,30 @@ fn check_call(cx: @ctx, f: @expr, args: [@expr]) { } } +fn check_bind(cx: @ctx, f: @expr, args: [option::t<@expr>]) { + let arg_ts = ty::ty_fn_args(cx.tcx, ty::expr_ty(cx.tcx, f)); + let i = 0u; + for arg in args { + alt arg { + some(expr) { + alt (alt arg_ts[i].mode { + by_mut_ref. { some("by mutable reference") } + by_move. { some("by move") } + _ { none } + }) { + some(name) { + cx.tcx.sess.span_err( + expr.span, "can not bind an argument passed " + name); + } + none. {} + } + } + _ {} + } + i += 1u; + } +} + fn is_immutable_def(def: def) -> option::t<str> { alt def { def_fn(_, _) | def_mod(_) | def_native_mod(_) | def_const(_) | diff --git a/src/comp/middle/trans.rs b/src/comp/middle/trans.rs index 693c8ae79e8..107c53cfac3 100644 --- a/src/comp/middle/trans.rs +++ b/src/comp/middle/trans.rs @@ -3462,6 +3462,12 @@ fn trans_bind_thunk(cx: @local_ctxt, sp: span, incoming_fty: ty::t, bcx = bound_arg.bcx; let val = bound_arg.val; if out_arg.mode == ast::by_val { val = Load(bcx, val); } + if out_arg.mode == ast::by_copy { + let {bcx: cx, val: alloc} = alloc_ty(bcx, out_arg.ty); + bcx = memmove_ty(cx, alloc, val, out_arg.ty); + bcx = take_ty(bcx, alloc, out_arg.ty); + val = alloc; + } // If the type is parameterized, then we need to cast the // type we actually have to the parameterized out type. if ty::type_contains_params(cx.ccx.tcx, out_arg.ty) { |
