File tree Expand file tree Collapse file tree 16 files changed +135
-76
lines changed Expand file tree Collapse file tree 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
You can’t perform that action at this time.
0 commit comments