Skip to content

librustc: Remove ~EXPR, ~TYPE, and ~PAT from the language, except #13958

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 7, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
2 changes: 1 addition & 1 deletion src/doc/guide-ffi.md
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ extern {

fn main() {
// Create the object that will be referenced in the callback
let mut rust_object = ~RustObject{ a: 5 };
let mut rust_object = box RustObject { a: 5 };

unsafe {
register_callback(&mut *rust_object, callback);
Expand Down
70 changes: 35 additions & 35 deletions src/doc/guide-lifetimes.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ point, but allocated in a different place:

~~~
# struct Point {x: f64, y: f64}
let on_the_stack : Point = Point {x: 3.0, y: 4.0};
let managed_box : @Point = @Point {x: 5.0, y: 1.0};
let owned_box : ~Point = ~Point {x: 7.0, y: 9.0};
let on_the_stack : Point = Point {x: 3.0, y: 4.0};
let managed_box : @Point = @Point {x: 5.0, y: 1.0};
let owned_box : Box<Point> = box Point {x: 7.0, y: 9.0};
~~~

Suppose we wanted to write a procedure that computed the distance between any
Expand Down Expand Up @@ -72,9 +72,9 @@ Now we can call `compute_distance()` in various ways:

~~~
# struct Point {x: f64, y: f64}
# let on_the_stack : Point = Point{x: 3.0, y: 4.0};
# let managed_box : @Point = @Point{x: 5.0, y: 1.0};
# let owned_box : ~Point = ~Point{x: 7.0, y: 9.0};
# let on_the_stack : Point = Point{x: 3.0, y: 4.0};
# let managed_box : @Point = @Point{x: 5.0, y: 1.0};
# let owned_box : Box<Point> = box Point{x: 7.0, y: 9.0};
# fn compute_distance(p1: &Point, p2: &Point) -> f64 { 0.0 }
compute_distance(&on_the_stack, managed_box);
compute_distance(managed_box, owned_box);
Expand Down Expand Up @@ -151,12 +151,12 @@ Now, as before, we can define rectangles in a few different ways:
# struct Point {x: f64, y: f64}
# struct Size {w: f64, h: f64} // as before
# struct Rectangle {origin: Point, size: Size}
let rect_stack = &Rectangle {origin: Point {x: 1.0, y: 2.0},
size: Size {w: 3.0, h: 4.0}};
let rect_managed = @Rectangle {origin: Point {x: 3.0, y: 4.0},
size: Size {w: 3.0, h: 4.0}};
let rect_owned = ~Rectangle {origin: Point {x: 5.0, y: 6.0},
size: Size {w: 3.0, h: 4.0}};
let rect_stack = &Rectangle {origin: Point {x: 1.0, y: 2.0},
size: Size {w: 3.0, h: 4.0}};
let rect_managed = @Rectangle {origin: Point {x: 3.0, y: 4.0},
size: Size {w: 3.0, h: 4.0}};
let rect_owned = box Rectangle {origin: Point {x: 5.0, y: 6.0},
size: Size {w: 3.0, h: 4.0}};
~~~

In each case, we can extract out individual subcomponents with the `&`
Expand All @@ -168,7 +168,7 @@ operator. For example, I could write:
# struct Rectangle {origin: Point, size: Size}
# let rect_stack = &Rectangle {origin: Point {x: 1.0, y: 2.0}, size: Size {w: 3.0, h: 4.0}};
# let rect_managed = @Rectangle {origin: Point {x: 3.0, y: 4.0}, size: Size {w: 3.0, h: 4.0}};
# let rect_owned = ~Rectangle {origin: Point {x: 5.0, y: 6.0}, size: Size {w: 3.0, h: 4.0}};
# let rect_owned = box Rectangle {origin: Point {x: 5.0, y: 6.0}, size: Size {w: 3.0, h: 4.0}};
# fn compute_distance(p1: &Point, p2: &Point) -> f64 { 0.0 }
compute_distance(&rect_stack.origin, &rect_managed.origin);
~~~
Expand Down Expand Up @@ -276,12 +276,12 @@ the following function is legal:
# fn some_condition() -> bool { true }
# struct Foo { f: int }
fn example3() -> int {
let mut x = ~Foo {f: 3};
let mut x = box Foo {f: 3};
if some_condition() {
let y = &x.f; // -+ L
return *y; // |
} // -+
x = ~Foo {f: 4};
x = box Foo {f: 4};
// ...
# return 0;
}
Expand All @@ -301,9 +301,9 @@ rejected by the compiler):

~~~ {.ignore}
fn example3() -> int {
let mut x = ~X {f: 3};
let mut x = box X {f: 3};
let y = &x.f;
x = ~X {f: 4}; // Error reported here.
x = box X {f: 4}; // Error reported here.
*y
}
~~~
Expand All @@ -314,27 +314,27 @@ memory immediately before the re-assignment of `x`:
~~~ {.notrust}
Stack Exchange Heap

x +----------+
| ~{f:int} | ----+
y +----------+ |
| &int | ----+
+----------+ | +---------+
+--> | f: 3 |
+---------+
x +-------------+
| box {f:int} | ----+
y +-------------+ |
| &int | ----+
+-------------+ | +---------+
+--> | f: 3 |
+---------+
~~~

Once the reassignment occurs, the memory will look like this:

~~~ {.notrust}
Stack Exchange Heap

x +----------+ +---------+
| ~{f:int} | -------> | f: 4 |
y +----------+ +---------+
| &int | ----+
+----------+ | +---------+
+--> | (freed) |
+---------+
x +-------------+ +---------+
| box {f:int} | -------> | f: 4 |
y +-------------+ +---------+
| &int | ----+
+-------------+ | +---------+
+--> | (freed) |
+---------+
~~~

Here you can see that the variable `y` still points at the old box,
Expand All @@ -349,12 +349,12 @@ mutations:
~~~ {.ignore}
fn example3() -> int {
struct R { g: int }
struct S { f: ~R }
struct S { f: Box<R> }

let mut x = ~S {f: ~R {g: 3}};
let mut x = box S {f: box R {g: 3}};
let y = &x.f.g;
x = ~S {f: ~R {g: 4}}; // Error reported here.
x.f = ~R {g: 5}; // Error reported here.
x = box S {f: box R {g: 4}}; // Error reported here.
x.f = box R {g: 5}; // Error reported here.
*y
}
~~~
Expand Down
37 changes: 19 additions & 18 deletions src/doc/guide-pointers.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,21 +182,22 @@ trait. Therefore, unboxed traits don't make any sense, and aren't allowed.
Sometimes, you need a recursive data structure. The simplest is known as a 'cons list':

~~~rust

enum List<T> {
Nil,
Cons(T, ~List<T>),
Cons(T, Box<List<T>>),
}

fn main() {
let list: List<int> = Cons(1, ~Cons(2, ~Cons(3, ~Nil)));
let list: List<int> = Cons(1, box Cons(2, box Cons(3, box Nil)));
println!("{:?}", list);
}
~~~

This prints:

~~~ {.notrust}
Cons(1, ~Cons(2, ~Cons(3, ~Nil)))
Cons(1, box Cons(2, box Cons(3, box Nil)))
~~~

The inner lists _must_ be an owned pointer, because we can't know how many
Expand Down Expand Up @@ -237,7 +238,7 @@ struct Point {
}

fn main() {
let a = ~Point { x: 10, y: 20 };
let a = box Point { x: 10, y: 20 };
spawn(proc() {
println!("{}", a.x);
});
Expand Down Expand Up @@ -268,7 +269,7 @@ struct Point {
}

fn main() {
let a = ~Point { x: 10, y: 20 };
let a = box Point { x: 10, y: 20 };
let b = a;
println!("{}", b.x);
println!("{}", a.x);
Expand All @@ -285,7 +286,7 @@ note: in expansion of format_args!
<std-macros>:158:27: 158:81 note: expansion site
<std-macros>:157:5: 159:6 note: in expansion of println!
test.rs:10:5: 10:25 note: expansion site
test.rs:8:9: 8:10 note: `a` moved here because it has type `~Point`, which is moved by default (use `ref` to override)
test.rs:8:9: 8:10 note: `a` moved here because it has type `Box<Point>`, which is moved by default (use `ref` to override)
test.rs:8 let b = a;
^
~~~
Expand Down Expand Up @@ -345,8 +346,8 @@ fn compute_distance(p1: &Point, p2: &Point) -> f32 {
}

fn main() {
let origin = @Point { x: 0.0, y: 0.0 };
let p1 = ~Point { x: 5.0, y: 3.0 };
let origin = @Point { x: 0.0, y: 0.0 };
let p1 = box Point { x: 5.0, y: 3.0 };

println!("{:?}", compute_distance(origin, p1));
}
Expand Down Expand Up @@ -381,7 +382,7 @@ duration a 'lifetime'. Let's try a more complex example:

~~~rust
fn main() {
let mut x = ~5;
let mut x = box 5;
if *x < 10 {
let y = &x;
println!("Oh no: {:?}", y);
Expand All @@ -398,7 +399,7 @@ mutated, and therefore, lets us pass. This wouldn't work:

~~~rust{.ignore}
fn main() {
let mut x = ~5;
let mut x = box 5;
if *x < 10 {
let y = &x;
*x -= 1;
Expand Down Expand Up @@ -437,39 +438,39 @@ is best.
What does that mean? Don't do this:

~~~rust
fn foo(x: ~int) -> ~int {
return ~*x;
fn foo(x: Box<int>) -> Box<int> {
return box *x;
}

fn main() {
let x = ~5;
let x = box 5;
let y = foo(x);
}
~~~

Do this:

~~~rust
fn foo(x: ~int) -> int {
fn foo(x: Box<int>) -> int {
return *x;
}

fn main() {
let x = ~5;
let y = ~foo(x);
let x = box 5;
let y = box foo(x);
}
~~~

This gives you flexibility, without sacrificing performance. For example, this will
also work:

~~~rust
fn foo(x: ~int) -> int {
fn foo(x: Box<int>) -> int {
return *x;
}

fn main() {
let x = ~5;
let x = box 5;
let y = @foo(x);
}
~~~
Expand Down
4 changes: 2 additions & 2 deletions src/doc/guide-unsafe.md
Original file line number Diff line number Diff line change
Expand Up @@ -258,10 +258,10 @@ impl<T: Send> Drop for Unique<T> {
}
}

// A comparison between the built-in ~ and this reimplementation
// A comparison between the built-in `Box` and this reimplementation
fn main() {
{
let mut x = ~5;
let mut x = box 5;
*x = 10;
} // `x` is freed here

Expand Down
11 changes: 6 additions & 5 deletions src/doc/intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,13 @@ That's a great example for stack memory,
but what about heap memory?
Rust has a second kind of pointer,
an 'owned box',
that you can create with a `~`.
that you can create with the `box` operator.
Check it out:

```
fn dangling() -> ~int {
let i = ~1234;

fn dangling() -> Box<int> {
let i = box 1234;
return i;
}

Expand All @@ -143,15 +144,15 @@ fn add_one() -> int {
```

Now instead of a stack allocated `1234`,
we have a heap allocated `~1234`.
we have a heap allocated `box 1234`.
Whereas `&` borrows a pointer to existing memory,
creating an owned box allocates memory on the heap and places a value in it,
giving you the sole pointer to that memory.
You can roughly compare these two lines:

```
// Rust
let i = ~1234;
let i = box 1234;
```

```notrust
Expand Down
Loading