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 11// Error messages for EXXXX errors. Each message should start and end with a
22// new line, and be wrapped to 80 characters. In vim you can `:set tw=80` and
33// 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
49
510crate :: register_diagnostics! {
611
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:
44
55```
66enum Fruit {
@@ -9,6 +9,9 @@ enum Fruit {
99}
1010```
1111
12+ A pattern used to match against an enum variant must provide a sub-pattern for
13+ each field of the enum variant.
14+
1215Here the ` Apple ` variant has two fields, and should be matched against like so:
1316
1417```
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:
34
45``` compile_fail,E0025
56struct 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.
52
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:
244
255``` compile_fail,E0026
266struct Thing {
277 x: u32,
28- y: u32
8+ y: u32,
299}
3010
3111let thing = Thing { x: 0, y: 0 };
3212
3313match thing {
34- Thing { x, z } => {}
14+ Thing { x, z } => {} // error: `Thing::z` field doesn't exist
3515}
3616```
3717
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.
3922
4023```
4124struct Thing {
4225 x: u32,
43- y: u32
26+ y: u32,
4427}
4528
4629let thing = Thing { x: 0, y: 0 };
4730
4831match thing {
49- Thing { x, y: z } => {}
32+ Thing { x, y: z } => {} // we renamed `y` to `z`
5033}
5134```
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.
43
5- For example:
4+ Erroneous code example:
65
76``` compile_fail,E0027
87struct Dog {
@@ -18,7 +17,8 @@ match d {
1817}
1918```
2019
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:
2222
2323```
2424struct 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:
64
75``` compile_fail,E0029
86let string = "salutations !";
@@ -20,3 +18,9 @@ match string {
2018 _ => {}
2119}
2220```
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:
64
75``` compile_fail,E0033
86# trait SomeTrait { fn method_one(&self){} fn method_two(&self){} }
@@ -17,7 +15,13 @@ trait_obj.method_one();
1715trait_obj.method_two();
1816```
1917
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+
2024You can read more about trait objects in the [ Trait Objects] section of the
2125Reference.
2226
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 11The 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:
35
46``` compile_fail,E0034
57struct 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.
42
5- Here's an example of this error :
3+ Erroneous code example:
64
75``` compile_fail,E0040
86struct Foo {
@@ -20,3 +18,22 @@ fn main() {
2018 x.drop(); // error: explicit use of destructor method
2119}
2220```
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 11You cannot use type or const parameters on foreign items.
2+
23Example of erroneous code:
34
45``` compile_fail,E0044
You can’t perform that action at this time.
0 commit comments