Skip to content

Commit dc07280

Browse files
committed
make --enforce-mut-vars always on, add mut annotations to remaining files
1 parent ea60308 commit dc07280

File tree

179 files changed

+519
-508
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

179 files changed

+519
-508
lines changed

doc/rust.md

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1003,7 +1003,7 @@ fn iter<T>(seq: [T], f: fn(T)) {
10031003
for elt: T in seq { f(elt); }
10041004
}
10051005
fn map<T, U>(seq: [T], f: fn(T) -> U) -> [U] {
1006-
let acc = [];
1006+
let mut acc = [];
10071007
for elt in seq { acc += [f(elt)]; }
10081008
acc
10091009
}
@@ -1104,7 +1104,7 @@ enum animal {
11041104
cat
11051105
}
11061106
1107-
let a: animal = dog;
1107+
let mut a: animal = dog;
11081108
a = cat;
11091109
~~~~
11101110

@@ -1254,7 +1254,7 @@ not given, and the name is mandatory.
12541254
~~~~
12551255
impl uint_loops for uint {
12561256
fn times(f: fn(uint)) {
1257-
let i = 0u;
1257+
let mut i = 0u;
12581258
while i < self { f(i); i += 1u; }
12591259
}
12601260
}
@@ -1775,7 +1775,7 @@ expression. No allocation or destruction is entailed.
17751775
An example of three different move expressions:
17761776

17771777
~~~~~~~~
1778-
# let x = [mut 0];
1778+
# let mut x = [mut 0];
17791779
# let a = [mut 0];
17801780
# let b = 0;
17811781
# let y = {mut z: 0};
@@ -1804,8 +1804,8 @@ expression. No allocation or destruction is entailed.
18041804
An example of three different swap expressions:
18051805

