Skip to content

Commit 1529ba8

Browse files
committed
---
yaml --- r: 23149 b: refs/heads/master c: 713487d h: refs/heads/master i: 23147: 3f34c71 v: v3
1 parent 9cd3200 commit 1529ba8

File tree

327 files changed

+6717
-3611
lines changed

Some content is hidden

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

327 files changed

+6717
-3611
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
refs/heads/master: 8d26d86b708b4597e761034fe3a2cc8a7a08c85f
2+
refs/heads/master: 713487ddd3c47ead79ccb6566d6a029bd6a817a0
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: cd6f24f9d14ac90d167386a56e7a6ac1f0318195
55
refs/heads/try: ffbe0e0e00374358b789b0037bcb3a577cd218be

trunk/doc/rust.md

Lines changed: 25 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -899,58 +899,51 @@ express that `f` requires no explicit `return`, as if it returns
899899
control to the caller, it returns a value (true because it never returns
900900
control).
901901

902-
#### Predicate functions
902+
#### Pure functions
903903

904-
Any pure boolean function is called a *predicate function*, and may be used in
905-
a [constraint](#constraints), as part of the static [typestate
906-
system](#typestate-system). A predicate declaration is identical to a function
907-
declaration, except that it is declared with the additional keyword `pure`. In
908-
addition, the typechecker checks the body of a predicate with a restricted set
909-
of typechecking rules. A predicate
904+
A pure function declaration is identical to a function declaration, except that
905+
it is declared with the additional keyword `pure`. In addition, the typechecker
906+
checks the body of a pure function with a restricted set of typechecking rules.
907+
A pure function
910908

911909
* may not contain an assignment or self-call expression; and
912-
* may only call other predicates, not general functions.
910+
* may only call other pure functions, not general functions.
913911

914-
An example of a predicate:
912+
An example of a pure function:
915913

916914
~~~~
917915
pure fn lt_42(x: int) -> bool {
918916
return (x < 42);
919917
}
920918
~~~~
921919

922-
A non-boolean function may also be declared with `pure fn`. This allows
923-
predicates to call non-boolean functions as long as they are pure. For example:
920+
Pure functions may call other pure functions:
924921

925922
~~~~{.xfail-test}
926923
pure fn pure_length<T>(ls: list<T>) -> uint { /* ... */ }
927924
928925
pure fn nonempty_list<T>(ls: list<T>) -> bool { pure_length(ls) > 0u }
929926
~~~~
930927

931-
In this example, `nonempty_list` is a predicate---it can be used in a
932-
typestate constraint---but the auxiliary function `pure_length` is
933-
not.
934-
935928
*TODO:* should actually define referential transparency.
936929

937930
The effect checking rules previously enumerated are a restricted set of
938931
typechecking rules meant to approximate the universe of observably
939932
referentially transparent Rust procedures conservatively. Sometimes, these
940933
rules are *too* restrictive. Rust allows programmers to violate these rules by
941-
writing predicates that the compiler cannot prove to be referentially
934+
writing pure functions that the compiler cannot prove to be referentially
942935
transparent, using an escape-hatch feature called "unchecked blocks". When
943936
writing code that uses unchecked blocks, programmers should always be aware
944937
that they have an obligation to show that the code *behaves* referentially
945938
transparently at all times, even if the compiler cannot *prove* automatically
946939
that the code is referentially transparent. In the presence of unchecked
947940
blocks, the compiler provides no static guarantee that the code will behave as
948941
expected at runtime. Rather, the programmer has an independent obligation to
949-
verify the semantics of the predicates they write.
942+
verify the semantics of the pure functions they write.
950943

951944
*TODO:* last two sentences are vague.
952945

953-
An example of a predicate that uses an unchecked block:
946+
An example of a pure function that uses an unchecked block:
954947

955948
~~~~
956949
# import std::list::*;
@@ -972,7 +965,7 @@ pure fn pure_length<T>(ls: list<T>) -> uint {
972965

973966
Despite its name, `pure_foldl` is a `fn`, not a `pure fn`, because there is no
974967
way in Rust to specify that the higher-order function argument `f` is a pure
975-
function. So, to use `foldl` in a pure list length function that a predicate
968+
function. So, to use `foldl` in a pure list length function that a pure function
976969
could then use, we must use an `unchecked` block wrapped around the call to
977970
`pure_foldl` in the definition of `pure_length`.
978971

@@ -1136,8 +1129,8 @@ looks like:
11361129

11371130
The only exception is that the body of the class constructor begins
11381131
with all the class's fields uninitialized, and is allowed to -- in
1139-
fact, must -- initialize all the fields. A special case in the
1140-
typestate pass enforces this invariant.
1132+
fact, must -- initialize all the fields. The compiler enforces this
1133+
invariant.
11411134

11421135
Usually, the class constructor stores its argument or arguments in the
11431136
class's named fields. In this case, the `file_descriptor`'s data field
@@ -1306,7 +1299,7 @@ type.
13061299

13071300
~~~~
13081301
# trait shape { }
1309-
# impl of shape for int { }
1302+
# impl int: shape { }
13101303
# let mycircle = 0;
13111304
13121305
let myshape: shape = mycircle as shape;
@@ -1332,7 +1325,7 @@ An _implementation item_ provides an implementation of a
13321325
13331326
type circle = {radius: float, center: point};
13341327
1335-
impl circle_shape of shape for circle {
1328+
impl circle: shape {
13361329
fn draw(s: surface) { do_draw_circle(s, self); }
13371330
fn bounding_box() -> bounding_box {
13381331
let r = self.radius;
@@ -1370,10 +1363,10 @@ specified, after the `impl` keyword.
13701363
~~~~
13711364
# trait seq<T> { }
13721365
1373-
impl <T> of seq<T> for ~[T] {
1366+
impl<T> ~[T]: seq<T> {
13741367
/* ... */
13751368
}
1376-
impl of seq<bool> for u32 {
1369+
impl u32: seq<bool> {
13771370
/* Treat the integer as a sequence of bits */
13781371
}
13791372
~~~~
@@ -1934,13 +1927,15 @@ x <- copy y;
19341927

19351928
The former is just more terse and familiar.
19361929

1937-
#### Operator-assignment expressions
1930+
#### Compound assignment expressions
19381931

19391932
The `+`, `-`, `*`, `/`, `%`, `&`, `|`, `^`, `<<`, `>>`, and `>>>`
19401933
operators may be composed with the `=` operator. The expression `lval
19411934
OP= val` is equivalent to `lval = lval OP val`. For example, `x = x +
19421935
1` may be written as `x += 1`.
19431936

1937+
Any such expression always has the [`nil`](#primitive-types) type.
1938+
19441939
#### Operator precedence
19451940

19461941
The precedence of Rust binary operators is ordered as follows, going
@@ -2074,31 +2069,6 @@ A `loop` expression denotes an infinite loop:
20742069
loop_expr : "loop" '{' block '}';
20752070
~~~~~~~~
20762071

2077-
For a block `b`, the expression `loop b` is semantically equivalent to
2078-
`while true b`. However, `loop`s differ from `while` loops in that the
2079-
typestate analysis pass takes into account that `loop`s are infinite.
2080-
2081-
For example, the following (contrived) function uses a `loop` with a
2082-
`return` expression:
2083-
2084-
~~~~
2085-
fn count() -> bool {
2086-
let mut i = 0;
2087-
loop {
2088-
i += 1;
2089-
if i == 20 { return true; }
2090-
}
2091-
}
2092-
~~~~
2093-
2094-
This function compiles, because typestate recognizes that the `loop`
2095-
never terminates (except non-locally, with `return`), thus there is no
2096-
need to insert a spurious `fail` or `return` after the `loop`. If `loop`
2097-
were replaced with `while true`, the function would be rejected
2098-
because from the compiler's perspective, there would be a control path
2099-
along which `count` does not return a value (that is, if the loop
2100-
condition is always false).
2101-
21022072
### Break expressions
21032073

21042074
~~~~~~~~{.ebnf .gram}
@@ -2540,7 +2510,7 @@ macro-generated and user-written code can cause unintentional capture.
25402510
Future versions of Rust will address these issues.
25412511

25422512

2543-
# Types and typestates
2513+
# Type system
25442514

25452515
## Types
25462516

@@ -2758,7 +2728,7 @@ trait printable {
27582728
fn to_str() -> ~str;
27592729
}
27602730
2761-
impl of printable for ~str {
2731+
impl ~str: printable {
27622732
fn to_str() -> ~str { self }
27632733
}
27642734
@@ -2805,7 +2775,7 @@ trait printable {
28052775
fn to_str() -> ~str;
28062776
}
28072777
2808-
impl of printable for ~str {
2778+
impl ~str: printable {
28092779
fn to_str() -> ~str { self }
28102780
}
28112781
~~~~~~
@@ -2966,7 +2936,7 @@ Local variables are not initialized when allocated; the entire frame worth of
29662936
local variables are allocated at once, on frame-entry, in an uninitialized
29672937
state. Subsequent statements within a function may or may not initialize the
29682938
local variables. Local variables can be used only after they have been
2969-
initialized; this condition is guaranteed by the typestate system.
2939+
initialized; this is enforced by the compiler.
29702940

29712941
References are created for function arguments. If the compiler can not prove
29722942
that the referred-to value will outlive the reference, it will try to set

0 commit comments

Comments
 (0)