File tree 16 files changed +135
-76
lines changed
16 files changed +135
-76
lines changed Original file line number Diff line number Diff line change 1
1
// Error messages for EXXXX errors. Each message should start and end with a
2
2
// new line, and be wrapped to 80 characters. In vim you can `:set tw=80` and
3
3
// use `gq` to wrap paragraphs. Use `:set tw=0` to disable.
4
+ //
5
+ // /!\ IMPORTANT /!\
6
+ //
7
+ // Error messages' format must follow the RFC 1567 available here:
8
+ // https://github.com/rust-lang/rfcs/pull/1567
4
9
5
10
crate :: register_diagnostics! {
6
11
Original file line number Diff line number Diff line change 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.
1
+ A pattern attempted to extract an incorrect number of fields from a variant.
2
+
3
+ Erroneous code example:
4
4
5
5
```
6
6
enum Fruit {
@@ -9,6 +9,9 @@ enum Fruit {
9
9
}
10
10
```
11
11
12
+ A pattern used to match against an enum variant must provide a sub-pattern for
13
+ each field of the enum variant.
14
+
12
15
Here the ` Apple ` variant has two fields, and should be matched against like so:
13
16
14
17
```
Original file line number Diff line number Diff line change 1
- Each field of a struct can only be bound once in a pattern. Erroneous code
2
- example:
1
+ Each field of a struct can only be bound once in a pattern.
2
+
3
+ Erroneous code example:
3
4
4
5
``` compile_fail,E0025
5
6
struct Foo {
Original file line number Diff line number Diff line change 1
- This error indicates that a struct pattern attempted to extract a non-existent
2
- field from a struct. Struct fields are identified by the name used before the
3
- colon ` : ` so struct patterns should resemble the declaration of the struct type
4
- being matched.
1
+ A struct pattern attempted to extract a non-existent field from a struct.
5
2
6
- ```
7
- // Correct matching.
8
- struct Thing {
9
- x: u32,
10
- y: u32
11
- }
12
-
13
- let thing = Thing { x: 1, y: 2 };
14
-
15
- match thing {
16
- Thing { x: xfield, y: yfield } => {}
17
- }
18
- ```
19
-
20
- If you are using shorthand field patterns but want to refer to the struct field
21
- by a different name, you should rename it explicitly.
22
-
23
- Change this:
3
+ Erroneous code example:
24
4
25
5
``` compile_fail,E0026
26
6
struct Thing {
27
7
x: u32,
28
- y: u32
8
+ y: u32,
29
9
}
30
10
31
11
let thing = Thing { x: 0, y: 0 };
32
12
33
13
match thing {
34
- Thing { x, z } => {}
14
+ Thing { x, z } => {} // error: `Thing::z` field doesn't exist
35
15
}
36
16
```
37
17
38
- To this:
18
+ If you are using shorthand field patterns but want to refer to the struct field
19
+ by a different name, you should rename it explicitly. Struct fields are
20
+ identified by the name used before the colon ` : ` so struct patterns should
21
+ resemble the declaration of the struct type being matched.
39
22
40
23
```
41
24
struct Thing {
42
25
x: u32,
43
- y: u32
26
+ y: u32,
44
27
}
45
28
46
29
let thing = Thing { x: 0, y: 0 };
47
30
48
31
match thing {
49
- Thing { x, y: z } => {}
32
+ Thing { x, y: z } => {} // we renamed `y` to `z`
50
33
}
51
34
```
Original file line number Diff line number Diff line change 1
- This error indicates that a pattern for a struct fails to specify a sub-pattern
2
- for every one of the struct's fields. Ensure that each field from the struct's
3
- definition is mentioned in the pattern, or use ` .. ` to ignore unwanted fields.
1
+ A pattern for a struct fails to specify a sub-pattern for every one of the
2
+ struct's fields.
4
3
5
- For example:
4
+ Erroneous code example:
6
5
7
6
``` compile_fail,E0027
8
7
struct Dog {
@@ -18,7 +17,8 @@ match d {
18
17
}
19
18
```
20
19
21
- This is correct (explicit):
20
+ To fix this error, ensure that each field from the struct's definition is
21
+ mentioned in the pattern, or use ` .. ` to ignore unwanted fields. Example:
22
22
23
23
```
24
24
struct Dog {
Original file line number Diff line number Diff line change 1
- In a match expression, only numbers and characters can be matched against a
2
- range. This is because the compiler checks that the range is non-empty at
3
- compile-time, and is unable to evaluate arbitrary comparison functions. If you
4
- want to capture values of an orderable type between two end-points, you can use
5
- a guard.
1
+ Something other than numbers and characters has been used for a range.
2
+
3
+ Erroneous code example:
6
4
7
5
``` compile_fail,E0029
8
6
let string = "salutations !";
@@ -20,3 +18,9 @@ match string {
20
18
_ => {}
21
19
}
22
20
```
21
+
22
+ In a match expression, only numbers and characters can be matched against a
23
+ range. This is because the compiler checks that the range is non-empty at
24
+ compile-time, and is unable to evaluate arbitrary comparison functions. If you
25
+ want to capture values of an orderable type between two end-points, you can use
26
+ a guard.
Original file line number Diff line number Diff line change 1
- This error indicates that a pointer to a trait type cannot be implicitly
2
- dereferenced by a pattern. Every trait defines a type, but because the
3
- size of trait implementers isn't fixed, this type has no compile-time size.
4
- Therefore, all accesses to trait types must be through pointers. If you
5
- encounter this error you should try to avoid dereferencing the pointer.
1
+ A trait type has been dereferenced.
2
+
3
+ Erroneous code example:
6
4
7
5
``` compile_fail,E0033
8
6
# trait SomeTrait { fn method_one(&self){} fn method_two(&self){} }
@@ -17,7 +15,13 @@ trait_obj.method_one();
17
15
trait_obj.method_two();
18
16
```
19
17
18
+ A pointer to a trait type cannot be implicitly dereferenced by a pattern. Every
19
+ trait defines a type, but because the size of trait implementers isn't fixed,
20
+ this type has no compile-time size. Therefore, all accesses to trait types must
21
+ be through pointers. If you encounter this error you should try to avoid
22
+ dereferencing the pointer.
23
+
20
24
You can read more about trait objects in the [ Trait Objects] section of the
21
25
Reference.
22
26
23
- [ Trait Objects ] : https://doc.rust-lang.org/reference/types.html#trait-objects
27
+ [ Trait Objects ] : https://doc.rust-lang.org/reference/types.html#trait-objects
Original file line number Diff line number Diff line change 1
1
The compiler doesn't know what method to call because more than one method
2
- has the same prototype. Erroneous code example:
2
+ has the same prototype.
3
+
4
+ Erroneous code example:
3
5
4
6
``` compile_fail,E0034
5
7
struct Test;
Original file line number Diff line number Diff line change 1
- It is not allowed to manually call destructors in Rust. It is also not
2
- necessary to do this since ` drop ` is called automatically whenever a value goes
3
- out of scope.
1
+ It is not allowed to manually call destructors in Rust.
4
2
5
- Here's an example of this error :
3
+ Erroneous code example:
6
4
7
5
``` compile_fail,E0040
8
6
struct Foo {
@@ -20,3 +18,22 @@ fn main() {
20
18
x.drop(); // error: explicit use of destructor method
21
19
}
22
20
```
21
+
22
+ It is unnecessary to do this since ` drop ` is called automatically whenever a
23
+ value goes out of scope. However, if you really need to drop a value by hand,
24
+ you can use the ` std::mem::drop ` function:
25
+
26
+ ```
27
+ struct Foo {
28
+ x: i32,
29
+ }
30
+ impl Drop for Foo {
31
+ fn drop(&mut self) {
32
+ println!("kaboom");
33
+ }
34
+ }
35
+ fn main() {
36
+ let mut x = Foo { x: -7 };
37
+ drop(x); // ok!
38
+ }
39
+ ```
Original file line number Diff line number Diff line change 1
1
You cannot use type or const parameters on foreign items.
2
+
2
3
Example of erroneous code:
3
4
4
5
``` compile_fail,E0044
Original file line number Diff line number Diff line change 1
- Rust only supports variadic parameters for interoperability with C code in its
2
- FFI. As such, variadic parameters can only be used with functions which are
3
- using the C ABI. Examples of erroneous code:
4
-
5
- ``` compile_fail
6
- #![feature(unboxed_closures)]
1
+ Variadic parameters have been used on a non-C ABI function.
7
2
8
- extern "rust-call" { fn foo(x: u8, ...); }
3
+ Erroneous code example:
9
4
10
- // or
5
+ ``` compile_fail,E0045
6
+ #![feature(unboxed_closures)]
11
7
12
- fn foo(x: u8, ...) {}
8
+ extern "rust-call" {
9
+ fn foo(x: u8, ...); // error!
10
+ }
13
11
```
14
12
15
- To fix such code, put them in an extern "C" block:
13
+ Rust only supports variadic parameters for interoperability with C code in its
14
+ FFI. As such, variadic parameters can only be used with functions which are
15
+ using the C ABI. To fix such code, put them in an extern "C" block:
16
16
17
17
```
18
18
extern "C" {
Original file line number Diff line number Diff line change 1
- Items are missing in a trait implementation. Erroneous code example:
1
+ Items are missing in a trait implementation.
2
+
3
+ Erroneous code example:
2
4
3
5
``` compile_fail,E0046
4
6
trait Foo {
Original file line number Diff line number Diff line change 1
- This error indicates that an attempted implementation of a trait method
2
- has the wrong number of type or const parameters.
1
+ An attempted implementation of a trait method has the wrong number of type or
2
+ const parameters.
3
3
4
- For example, the trait below has a method ` foo ` with a type parameter ` T ` ,
5
- but the implementation of ` foo ` for the type ` Bar ` is missing this parameter:
4
+ Erroneous code example:
6
5
7
6
``` compile_fail,E0049
8
7
trait Foo {
@@ -17,3 +16,21 @@ impl Foo for Bar {
17
16
fn foo(x: bool) -> Self { Bar }
18
17
}
19
18
```
19
+
20
+ For example, the ` Foo ` trait has a method ` foo ` with a type parameter ` T ` ,
21
+ but the implementation of ` foo ` for the type ` Bar ` is missing this parameter.
22
+ To fix this error, they must have the same type parameters:
23
+
24
+ ```
25
+ trait Foo {
26
+ fn foo<T: Default>(x: T) -> Self;
27
+ }
28
+
29
+ struct Bar;
30
+
31
+ impl Foo for Bar {
32
+ fn foo<T: Default>(x: T) -> Self { // ok!
33
+ Bar
34
+ }
35
+ }
36
+ ```
Original file line number Diff line number Diff line change 1
- This error indicates that an attempted implementation of a trait method
2
- has the wrong number of function parameters.
1
+ An attempted implementation of a trait method has the wrong number of function
2
+ parameters.
3
3
4
- For example, the trait below has a method ` foo ` with two function parameters
5
- (` &self ` and ` u8 ` ), but the implementation of ` foo ` for the type ` Bar ` omits
6
- the ` u8 ` parameter:
4
+ Erroneous code example:
7
5
8
6
``` compile_fail,E0050
9
7
trait Foo {
@@ -18,3 +16,21 @@ impl Foo for Bar {
18
16
fn foo(&self) -> bool { true }
19
17
}
20
18
```
19
+
20
+ For example, the ` Foo ` trait has a method ` foo ` with two function parameters
21
+ (` &self ` and ` u8 ` ), but the implementation of ` foo ` for the type ` Bar ` omits
22
+ the ` u8 ` parameter. To fix this error, they must have the same parameters:
23
+
24
+ ```
25
+ trait Foo {
26
+ fn foo(&self, x: u8) -> bool;
27
+ }
28
+
29
+ struct Bar;
30
+
31
+ impl Foo for Bar {
32
+ fn foo(&self, x: u8) -> bool { // ok!
33
+ true
34
+ }
35
+ }
36
+ ```
Original file line number Diff line number Diff line change 1
1
The parameters of any trait method must match between a trait implementation
2
2
and the trait definition.
3
3
4
- Here are a couple examples of this error :
4
+ Erroneous code example :
5
5
6
6
``` compile_fail,E0053
7
7
trait Foo {
Original file line number Diff line number Diff line change 1
- It is not allowed to cast to a bool. If you are trying to cast a numeric type
2
- to a bool, you can compare it with zero instead:
1
+ It is not allowed to cast to a bool.
2
+
3
+ Erroneous code example:
3
4
4
5
``` compile_fail,E0054
5
6
let x = 5;
@@ -8,6 +9,9 @@ let x = 5;
8
9
let x_is_nonzero = x as bool;
9
10
```
10
11
12
+ If you are trying to cast a numeric type to a bool, you can compare it with
13
+ zero instead:
14
+
11
15
```
12
16
let x = 5;
13
17
You can’t perform that action at this time.
0 commit comments