From 2548e2ee58949667ccf27e1d842242ed21313b77 Mon Sep 17 00:00:00 2001 From: Jack Wilson Date: Thu, 3 Sep 2015 20:48:26 -0700 Subject: [PATCH 1/6] Fixes minor formatting inconsistencies --- src/doc/trpl/method-syntax.md | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/doc/trpl/method-syntax.md b/src/doc/trpl/method-syntax.md index 1afa622db7dd3..73cd771114b37 100644 --- a/src/doc/trpl/method-syntax.md +++ b/src/doc/trpl/method-syntax.md @@ -7,7 +7,7 @@ can be awkward. Consider this code: baz(bar(foo)); ``` -We would read this left-to right, and so we see ‘baz bar foo’. But this isn’t the +We would read this left-to-right, and so we see ‘baz bar foo’. But this isn’t the order that the functions would get called in, that’s inside-out: ‘foo bar baz’. Wouldn’t it be nice if we could do this instead? @@ -45,17 +45,17 @@ This will print `12.566371`. -We’ve made a struct that represents a circle. We then write an `impl` block, +We’ve made a `struct` that represents a circle. We then write an `impl` block, and inside it, define a method, `area`. -Methods take a special first parameter, of which there are three variants: +Methods take a special first parameter, of which there are three variants: `self`, `&self`, and `&mut self`. You can think of this first parameter as being the `foo` in `foo.bar()`. The three variants correspond to the three kinds of things `foo` could be: `self` if it’s just a value on the stack, `&self` if it’s a reference, and `&mut self` if it’s a mutable reference. Because we took the `&self` parameter to `area`, we can use it just like any other parameter. Because we know it’s a `Circle`, we can access the `radius` -just like we would with any other struct. +just like we would with any other `struct`. We should default to using `&self`, as you should prefer borrowing over taking ownership, as well as taking immutable references over mutable ones. Here’s an @@ -120,12 +120,12 @@ Check the return type: ```rust # struct Circle; # impl Circle { -fn grow(&self) -> Circle { +fn grow(&self, increment: f64) -> Circle { # Circle } } ``` We just say we’re returning a `Circle`. With this method, we can grow a new -circle to any arbitrary size. +`circle` to any arbitrary size. # Associated functions @@ -161,7 +161,7 @@ methods’. # Builder Pattern -Let’s say that we want our users to be able to create Circles, but we will +Let’s say that we want our users to be able to create `Circle`s, but we will allow them to only set the properties they care about. Otherwise, the `x` and `y` attributes will be `0.0`, and the `radius` will be `1.0`. Rust doesn’t have method overloading, named arguments, or variable arguments. We employ @@ -224,7 +224,7 @@ fn main() { } ``` -What we’ve done here is make another struct, `CircleBuilder`. We’ve defined our +What we’ve done here is make another `struct`, `CircleBuilder`. We’ve defined our builder methods on it. We’ve also defined our `area()` method on `Circle`. We also made one more method on `CircleBuilder`: `finalize()`. This method creates our final `Circle` from the builder. Now, we’ve used the type system to enforce From f44fbf08f95e86e0eabdb492e8d003ccfcefa258 Mon Sep 17 00:00:00 2001 From: Jack Wilson Date: Thu, 3 Sep 2015 20:51:08 -0700 Subject: [PATCH 2/6] Capitalize circle reference --- src/doc/trpl/method-syntax.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/doc/trpl/method-syntax.md b/src/doc/trpl/method-syntax.md index 73cd771114b37..a2bdd66b0c253 100644 --- a/src/doc/trpl/method-syntax.md +++ b/src/doc/trpl/method-syntax.md @@ -125,7 +125,7 @@ fn grow(&self, increment: f64) -> Circle { ``` We just say we’re returning a `Circle`. With this method, we can grow a new -`circle` to any arbitrary size. +`Circle` to any arbitrary size. # Associated functions From ec4ba272b00c7a7cae537a73d52f692a1a943e1b Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Fri, 4 Sep 2015 18:09:16 +0200 Subject: [PATCH 3/6] Add span_help for E0002 --- src/librustc/diagnostics.rs | 2 +- src/librustc/middle/check_match.rs | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/librustc/diagnostics.rs b/src/librustc/diagnostics.rs index 696b38219f440..61cc63fa31771 100644 --- a/src/librustc/diagnostics.rs +++ b/src/librustc/diagnostics.rs @@ -44,7 +44,7 @@ E0002: r##" This error indicates that an empty match expression is invalid because the type it is matching on is non-empty (there exist values of this type). In safe code it is impossible to create an instance of an empty type, so empty match -expressions are almost never desired. This error is typically fixed by adding +expressions are almost never desired. This error is typically fixed by adding one or more cases to the match expression. An example of an empty type is `enum Empty { }`. So, the following will work: diff --git a/src/librustc/middle/check_match.rs b/src/librustc/middle/check_match.rs index d0111860b4406..767eab69733c3 100644 --- a/src/librustc/middle/check_match.rs +++ b/src/librustc/middle/check_match.rs @@ -218,6 +218,9 @@ fn check_expr(cx: &mut MatchCheckCtxt, ex: &hir::Expr) { span_err!(cx.tcx.sess, ex.span, E0002, "non-exhaustive patterns: type {} is non-empty", pat_ty); + span_help!(cx.tcx.sess, ex.span, + "Please ensure that all possible cases are being handled; \ + possibly adding wildcards or more match arms."); } // If the type *is* empty, it's vacuously exhaustive return; From 8175dce5172bfb66dd6bc8e57175a5102acb1f94 Mon Sep 17 00:00:00 2001 From: Alisdair Owens Date: Fri, 4 Sep 2015 19:21:22 +0100 Subject: [PATCH 4/6] Add long diagnostics for E0247 --- src/librustc_typeck/diagnostics.rs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/librustc_typeck/diagnostics.rs b/src/librustc_typeck/diagnostics.rs index dfce7a9c31584..8a2f8b1cf4bb0 100644 --- a/src/librustc_typeck/diagnostics.rs +++ b/src/librustc_typeck/diagnostics.rs @@ -2475,6 +2475,24 @@ struct Bar { x: Foo } ``` "##, +//NB: not currently reachable +E0247: r##" +This error indicates an attempt to use a module name where a type is expected. +For example: + +``` +mod MyMod { + mod MySubMod { } +} + +fn do_something(x: MyMod::MySubMod) { } +``` + +In this example, we're attempting to take a parameter of type `MyMod::MySubMod` +in the do_something function. This is not legal: `MyMod::MySubMod` is a module +name, not a type. +"##, + E0248: r##" This error indicates an attempt to use a value where a type is expected. For example: @@ -3291,7 +3309,6 @@ register_diagnostics! { E0242, // internal error looking up a definition E0245, // not a trait // E0246, // invalid recursive type - E0247, // found module name used as a type // E0319, // trait impls for defaulted traits allowed just for structs/enums E0320, // recursive overflow during dropck E0321, // extended coherence rules for defaulted traits violated From f7ffd502e57c0e53c77f47a7c80d2fdc67559fd3 Mon Sep 17 00:00:00 2001 From: Murarth Date: Fri, 4 Sep 2015 21:27:55 -0700 Subject: [PATCH 5/6] Fix typo in prelude docs --- src/libstd/prelude/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libstd/prelude/mod.rs b/src/libstd/prelude/mod.rs index 4597db41e907d..82578172802f6 100644 --- a/src/libstd/prelude/mod.rs +++ b/src/libstd/prelude/mod.rs @@ -103,8 +103,8 @@ //! `Some` and `None`. //! * `std::result::Result::`{ //! [`self`](../result/enum.Result.html), -//! [`Some`](../result/enum.Result.html), -//! [`None`](../result/enum.Result.html) +//! [`Ok`](../result/enum.Result.html), +//! [`Err`](../result/enum.Result.html) //! }. //! The ubiquitous `Result` type and its two [variants][book-enums], //! `Ok` and `Err`. From 771ab35c3d3ee684a0d938fe50b1dc21edbca621 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Fri, 4 Sep 2015 18:11:51 +0200 Subject: [PATCH 6/6] Add erroneous code example for E0010 --- src/librustc/diagnostics.rs | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/librustc/diagnostics.rs b/src/librustc/diagnostics.rs index 61cc63fa31771..91845e916d4d4 100644 --- a/src/librustc/diagnostics.rs +++ b/src/librustc/diagnostics.rs @@ -218,7 +218,14 @@ match x { E0010: r##" The value of statics and constants must be known at compile time, and they live for the entire lifetime of a program. Creating a boxed value allocates memory on -the heap at runtime, and therefore cannot be done at compile time. +the heap at runtime, and therefore cannot be done at compile time. Erroneous +code example: + +``` +#![feature(box_syntax)] + +const CON : Box = box 0; +``` "##, E0011: r##" @@ -335,7 +342,6 @@ is not allowed. If you really want global mutable state, try using `static mut` or a global `UnsafeCell`. - "##, E0018: r##" @@ -399,7 +405,13 @@ fn main() { E0020: r##" This error indicates that an attempt was made to divide by zero (or take the -remainder of a zero divisor) in a static or constant expression. +remainder of a zero divisor) in a static or constant expression. Erroneous +code example: + +``` +const X: i32 = 42 / 0; +// error: attempted to divide by zero in a constant expression +``` "##, E0022: r##"