3
3
syntax:: register_diagnostics! {
4
4
5
5
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:
9
9
10
10
```
11
11
enum Fruit {
@@ -14,6 +14,9 @@ enum Fruit {
14
14
}
15
15
```
16
16
17
+ A pattern used to match against an enum variant must provide a sub-pattern for
18
+ each field of the enum variant.
19
+
17
20
Here the `Apple` variant has two fields, and should be matched against like so:
18
21
19
22
```
@@ -53,8 +56,9 @@ uses the same number.
53
56
"## ,
54
57
55
58
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:
58
62
59
63
```compile_fail,E0025
60
64
struct Foo {
@@ -89,65 +93,47 @@ fn main(){
89
93
"## ,
90
94
91
95
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.
105
97
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:
115
99
116
100
```compile_fail,E0026
117
101
struct Thing {
118
102
x: u32,
119
- y: u32
103
+ y: u32,
120
104
}
121
105
122
106
let thing = Thing { x: 0, y: 0 };
123
107
124
108
match thing {
125
- Thing { x, z } => {}
109
+ Thing { x, z } => {} // error: `Thing::z` field doesn't exist
126
110
}
127
111
```
128
112
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.
130
117
131
118
```
132
119
struct Thing {
133
120
x: u32,
134
- y: u32
121
+ y: u32,
135
122
}
136
123
137
124
let thing = Thing { x: 0, y: 0 };
138
125
139
126
match thing {
140
- Thing { x, y: z } => {}
127
+ Thing { x, y: z } => {} // we renamed `y` to `z`
141
128
}
142
129
```
143
130
"## ,
144
131
145
132
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.
149
135
150
- For example:
136
+ Erroneous code example:
151
137
152
138
```compile_fail,E0027
153
139
struct Dog {
@@ -163,7 +149,8 @@ match d {
163
149
}
164
150
```
165
151
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:
167
154
168
155
```
169
156
struct Dog {
@@ -185,11 +172,9 @@ match d {
185
172
"## ,
186
173
187
174
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:
193
178
194
179
```compile_fail,E0029
195
180
let string = "salutations !";
@@ -207,14 +192,18 @@ match string {
207
192
_ => {}
208
193
}
209
194
```
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.
210
201
"## ,
211
202
212
203
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:
218
207
219
208
```compile_fail,E0033
220
209
# trait SomeTrait { fn method_one(&self){} fn method_two(&self){} }
@@ -229,6 +218,12 @@ trait_obj.method_one();
229
218
trait_obj.method_two();
230
219
```
231
220
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
+
232
227
You can read more about trait objects in the [Trait Objects] section of the
233
228
Reference.
234
229
@@ -237,7 +232,9 @@ Reference.
237
232
238
233
E0034 : r##"
239
234
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:
241
238
242
239
```compile_fail,E0034
243
240
struct Test;
@@ -323,11 +320,9 @@ fn main() {
323
320
"## ,
324
321
325
322
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.
329
324
330
- Here's an example of this error :
325
+ Erroneous code example:
331
326
332
327
```compile_fail,E0040
333
328
struct Foo {
@@ -345,11 +340,33 @@ fn main() {
345
340
x.drop(); // error: explicit use of destructor method
346
341
}
347
342
```
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
+ ```
348
364
"## ,
349
365
350
366
E0044 : r##"
351
367
You cannot use type or const parameters on foreign items.
352
- Example of erroneous code:
368
+
369
+ Erroneous code example:
353
370
354
371
```compile_fail,E0044
355
372
extern { fn some_func<T>(x: T); }
@@ -365,21 +382,21 @@ extern { fn some_func_i64(x: i64); }
365
382
"## ,
366
383
367
384
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.
371
386
372
- ```compile_fail
373
- #![feature(unboxed_closures)]
374
-
375
- extern "rust-call" { fn foo(x: u8, ...); }
387
+ Erroneous code example:
376
388
377
- // or
389
+ ```compile_fail,E0045
390
+ #![feature(unboxed_closures)]
378
391
379
- fn foo(x: u8, ...) {}
392
+ extern "rust-call" {
393
+ fn foo(x: u8, ...); // error!
394
+ }
380
395
```
381
396
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:
383
400
384
401
```
385
402
extern "C" {
@@ -389,7 +406,9 @@ extern "C" {
389
406
"## ,
390
407
391
408
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:
393
412
394
413
```compile_fail,E0046
395
414
trait Foo {
@@ -421,11 +440,10 @@ impl Foo for Bar {
421
440
"## ,
422
441
423
442
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.
426
445
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:
429
447
430
448
```compile_fail,E0049
431
449
trait Foo {
@@ -440,15 +458,31 @@ impl Foo for Bar {
440
458
fn foo(x: bool) -> Self { Bar }
441
459
}
442
460
```
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
+ ```
443
479
"## ,
444
480
445
481
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.
448
484
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:
452
486
453
487
```compile_fail,E0050
454
488
trait Foo {
@@ -463,13 +497,31 @@ impl Foo for Bar {
463
497
fn foo(&self) -> bool { true }
464
498
}
465
499
```
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
+ ```
466
518
"## ,
467
519
468
520
E0053 : r##"
469
521
The parameters of any trait method must match between a trait implementation
470
522
and the trait definition.
471
523
472
- Here are a couple examples of this error :
524
+ Erroneous code example :
473
525
474
526
```compile_fail,E0053
475
527
trait Foo {
@@ -490,8 +542,9 @@ impl Foo for Bar {
490
542
"## ,
491
543
492
544
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:
495
548
496
549
```compile_fail,E0054
497
550
let x = 5;
@@ -500,6 +553,9 @@ let x = 5;
500
553
let x_is_nonzero = x as bool;
501
554
```
502
555
556
+ If you are trying to cast a numeric type to a bool, you can compare it with
557
+ zero instead:
558
+
503
559
```
504
560
let x = 5;
505
561
0 commit comments