Skip to content

Commit 3294f41

Browse files
authored
Rollup merge of #66443 - GuillaumeGomez:port-erased-cleanup, r=Mark-Simulacrum
Port erased cleanup Just realised that the changes I made in #65965 were removed after the move of all error codes so here it is. I made them into separate commits to make the history look better this time. r? @Mark-Simulacrum
2 parents 511573e + f9fdc38 commit 3294f41

16 files changed

+135
-76
lines changed

src/librustc_error_codes/error_codes.rs

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
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

510
crate::register_diagnostics! {
611

src/librustc_error_codes/error_codes/E0023.md

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
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
```
66
enum 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+
1215
Here the `Apple` variant has two fields, and should be matched against like so:
1316

1417
```

src/librustc_error_codes/error_codes/E0025.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
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
56
struct Foo {
+10-27
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,34 @@
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
266
struct Thing {
277
x: u32,
28-
y: u32
8+
y: u32,
299
}
3010
3111
let thing = Thing { x: 0, y: 0 };
3212
3313
match 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
```
4124
struct Thing {
4225
x: u32,
43-
y: u32
26+
y: u32,
4427
}
4528
4629
let thing = Thing { x: 0, y: 0 };
4730
4831
match thing {
49-
Thing { x, y: z } => {}
32+
Thing { x, y: z } => {} // we renamed `y` to `z`
5033
}
5134
```

src/librustc_error_codes/error_codes/E0027.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
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
87
struct 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
```
2424
struct Dog {

src/librustc_error_codes/error_codes/E0029.md

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
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
86
let 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.

src/librustc_error_codes/error_codes/E0033.md

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
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();
1715
trait_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+
2024
You can read more about trait objects in the [Trait Objects] section of the
2125
Reference.
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

src/librustc_error_codes/error_codes/E0034.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
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:
35

46
```compile_fail,E0034
57
struct Test;

src/librustc_error_codes/error_codes/E0040.md

+21-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
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
86
struct 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+
```

src/librustc_error_codes/error_codes/E0044.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
You cannot use type or const parameters on foreign items.
2+
23
Example of erroneous code:
34

45
```compile_fail,E0044

src/librustc_error_codes/error_codes/E0045.md

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
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.
72

8-
extern "rust-call" { fn foo(x: u8, ...); }
3+
Erroneous code example:
94

10-
// or
5+
```compile_fail,E0045
6+
#![feature(unboxed_closures)]
117
12-
fn foo(x: u8, ...) {}
8+
extern "rust-call" {
9+
fn foo(x: u8, ...); // error!
10+
}
1311
```
1412

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:
1616

1717
```
1818
extern "C" {

src/librustc_error_codes/error_codes/E0046.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
Items are missing in a trait implementation. Erroneous code example:
1+
Items are missing in a trait implementation.
2+
3+
Erroneous code example:
24

35
```compile_fail,E0046
46
trait Foo {

src/librustc_error_codes/error_codes/E0049.md

+21-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
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.
33

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:
65

76
```compile_fail,E0049
87
trait Foo {
@@ -17,3 +16,21 @@ impl Foo for Bar {
1716
fn foo(x: bool) -> Self { Bar }
1817
}
1918
```
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+
```

src/librustc_error_codes/error_codes/E0050.md

+21-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
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.
33

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:
75

86
```compile_fail,E0050
97
trait Foo {
@@ -18,3 +16,21 @@ impl Foo for Bar {
1816
fn foo(&self) -> bool { true }
1917
}
2018
```
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+
```

src/librustc_error_codes/error_codes/E0053.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
The parameters of any trait method must match between a trait implementation
22
and the trait definition.
33

4-
Here are a couple examples of this error:
4+
Erroneous code example:
55

66
```compile_fail,E0053
77
trait Foo {

src/librustc_error_codes/error_codes/E0054.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
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:
34

45
```compile_fail,E0054
56
let x = 5;
@@ -8,6 +9,9 @@ let x = 5;
89
let x_is_nonzero = x as bool;
910
```
1011

12+
If you are trying to cast a numeric type to a bool, you can compare it with
13+
zero instead:
14+
1115
```
1216
let x = 5;
1317

0 commit comments

Comments
 (0)