Skip to content

Commit ed3f6f9

Browse files
committed
---
yaml --- r: 138619 b: refs/heads/try2 c: febdb49 h: refs/heads/master i: 138617: 4d7eaf4 138615: f549390 v: v3
1 parent a9e857b commit ed3f6f9

File tree

398 files changed

+3763
-4843
lines changed

Some content is hidden

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

398 files changed

+3763
-4843
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ refs/heads/snap-stage3: 78a7676898d9f80ab540c6df5d4c9ce35bb50463
55
refs/heads/try: 519addf6277dbafccbb4159db4b710c37eaa2ec5
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
8-
refs/heads/try2: 9519ee5d808d8015faad6681693cc406e00c9f81
8+
refs/heads/try2: febdb49e9269724058aacf645610912bf26ecdb4
99
refs/heads/dist-snap: ba4081a5a8573875fed17545846f6f6902c8ba8d
1010
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1111
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503

branches/try2/configure

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -678,7 +678,7 @@ do
678678
LLVM_BUILD_DIR=${CFG_BUILD_DIR}llvm/$t
679679
if [ ! -z "$CFG_DISABLE_OPTIMIZE_LLVM" ]
680680
then
681-
LLVM_DBG_OPTS="--enable-debug-symbols --disable-optimized"
681+
LLVM_DBG_OPTS=""
682682
# Just use LLVM straight from its build directory to
683683
# avoid 'make install' time
684684
LLVM_INST_DIR=$LLVM_BUILD_DIR/Debug+Asserts

branches/try2/doc/rust.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ the following forms:
297297
num_lit : nonzero_dec [ dec_digit | '_' ] * num_suffix ?
298298
| '0' [ [ dec_digit | '_' ] + num_suffix ?
299299
| 'b' [ '1' | '0' | '_' ] + int_suffix ?
300-
| 'x' [ hex_digit | '_' ] + int_suffix ? ] ;
300+
| 'x' [ hex_digit | '-' ] + int_suffix ? ] ;
301301
302302
num_suffix : int_suffix | float_suffix ;
303303
@@ -1610,10 +1610,11 @@ The following are examples of structure expressions:
16101610
~~~~
16111611
# struct Point { x: float, y: float }
16121612
# struct TuplePoint(float, float);
1613-
# mod game { pub struct User { name: &str, age: uint, score: uint } }
1613+
# mod game { pub struct User { name: &str, age: uint, mut score: uint } }
1614+
# use game;
16141615
Point {x: 10f, y: 20f};
16151616
TuplePoint(10f, 20f);
1616-
let u = game::User {name: "Joe", age: 35u, score: 100_000};
1617+
let u = game::User {name: "Joe", age: 35u, mut score: 100_000};
16171618
~~~~
16181619

16191620
A structure expression forms a new value of the named structure type.

branches/try2/doc/tutorial-borrowed-ptr.md

Lines changed: 157 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -348,12 +348,12 @@ mutations:
348348
~~~ {.xfail-test}
349349
fn example3() -> int {
350350
struct R { g: int }
351-
struct S { f: ~R }
351+
struct S { mut f: ~R }
352352
353-
let mut x = ~S {f: ~R {g: 3}};
353+
let mut x = ~S {mut f: ~R {g: 3}};
354354
let y = &x.f.g;
355-
x = ~S {f: ~R {g: 4}}; // Error reported here.
356-
x.f = ~R {g: 5}; // Error reported here.
355+
x = ~S {mut f: ~R {g: 4}}; // Error reported here.
356+
x.f = ~R {g: 5}; // Error reported here.
357357
*y
358358
}
359359
~~~
@@ -362,6 +362,91 @@ In this case, two errors are reported, one when the variable `x` is
362362
modified and another when `x.f` is modified. Either modification would
363363
invalidate the pointer `y`.
364364