18061806
~~~~~~~~
1807-
# let x = [mut 0];
1808-
# let a = [mut 0];
1807+
# let mut x = [mut 0];
1808+
# let mut a = [mut 0];
18091809
# let i = 0;
18101810
# let y = {mut z: 0};
18111811
# let b = {mut c: 0};
@@ -1827,7 +1827,7 @@ expression](#unary-copy-expressions). For example, the following two
18271827
expressions have the same effect:
18281828

18291829
~~~~
1830-
# let x = 0;
1830+
# let mut x = 0;
18311831
# let y = 0;
18321832
18331833
x = y;
@@ -2015,7 +2015,7 @@ loop body. If it evaluates to `false`, control exits the loop.
20152015
An example of a simple `while` expression:
20162016

20172017
~~~~
2018-
# let i = 0;
2018+
# let mut i = 0;
20192019
# let println = io::println;
20202020
20212021
while i < 10 {
@@ -2027,7 +2027,7 @@ while i < 10 {
20272027
An example of a `do`-`while` expression:
20282028

20292029
~~~~
2030-
# let i = 0;
2030+
# let mut i = 0;
20312031
# let println = io::println;
20322032
20332033
do {
@@ -2053,7 +2053,7 @@ For example, the following (contrived) function uses a `loop` with a
20532053

20542054
~~~~
20552055
fn count() -> bool {
2056-
let i = 0;
2056+
let mut i = 0;
20572057
loop {
20582058
i += 1;
20592059
if i == 20 { ret true; }
@@ -2801,7 +2801,7 @@ fn add(x: int, y: int) -> int {
28012801
ret x + y;
28022802
}
28032803
2804-
let x = add(5,7);
2804+
let mut x = add(5,7);
28052805
28062806
type binop = fn(int,int) -> int;
28072807
let bo: binop = add;
@@ -2880,7 +2880,7 @@ has a set of points before and after it in the implied control flow.
28802880
For example, this code:
28812881

28822882
~~~~~~~~
2883-
# let s;
2883+
# let mut s;
28842884
28852885
s = "hello, world";
28862886
io::println(s);
@@ -3154,7 +3154,10 @@ A _reference_ references a value outside the frame. It may refer to a
31543154
value allocated in another frame *or* a boxed value in the heap. The
31553155
reference-formation rules ensure that the referent will outlive the reference.
31563156

3157-
Local variables are always implicitly mutable.
3157+
Local variables are immutable unless declared with `let mut`. The
3158+
`mut` keyword applies to all local variables declared within that
3159+
declaration (so `let mut x, y` declares two mutable variables, `x` and
3160+
`y`).
31583161

31593162
Local variables are not initialized when allocated; the entire frame worth of
31603163
local variables are allocated at once, on frame-entry, in an uninitialized

doc/tutorial.md

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ a curly-brace language in the tradition of C, C++, and JavaScript.
2626

2727
~~~~
2828
fn fac(n: int) -> int {
29-
let result = 1, i = 1;
29+
let mut result = 1, i = 1;
3030
while i <= n {
3131
result *= i;
3232
i += 1;
@@ -286,16 +286,19 @@ fn this_doesnt(_x: int) {}
286286

287287
## Variable declaration
288288

289-
The `let` keyword, as we've seen, introduces a local variable. Global
290-
constants can be defined with `const`:
289+
The `let` keyword, as we've seen, introduces a local variable. Local
290+
variables are immutable by default: `let mut` can be used to introduce
291+
a local variable that can be reassigned. Global constants can be
292+
defined with `const`:
291293

292294
~~~~
293295
use std;
294296
const repeat: uint = 5u;
295297
fn main() {
296-
let count = 0u;
298+
let hi = "Hi!";
299+
let mut count = 0u;
297300
while count < repeat {
298-
io::println("Hi!");
301+
io::println(hi);
299302
count += 1u;
300303
}
301304
}
@@ -320,7 +323,7 @@ annotation:
320323
~~~~
321324
// The type of this vector will be inferred based on its use.
322325
let x = [];
323-
# x = [3];
326+
# vec::map(x, fn&(&&_y:int) -> int { _y });
324327
// Explicitly say this is a vector of integers.
325328
let y: [int] = [];
326329
~~~~
@@ -665,7 +668,7 @@ keyword `break` can be used to abort the loop, and `cont` can be used
665668
to abort the current iteration and continue with the next.
666669

667670
~~~~
668-
let x = 5;
671+
let mut x = 5;
669672
while true {
670673
x += x - 3;
671674
if x % 5 == 0 { break; }
@@ -761,7 +764,7 @@ failure otherwise. It is typically used to double-check things that
761764
*should* hold at a certain point in a program.
762765

763766
~~~~
764-
let x = 100;
767+
let mut x = 100;
765768
while (x > 10) { x -= 10; }
766769
assert x == 10;
767770
~~~~
@@ -933,7 +936,7 @@ of integers backwards:
933936

934937
~~~~
935938
fn for_rev(v: [int], act: fn(int)) {
936-
let i = vec::len(v);
939+
let mut i = vec::len(v);
937940
while (i > 0u) {
938941
i -= 1u;
939942
act(v[i]);
@@ -1273,7 +1276,7 @@ The `+` operator means concatenation when applied to vector types.
12731276
Growing a vector in Rust is not as inefficient as it looks :
12741277

12751278
~~~~
1276-
let myvec = [], i = 0;
1279+
let mut myvec = [], i = 0;
12771280
while i < 100 {
12781281
myvec += [i];
12791282
i += 1;
@@ -1376,7 +1379,7 @@ in `main`, so we're good. But the call could also look like this:
13761379
~~~~
13771380
# fn myfunc(a: int, b: fn()) {}
13781381
# fn get_another_record() -> int { 1 }
1379-
# let x = 1;
1382+
# let mut x = 1;
13801383
myfunc(x, {|| x = get_another_record(); });
13811384
~~~~
13821385

@@ -1436,7 +1439,7 @@ very cheap, but you'll occasionally have to copy them to ensure
14361439
safety.
14371440

14381441
~~~~
1439-
let my_rec = {a: 4, b: [1, 2, 3]};
1442+
let mut my_rec = {a: 4, b: [1, 2, 3]};
14401443
alt my_rec {
14411444
{a, b} {
14421445
log(info, b); // This is okay
@@ -1497,15 +1500,15 @@ Thus, Rust allows functions and datatypes to have type parameters.
14971500

14981501
~~~~
14991502
fn for_rev<T>(v: [T], act: fn(T)) {
1500-
let i = vec::len(v);
1503+
let mut i = vec::len(v);
15011504
while i > 0u {
15021505
i -= 1u;
15031506
act(v[i]);
15041507
}
15051508
}
15061509
15071510
fn map<T, U>(v: [T], f: fn(T) -> U) -> [U] {
1508-
let acc = [];
1511+
let mut acc = [];
15091512
for elt in v { acc += [f(elt)]; }
15101513
ret acc;
15111514
}
@@ -1548,7 +1551,7 @@ programs that just can't be typed.
15481551

15491552
~~~~
15501553
let n = option::none;
1551-
# n = option::some(1);
1554+
# option::may(n, fn&(&&x:int) {})
15521555
~~~~
15531556

15541557
If you never do anything else with `n`, the compiler will not be able
@@ -1982,7 +1985,7 @@ parameters.
19821985
~~~~
19831986
# iface to_str { fn to_str() -> str; }
19841987
fn comma_sep<T: to_str>(elts: [T]) -> str {
1985-
let result = "", first = true;
1988+
let mut result = "", first = true;
19861989
for elt in elts {
19871990
if first { first = false; }
19881991
else { result += ", "; }
@@ -2094,7 +2097,7 @@ to leave off the `of` clause.
20942097
# fn mk_currency(x: int, s: str) {}
20952098
impl int_util for int {
20962099
fn times(b: fn(int)) {
2097-
let i = 0;
2100+
let mut i = 0;
20982101
while i < self { b(i); i += 1; }
20992102
}
21002103
fn dollars() -> currency {
@@ -2450,7 +2453,7 @@ Here is the function which implements the child task:
24502453
~~~~
24512454
fn stringifier(from_parent: comm::port<uint>,
24522455
to_parent: comm::chan<str>) {
2453-
let value: uint;
2456+
let mut value: uint;
24542457
do {
24552458
value = comm::recv(from_parent);
24562459
comm::send(to_parent, uint::to_str(value, 10u));

mk/target.mk

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,6 @@
1111
USE_SNAPSHOT_RUNTIME=0
1212
USE_SNAPSHOT_CORELIB=0
1313

14-
# Do not use --enforce-mut-vars in stage0, for now, as the snapshot
15-
# has an older version of the check.
16-
ENFORCE_MUT_VARS_0=
17-
ENFORCE_MUT_VARS_1=--enforce-mut-vars
18-
ENFORCE_MUT_VARS_2=--enforce-mut-vars
19-
ENFORCE_MUT_VARS_3=--enforce-mut-vars
20-
2114
define TARGET_STAGE_N
2215

2316
$$(TLIB$(1)_T_$(2)_H_$(3))/intrinsics.ll: \
@@ -41,8 +34,7 @@ $$(TLIB$(1)_T_$(2)_H_$(3))/$$(CFG_STDLIB): \
4134
$$(TLIB$(1)_T_$(2)_H_$(3))/$$(CFG_CORELIB) \
4235
$$(TSREQ$(1)_T_$(2)_H_$(3))
4336
@$$(call E, compile_and_link: $$@)
44-
$$(STAGE$(1)_T_$(2)_H_$(3)) $$(ENFORCE_MUT_VARS_$(1)) \
45-
-o $$@ $$< && touch $$@
37+
$$(STAGE$(1)_T_$(2)_H_$(3)) -o $$@ $$< && touch $$@
4638

4739
$$(TLIB$(1)_T_$(2)_H_$(3))/$$(CFG_RUSTLLVM): \
4840
rustllvm/$(2)/$$(CFG_RUSTLLVM)
@@ -53,8 +45,7 @@ $$(TBIN$(1)_T_$(2)_H_$(3))/rustc$$(X): \
5345
$$(RUSTC_INPUTS) \
5446
$$(TLIBRUSTC_DEFAULT$(1)_T_$(2)_H_$(3))
5547
@$$(call E, compile_and_link: $$@)
56-
$$(STAGE$(1)_T_$(2)_H_$(3)) $$(ENFORCE_MUT_VARS_$(1)) \
57-
-o $$@ $$<
48+
$$(STAGE$(1)_T_$(2)_H_$(3)) -o $$@ $$<
5849

5950
$$(TLIB$(1)_T_$(2)_H_$(3))/$$(CFG_LIBRUSTC): \
6051
$$(COMPILER_CRATE) $$(COMPILER_INPUTS) \
@@ -63,8 +54,7 @@ $$(TLIB$(1)_T_$(2)_H_$(3))/$$(CFG_LIBRUSTC): \
6354
$$(TCORELIB_DEFAULT$(1)_T_$(2)_H_$(3)) \
6455
$$(TSTDLIB_DEFAULT$(1)_T_$(2)_H_$(3))
6556
@$$(call E, compile_and_link: $$@)
66-
$$(STAGE$(1)_T_$(2)_H_$(3)) $$(ENFORCE_MUT_VARS_$(1)) \
67-
-o $$@ $$< && touch $$@
57+
$$(STAGE$(1)_T_$(2)_H_$(3)) -o $$@ $$< && touch $$@
6858

6959
endef
7060

@@ -127,7 +117,7 @@ $$(TLIB$(1)_T_$(2)_H_$(3))/$$(CFG_CORELIB): \
127117
$$(CORELIB_CRATE) $$(CORELIB_INPUTS) \
128118
$$(TSREQ$(1)_T_$(2)_H_$(3))
129119
@$$(call E, compile_and_link: $$@)
130-
$$(STAGE$(1)_T_$(2)_H_$(3)) --enforce-mut-vars -o $$@ $$< && touch $$@
120+
$$(STAGE$(1)_T_$(2)_H_$(3)) -o $$@ $$< && touch $$@
131121

132122
endef
133123

0 commit comments

Comments
 (0)