Skip to content

Commit b850620

Browse files
authored
Rollup merge of #65965 - GuillaumeGomez:clean-up-librustc_typeck-error-codes, r=Mark-Simulacrum
Clean up librustc_typeck error_codes file r? @Dylan-DPC
2 parents e931f00 + db1dd8f commit b850620

File tree

1 file changed

+132
-76
lines changed

1 file changed

+132
-76
lines changed

src/librustc_typeck/error_codes.rs

Lines changed: 132 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
syntax::register_diagnostics! {
44

55
E0023: r##"
6-
A pattern used to match against an enum variant must provide a sub-pattern for
7-
each field of the enum variant. This error indicates that a pattern attempted to
8-
extract an incorrect number of fields from a variant.
6+
A pattern attempted to extract an incorrect number of fields from a variant.
7+
8+
Erroneous code example:
99
1010
```
1111
enum Fruit {
@@ -14,6 +14,9 @@ enum Fruit {
1414
}
1515
```
1616
17+
A pattern used to match against an enum variant must provide a sub-pattern for
18+
each field of the enum variant.
19+
1720
Here the `Apple` variant has two fields, and should be matched against like so:
1821
1922
```
@@ -53,8 +56,9 @@ uses the same number.
5356
"##,
5457

5558
E0025: r##"
56-
Each field of a struct can only be bound once in a pattern. Erroneous code
57-
example:
59+
Each field of a struct can only be bound once in a pattern.
60+
61+
Erroneous code example:
5862
5963
```compile_fail,E0025
6064
struct Foo {
@@ -89,65 +93,47 @@ fn main(){
8993
"##,
9094

9195
E0026: r##"
92-
This error indicates that a struct pattern attempted to extract a non-existent
93-
field from a struct. Struct fields are identified by the name used before the
94-
colon `:` so struct patterns should resemble the declaration of the struct type
95-
being matched.
96-
97-
```
98-
// Correct matching.
99-
struct Thing {
100-
x: u32,
101-
y: u32
102-
}
103-
104-
let thing = Thing { x: 1, y: 2 };
96+
A struct pattern attempted to extract a non-existent field from a struct.
10597
106-
match thing {
107-
Thing { x: xfield, y: yfield } => {}
108-
}
109-
```
110-
111-
If you are using shorthand field patterns but want to refer to the struct field
112-
by a different name, you should rename it explicitly.
113-
114-
Change this:
98+
Erroneous code example:
11599
116100
```compile_fail,E0026
117101
struct Thing {
118102
x: u32,
119-
y: u32
103+
y: u32,
120104
}
121105
122106
let thing = Thing { x: 0, y: 0 };
123107
124108
match thing {
125-
Thing { x, z } => {}
109+
Thing { x, z } => {} // error: `Thing::z` field doesn't exist
126110
}
127111
```
128112
129-
To this:
113+
If you are using shorthand field patterns but want to refer to the struct field
114+
by a different name, you should rename it explicitly. Struct fields are
115+
identified by the name used before the colon `:` so struct patterns should
116+
resemble the declaration of the struct type being matched.
130117
131118
```
132119
struct Thing {
133120
x: u32,
134-
y: u32
121+
y: u32,
135122
}
136123
137124
let thing = Thing { x: 0, y: 0 };
138125
139126
match thing {
140-
Thing { x, y: z } => {}
127+
Thing { x, y: z } => {} // we renamed `y` to `z`
141128
}
142129
```
143130
"##,
144131

145132
E0027: r##"
146-
This error indicates that a pattern for a struct fails to specify a sub-pattern
147-
for every one of the struct's fields. Ensure that each field from the struct's
148-
definition is mentioned in the pattern, or use `..` to ignore unwanted fields.
133+
A pattern for a struct fails to specify a sub-pattern for every one of the
134+
struct's fields.
149135
150-
For example:
136+
Erroneous code example:
151137
152138
```compile_fail,E0027
153139
struct Dog {
@@ -163,7 +149,8 @@ match d {
163149
}
164150
```
165151
166-
This is correct (explicit):
152+
To fix this error, ensure that each field from the struct's definition is
153+
mentioned in the pattern, or use `..` to ignore unwanted fields. Example:
167154
168155
```
169156
struct Dog {
@@ -185,11 +172,9 @@ match d {
185172
"##,
186173

187174
E0029: r##"
188-
In a match expression, only numbers and characters can be matched against a
189-
range. This is because the compiler checks that the range is non-empty at
190-
compile-time, and is unable to evaluate arbitrary comparison functions. If you
191-
want to capture values of an orderable type between two end-points, you can use
192-
a guard.
175+
Something other than numbers and characters has been used for a range.
176+
177+
Erroneous code example:
193178
194179
```compile_fail,E0029
195180
let string = "salutations !";
@@ -207,14 +192,18 @@ match string {
207192
_ => {}
208193
}
209194
```
195+
196+
In a match expression, only numbers and characters can be matched against a
197+
range. This is because the compiler checks that the range is non-empty at
198+
compile-time, and is unable to evaluate arbitrary comparison functions. If you
199+
want to capture values of an orderable type between two end-points, you can use
200+
a guard.
210201
"##,
211202

212203
E0033: r##"
213-
This error indicates that a pointer to a trait type cannot be implicitly
214-
dereferenced by a pattern. Every trait defines a type, but because the
215-
size of trait implementers isn't fixed, this type has no compile-time size.
216-
Therefore, all accesses to trait types must be through pointers. If you
217-
encounter this error you should try to avoid dereferencing the pointer.
204+
A trait type has been dereferenced.
205+
206+
Erroneous code example:
218207
219208
```compile_fail,E0033
220209
# trait SomeTrait { fn method_one(&self){} fn method_two(&self){} }
@@ -229,6 +218,12 @@ trait_obj.method_one();
229218
trait_obj.method_two();
230219
```
231220
221+
A pointer to a trait type cannot be implicitly dereferenced by a pattern. Every
222+
trait defines a type, but because the size of trait implementers isn't fixed,
223+
this type has no compile-time size. Therefore, all accesses to trait types must
224+
be through pointers. If you encounter this error you should try to avoid
225+
dereferencing the pointer.
226+
232227
You can read more about trait objects in the [Trait Objects] section of the
233228
Reference.
234229
@@ -237,7 +232,9 @@ Reference.
237232

238233
E0034: r##"
239234
The compiler doesn't know what method to call because more than one method
240-
has the same prototype. Erroneous code example:
235+
has the same prototype.
236+
237+
Erroneous code example:
241238
242239
```compile_fail,E0034
243240
struct Test;
@@ -323,11 +320,9 @@ fn main() {
323320
"##,
324321

325322
E0040: r##"
326-
It is not allowed to manually call destructors in Rust. It is also not
327-
necessary to do this since `drop` is called automatically whenever a value goes
328-
out of scope.
323+
It is not allowed to manually call destructors in Rust.
329324
330-
Here's an example of this error:
325+
Erroneous code example:
331326
332327
```compile_fail,E0040
333328
struct Foo {
@@ -345,11 +340,33 @@ fn main() {
345340
x.drop(); // error: explicit use of destructor method
346341
}
347342
```
343+
344+
It is unnecessary to do this since `drop` is called automatically whenever a
345+
value goes out of scope. However, if you really need to drop a value by hand,
346+
you can use the `std::mem::drop` function:
347+
348+
```
349+
struct Foo {
350+
x: i32,
351+
}
352+
353+
impl Drop for Foo {
354+
fn drop(&mut self) {
355+
println!("kaboom");
356+
}
357+
}
358+
359+
fn main() {
360+
let mut x = Foo { x: -7 };
361+
drop(x); // ok!
362+
}
363+
```
348364
"##,
349365

350366
E0044: r##"
351367
You cannot use type or const parameters on foreign items.
352-
Example of erroneous code:
368+
369+
Erroneous code example:
353370
354371
```compile_fail,E0044
355372
extern { fn some_func<T>(x: T); }
@@ -365,21 +382,21 @@ extern { fn some_func_i64(x: i64); }
365382
"##,
366383

367384
E0045: r##"
368-
Rust only supports variadic parameters for interoperability with C code in its
369-
FFI. As such, variadic parameters can only be used with functions which are
370-
using the C ABI. Examples of erroneous code:
385+
Variadic parameters have been used on a non-C ABI function.
371386
372-
```compile_fail
373-
#![feature(unboxed_closures)]
374-
375-
extern "rust-call" { fn foo(x: u8, ...); }
387+
Erroneous code example:
376388
377-
// or
389+
```compile_fail,E0045
390+
#![feature(unboxed_closures)]
378391
379-
fn foo(x: u8, ...) {}
392+
extern "rust-call" {
393+
fn foo(x: u8, ...); // error!
394+
}
380395
```
381396
382-
To fix such code, put them in an extern "C" block:
397+
Rust only supports variadic parameters for interoperability with C code in its
398+
FFI. As such, variadic parameters can only be used with functions which are
399+
using the C ABI. To fix such code, put them in an extern "C" block:
383400
384401
```
385402
extern "C" {
@@ -389,7 +406,9 @@ extern "C" {
389406
"##,
390407

391408
E0046: r##"
392-
Items are missing in a trait implementation. Erroneous code example:
409+
Items are missing in a trait implementation.
410+
411+
Erroneous code example:
393412
394413
```compile_fail,E0046
395414
trait Foo {
@@ -421,11 +440,10 @@ impl Foo for Bar {
421440
"##,
422441

423442
E0049: r##"
424-
This error indicates that an attempted implementation of a trait method
425-
has the wrong number of type or const parameters.
443+
An attempted implementation of a trait method has the wrong number of type or
444+
const parameters.
426445
427-
For example, the trait below has a method `foo` with a type parameter `T`,
428-
but the implementation of `foo` for the type `Bar` is missing this parameter:
446+
Erroneous code example:
429447
430448
```compile_fail,E0049
431449
trait Foo {
@@ -440,15 +458,31 @@ impl Foo for Bar {
440458
fn foo(x: bool) -> Self { Bar }
441459
}
442460
```
461+
462+
For example, the `Foo` trait has a method `foo` with a type parameter `T`,
463+
but the implementation of `foo` for the type `Bar` is missing this parameter.
464+
To fix this error, they must have the same type parameters:
465+
466+
```
467+
trait Foo {
468+
fn foo<T: Default>(x: T) -> Self;
469+
}
470+
471+
struct Bar;
472+
473+
impl Foo for Bar {
474+
fn foo<T: Default>(x: T) -> Self { // ok!
475+
Bar
476+
}
477+
}
478+
```
443479
"##,
444480

445481
E0050: r##"
446-
This error indicates that an attempted implementation of a trait method
447-
has the wrong number of function parameters.
482+
An attempted implementation of a trait method has the wrong number of function
483+
parameters.
448484
449-
For example, the trait below has a method `foo` with two function parameters
450-
(`&self` and `u8`), but the implementation of `foo` for the type `Bar` omits
451-
the `u8` parameter:
485+
Erroneous code example:
452486
453487
```compile_fail,E0050
454488
trait Foo {
@@ -463,13 +497,31 @@ impl Foo for Bar {
463497
fn foo(&self) -> bool { true }
464498
}
465499
```
500+
501+
For example, the `Foo` trait has a method `foo` with two function parameters
502+
(`&self` and `u8`), but the implementation of `foo` for the type `Bar` omits
503+
the `u8` parameter. To fix this error, they must have the same parameters:
504+
505+
```
506+
trait Foo {
507+
fn foo(&self, x: u8) -> bool;
508+
}
509+
510+
struct Bar;
511+
512+
impl Foo for Bar {
513+
fn foo(&self, x: u8) -> bool { // ok!
514+
true
515+
}
516+
}
517+
```
466518
"##,
467519

468520
E0053: r##"
469521
The parameters of any trait method must match between a trait implementation
470522
and the trait definition.
471523
472-
Here are a couple examples of this error:
524+
Erroneous code example:
473525
474526
```compile_fail,E0053
475527
trait Foo {
@@ -490,8 +542,9 @@ impl Foo for Bar {
490542
"##,
491543

492544
E0054: r##"
493-
It is not allowed to cast to a bool. If you are trying to cast a numeric type
494-
to a bool, you can compare it with zero instead:
545+
It is not allowed to cast to a bool.
546+
547+
Erroneous code example:
495548
496549
```compile_fail,E0054
497550
let x = 5;
@@ -500,6 +553,9 @@ let x = 5;
500553
let x_is_nonzero = x as bool;
501554
```
502555
556+
If you are trying to cast a numeric type to a bool, you can compare it with
557+
zero instead:
558+
503559
```
504560
let x = 5;
505561

0 commit comments

Comments
 (0)