365+
Things get trickier when the unique box is not uniquely owned by the
366+
stack frame, or when there is no way for the compiler to determine the
367+
box's owner. Consider a program like this:
368+
369+
~~~ {.xfail-test}
370+
struct R { g: int }
371+
struct S { mut f: ~R }
372+
fn example5a(x: @S, callback: @fn()) -> int {
373+
let y = &x.f.g; // Error reported here.
374+
...
375+
callback();
376+
...
377+
# return 0;
378+
}
379+
~~~
380+
381+
Here the heap looks something like:
382+
383+
~~~ {.notrust}
384+
Stack Managed Heap Exchange Heap
385+
386+
x +------+ +-------------+ +------+
387+
| @... | ----> | mut f: ~... | --+-> | g: 3 |
388+
y +------+ +-------------+ | +------+
389+
| &int | -------------------------+
390+
+------+
391+
~~~
392+
393+
In this case, the owning reference to the value being borrowed is
394+
`x.f`. Moreover, `x.f` is both mutable and *aliasable*. Aliasable
395+
means that there may be other pointers to that same managed box, so
396+
even if the compiler were to prove an absence of mutations to `x.f`,
397+
code could mutate `x.f` indirectly by changing an alias of
398+
`x`. Therefore, to be safe, the compiler only accepts *pure* actions
399+
during the lifetime of `y`. We define what "pure" means in the section
400+
on [purity](#purity).
401+
402+
Besides ensuring purity, the only way to borrow the interior of a
403+
unique found in aliasable memory is to ensure that the borrowed field
404+
itself is also unique, as in the following example:
405+
406+
~~~
407+
struct R { g: int }
408+
struct S { f: ~R }
409+
fn example5b(x: @S) -> int {
410+
let y = &x.f.g;
411+
...
412+
# return 0;
413+
}
414+
~~~
415+
416+
Here, the field `f` is not declared as mutable. But that is enough for
417+
the compiler to know that, even if aliases to `x` exist, the field `f`
418+
cannot be changed and hence the unique box `g` will remain valid.
419+
420+
If you do have a unique box in a mutable field, and you wish to borrow
421+
it, one option is to use the swap operator to move that unique box
422+
onto your stack:
423+
424+
~~~
425+
struct R { g: int }
426+
struct S { mut f: ~R }
427+
fn example5c(x: @S) -> int {
428+
let mut v = ~R {g: 0};
429+
v <-> x.f; // Swap v and x.f
430+
{ // Block constrains the scope of `y`:
431+
let y = &v.g;
432+
...
433+
}
434+
x.f = v; // Replace x.f
435+
...
436+
# return 0;
437+
}
438+
~~~
439+
440+
Of course, this has the side effect of modifying your managed box for
441+
the duration of the borrow, so it only works when you know that you
442+
won't be accessing that same box for the duration of the loan. Also,
443+
it is sometimes necessary to introduce additional blocks to constrain
444+
the scope of the loan. In this example, the borrowed pointer `y`
445+
would still be in scope when you moved the value `v` back into `x.f`,
446+
and hence moving `v` would be considered illegal. You cannot move
447+
values if they are the targets of valid outstanding loans. Introducing
448+
the block restricts the scope of `y`, making the move legal.
449+
365450
# Borrowing and enums
366451

367452
The previous example showed that the type system forbids any borrowing
@@ -473,6 +558,11 @@ permit `ref` bindings into data owned by the stack frame even if the
473558
data are mutable, but otherwise it requires that the data reside in
474559
immutable memory.
475560

561+
> ***Note:*** Right now, pattern bindings not explicitly annotated
562+
> with `ref` or `copy` use a special mode of "implicit by reference".
563+
> This is changing as soon as we finish updating all the existing code
564+
> in the compiler that relies on the current settings.
565+
476566
# Returning borrowed pointers
477567

478568
So far, all of the examples we've looked at use borrowed pointers in a
@@ -655,6 +745,69 @@ fn select<T>(shape: &Shape, threshold: float,
655745

656746
This is equivalent to the previous definition.
657747

748+
# Purity
749+
750+
As mentioned before, the Rust compiler offers a kind of escape hatch
751+
that permits borrowing of any data, as long as the actions that occur
752+
during the lifetime of the borrow are pure. Pure actions are those
753+
that only modify data owned by the current stack frame. The compiler
754+
can therefore permit arbitrary pointers into the heap, secure in the
755+
knowledge that no pure action will ever cause them to become
756+
invalidated (the compiler must still track data on the stack which is
757+
borrowed and enforce those rules normally, of course). A pure function
758+
in Rust is referentially transparent: it returns the same results
759+
given the same (observably equivalent) inputs. That is because while
760+
pure functions are allowed to modify data, they may only modify
761+
*stack-local* data, which cannot be observed outside the scope of the
762+
function itself. (Using an `unsafe` block invalidates this guarantee.)
763+
764+
Let’s revisit a previous example and show how purity can affect
765+
typechecking. Here is `example5a()`, which borrows the interior of a
766+
unique box found in an aliasable, mutable location, only now we’ve
767+
replaced the `...` with some specific code:
768+
769+
~~~
770+
struct R { g: int }
771+
struct S { mut f: ~R }
772+
fn example5a(x: @S ...) -> int {
773+
let y = &x.f.g; // Unsafe
774+
*y + 1
775+
}
776+
~~~
777+
778+
The new code simply returns an incremented version of `y`. This code
779+
clearly doesn't mutate the heap, so the compiler is satisfied.
780+
781+
But suppose we wanted to pull the increment code into a helper, like
782+
this:
783+
784+
~~~
785+
fn add_one(x: &int) -> int { *x + 1 }
786+
~~~
787+
788+
We can now update `example5a()` to use `add_one()`:
789+
790+
~~~
791+
# struct R { g: int }
792+
# struct S { mut f: ~R }
793+
# pure fn add_one(x: &int) -> int { *x + 1 }
794+
fn example5a(x: @S ...) -> int {
795+
let y = &x.f.g;
796+
add_one(y) // Error reported here
797+
}
798+
~~~
799+
800+
But now the compiler will report an error again. The reason is that it
801+
only considers one function at a time (like most typecheckers), and
802+
so it does not know that `add_one()` consists of pure code. We can
803+
help the compiler by labeling `add_one()` as pure:
804+
805+
~~~
806+
pure fn add_one(x: &int) -> int { *x + 1 }
807+
~~~
808+
809+
With this change, the modified version of `example5a()` will again compile.
810+
658811
# Conclusion
659812

660813
So there you have it: a (relatively) brief tour of the borrowed pointer

branches/try2/doc/tutorial-ffi.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -220,21 +220,21 @@ extern mod std;
220220
use libc::c_ulonglong;
221221
222222
struct timeval {
223-
tv_sec: c_ulonglong,
224-
tv_usec: c_ulonglong
223+
mut tv_sec: c_ulonglong,
224+
mut tv_usec: c_ulonglong
225225
}
226226
227227
#[nolink]
228228
extern mod lib_c {
229-
fn gettimeofday(tv: *mut timeval, tz: *()) -> i32;
229+
fn gettimeofday(tv: *timeval, tz: *()) -> i32;
230230
}
231231
fn unix_time_in_microseconds() -> u64 {
232232
unsafe {
233-
let mut x = timeval {
234-
tv_sec: 0 as c_ulonglong,
235-
tv_usec: 0 as c_ulonglong
233+
let x = timeval {
234+
mut tv_sec: 0 as c_ulonglong,
235+
mut tv_usec: 0 as c_ulonglong
236236
};
237-
lib_c::gettimeofday(&mut x, ptr::null());
237+
lib_c::gettimeofday(ptr::addr_of(&x), ptr::null());
238238
return (x.tv_sec as u64) * 1000_000_u64 + (x.tv_usec as u64);
239239
}
240240
}

branches/try2/doc/tutorial-tasks.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,7 @@ Here is the function that implements the child task:
468468

469469
~~~~
470470
# use std::comm::DuplexStream;
471+
# use comm::{Port, Chan};
471472
fn stringifier(channel: &DuplexStream<~str, uint>) {
472473
let mut value: uint;
473474
loop {
@@ -490,6 +491,7 @@ Here is the code for the parent task:
490491

491492
~~~~
492493
# use std::comm::DuplexStream;
494+
# use comm::{Port, Chan};
493495
# use task::spawn;
494496
# fn stringifier(channel: &DuplexStream<~str, uint>) {
495497
# let mut value: uint;

branches/try2/doc/tutorial.md

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,7 @@ let mut x = 5;
556556
loop {
557557
x += x - 3;
558558
if x % 5 == 0 { break; }
559-
io::println(int::to_str(x));
559+
io::println(int::str(x));
560560
}
561561
~~~~
562562

@@ -583,16 +583,19 @@ Inherited mutability means that any field of a struct may be mutable, if the
583583
struct is in a mutable slot (or a field of a struct in a mutable slot, and
584584
so forth).
585585

586+
A struct that is not mutable due to inherited mutability may declare some
587+
of its fields nevertheless mutable, using the `mut` keyword.
588+
586589
~~~~
587590
struct Stack {
588591
content: ~[int],
589-
head: uint
592+
mut head: uint
590593
}
591594
~~~~
592595

593-
With a value (say, `mystack`) of such a type in a mutable location, you can do
594-
`mystack.head += 1`. But in an immutable location, such an assignment to a
595-
struct without inherited mutability would result in a type error.
596+
With a value of such a type, you can do `mystack.head += 1`. If `mut` were
597+
omitted from the type, such an assignment to a struct without inherited
598+
mutability would result in a type error.
596599

597600
`match` patterns destructure structs. The basic syntax is
598601
`Name { fieldname: pattern, ... }`:
@@ -935,19 +938,19 @@ type that contains managed boxes or other managed types.
935938
~~~
936939
// A linked list node
937940
struct Node {
938-
next: MaybeNode,
939-
prev: MaybeNode,
941+
mut next: MaybeNode,
942+
mut prev: MaybeNode,
940943
payload: int
941944
}
942945
943946
enum MaybeNode {
944-
SomeNode(@mut Node),
947+
SomeNode(@Node),
945948
NoNode
946949
}
947950
948-
let node1 = @mut Node { next: NoNode, prev: NoNode, payload: 1 };
949-
let node2 = @mut Node { next: NoNode, prev: NoNode, payload: 2 };
950-
let node3 = @mut Node { next: NoNode, prev: NoNode, payload: 3 };
951+
let node1 = @Node { next: NoNode, prev: NoNode, payload: 1 };
952+
let node2 = @Node { next: NoNode, prev: NoNode, payload: 2 };
953+
let node3 = @Node { next: NoNode, prev: NoNode, payload: 3 };
951954
952955
// Link the three list nodes together
953956
node1.next = SomeNode(node2);
@@ -1126,7 +1129,7 @@ points to.
11261129
11271130
~~~
11281131
let managed = @mut 10;
1129-
let mut owned = ~20;
1132+
let owned = ~mut 20;
11301133

11311134
let mut value = 30;
11321135
let borrowed = &mut value;
@@ -2270,9 +2273,7 @@ fn chicken_farmer() {
22702273
// The same, but name it `my_chicken`
22712274
use my_chicken = farm::chicken;
22722275
...
2273-
# my_chicken();
22742276
}
2275-
# chicken();
22762277
# }
22772278
~~~
22782279

@@ -2299,15 +2300,16 @@ mod farm {
22992300
# impl Human { fn rest(&self) { } }
23002301
# pub fn make_me_a_farm() -> farm::Farm { farm::Farm { chickens: ~[], cows: ~[], farmer: Human(0) } }
23012302
pub struct Farm {
2302-
priv chickens: ~[Chicken],
2303-
priv cows: ~[Cow],
2303+
priv mut chickens: ~[Chicken],
2304+
priv mut cows: ~[Cow],
23042305
farmer: Human
23052306
}
23062307
2308+
// Note - visibility modifiers on impls currently have no effect
23072309
impl Farm {
23082310
priv fn feed_chickens(&self) { ... }
23092311
priv fn feed_cows(&self) { ... }
2310-
pub fn add_chicken(&self, c: Chicken) { ... }
2312+
fn add_chicken(&self, c: Chicken) { ... }
23112313
}
23122314
23132315
pub fn feed_animals(farm: &Farm) {

branches/try2/src/etc/gedit/readme.txt

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)