diff options
| author | Graydon Hoare <graydon@mozilla.com> | 2011-08-08 15:53:31 -0700 |
|---|---|---|
| committer | Graydon Hoare <graydon@mozilla.com> | 2011-08-08 15:53:41 -0700 |
| commit | b54e7e45069a84e9e9ee6ef3431f733d96d93586 (patch) | |
| tree | 67ec4b8273da05a6f16ff6255fdb5422219631d6 | |
| parent | 3dda9aabf217d59881970f847b06338d417f6f6f (diff) | |
| download | rust-b54e7e45069a84e9e9ee6ef3431f733d96d93586.tar.gz rust-b54e7e45069a84e9e9ee6ef3431f733d96d93586.zip | |
Add new arg-passing mode 'move' denoted with '-T'. Translate as pass-by-value, doesn't deinit source yet nor get proper analysis in typestate, alias passes.
| -rw-r--r-- | src/comp/metadata/tydecode.rs | 3 | ||||
| -rw-r--r-- | src/comp/metadata/tyencode.rs | 3 | ||||
| -rw-r--r-- | src/comp/middle/trans.rs | 1 | ||||
| -rw-r--r-- | src/comp/middle/ty.rs | 3 | ||||
| -rw-r--r-- | src/comp/middle/typeck.rs | 8 | ||||
| -rw-r--r-- | src/comp/syntax/ast.rs | 2 | ||||
| -rw-r--r-- | src/comp/syntax/parse/parser.rs | 2 | ||||
| -rw-r--r-- | src/comp/syntax/print/pprust.rs | 1 | ||||
| -rw-r--r-- | src/test/compile-fail/move-arg.rs | 14 | ||||
| -rw-r--r-- | src/test/run-pass/move-arg.rs | 8 |
10 files changed, 39 insertions, 6 deletions
diff --git a/src/comp/metadata/tydecode.rs b/src/comp/metadata/tydecode.rs index a54b8622bb0..093c2a7beba 100644 --- a/src/comp/metadata/tydecode.rs +++ b/src/comp/metadata/tydecode.rs @@ -381,6 +381,9 @@ fn parse_ty_fn(st: @pstate, sd: str_def) -> mode = ty::mo_alias(true); st.pos += 1u; } + } else if peek(st) as char == '-' { + mode = ty::mo_move; + st.pos += 1u; } inputs += ~[{mode: mode, ty: parse_ty(st, sd)}]; } diff --git a/src/comp/metadata/tyencode.rs b/src/comp/metadata/tyencode.rs index 6227876a7b3..2781a7dc6a3 100644 --- a/src/comp/metadata/tyencode.rs +++ b/src/comp/metadata/tyencode.rs @@ -208,6 +208,9 @@ fn enc_ty_fn(w: &ioivec::writer, cx: &@ctxt, args: &ty::arg[], out: &ty::t, w.write_char('&'); if mut { w.write_char('m'); } } + ty::mo_move. { + w.write_char('-'); + } ty::mo_val. { } } enc_ty(w, cx, arg.ty); diff --git a/src/comp/middle/trans.rs b/src/comp/middle/trans.rs index 15e6f8862fc..e4f4aef7d9d 100644 --- a/src/comp/middle/trans.rs +++ b/src/comp/middle/trans.rs @@ -95,6 +95,7 @@ fn type_of_explicit_args(cx: &@crate_ctxt, sp: &span, inputs: &ty::arg[]) -> let t: TypeRef = type_of_inner(cx, sp, arg.ty); t = alt arg.mode { ty::mo_alias(_) { T_ptr(t) } + ty::mo_move. { T_ptr(t) } _ { t } }; atys += ~[t]; diff --git a/src/comp/middle/ty.rs b/src/comp/middle/ty.rs index 00f2e6a1b86..eba4d8ccc60 100644 --- a/src/comp/middle/ty.rs +++ b/src/comp/middle/ty.rs @@ -95,6 +95,7 @@ export mk_iter_body_fn; export mode; export mo_val; export mo_alias; +export mo_move; export mt; export node_type_table; export pat_ty; @@ -187,7 +188,7 @@ export walk_ty; export occurs_check_fails; // Data types -tag mode { mo_val; mo_alias(bool); } +tag mode { mo_val; mo_alias(bool); mo_move; } type arg = {mode: mode, ty: t}; diff --git a/src/comp/middle/typeck.rs b/src/comp/middle/typeck.rs index 99e10eafa3c..b88f761dede 100644 --- a/src/comp/middle/typeck.rs +++ b/src/comp/middle/typeck.rs @@ -21,6 +21,7 @@ import middle::ty::field; import middle::ty::method; import middle::ty::mo_val; import middle::ty::mo_alias; +import middle::ty::mo_move; import middle::ty::node_type_table; import middle::ty::pat_ty; import middle::ty::ty_param_substs_opt_and_ty; @@ -204,12 +205,11 @@ fn instantiate_path(fcx: &@fn_ctxt, pth: &ast::path, } fn ast_mode_to_mode(mode: ast::mode) -> ty::mode { - let ty_mode; alt mode { - ast::val. { ty_mode = mo_val; } - ast::alias(mut) { ty_mode = mo_alias(mut); } + ast::val. { mo_val } + ast::alias(mut) { mo_alias(mut) } + ast::move. { mo_move } } - ret ty_mode; } diff --git a/src/comp/syntax/ast.rs b/src/comp/syntax/ast.rs index 5b0787a5fc4..be27e9c2dec 100644 --- a/src/comp/syntax/ast.rs +++ b/src/comp/syntax/ast.rs @@ -249,7 +249,7 @@ fn unop_to_str(op: unop) -> str { } } -tag mode { val; alias(bool); } +tag mode { val; alias(bool); move; } type stmt = spanned[stmt_]; diff --git a/src/comp/syntax/parse/parser.rs b/src/comp/syntax/parse/parser.rs index f0b25eb9a53..133b3c41bc7 100644 --- a/src/comp/syntax/parse/parser.rs +++ b/src/comp/syntax/parse/parser.rs @@ -597,6 +597,8 @@ fn parse_arg(p: &parser) -> ast::arg { expect(p, token::COLON); if eat(p, token::BINOP(token::AND)) { m = ast::alias(eat_word(p, "mutable")); + } else if eat(p, token::BINOP(token::MINUS)) { + m = ast::move; } let t: @ast::ty = parse_ty(p); ret {mode: m, ty: t, ident: i, id: p.get_id()}; diff --git a/src/comp/syntax/print/pprust.rs b/src/comp/syntax/print/pprust.rs index dab764c5baa..79fe8a9f8a4 100644 --- a/src/comp/syntax/print/pprust.rs +++ b/src/comp/syntax/print/pprust.rs @@ -1191,6 +1191,7 @@ fn print_alias(s: &ps, m: ast::mode) { alt m { ast::alias(true) { word_space(s, "&mutable"); } ast::alias(false) { word(s.s, "&"); } + ast::move. { word(s.s, "-"); } ast::val. { } } } diff --git a/src/test/compile-fail/move-arg.rs b/src/test/compile-fail/move-arg.rs new file mode 100644 index 00000000000..1b174ea64c6 --- /dev/null +++ b/src/test/compile-fail/move-arg.rs @@ -0,0 +1,14 @@ +// xfail-stage0 +// xfail-stage1 +// xfail-stage2 +// xfail-stage3 +// error-pattern: Unsatisfied precondition constraint +fn test(foo: -int) { + assert (foo == 10); +} + +fn main() { + let x = 10; + test(x); + log x; +} \ No newline at end of file diff --git a/src/test/run-pass/move-arg.rs b/src/test/run-pass/move-arg.rs new file mode 100644 index 00000000000..131f69558ea --- /dev/null +++ b/src/test/run-pass/move-arg.rs @@ -0,0 +1,8 @@ +fn test(foo: -int) { + assert (foo == 10); +} + +fn main() { + let x = 10; + test(x); +} \ No newline at end of file |
