From 88bec09e6345d64d334c6dc1ef4947b35c467373 Mon Sep 17 00:00:00 2001 From: "Felix S. Klock II" Date: Tue, 29 Jan 2013 10:14:05 +0100 Subject: [PATCH 1/4] Fix two code examples in main rust tutorial. 1. The section on trait definitions of static methods should include a trait with a static method in the generated document. 2. The section on trait inheritance had a expression that appears nonsensical ("let mycircle = @mycircle") in the generated document. The text would be clearer (IMO) if we continued with the running example of CircleStruct. --- doc/tutorial.md | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/doc/tutorial.md b/doc/tutorial.md index 509f87e995d89..34757edb4d1ad 100644 --- a/doc/tutorial.md +++ b/doc/tutorial.md @@ -2015,7 +2015,7 @@ the method name with the trait name. The compiler will use type inference to decide which implementation to call. ~~~~ -# trait Shape { static fn new(area: float) -> self; } +trait Shape { static fn new(area: float) -> self; } # use float::consts::pi; # use float::sqrt; struct Circle { radius: float } @@ -2211,11 +2211,16 @@ Likewise, supertrait methods may also be called on trait objects. ~~~ {.xfail-test} # trait Shape { fn area(&self) -> float; } # trait Circle : Shape { fn radius(&self) -> float; } -# impl int: Shape { fn area(&self) -> float { 0.0 } } -# impl int: Circle { fn radius(&self) -> float { 0.0 } } -# let mycircle = 0; +# use float::consts::pi; +# use float::sqrt; +# struct Point { x: float, y: float } +# struct CircleStruct { center: Point, radius: float } +# impl CircleStruct: Circle { fn radius(&self) -> float { sqrt(self.area() / pi) } } +# impl CircleStruct: Shape { fn area(&self) -> float { pi * square(self.radius) } } + +let concrete = @CircleStruct{center:Point{x:3f,y:4f},radius:5f}; -let mycircle: Circle = @mycircle as @Circle; +let mycircle: Circle = concrete as @Circle; let nonsense = mycircle.radius() * mycircle.area(); ~~~ From 6cabe2b90226e2262c8c1937b604d9f2c6f14059 Mon Sep 17 00:00:00 2001 From: "Felix S. Klock II" Date: Tue, 29 Jan 2013 10:20:28 +0100 Subject: [PATCH 2/4] Fixed two examples of erroneous code so their errors match expectation. 1. In the first case, the previous code was failing during type inference due to mismatched structure. Fix is to use the X structure at both points in the code. 2. In the second case, a naive transcription that subsitutes *nothing* in for the omitted statements signified by "..." will actually compile without an error. Furthermore, any pure code could also be substituted for the ellipsis and the code would compile (as the text already states). So to make the example more illustrative, it would be better to include an impure callback, which makes the potential for aliasing immediately obvious to the reader. --- doc/tutorial-borrowed-ptr.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/doc/tutorial-borrowed-ptr.md b/doc/tutorial-borrowed-ptr.md index 4866f7b8f5e6f..59d58cadbecdf 100644 --- a/doc/tutorial-borrowed-ptr.md +++ b/doc/tutorial-borrowed-ptr.md @@ -302,7 +302,7 @@ rejected by the compiler): fn example3() -> int { let mut x = ~X {f: 3}; let y = &x.f; - x = ~{f: 4}; // Error reported here. + x = ~X {f: 4}; // Error reported here. *y } ~~~ @@ -369,9 +369,11 @@ box's owner. Consider a program like this: ~~~ struct R { g: int } struct S { mut f: ~R } -fn example5a(x: @S ...) -> int { +fn example5a(x: @S, callback: @fn()) -> int { let y = &x.f.g; // Error reported here. ... + callback(); + ... # return 0; } ~~~ From 2dda6d6f5d66aeacf0493f9c37663cd11ea0e382 Mon Sep 17 00:00:00 2001 From: "Felix S. Klock II" Date: Tue, 29 Jan 2013 11:23:41 +0100 Subject: [PATCH 3/4] Formatting cleanup. I had put a line break in to try to stress the binding of mycircle, but generated document looks cleaner without newline. --- doc/tutorial.md | 1 - 1 file changed, 1 deletion(-) diff --git a/doc/tutorial.md b/doc/tutorial.md index 34757edb4d1ad..9df646fbf9180 100644 --- a/doc/tutorial.md +++ b/doc/tutorial.md @@ -2219,7 +2219,6 @@ Likewise, supertrait methods may also be called on trait objects. # impl CircleStruct: Shape { fn area(&self) -> float { pi * square(self.radius) } } let concrete = @CircleStruct{center:Point{x:3f,y:4f},radius:5f}; - let mycircle: Circle = concrete as @Circle; let nonsense = mycircle.radius() * mycircle.area(); ~~~ From 20af4d73616a0abebb1a26aa4cc26f1c240e66e5 Mon Sep 17 00:00:00 2001 From: "Felix S. Klock II" Date: Tue, 29 Jan 2013 13:18:40 +0100 Subject: [PATCH 4/4] Add expected failure annotation on "fixed" example to placate 'make check'. --- doc/tutorial-borrowed-ptr.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/tutorial-borrowed-ptr.md b/doc/tutorial-borrowed-ptr.md index 59d58cadbecdf..6ec51c5035e93 100644 --- a/doc/tutorial-borrowed-ptr.md +++ b/doc/tutorial-borrowed-ptr.md @@ -366,7 +366,7 @@ Things get trickier when the unique box is not uniquely owned by the stack frame, or when there is no way for the compiler to determine the box's owner. Consider a program like this: -~~~ +~~~ {.xfail-test} struct R { g: int } struct S { mut f: ~R } fn example5a(x: @S, callback: @fn()) -> int {