Skip to content

Commit cd27463

Browse files
Put each error code long explanation into their own markdown file
1 parent 3302190 commit cd27463

File tree

397 files changed

+12609
-13403
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

397 files changed

+12609
-13403
lines changed

src/librustc_error_codes/error_codes.rs

Lines changed: 396 additions & 13403 deletions
Large diffs are not rendered by default.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#### Note: this error code is no longer emitted by the compiler.
2+
3+
This error suggests that the expression arm corresponding to the noted pattern
4+
will never be reached as for all possible values of the expression being
5+
matched, one of the preceding patterns will match.
6+
7+
This means that perhaps some of the preceding patterns are too general, this
8+
one is too specific or the ordering is incorrect.
9+
10+
For example, the following `match` block has too many arms:
11+
12+
```
13+
match Some(0) {
14+
Some(bar) => {/* ... */}
15+
x => {/* ... */} // This handles the `None` case
16+
_ => {/* ... */} // All possible cases have already been handled
17+
}
18+
```
19+
20+
`match` blocks have their patterns matched in order, so, for example, putting
21+
a wildcard arm above a more specific arm will make the latter arm irrelevant.
22+
23+
Ensure the ordering of the match arm is correct and remove any superfluous
24+
arms.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#### Note: this error code is no longer emitted by the compiler.
2+
3+
This error indicates that an empty match expression is invalid because the type
4+
it is matching on is non-empty (there exist values of this type). In safe code
5+
it is impossible to create an instance of an empty type, so empty match
6+
expressions are almost never desired. This error is typically fixed by adding
7+
one or more cases to the match expression.
8+
9+
An example of an empty type is `enum Empty { }`. So, the following will work:
10+
11+
```
12+
enum Empty {}
13+
14+
fn foo(x: Empty) {
15+
match x {
16+
// empty
17+
}
18+
}
19+
```
20+
21+
However, this won't:
22+
23+
```compile_fail
24+
fn foo(x: Option<String>) {
25+
match x {
26+
// empty
27+
}
28+
}
29+
```
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
This error indicates that the compiler cannot guarantee a matching pattern for
2+
one or more possible inputs to a match expression. Guaranteed matches are
3+
required in order to assign values to match expressions, or alternatively,
4+
determine the flow of execution.
5+
6+
Erroneous code example:
7+
8+
```compile_fail,E0004
9+
enum Terminator {
10+
HastaLaVistaBaby,
11+
TalkToMyHand,
12+
}
13+
14+
let x = Terminator::HastaLaVistaBaby;
15+
16+
match x { // error: non-exhaustive patterns: `HastaLaVistaBaby` not covered
17+
Terminator::TalkToMyHand => {}
18+
}
19+
```
20+
21+
If you encounter this error you must alter your patterns so that every possible
22+
value of the input type is matched. For types with a small number of variants
23+
(like enums) you should probably cover all cases explicitly. Alternatively, the
24+
underscore `_` wildcard pattern can be added after all other patterns to match
25+
"anything else". Example:
26+
27+
```
28+
enum Terminator {
29+
HastaLaVistaBaby,
30+
TalkToMyHand,
31+
}
32+
33+
let x = Terminator::HastaLaVistaBaby;
34+
35+
match x {
36+
Terminator::TalkToMyHand => {}
37+
Terminator::HastaLaVistaBaby => {}
38+
}
39+
40+
// or:
41+
42+
match x {
43+
Terminator::TalkToMyHand => {}
44+
_ => {}
45+
}
46+
```
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
Patterns used to bind names must be irrefutable, that is, they must guarantee
2+
that a name will be extracted in all cases.
3+
4+
Erroneous code example:
5+
6+
```compile_fail,E0005
7+
let x = Some(1);
8+
let Some(y) = x;
9+
// error: refutable pattern in local binding: `None` not covered
10+
```
11+
12+
If you encounter this error you probably need to use a `match` or `if let` to
13+
deal with the possibility of failure. Example:
14+
15+
```
16+
let x = Some(1);
17+
18+
match x {
19+
Some(y) => {
20+
// do something
21+
},
22+
None => {}
23+
}
24+
25+
// or:
26+
27+
if let Some(y) = x {
28+
// do something
29+
}
30+
```
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
This error indicates that the bindings in a match arm would require a value to
2+
be moved into more than one location, thus violating unique ownership. Code
3+
like the following is invalid as it requires the entire `Option<String>` to be
4+
moved into a variable called `op_string` while simultaneously requiring the
5+
inner `String` to be moved into a variable called `s`.
6+
7+
Erroneous code example:
8+
9+
```compile_fail,E0007
10+
let x = Some("s".to_string());
11+
12+
match x {
13+
op_string @ Some(s) => {}, // error: cannot bind by-move with sub-bindings
14+
None => {},
15+
}
16+
```
17+
18+
See also the error E0303.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
In a pattern, all values that don't implement the `Copy` trait have to be bound
2+
the same way. The goal here is to avoid binding simultaneously by-move and
3+
by-ref.
4+
5+
This limitation may be removed in a future version of Rust.
6+
7+
Erroneous code example:
8+
9+
```compile_fail,E0009
10+
struct X { x: (), }
11+
12+
let x = Some((X { x: () }, X { x: () }));
13+
match x {
14+
Some((y, ref z)) => {}, // error: cannot bind by-move and by-ref in the
15+
// same pattern
16+
None => panic!()
17+
}
18+
```
19+
20+
You have two solutions:
21+
22+
Solution #1: Bind the pattern's values the same way.
23+
24+
```
25+
struct X { x: (), }
26+
27+
let x = Some((X { x: () }, X { x: () }));
28+
match x {
29+
Some((ref y, ref z)) => {},
30+
// or Some((y, z)) => {}
31+
None => panic!()
32+
}
33+
```
34+
35+
Solution #2: Implement the `Copy` trait for the `X` structure.
36+
37+
However, please keep in mind that the first solution should be preferred.
38+
39+
```
40+
#[derive(Clone, Copy)]
41+
struct X { x: (), }
42+
43+
let x = Some((X { x: () }, X { x: () }));
44+
match x {
45+
Some((y, ref z)) => {},
46+
None => panic!()
47+
}
48+
```
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
The value of statics and constants must be known at compile time, and they live
2+
for the entire lifetime of a program. Creating a boxed value allocates memory on
3+
the heap at runtime, and therefore cannot be done at compile time.
4+
5+
Erroneous code example:
6+
7+
```compile_fail,E0010
8+
#![feature(box_syntax)]
9+
10+
const CON : Box<i32> = box 0;
11+
```
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
Static and const variables can refer to other const variables. But a const
2+
variable cannot refer to a static variable.
3+
4+
Erroneous code example:
5+
6+
```compile_fail,E0013
7+
static X: i32 = 42;
8+
const Y: i32 = X;
9+
```
10+
11+
In this example, `Y` cannot refer to `X` here. To fix this, the value can be
12+
extracted as a const and then used:
13+
14+
```
15+
const A: i32 = 42;
16+
static X: i32 = A;
17+
const Y: i32 = A;
18+
```
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#### Note: this error code is no longer emitted by the compiler.
2+
3+
Constants can only be initialized by a constant value or, in a future
4+
version of Rust, a call to a const function. This error indicates the use
5+
of a path (like a::b, or x) denoting something other than one of these
6+
allowed items.
7+
8+
Erroneous code example:
9+
10+
```
11+
const FOO: i32 = { let x = 0; x }; // 'x' isn't a constant nor a function!
12+
```
13+
14+
To avoid it, you have to replace the non-constant value:
15+
16+
```
17+
const FOO: i32 = { const X : i32 = 0; X };
18+
// or even:
19+
const FOO2: i32 = { 0 }; // but brackets are useless here
20+
```
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
The only functions that can be called in static or constant expressions are
2+
`const` functions, and struct/enum constructors. `const` functions are only
3+
available on a nightly compiler. Rust currently does not support more general
4+
compile-time function execution.
5+
6+
```
7+
const FOO: Option<u8> = Some(1); // enum constructor
8+
struct Bar {x: u8}
9+
const BAR: Bar = Bar {x: 1}; // struct constructor
10+
```
11+
12+
See [RFC 911] for more details on the design of `const fn`s.
13+
14+
[RFC 911]: https://github.com/rust-lang/rfcs/blob/master/text/0911-const-fn.md
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
References in statics and constants may only refer to immutable values.
2+
3+
Erroneous code example:
4+
5+
```compile_fail,E0017
6+
static X: i32 = 1;
7+
const C: i32 = 2;
8+
9+
// these three are not allowed:
10+
const CR: &mut i32 = &mut C;
11+
static STATIC_REF: &'static mut i32 = &mut X;
12+
static CONST_REF: &'static mut i32 = &mut C;
13+
```
14+
15+
Statics are shared everywhere, and if they refer to mutable data one might
16+
violate memory safety since holding multiple mutable references to shared data
17+
is not allowed.
18+
19+
If you really want global mutable state, try using `static mut` or a global
20+
`UnsafeCell`.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
A function call isn't allowed in the const's initialization expression
2+
because the expression's value must be known at compile-time.
3+
4+
Erroneous code example:
5+
6+
```compile_fail,E0019
7+
#![feature(box_syntax)]
8+
9+
fn main() {
10+
struct MyOwned;
11+
12+
static STATIC11: Box<MyOwned> = box MyOwned; // error!
13+
}
14+
```
15+
16+
Remember: you can't use a function call inside a const's initialization
17+
expression! However, you can totally use it anywhere else:
18+
19+
```
20+
enum Test {
21+
V1
22+
}
23+
24+
impl Test {
25+
fn func(&self) -> i32 {
26+
12
27+
}
28+
}
29+
30+
fn main() {
31+
const FOO: Test = Test::V1;
32+
33+
FOO.func(); // here is good
34+
let x = FOO.func(); // or even here!
35+
}
36+
```
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
A pattern used to match against an enum variant must provide a sub-pattern for
2+
each field of the enum variant. This error indicates that a pattern attempted to
3+
extract an incorrect number of fields from a variant.
4+
5+
```
6+
enum Fruit {
7+
Apple(String, String),
8+
Pear(u32),
9+
}
10+
```
11+
12+
Here the `Apple` variant has two fields, and should be matched against like so:
13+
14+
```
15+
enum Fruit {
16+
Apple(String, String),
17+
Pear(u32),
18+
}
19+
20+
let x = Fruit::Apple(String::new(), String::new());
21+
22+
// Correct.
23+
match x {
24+
Fruit::Apple(a, b) => {},
25+
_ => {}
26+
}
27+
```
28+
29+
Matching with the wrong number of fields has no sensible interpretation:
30+
31+
```compile_fail,E0023
32+
enum Fruit {
33+
Apple(String, String),
34+
Pear(u32),
35+
}
36+
37+
let x = Fruit::Apple(String::new(), String::new());
38+
39+
// Incorrect.
40+
match x {
41+
Fruit::Apple(a) => {},
42+
Fruit::Apple(a, b, c) => {},
43+
}
44+
```
45+
46+
Check how many fields the enum was declared with and ensure that your pattern
47+
uses the same number.

0 commit comments

Comments
 (0)