Skip to content

Commit 8903885

Browse files
committed
---
yaml --- r: 44870 b: refs/heads/master c: aa3505d h: refs/heads/master v: v3
1 parent 380c3f4 commit 8903885

Some content is hidden

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

101 files changed

+3204
-2332
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: 95bc9ea26df56b29f74583317ab080fdc7b99757
2+
refs/heads/master: aa3505d8ff043f0c1da62de4f517eed6defb6187
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: a6d9689399d091c3265f00434a69c551a61c28dc
55
refs/heads/try: ef355f6332f83371e4acf04fc4eb940ab41d78d3

trunk/doc/rust.md

Lines changed: 95 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1285,22 +1285,19 @@ An _implementation_ is an item that implements a [trait](#traits) for a specific
12851285
Implementations are defined with the keyword `impl`.
12861286

12871287
~~~~
1288-
# struct Point {x: float, y: float};
1288+
# type Point = {x: float, y: float};
12891289
# type Surface = int;
1290-
# struct BoundingBox {x: float, y: float, width: float, height: float};
1290+
# type BoundingBox = {x: float, y: float, width: float, height: float};
12911291
# trait Shape { fn draw(Surface); fn bounding_box() -> BoundingBox; }
12921292
# fn do_draw_circle(s: Surface, c: Circle) { }
12931293

1294-
struct Circle {
1295-
radius: float,
1296-
center: Point,
1297-
}
1294+
type Circle = {radius: float, center: Point};
12981295

12991296
impl Shape for Circle {
13001297
fn draw(s: Surface) { do_draw_circle(s, self); }
13011298
fn bounding_box() -> BoundingBox {
13021299
let r = self.radius;
1303-
BoundingBox{x: self.center.x - r, y: self.center.y - r,
1300+
{x: self.center.x - r, y: self.center.y - r,
13041301
width: 2.0 * r, height: 2.0 * r}
13051302
}
13061303
}
@@ -1640,6 +1637,38 @@ rec_expr : '{' ident ':' expr
16401637
[ ".." expr ] '}'
16411638
~~~~~~~~
16421639

1640+
> **Note:** In future versions of Rust, record expressions and [record types](#record-types) will be removed.
1641+
1642+
A [_record_](#record-types) _expression_ is one or more comma-separated
1643+
name-value pairs enclosed by braces. A fieldname can be any identifier,
1644+
and is separated from its value expression by a
1645+
colon. To indicate that a field is mutable, the `mut` keyword is
1646+
written before its name.
1647+
1648+
~~~~
1649+
{x: 10f, y: 20f};
1650+
{name: "Joe", age: 35u, score: 100_000};
1651+
{ident: "X", mut count: 0u};
1652+
~~~~
1653+
1654+
The order of the fields in a record expression is significant, and
1655+
determines the type of the resulting value. `{a: u8, b: u8}` and `{b:
1656+
u8, a: u8}` are two different fields.
1657+
1658+
A record expression can terminate with the syntax `..` followed by an
1659+
expression to denote a functional update. The expression following
1660+
`..` (the base) must be of a record type that includes at least all the
1661+
fields mentioned in the record expression. A new record will be
1662+
created, of the same type as the base expression, with the given
1663+
values for the fields that were explicitly specified, and the values
1664+
in the base record for all other fields. The ordering of the fields in
1665+
such a record expression is not significant.
1666+
1667+
~~~~
1668+
let base = {x: 1, y: 2, z: 3};
1669+
{y: 0, z: 10, .. base};
1670+
~~~~
1671+
16431672
### Method-call expressions
16441673

16451674
~~~~~~~~{.ebnf .gram}
@@ -1660,7 +1689,7 @@ field_expr : expr '.' ident
16601689

16611690
A _field expression_ consists of an expression followed by a single dot and an identifier,
16621691
when not immediately followed by a parenthesized expression-list (the latter is a [method call expression](#method-call-expressions)).
1663-
A field expression denotes a field of a [structure](#structure-types).
1692+
A field expression denotes a field of a [structure](#structure-types) or [record](#record-types).
16641693

16651694
~~~~~~~~ {.field}
16661695
myrecord.myfield;
@@ -1876,10 +1905,8 @@ An example of three different swap expressions:
18761905
# let mut x = &mut [0];
18771906
# let mut a = &mut [0];
18781907
# let i = 0;
1879-
# struct S1 { z: int };
1880-
# struct S2 { c: int };
1881-
# let mut y = S1{z: 0};
1882-
# let mut b = S2{c: 0};
1908+
# let y = {mut z: 0};
1909+
# let b = {mut c: 0};
18831910

18841911
x <-> a;
18851912
x[i] <-> a[i];
@@ -2301,6 +2328,42 @@ match x {
23012328
}
23022329
~~~~
23032330

2331+
Records and structures can also be pattern-matched and their fields bound to variables.
2332+
When matching fields of a record,
2333+
the fields being matched are specified first,
2334+
then a placeholder (`_`) represents the remaining fields.
2335+
2336+
~~~~
2337+
# type options = {choose: bool, size: ~str};
2338+
# type player = {player: ~str, stats: (), options: options};
2339+
# fn load_stats() { }
2340+
# fn choose_player(r: &player) { }
2341+
# fn next_player() { }
2342+
2343+
fn main() {
2344+
let r = {
2345+
player: ~"ralph",
2346+
stats: load_stats(),
2347+
options: {
2348+
choose: true,
2349+
size: ~"small"
2350+
}
2351+
};
2352+
2353+
match r {
2354+
{options: {choose: true, _}, _} => {
2355+
choose_player(&r)
2356+
}
2357+
{player: ref p, options: {size: ~"small", _}, _} => {
2358+
log(info, (copy *p) + ~" is small");
2359+
}
2360+
_ => {
2361+
next_player();
2362+
}
2363+
}
2364+
}
2365+
~~~~
2366+
23042367
Patterns that bind variables default to binding to a copy of the matched value. This can be made
23052368
explicit using the ```copy``` keyword, changed to bind to a borrowed pointer by using the ```ref```
23062369
keyword, or to a mutable borrowed pointer using ```ref mut```, or the value can be moved into
@@ -2629,6 +2692,25 @@ let a: List<int> = Cons(7, @Cons(13, @Nil));
26292692
~~~~
26302693

26312694

2695+
### Record types
2696+
2697+
> **Note:** Records are not nominal types, thus do not directly support recursion, visibility control,
2698+
> out-of-order field initialization, or coherent trait implementation.
2699+
> Records are therefore deprecated and will be removed in future versions of Rust.
2700+
> [Structure types](#structure-types) should be used instead.
2701+
2702+
The record type-constructor forms a new heterogeneous product of values.
2703+
Fields of a record type are accessed by name and are arranged in memory in the order specified by the record type.
2704+
2705+
An example of a record type and its use:
2706+
2707+
~~~~
2708+
type Point = {x: int, y: int};
2709+
let p: Point = {x: 10, y: 11};
2710+
let px: int = p.x;
2711+
~~~~
2712+
2713+
26322714
### Pointer types
26332715

26342716
All pointers in Rust are explicit first-class values.
@@ -2958,8 +3040,7 @@ Some operations (such as field selection) implicitly dereference boxes. An
29583040
example of an _implicit dereference_ operation performed on box values:
29593041

29603042
~~~~~~~~
2961-
struct Foo { y: int }
2962-
let x = @Foo{y: 10};
3043+
let x = @{y: 10};
29633044
assert x.y == 10;
29643045
~~~~~~~~
29653046

trunk/doc/tutorial-borrowed-ptr.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,9 @@ operator. For example, I could write:
166166
# struct Point {x: float, y: float} // as before
167167
# struct Size {w: float, h: float} // as before
168168
# struct Rectangle {origin: Point, size: Size}
169-
# let rect_stack = &Rectangle {origin: Point {x: 1f, y: 2f}, size: Size {w: 3f, h: 4f}};
170-
# let rect_managed = @Rectangle {origin: Point {x: 3f, y: 4f}, size: Size {w: 3f, h: 4f}};
171-
# let rect_unique = ~Rectangle {origin: Point {x: 5f, y: 6f}, size: Size {w: 3f, h: 4f}};
169+
# let rect_stack = &{origin: Point {x: 1f, y: 2f}, size: Size {w: 3f, h: 4f}};
170+
# let rect_managed = @{origin: Point {x: 3f, y: 4f}, size: Size {w: 3f, h: 4f}};
171+
# let rect_unique = ~{origin: Point {x: 5f, y: 6f}, size: Size {w: 3f, h: 4f}};
172172
# fn compute_distance(p1: &Point, p2: &Point) -> float { 0f }
173173
compute_distance(&rect_stack.origin, &rect_managed.origin);
174174
~~~
@@ -274,14 +274,13 @@ the following function is legal:
274274

275275
~~~
276276
# fn some_condition() -> bool { true }
277-
# struct Foo { f: int }
278277
fn example3() -> int {
279-
let mut x = ~Foo {f: 3};
278+
let mut x = ~{f: 3};
280279
if some_condition() {
281280
let y = &x.f; // -+ L
282281
return *y; // |
283282
} // -+
284-
x = ~Foo {f: 4};
283+
x = ~{f: 4};
285284
...
286285
# return 0;
287286
}

trunk/src/libcore/dvec.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
1+
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
22
// file at the top-level directory of this distribution and at
33
// http://rust-lang.org/COPYRIGHT.
44
//
@@ -62,17 +62,17 @@ pub struct DVec<A> {
6262

6363
/// Creates a new, empty dvec
6464
pub pure fn DVec<A>() -> DVec<A> {
65-
DVec {data: ~[]}
65+
DVec {mut data: ~[]}
6666
}
6767

6868
/// Creates a new dvec with a single element
6969
pub pure fn from_elem<A>(e: A) -> DVec<A> {
70-
DVec {data: ~[e]}
70+
DVec {mut data: ~[e]}
7171
}
7272

7373
/// Creates a new dvec with the contents of a vector
7474
pub pure fn from_vec<A>(v: ~[A]) -> DVec<A> {
75-
DVec {data: v}
75+
DVec {mut data: v}
7676
}
7777

7878
/// Consumes the vector and returns its contents

trunk/src/libcore/os.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
1+
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
22
// file at the top-level directory of this distribution and at
33
// http://rust-lang.org/COPYRIGHT.
44
//
@@ -322,8 +322,8 @@ pub struct Pipe { mut in: c_int, mut out: c_int }
322322
#[cfg(unix)]
323323
pub fn pipe() -> Pipe {
324324
unsafe {
325-
let mut fds = Pipe {in: 0 as c_int,
326-
out: 0 as c_int };
325+
let mut fds = Pipe {mut in: 0 as c_int,
326+
mut out: 0 as c_int };
327327
assert (libc::pipe(&mut fds.in) == (0 as c_int));
328328
return Pipe {in: fds.in, out: fds.out};
329329
}
@@ -339,8 +339,8 @@ pub fn pipe() -> Pipe {
339339
// fully understand. Here we explicitly make the pipe non-inheritable,
340340
// which means to pass it to a subprocess they need to be duplicated
341341
// first, as in rust_run_program.
342-
let mut fds = Pipe {in: 0 as c_int,
343-
out: 0 as c_int };
342+
let mut fds = Pipe { mut in: 0 as c_int,
343+
mut out: 0 as c_int };
344344
let res = libc::pipe(&mut fds.in, 1024 as c_uint,
345345
(libc::O_BINARY | libc::O_NOINHERIT) as c_int);
346346
assert (res == 0 as c_int);
@@ -566,17 +566,13 @@ pub fn path_exists(p: &Path) -> bool {
566566
*
567567
* If the given path is relative, return it prepended with the current working
568568
* directory. If the given path is already an absolute path, return it
569-
* as is.
569+
* as is. This is a shortcut for calling os::getcwd().unsafe_join(p)
570570
*/
571571
// NB: this is here rather than in path because it is a form of environment
572572
// querying; what it does depends on the process working directory, not just
573573
// the input paths.
574574
pub fn make_absolute(p: &Path) -> Path {
575-
if p.is_absolute {
576-
copy *p
577-
} else {
578-
getcwd().push_many(p.components)
579-
}
575+
getcwd().unsafe_join(p)
580576
}
581577

582578

trunk/src/libcore/ptr.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
1+
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
22
// file at the top-level directory of this distribution and at
33
// http://rust-lang.org/COPYRIGHT.
44
//
@@ -300,15 +300,15 @@ impl<T:Ord> Ord for &const T {
300300
pub fn test() {
301301
unsafe {
302302
struct Pair {mut fst: int, mut snd: int};
303-
let mut p = Pair {fst: 10, snd: 20};
303+
let mut p = Pair {mut fst: 10, mut snd: 20};
304304
let pptr: *mut Pair = &mut p;
305305
let iptr: *mut int = cast::reinterpret_cast(&pptr);
306306
assert (*iptr == 10);;
307307
*iptr = 30;
308308
assert (*iptr == 30);
309309
assert (p.fst == 30);;
310310

311-
*pptr = Pair {fst: 50, snd: 60};
311+
*pptr = Pair {mut fst: 50, mut snd: 60};
312312
assert (*iptr == 50);
313313
assert (p.fst == 50);
314314
assert (p.snd == 60);

trunk/src/libfuzzer/fuzzer.rc

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ pub pure fn safe_to_use_expr(e: ast::expr, tm: test_mode) -> bool {
134134

135135
pub fn safe_to_steal_ty(t: @ast::Ty, tm: test_mode) -> bool {
136136
// Restrictions happen to be the same.
137-
safe_to_replace_ty(t.node, tm)
137+
safe_to_replace_ty(&t.node, tm)
138138
}
139139

140140
// Not type-parameterized: https://github.com/mozilla/rust/issues/898 (FIXED)
@@ -175,8 +175,8 @@ pub fn steal(crate: ast::crate, tm: test_mode) -> StolenStuff {
175175
}
176176

177177

178-
pub fn safe_to_replace_expr(e: ast::expr_, _tm: test_mode) -> bool {
179-
match e {
178+
pub fn safe_to_replace_expr(e: &ast::expr_, _tm: test_mode) -> bool {
179+
match *e {
180180
// https://github.com/mozilla/rust/issues/652
181181
ast::expr_if(*) => { false }
182182
ast::expr_block(_) => { false }
@@ -188,8 +188,8 @@ pub fn safe_to_replace_expr(e: ast::expr_, _tm: test_mode) -> bool {
188188
}
189189
}
190190

191-
pub fn safe_to_replace_ty(t: ast::ty_, _tm: test_mode) -> bool {
192-
match t {
191+
pub fn safe_to_replace_ty(t: &ast::ty_, _tm: test_mode) -> bool {
192+
match *t {
193193
ast::ty_infer => { false } // always implicit, always top level
194194
ast::ty_bot => { false } // in source, can only appear
195195
// as the out type of a function
@@ -204,7 +204,7 @@ pub fn replace_expr_in_crate(crate: ast::crate, i: uint,
204204
ast::crate {
205205
let j: @mut uint = @mut 0u;
206206
fn fold_expr_rep(j_: @mut uint, i_: uint, newexpr_: ast::expr_,
207-
original: ast::expr_, fld: fold::ast_fold,
207+
original: &ast::expr_, fld: fold::ast_fold,
208208
tm_: test_mode) ->
209209
ast::expr_ {
210210
*j_ += 1u;
@@ -221,7 +221,7 @@ pub fn replace_expr_in_crate(crate: ast::crate, i: uint,
221221
.. *fold::default_ast_fold()
222222
};
223223
let af = fold::make_fold(afp);
224-
let crate2: @ast::crate = @af.fold_crate(crate);
224+
let crate2: @ast::crate = @af.fold_crate(&crate);
225225
*crate2
226226
}
227227

@@ -231,7 +231,7 @@ pub fn replace_ty_in_crate(crate: ast::crate, i: uint, newty: ast::Ty,
231231
tm: test_mode) -> ast::crate {
232232
let j: @mut uint = @mut 0u;
233233
fn fold_ty_rep(j_: @mut uint, i_: uint, newty_: ast::ty_,
234-
original: ast::ty_, fld: fold::ast_fold,
234+
original: &ast::ty_, fld: fold::ast_fold,
235235
tm_: test_mode) ->
236236
ast::ty_ {
237237
*j_ += 1u;
@@ -244,7 +244,7 @@ pub fn replace_ty_in_crate(crate: ast::crate, i: uint, newty: ast::Ty,
244244
.. *fold::default_ast_fold()
245245
};
246246
let af = fold::make_fold(afp);
247-
let crate2: @ast::crate = @af.fold_crate(crate);
247+
let crate2: @ast::crate = @af.fold_crate(&crate);
248248
*crate2
249249
}
250250

trunk/src/librustc/driver/driver.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ pub fn pretty_print_input(sess: Session, +cfg: ast::crate_cfg, input: input,
396396
pprust::node_block(s, ref blk) => {
397397
pp::space(s.s);
398398
pprust::synth_comment(
399-
s, ~"block " + int::to_str((*blk).node.id));
399+
s, ~"block " + int::to_str(blk.node.id));
400400
}
401401
pprust::node_expr(s, expr) => {
402402
pp::space(s.s);

trunk/src/librustc/driver/session.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ pub mod test {
358358
pub fn make_crate_type_attr(+t: ~str) -> ast::attribute {
359359
codemap::respan(codemap::dummy_sp(), ast::attribute_ {
360360
style: ast::attr_outer,
361-
value: codemap::respan(codemap::dummy_sp(),
361+
value: @codemap::respan(codemap::dummy_sp(),
362362
ast::meta_name_value(
363363
@~"crate_type",
364364
codemap::respan(codemap::dummy_sp(),

0 commit comments

Comments
 (0)