Skip to content

Commit

Permalink
make --enforce-mut-vars always on, add mut annotations to remaining f…
Browse files Browse the repository at this point in the history
…iles
  • Loading branch information
nikomatsakis committed Mar 22, 2012
1 parent ea60308 commit dc07280
Show file tree
Hide file tree
Showing 179 changed files with 519 additions and 508 deletions.
29 changes: 16 additions & 13 deletions doc/rust.md
Original file line number Diff line number Diff line change
Expand Up @@ -1003,7 +1003,7 @@ fn iter<T>(seq: [T], f: fn(T)) {
for elt: T in seq { f(elt); }
}
fn map<T, U>(seq: [T], f: fn(T) -> U) -> [U] {
let acc = [];
let mut acc = [];
for elt in seq { acc += [f(elt)]; }
acc
}
Expand Down Expand Up @@ -1104,7 +1104,7 @@ enum animal {
cat
}
let a: animal = dog;
let mut a: animal = dog;
a = cat;
~~~~

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

~~~~~~~~
# let x = [mut 0];
# let mut x = [mut 0];
# let a = [mut 0];
# let b = 0;
# let y = {mut z: 0};
Expand Down Expand Up @@ -1804,8 +1804,8 @@ expression. No allocation or destruction is entailed.
An example of three different swap expressions:

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

~~~~
# let x = 0;
# let mut x = 0;
# let y = 0;
x = y;
Expand Down Expand Up @@ -2015,7 +2015,7 @@ loop body. If it evaluates to `false`, control exits the loop.
An example of a simple `while` expression:

~~~~
# let i = 0;
# let mut i = 0;
# let println = io::println;
while i < 10 {
Expand All @@ -2027,7 +2027,7 @@ while i < 10 {
An example of a `do`-`while` expression:

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

~~~~
fn count() -> bool {
let i = 0;
let mut i = 0;
loop {
i += 1;
if i == 20 { ret true; }
Expand Down Expand Up @@ -2801,7 +2801,7 @@ fn add(x: int, y: int) -> int {
ret x + y;
}
let x = add(5,7);
let mut x = add(5,7);
type binop = fn(int,int) -> int;
let bo: binop = add;
Expand Down Expand Up @@ -2880,7 +2880,7 @@ has a set of points before and after it in the implied control flow.
For example, this code:

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

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

Local variables are not initialized when allocated; the entire frame worth of
local variables are allocated at once, on frame-entry, in an uninitialized
Expand Down
39 changes: 21 additions & 18 deletions doc/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ a curly-brace language in the tradition of C, C++, and JavaScript.

~~~~
fn fac(n: int) -> int {
let result = 1, i = 1;
let mut result = 1, i = 1;
while i <= n {
result *= i;
i += 1;
Expand Down Expand Up @@ -286,16 +286,19 @@ fn this_doesnt(_x: int) {}
## Variable declaration
The `let` keyword, as we've seen, introduces a local variable. Global
constants can be defined with `const`:
The `let` keyword, as we've seen, introduces a local variable. Local
variables are immutable by default: `let mut` can be used to introduce
a local variable that can be reassigned. Global constants can be
defined with `const`:
~~~~
use std;
const repeat: uint = 5u;
fn main() {
let count = 0u;
let hi = "Hi!";
let mut count = 0u;
while count < repeat {
io::println("Hi!");
io::println(hi);
count += 1u;
}
}
Expand All @@ -320,7 +323,7 @@ annotation:
~~~~
// The type of this vector will be inferred based on its use.
let x = [];
# x = [3];
# vec::map(x, fn&(&&_y:int) -> int { _y });
// Explicitly say this is a vector of integers.
let y: [int] = [];
~~~~
Expand Down Expand Up @@ -665,7 +668,7 @@ keyword `break` can be used to abort the loop, and `cont` can be used
to abort the current iteration and continue with the next.

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

~~~~
let x = 100;
let mut x = 100;
while (x > 10) { x -= 10; }
assert x == 10;
~~~~
Expand Down Expand Up @@ -933,7 +936,7 @@ of integers backwards:

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

~~~~
let myvec = [], i = 0;
let mut myvec = [], i = 0;
while i < 100 {
myvec += [i];
i += 1;
Expand Down Expand Up @@ -1376,7 +1379,7 @@ in `main`, so we're good. But the call could also look like this:
~~~~
# fn myfunc(a: int, b: fn()) {}
# fn get_another_record() -> int { 1 }
# let x = 1;
# let mut x = 1;
myfunc(x, {|| x = get_another_record(); });
~~~~

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

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

~~~~
fn for_rev<T>(v: [T], act: fn(T)) {
let i = vec::len(v);
let mut i = vec::len(v);
while i > 0u {
i -= 1u;
act(v[i]);
}
}
fn map<T, U>(v: [T], f: fn(T) -> U) -> [U] {
let acc = [];
let mut acc = [];
for elt in v { acc += [f(elt)]; }
ret acc;
}
Expand Down Expand Up @@ -1548,7 +1551,7 @@ programs that just can't be typed.

~~~~
let n = option::none;
# n = option::some(1);
# option::may(n, fn&(&&x:int) {})
~~~~

If you never do anything else with `n`, the compiler will not be able
Expand Down Expand Up @@ -1982,7 +1985,7 @@ parameters.
~~~~
# iface to_str { fn to_str() -> str; }
fn comma_sep<T: to_str>(elts: [T]) -> str {
let result = "", first = true;
let mut result = "", first = true;
for elt in elts {
if first { first = false; }
else { result += ", "; }
Expand Down Expand Up @@ -2094,7 +2097,7 @@ to leave off the `of` clause.
# fn mk_currency(x: int, s: str) {}
impl int_util for int {
fn times(b: fn(int)) {
let i = 0;
let mut i = 0;
while i < self { b(i); i += 1; }
}
fn dollars() -> currency {
Expand Down Expand Up @@ -2450,7 +2453,7 @@ Here is the function which implements the child task:
~~~~
fn stringifier(from_parent: comm::port<uint>,
to_parent: comm::chan<str>) {
let value: uint;
let mut value: uint;
do {
value = comm::recv(from_parent);
comm::send(to_parent, uint::to_str(value, 10u));
Expand Down
18 changes: 4 additions & 14 deletions mk/target.mk
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,6 @@
USE_SNAPSHOT_RUNTIME=0
USE_SNAPSHOT_CORELIB=0

# Do not use --enforce-mut-vars in stage0, for now, as the snapshot
# has an older version of the check.
ENFORCE_MUT_VARS_0=
ENFORCE_MUT_VARS_1=--enforce-mut-vars
ENFORCE_MUT_VARS_2=--enforce-mut-vars
ENFORCE_MUT_VARS_3=--enforce-mut-vars

define TARGET_STAGE_N

$$(TLIB$(1)_T_$(2)_H_$(3))/intrinsics.ll: \
Expand All @@ -41,8 +34,7 @@ $$(TLIB$(1)_T_$(2)_H_$(3))/$$(CFG_STDLIB): \
$$(TLIB$(1)_T_$(2)_H_$(3))/$$(CFG_CORELIB) \
$$(TSREQ$(1)_T_$(2)_H_$(3))
@$$(call E, compile_and_link: $$@)
$$(STAGE$(1)_T_$(2)_H_$(3)) $$(ENFORCE_MUT_VARS_$(1)) \
-o $$@ $$< && touch $$@
$$(STAGE$(1)_T_$(2)_H_$(3)) -o $$@ $$< && touch $$@

$$(TLIB$(1)_T_$(2)_H_$(3))/$$(CFG_RUSTLLVM): \
rustllvm/$(2)/$$(CFG_RUSTLLVM)
Expand All @@ -53,8 +45,7 @@ $$(TBIN$(1)_T_$(2)_H_$(3))/rustc$$(X): \
$$(RUSTC_INPUTS) \
$$(TLIBRUSTC_DEFAULT$(1)_T_$(2)_H_$(3))
@$$(call E, compile_and_link: $$@)
$$(STAGE$(1)_T_$(2)_H_$(3)) $$(ENFORCE_MUT_VARS_$(1)) \
-o $$@ $$<
$$(STAGE$(1)_T_$(2)_H_$(3)) -o $$@ $$<

$$(TLIB$(1)_T_$(2)_H_$(3))/$$(CFG_LIBRUSTC): \
$$(COMPILER_CRATE) $$(COMPILER_INPUTS) \
Expand All @@ -63,8 +54,7 @@ $$(TLIB$(1)_T_$(2)_H_$(3))/$$(CFG_LIBRUSTC): \
$$(TCORELIB_DEFAULT$(1)_T_$(2)_H_$(3)) \
$$(TSTDLIB_DEFAULT$(1)_T_$(2)_H_$(3))
@$$(call E, compile_and_link: $$@)
$$(STAGE$(1)_T_$(2)_H_$(3)) $$(ENFORCE_MUT_VARS_$(1)) \
-o $$@ $$< && touch $$@
$$(STAGE$(1)_T_$(2)_H_$(3)) -o $$@ $$< && touch $$@

endef

Expand Down Expand Up @@ -127,7 +117,7 @@ $$(TLIB$(1)_T_$(2)_H_$(3))/$$(CFG_CORELIB): \
$$(CORELIB_CRATE) $$(CORELIB_INPUTS) \
$$(TSREQ$(1)_T_$(2)_H_$(3))
@$$(call E, compile_and_link: $$@)
$$(STAGE$(1)_T_$(2)_H_$(3)) --enforce-mut-vars -o $$@ $$< && touch $$@
$$(STAGE$(1)_T_$(2)_H_$(3)) -o $$@ $$< && touch $$@

endef

Expand Down
Loading

0 comments on commit dc07280

Please sign in to comment.