Skip to content
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

Error code's long explanation cleanup #66633

Merged
merged 5 commits into from
Nov 23, 2019
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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