Skip to content

Commit

Permalink
Rollup merge of rust-lang#66633 - GuillaumeGomez:err-codes-cleanup, r…
Browse files Browse the repository at this point in the history
…=Dylan-DPC

Error code's long explanation cleanup

Continuing to clean up the error code's long explanation.

r? @Dylan-DPC
  • Loading branch information
Centril authored Nov 22, 2019
2 parents a699945 + a8de11c commit 8be9e90
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 17 deletions.
36 changes: 27 additions & 9 deletions src/librustc_error_codes/error_codes/E0015.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,32 @@
The only functions that can be called in static or constant expressions are
`const` functions, and struct/enum constructors. `const` functions are only
available on a nightly compiler. Rust currently does not support more general
compile-time function execution.
A constant item was initialized with something that is not a constant expression.

Erroneous code example:

```compile_fail,E0015
fn create_some() -> Option<u8> {
Some(1)
}
const FOO: Option<u8> = create_some(); // error!
```
const FOO: Option<u8> = Some(1); // enum constructor
struct Bar {x: u8}
const BAR: Bar = Bar {x: 1}; // struct constructor

The only functions that can be called in static or constant expressions are
`const` functions, and struct/enum constructors.

To fix this error, you can declare `create_some` as a constant function:

```
const fn create_some() -> Option<u8> { // declared as a const function
Some(1)
}
See [RFC 911] for more details on the design of `const fn`s.
const FOO: Option<u8> = create_some(); // ok!
[RFC 911]: https://github.com/rust-lang/rfcs/blob/master/text/0911-const-fn.md
// These are also working:
struct Bar {
x: u8,
}
const OTHER_FOO: Option<u8> = Some(1);
const BAR: Bar = Bar {x: 1};
```
9 changes: 8 additions & 1 deletion src/librustc_error_codes/error_codes/E0023.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,18 @@ A pattern attempted to extract an incorrect number of fields from a variant.

Erroneous code example:

```
```compile_fail,E0023
enum Fruit {
Apple(String, String),
Pear(u32),
}
let x = Fruit::Apple(String::new(), String::new());
match x {
Fruit::Apple(a) => {}, // error!
_ => {}
}
```

A pattern used to match against an enum variant must provide a sub-pattern for
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_error_codes/error_codes/E0033.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ dereferencing the pointer.
You can read more about trait objects in the [Trait Objects] section of the
Reference.

[Trait Objects]: https://doc.rust-lang.org/reference/types.html#trait-objects
[Trait Objects]: https://doc.rust-lang.org/reference/types.html#trait-objects
4 changes: 2 additions & 2 deletions src/librustc_error_codes/error_codes/E0038.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ cause this problem.)
In such a case, the compiler cannot predict the return type of `foo()` in a
situation like the following:

```compile_fail
```compile_fail,E0038
trait Trait {
fn foo(&self) -> Self;
}
Expand Down Expand Up @@ -183,7 +183,7 @@ fn call_foo(thing: Box<Trait>) {

We don't just need to create a table of all implementations of all methods of
`Trait`, we need to create such a table, for each different type fed to
`foo()`. In this case this turns out to be (10 types implementing `Trait`)*(3
`foo()`. In this case this turns out to be (10 types implementing `Trait`)\*(3
types being fed to `foo()`) = 30 implementations!

With real world traits these numbers can grow drastically.
Expand Down
10 changes: 6 additions & 4 deletions src/librustc_error_codes/error_codes/E0057.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
When invoking closures or other implementations of the function traits `Fn`,
`FnMut` or `FnOnce` using call notation, the number of parameters passed to the
function must match its definition.
An invalid number of arguments was given when calling a closure.

An example using a closure:
Erroneous code example:

```compile_fail,E0057
let f = |x| x * 3;
Expand All @@ -11,6 +9,10 @@ let b = f(4); // this works!
let c = f(2, 3); // invalid, too many parameters
```

When invoking closures or other implementations of the function traits `Fn`,
`FnMut` or `FnOnce` using call notation, the number of parameters passed to the
function must match its definition.

A generic function must be treated similarly:

```
Expand Down
10 changes: 10 additions & 0 deletions src/librustc_error_codes/error_codes/E0061.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
An invalid number of arguments was passed when calling a function.

Erroneous code example:

```compile_fail,E0061
fn f(u: i32) {}
f(); // error!
```

The number of arguments passed to a function must match the number of arguments
specified in the function signature.

Expand Down

0 comments on commit 8be9e90

Please sign in to comment.