Skip to content

Commit f96a2a2

Browse files
committed
Remove by-mutable-ref mode from the compiler
and test cases. Closes #3513
1 parent 0599929 commit f96a2a2

26 files changed

+59
-95
lines changed

doc/rust.md

+4-6
Original file line numberDiff line numberDiff line change
@@ -2784,14 +2784,12 @@ aside a copy of that value to refer to. If this is not semantically safe (for
27842784
example, if the referred-to value contains mutable fields), it will reject the
27852785
program. If the compiler deems copying the value expensive, it will warn.
27862786

2787-
A function can be declared to take an argument by mutable reference. This
2788-
allows the function to write to the slot that the reference refers to.
2789-
2790-
An example function that accepts an value by mutable reference:
2787+
A function with an argument of type `&mut T`, for some type `T`, can write to
2788+
the slot that its argument refers to. An example of such a function is:
27912789

27922790
~~~~~~~~
2793-
fn incr(&i: int) {
2794-
i = i + 1;
2791+
fn incr(i: &mut int) {
2792+
*i = *i + 1;
27952793
}
27962794
~~~~~~~~
27972795

src/libsyntax/ast.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,7 @@ impl<T:cmp::Eq> inferable<T> : cmp::Eq {
574574

575575
// "resolved" mode: the real modes.
576576
#[auto_serialize]
577-
enum rmode { by_ref, by_val, by_mutbl_ref, by_move, by_copy }
577+
enum rmode { by_ref, by_val, by_move, by_copy }
578578

579579
impl rmode : to_bytes::IterBytes {
580580
pure fn iter_bytes(+lsb0: bool, f: to_bytes::Cb) {

src/libsyntax/parse/obsolete.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ pub enum ObsoleteSyntax {
2222
ObsoleteClassMethod,
2323
ObsoleteClassTraits,
2424
ObsoletePrivSection,
25-
ObsoleteModeInFnType
25+
ObsoleteModeInFnType,
26+
ObsoleteByMutRefMode
2627
}
2728

2829
impl ObsoleteSyntax : cmp::Eq {
@@ -94,6 +95,10 @@ impl parser : ObsoleteReporter {
9495
"to use a (deprecated) mode in a fn type, you should \
9596
give the argument an explicit name (like `&&v: int`)"
9697
),
98+
ObsoleteByMutRefMode => (
99+
"by-mutable-reference mode",
100+
"Declare an argument of type &mut T instead"
101+
),
97102
};
98103

99104
self.report(sp, kind, kind_str, desc);

src/libsyntax/parse/parser.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ use obsolete::{
2020
ObsoleteLowerCaseKindBounds, ObsoleteLet,
2121
ObsoleteFieldTerminator, ObsoleteStructCtor,
2222
ObsoleteWith, ObsoleteClassMethod, ObsoleteClassTraits,
23-
ObsoleteModeInFnType
23+
ObsoleteModeInFnType, ObsoleteByMutRefMode
2424
};
2525
use ast::{_mod, add, alt_check, alt_exhaustive, arg, arm, attribute,
2626
bind_by_ref, bind_by_implicit_ref, bind_by_value, bind_by_move,
2727
bitand, bitor, bitxor, blk, blk_check_mode, bound_const,
2828
bound_copy, bound_send, bound_trait, bound_owned, box, by_copy,
29-
by_move, by_mutbl_ref, by_ref, by_val, capture_clause,
29+
by_move, by_ref, by_val, capture_clause,
3030
capture_item, cdir_dir_mod, cdir_src_mod, cdir_view_item,
3131
class_immutable, class_mutable,
3232
crate, crate_cfg, crate_directive, decl, decl_item, decl_local,
@@ -570,9 +570,10 @@ impl parser {
570570

571571
fn parse_arg_mode() -> mode {
572572
if self.eat(token::BINOP(token::AND)) {
573-
self.span_fatal(copy self.last_span,
574-
~"Obsolete syntax has no effect");
575-
expl(by_mutbl_ref)
573+
self.obsolete(copy self.span,
574+
ObsoleteByMutRefMode);
575+
// Bogus mode, but doesn't matter since it's an error
576+
expl(by_ref)
576577
} else if self.eat(token::BINOP(token::MINUS)) {
577578
expl(by_move)
578579
} else if self.eat(token::ANDAND) {

src/libsyntax/print/pprust.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1688,7 +1688,6 @@ fn print_fn_block_args(s: ps, decl: ast::fn_decl,
16881688
16891689
fn mode_to_str(m: ast::mode) -> ~str {
16901690
match m {
1691-
ast::expl(ast::by_mutbl_ref) => ~"&",
16921691
ast::expl(ast::by_move) => ~"-",
16931692
ast::expl(ast::by_ref) => ~"&&",
16941693
ast::expl(ast::by_val) => ~"++",

src/rustc/metadata/tydecode.rs

-1
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,6 @@ fn parse_arg(st: @pstate, conv: conv_did) -> ty::arg {
394394

395395
fn parse_mode(st: @pstate) -> ast::mode {
396396
let m = ast::expl(match next(st) {
397-
'&' => ast::by_mutbl_ref,
398397
'-' => ast::by_move,
399398
'+' => ast::by_copy,
400399
'=' => ast::by_ref,

src/rustc/metadata/tyencode.rs

-1
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,6 @@ fn enc_arg(w: io::Writer, cx: @ctxt, arg: ty::arg) {
333333

334334
fn enc_mode(w: io::Writer, cx: @ctxt, m: mode) {
335335
match ty::resolved_mode(cx.tcx, m) {
336-
by_mutbl_ref => w.write_char('&'),
337336
by_move => w.write_char('-'),
338337
by_copy => w.write_char('+'),
339338
by_ref => w.write_char('='),

src/rustc/middle/borrowck/check_loans.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,7 @@ impl check_loan_ctxt {
529529
ast::by_move => {
530530
self.check_move_out(*arg);
531531
}
532-
ast::by_mutbl_ref | ast::by_ref |
532+
ast::by_ref |
533533
ast::by_copy | ast::by_val => {
534534
}
535535
}

src/rustc/middle/borrowck/gather_loans.rs

-4
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,6 @@ fn req_loans_in_expr(ex: @ast::expr,
115115
let scope_r = ty::re_scope(ex.id);
116116
for vec::each2(args, arg_tys) |arg, arg_ty| {
117117
match ty::resolved_mode(self.tcx(), arg_ty.mode) {
118-
ast::by_mutbl_ref => {
119-
let arg_cmt = self.bccx.cat_expr(*arg);
120-
self.guarantee_valid(arg_cmt, m_mutbl, scope_r);
121-
}
122118
ast::by_ref => {
123119
let arg_cmt = self.bccx.cat_expr(*arg);
124120
self.guarantee_valid(arg_cmt, m_imm, scope_r);

src/rustc/middle/kind.rs

+3-10
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ fn check_expr(e: @expr, cx: ctx, v: visit::vt<ctx>) {
323323
for ty::ty_fn_args(ty::expr_ty(cx.tcx, f)).each |arg_t| {
324324
match ty::arg_mode(cx.tcx, *arg_t) {
325325
by_copy => maybe_copy(cx, args[i], None),
326-
by_ref | by_val | by_mutbl_ref | by_move => ()
326+
by_ref | by_val | by_move => ()
327327
}
328328
i += 1u;
329329
}
@@ -335,7 +335,7 @@ fn check_expr(e: @expr, cx: ctx, v: visit::vt<ctx>) {
335335
Some(ref mme) => {
336336
match ty::arg_mode(cx.tcx, mme.self_arg) {
337337
by_copy => maybe_copy(cx, lhs, None),
338-
by_ref | by_val | by_mutbl_ref | by_move => ()
338+
by_ref | by_val | by_move => ()
339339
}
340340
}
341341
_ => ()
@@ -465,14 +465,7 @@ fn check_imm_free_var(cx: ctx, def: def, sp: span) {
465465
cx.tcx.sess.span_err(sp, msg);
466466
}
467467
}
468-
def_arg(_, mode) => {
469-
match ty::resolved_mode(cx.tcx, mode) {
470-
by_ref | by_val | by_move | by_copy => { /* ok */ }
471-
by_mutbl_ref => {
472-
cx.tcx.sess.span_err(sp, msg);
473-
}
474-
}
475-
}
468+
def_arg(*) => { /* ok */ }
476469
def_upvar(_, def1, _, _) => {
477470
check_imm_free_var(cx, *def1, sp);
478471
}

src/rustc/middle/liveness.rs

+4-21
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ impl IrMaps {
398398

399399
(*v).push(id);
400400
}
401-
Arg(_, _, by_ref) | Arg(_, _, by_mutbl_ref) |
401+
Arg(_, _, by_ref) |
402402
Arg(_, _, by_val) | Self | Field(_) | ImplicitRet |
403403
Local(LocalInfo {kind: FromMatch(bind_by_implicit_ref), _}) => {
404404
debug!("--but it is not owned");
@@ -919,7 +919,7 @@ impl Liveness {
919919
// inputs passed by & mode should be considered live on exit:
920920
for decl.inputs.each |arg| {
921921
match ty::resolved_mode(self.tcx, arg.mode) {
922-
by_mutbl_ref | by_ref | by_val => {
922+
by_ref | by_val => {
923923
// These are "non-owned" modes, so register a read at
924924
// the end. This will prevent us from moving out of
925925
// such variables but also prevent us from registering
@@ -1573,7 +1573,7 @@ fn check_expr(expr: @expr, &&self: @Liveness, vt: vt<@Liveness>) {
15731573
let targs = ty::ty_fn_args(ty::expr_ty(self.tcx, f));
15741574
for vec::each2(args, targs) |arg_expr, arg_ty| {
15751575
match ty::resolved_mode(self.tcx, arg_ty.mode) {
1576-
by_val | by_copy | by_ref | by_mutbl_ref => {}
1576+
by_val | by_copy | by_ref => {}
15771577
by_move => {
15781578
self.check_move_from_expr(*arg_expr, vt);
15791579
}
@@ -1865,24 +1865,7 @@ impl @Liveness {
18651865
fn warn_about_unused_args(sp: span, decl: fn_decl, entry_ln: LiveNode) {
18661866
for decl.inputs.each |arg| {
18671867
let var = self.variable(arg.id, arg.ty.span);
1868-
match ty::resolved_mode(self.tcx, arg.mode) {
1869-
by_mutbl_ref => {
1870-
// for mutable reference arguments, something like
1871-
// x = 1;
1872-
// is not worth warning about, as it has visible
1873-
// side effects outside the fn.
1874-
match self.assigned_on_entry(entry_ln, var) {
1875-
Some(_) => { /*ok*/ }
1876-
None => {
1877-
// but if it is not written, it ought to be used
1878-
self.warn_about_unused(sp, entry_ln, var);
1879-
}
1880-
}
1881-
}
1882-
by_val | by_ref | by_move | by_copy => {
1883-
self.warn_about_unused(sp, entry_ln, var);
1884-
}
1885-
}
1868+
self.warn_about_unused(sp, entry_ln, var);
18861869
}
18871870
}
18881871

src/rustc/middle/mem_categorization.rs

-3
Original file line numberDiff line numberDiff line change
@@ -523,9 +523,6 @@ impl &mem_categorization_ctxt {
523523
// m: mutability of the argument
524524
// lp: loan path, must be none for aliasable things
525525
let {m,lp} = match ty::resolved_mode(self.tcx, mode) {
526-
ast::by_mutbl_ref => {
527-
{m: m_mutbl, lp: None}
528-
}
529526
ast::by_move | ast::by_copy => {
530527
{m: m_imm, lp: Some(@lp_arg(vid))}
531528
}

src/rustc/middle/trans/base.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1503,7 +1503,7 @@ fn copy_args_to_allocas(fcx: fn_ctxt,
15031503
// the event it's not truly needed.
15041504
let llarg;
15051505
match ty::resolved_mode(tcx, arg_ty.mode) {
1506-
ast::by_ref | ast::by_mutbl_ref => {
1506+
ast::by_ref => {
15071507
llarg = raw_llarg;
15081508
}
15091509
ast::by_move | ast::by_copy => {

src/rustc/middle/trans/callee.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,7 @@ fn trans_arg_expr(bcx: block,
592592
DoAutorefArg => { val = arg_datum.to_ref_llval(bcx); }
593593
DontAutorefArg => {
594594
match arg_mode {
595-
ast::by_ref | ast::by_mutbl_ref => {
595+
ast::by_ref => {
596596
val = arg_datum.to_ref_llval(bcx);
597597
}
598598

src/rustc/middle/trans/meth.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ fn trans_method_callee(bcx: block, callee_id: ast::node_id,
154154
typeck::method_trait(_, off, vstore) => {
155155
trans_trait_callee(bcx, callee_id, off, self, vstore)
156156
}
157-
typeck::method_self(_, off) => {
157+
typeck::method_self(*) => {
158158
bcx.tcx().sess.span_bug(self.span, ~"self method call");
159159
}
160160
}

src/rustc/middle/trans/reflect.rs

-1
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,6 @@ impl reflector {
210210
ast::expl(e) => match e {
211211
ast::by_ref => 1u,
212212
ast::by_val => 2u,
213-
ast::by_mutbl_ref => 3u,
214213
ast::by_move => 4u,
215214
ast::by_copy => 5u
216215
}

src/rustc/middle/trans/type_use.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ fn type_uses_for(ccx: @crate_ctxt, fn_id: def_id, n_tps: uint)
5353
by_val | by_move | by_copy => {
5454
type_needs(cx, use_repr, arg.ty);
5555
}
56-
by_ref | by_mutbl_ref => {}
56+
by_ref => {}
5757
}
5858
}
5959
}

src/test/bench/task-perf-word-count-generic.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ mod map_reduce {
144144
fn start_mappers<K1: Copy Send, K2: Hash IterBytes Eq Const Copy Send,
145145
V: Copy Send>(
146146
map: &mapper<K1, K2, V>,
147-
&ctrls: ~[ctrl_proto::server::open<K2, V>],
147+
ctrls: &mut ~[ctrl_proto::server::open<K2, V>],
148148
inputs: &~[K1])
149149
-> ~[joinable_task]
150150
{
@@ -213,26 +213,26 @@ mod map_reduce {
213213
let mut is_done = false;
214214

215215
fn get<V: Copy Send>(p: Port<reduce_proto<V>>,
216-
&ref_count: int, &is_done: bool)
216+
ref_count: &mut int, is_done: &mut bool)
217217
-> Option<V> {
218-
while !is_done || ref_count > 0 {
218+
while !*is_done || *ref_count > 0 {
219219
match recv(p) {
220220
emit_val(v) => {
221221
// error!("received %d", v);
222222
return Some(v);
223223
}
224224
done => {
225225
// error!("all done");
226-
is_done = true;
226+
*is_done = true;
227227
}
228-
addref => { ref_count += 1; }
229-
release => { ref_count -= 1; }
228+
addref => { *ref_count += 1; }
229+
release => { *ref_count -= 1; }
230230
}
231231
}
232232
return None;
233233
}
234234

235-
(*reduce)(&key, || get(p, ref_count, is_done) );
235+
(*reduce)(&key, || get(p, &mut ref_count, &mut is_done) );
236236
}
237237

238238
fn map_reduce<K1: Copy Send, K2: Hash IterBytes Eq Const Copy Send, V: Copy Send>(
@@ -246,7 +246,7 @@ mod map_reduce {
246246
// to do the rest.
247247

248248
let reducers = map::HashMap();
249-
let mut tasks = start_mappers(&map, ctrl, &inputs);
249+
let mut tasks = start_mappers(&map, &mut ctrl, &inputs);
250250
let mut num_mappers = vec::len(inputs) as int;
251251

252252
while num_mappers > 0 {
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
#[forbid(deprecated_mode)];
22

3-
fn foo(_f: fn(&i: int)) { //~ ERROR explicit mode
4-
//~^ WARNING Obsolete syntax has no effect
3+
fn foo(_f: fn(&i: int)) { //~ ERROR by-mutable-reference mode
54
}
65

7-
type Bar = fn(&i: int); //~ ERROR explicit mode
8-
//~^ WARNING Obsolete syntax has no effect
6+
type Bar = fn(&i: int); //~ ERROR by-mutable-reference mode
97

108
fn main() {
119
}

src/test/compile-fail/unnamed_argument_mode.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
//error-pattern: mismatched types
1+
//error-pattern: by-mutable-reference mode
22

33
fn bad(&a: int) {
44
}
55

6-
// unnamed argument &int is now parsed x: &int
7-
// it's not parsed &x: int anymore
8-
96
fn called(f: fn(&int)) {
107
}
118

src/test/run-pass/argument-passing.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
// xfail-fast
22
#[legacy_modes];
33

4-
fn f1(a: {mut x: int}, &b: int, -c: int) -> int {
5-
let r = a.x + b + c;
4+
fn f1(a: {mut x: int}, b: &mut int, -c: int) -> int {
5+
let r = a.x + *b + c;
66
a.x = 0;
7-
b = 10;
7+
*b = 10;
88
return r;
99
}
1010

1111
fn f2(a: int, f: fn(int)) -> int { f(1); return a; }
1212

1313
fn main() {
1414
let mut a = {mut x: 1}, b = 2, c = 3;
15-
assert (f1(a, b, c) == 6);
15+
assert (f1(a, &mut b, c) == 6);
1616
assert (a.x == 0);
1717
assert (b == 10);
1818
assert (f2(a.x, |x| a.x = 50 ) == 0);

src/test/run-pass/fn-bare-assign.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
fn f(i: int, &called: bool) {
1+
fn f(i: int, called: &mut bool) {
22
assert i == 10;
3-
called = true;
3+
*called = true;
44
}
55

6-
fn g(f: extern fn(int, &v: bool), &called: bool) {
6+
fn g(f: extern fn(int, v: &mut bool), called: &mut bool) {
77
f(10, called);
88
}
99

1010
fn main() {
1111
let mut called = false;
1212
let h = f;
13-
g(h, called);
13+
g(h, &mut called);
1414
assert called == true;
1515
}

0 commit comments

Comments
 (0)