-
Notifications
You must be signed in to change notification settings - Fork 12.7k
/
diagnostics.rs
1702 lines (1269 loc) · 30.8 KB
/
diagnostics.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#![allow(non_snake_case)]
// Error messages for EXXXX errors. Each message should start and end with a
// new line, and be wrapped to 80 characters. In vim you can `:set tw=80` and
// use `gq` to wrap paragraphs. Use `:set tw=0` to disable.
register_long_diagnostics! {
E0128: r##"
Type parameter defaults can only use parameters that occur before them.
Erroneous code example:
```compile_fail,E0128
struct Foo<T=U, U=()> {
field1: T,
filed2: U,
}
// error: type parameters with a default cannot use forward declared
// identifiers
```
Since type parameters are evaluated in-order, you may be able to fix this issue
by doing:
```
struct Foo<U=(), T=U> {
field1: T,
filed2: U,
}
```
Please also verify that this wasn't because of a name-clash and rename the type
parameter if so.
"##,
E0154: r##"
#### Note: this error code is no longer emitted by the compiler.
Imports (`use` statements) are not allowed after non-item statements, such as
variable declarations and expression statements.
Here is an example that demonstrates the error:
```
fn f() {
// Variable declaration before import
let x = 0;
use std::io::Read;
// ...
}
```
The solution is to declare the imports at the top of the block, function, or
file.
Here is the previous example again, with the correct order:
```
fn f() {
use std::io::Read;
let x = 0;
// ...
}
```
See the Declaration Statements section of the reference for more information
about what constitutes an Item declaration and what does not:
https://doc.rust-lang.org/reference.html#statements
"##,
E0251: r##"
#### Note: this error code is no longer emitted by the compiler.
Two items of the same name cannot be imported without rebinding one of the
items under a new local name.
An example of this error:
```
use foo::baz;
use bar::*; // error, do `use foo::baz as quux` instead on the previous line
fn main() {}
mod foo {
pub struct baz;
}
mod bar {
pub mod baz {}
}
```
"##,
E0252: r##"
Two items of the same name cannot be imported without rebinding one of the
items under a new local name.
Erroneous code example:
```compile_fail,E0252
use foo::baz;
use bar::baz; // error, do `use bar::baz as quux` instead
fn main() {}
mod foo {
pub struct baz;
}
mod bar {
pub mod baz {}
}
```
You can use aliases in order to fix this error. Example:
```
use foo::baz as foo_baz;
use bar::baz; // ok!
fn main() {}
mod foo {
pub struct baz;
}
mod bar {
pub mod baz {}
}
```
Or you can reference the item with its parent:
```
use bar::baz;
fn main() {
let x = foo::baz; // ok!
}
mod foo {
pub struct baz;
}
mod bar {
pub mod baz {}
}
```
"##,
E0253: r##"
Attempt was made to import an unimportable value. This can happen when trying
to import a method from a trait.
Erroneous code example:
```compile_fail,E0253
mod foo {
pub trait MyTrait {
fn do_something();
}
}
use foo::MyTrait::do_something;
// error: `do_something` is not directly importable
fn main() {}
```
It's invalid to directly import methods belonging to a trait or concrete type.
"##,
E0254: r##"
Attempt was made to import an item whereas an extern crate with this name has
already been imported.
Erroneous code example:
```compile_fail,E0254
extern crate core;
mod foo {
pub trait core {
fn do_something();
}
}
use foo::core; // error: an extern crate named `core` has already
// been imported in this module
fn main() {}
```
To fix issue issue, you have to rename at least one of the two imports.
Example:
```
extern crate core as libcore; // ok!
mod foo {
pub trait core {
fn do_something();
}
}
use foo::core;
fn main() {}
```
"##,
E0255: r##"
You can't import a value whose name is the same as another value defined in the
module.
Erroneous code example:
```compile_fail,E0255
use bar::foo; // error: an item named `foo` is already in scope
fn foo() {}
mod bar {
pub fn foo() {}
}
fn main() {}
```
You can use aliases in order to fix this error. Example:
```
use bar::foo as bar_foo; // ok!
fn foo() {}
mod bar {
pub fn foo() {}
}
fn main() {}
```
Or you can reference the item with its parent:
```
fn foo() {}
mod bar {
pub fn foo() {}
}
fn main() {
bar::foo(); // we get the item by referring to its parent
}
```
"##,
E0256: r##"
#### Note: this error code is no longer emitted by the compiler.
You can't import a type or module when the name of the item being imported is
the same as another type or submodule defined in the module.
An example of this error:
```compile_fail
use foo::Bar; // error
type Bar = u32;
mod foo {
pub mod Bar { }
}
fn main() {}
```
"##,
E0259: r##"
The name chosen for an external crate conflicts with another external crate
that has been imported into the current module.
Erroneous code example:
```compile_fail,E0259
# #![feature(libc)]
extern crate core;
extern crate libc as core;
fn main() {}
```
The solution is to choose a different name that doesn't conflict with any
external crate imported into the current module.
Correct example:
```
# #![feature(libc)]
extern crate core;
extern crate libc as other_name;
fn main() {}
```
"##,
E0260: r##"
The name for an item declaration conflicts with an external crate's name.
Erroneous code example:
```compile_fail,E0260
extern crate core;
struct core;
```
There are two possible solutions:
Solution #1: Rename the item.
```
extern crate core;
struct xyz;
```
Solution #2: Import the crate with a different name.
```
extern crate core as xyz;
struct abc;
```
See the Declaration Statements section of the reference for more information
about what constitutes an Item declaration and what does not:
https://doc.rust-lang.org/reference.html#statements
"##,
E0364: r##"
Private items cannot be publicly re-exported. This error indicates that you
attempted to `pub use` a type or value that was not itself public.
Erroneous code example:
```compile_fail
mod foo {
const X: u32 = 1;
}
pub use foo::X;
fn main() {}
```
The solution to this problem is to ensure that the items that you are
re-exporting are themselves marked with `pub`:
```
mod foo {
pub const X: u32 = 1;
}
pub use foo::X;
fn main() {}
```
See the 'Use Declarations' section of the reference for more information on
this topic:
https://doc.rust-lang.org/reference.html#use-declarations
"##,
E0365: r##"
Private modules cannot be publicly re-exported. This error indicates that you
attempted to `pub use` a module that was not itself public.
Erroneous code example:
```compile_fail,E0365
mod foo {
pub const X: u32 = 1;
}
pub use foo as foo2;
fn main() {}
```
The solution to this problem is to ensure that the module that you are
re-exporting is itself marked with `pub`:
```
pub mod foo {
pub const X: u32 = 1;
}
pub use foo as foo2;
fn main() {}
```
See the 'Use Declarations' section of the reference for more information
on this topic:
https://doc.rust-lang.org/reference.html#use-declarations
"##,
E0401: r##"
Inner items do not inherit type parameters from the functions they are embedded
in.
Erroneous code example:
```compile_fail,E0401
fn foo<T>(x: T) {
fn bar(y: T) { // T is defined in the "outer" function
// ..
}
bar(x);
}
```
Nor will this:
```compile_fail,E0401
fn foo<T>(x: T) {
type MaybeT = Option<T>;
// ...
}
```
Or this:
```compile_fail,E0401
fn foo<T>(x: T) {
struct Foo {
x: T,
}
// ...
}
```
Items inside functions are basically just like top-level items, except
that they can only be used from the function they are in.
There are a couple of solutions for this.
If the item is a function, you may use a closure:
```
fn foo<T>(x: T) {
let bar = |y: T| { // explicit type annotation may not be necessary
// ..
};
bar(x);
}
```
For a generic item, you can copy over the parameters:
```
fn foo<T>(x: T) {
fn bar<T>(y: T) {
// ..
}
bar(x);
}
```
```
fn foo<T>(x: T) {
type MaybeT<T> = Option<T>;
}
```
Be sure to copy over any bounds as well:
```
fn foo<T: Copy>(x: T) {
fn bar<T: Copy>(y: T) {
// ..
}
bar(x);
}
```
```
fn foo<T: Copy>(x: T) {
struct Foo<T: Copy> {
x: T,
}
}
```
This may require additional type hints in the function body.
In case the item is a function inside an `impl`, defining a private helper
function might be easier:
```
# struct Foo<T>(T);
impl<T> Foo<T> {
pub fn foo(&self, x: T) {
self.bar(x);
}
fn bar(&self, y: T) {
// ..
}
}
```
For default impls in traits, the private helper solution won't work, however
closures or copying the parameters should still work.
"##,
E0403: r##"
Some type parameters have the same name.
Erroneous code example:
```compile_fail,E0403
fn foo<T, T>(s: T, u: T) {} // error: the name `T` is already used for a type
// parameter in this type parameter list
```
Please verify that none of the type parameterss are misspelled, and rename any
clashing parameters. Example:
```
fn foo<T, Y>(s: T, u: Y) {} // ok!
```
"##,
E0404: r##"
You tried to implement something which was not a trait on an object.
Erroneous code example:
```compile_fail,E0404
struct Foo;
struct Bar;
impl Foo for Bar {} // error: `Foo` is not a trait
```
Please verify that you didn't misspell the trait's name or otherwise use the
wrong identifier. Example:
```
trait Foo {
// some functions
}
struct Bar;
impl Foo for Bar { // ok!
// functions implementation
}
```
"##,
E0405: r##"
The code refers to a trait that is not in scope.
Erroneous code example:
```compile_fail,E0405
struct Foo;
impl SomeTrait for Foo {} // error: trait `SomeTrait` is not in scope
```
Please verify that the name of the trait wasn't misspelled and ensure that it
was imported. Example:
```
# #[cfg(for_demonstration_only)]
// solution 1:
use some_file::SomeTrait;
// solution 2:
trait SomeTrait {
// some functions
}
struct Foo;
impl SomeTrait for Foo { // ok!
// implements functions
}
```
"##,
E0407: r##"
A definition of a method not in the implemented trait was given in a trait
implementation.
Erroneous code example:
```compile_fail,E0407
trait Foo {
fn a();
}
struct Bar;
impl Foo for Bar {
fn a() {}
fn b() {} // error: method `b` is not a member of trait `Foo`
}
```
Please verify you didn't misspell the method name and you used the correct
trait. First example:
```
trait Foo {
fn a();
fn b();
}
struct Bar;
impl Foo for Bar {
fn a() {}
fn b() {} // ok!
}
```
Second example:
```
trait Foo {
fn a();
}
struct Bar;
impl Foo for Bar {
fn a() {}
}
impl Bar {
fn b() {}
}
```
"##,
E0408: r##"
An "or" pattern was used where the variable bindings are not consistently bound
across patterns.
Erroneous code example:
```compile_fail,E0408
match x {
Some(y) | None => { /* use y */ } // error: variable `y` from pattern #1 is
// not bound in pattern #2
_ => ()
}
```
Here, `y` is bound to the contents of the `Some` and can be used within the
block corresponding to the match arm. However, in case `x` is `None`, we have
not specified what `y` is, and the block will use a nonexistent variable.
To fix this error, either split into multiple match arms:
```
let x = Some(1);
match x {
Some(y) => { /* use y */ }
None => { /* ... */ }
}
```
or, bind the variable to a field of the same type in all sub-patterns of the
or pattern:
```
let x = (0, 2);
match x {
(0, y) | (y, 0) => { /* use y */}
_ => {}
}
```
In this example, if `x` matches the pattern `(0, _)`, the second field is set
to `y`. If it matches `(_, 0)`, the first field is set to `y`; so in all
cases `y` is set to some value.
"##,
E0409: r##"
An "or" pattern was used where the variable bindings are not consistently bound
across patterns.
Erroneous code example:
```compile_fail,E0409
let x = (0, 2);
match x {
(0, ref y) | (y, 0) => { /* use y */} // error: variable `y` is bound with
// different mode in pattern #2
// than in pattern #1
_ => ()
}
```
Here, `y` is bound by-value in one case and by-reference in the other.
To fix this error, just use the same mode in both cases.
Generally using `ref` or `ref mut` where not already used will fix this:
```
let x = (0, 2);
match x {
(0, ref y) | (ref y, 0) => { /* use y */}
_ => ()
}
```
Alternatively, split the pattern:
```
let x = (0, 2);
match x {
(y, 0) => { /* use y */ }
(0, ref y) => { /* use y */}
_ => ()
}
```
"##,
E0411: r##"
The `Self` keyword was used outside an impl or a trait.
Erroneous code example:
```compile_fail,E0411
<Self>::foo; // error: use of `Self` outside of an impl or trait
```
The `Self` keyword represents the current type, which explains why it can only
be used inside an impl or a trait. It gives access to the associated items of a
type:
```
trait Foo {
type Bar;
}
trait Baz : Foo {
fn bar() -> Self::Bar; // like this
}
```
However, be careful when two types have a common associated type:
```compile_fail
trait Foo {
type Bar;
}
trait Foo2 {
type Bar;
}
trait Baz : Foo + Foo2 {
fn bar() -> Self::Bar;
// error: ambiguous associated type `Bar` in bounds of `Self`
}
```
This problem can be solved by specifying from which trait we want to use the
`Bar` type:
```
trait Foo {
type Bar;
}
trait Foo2 {
type Bar;
}
trait Baz : Foo + Foo2 {
fn bar() -> <Self as Foo>::Bar; // ok!
}
```
"##,
E0412: r##"
The type name used is not in scope.
Erroneous code examples:
```compile_fail,E0412
impl Something {} // error: type name `Something` is not in scope
// or:
trait Foo {
fn bar(N); // error: type name `N` is not in scope
}
// or:
fn foo(x: T) {} // type name `T` is not in scope
```
To fix this error, please verify you didn't misspell the type name, you did
declare it or imported it into the scope. Examples:
```
struct Something;
impl Something {} // ok!
// or:
trait Foo {
type N;
fn bar(_: Self::N); // ok!
}
// or:
fn foo<T>(x: T) {} // ok!
```
Another case that causes this error is when a type is imported into a parent
module. To fix this, you can follow the suggestion and use File directly or
`use super::File;` which will import the types from the parent namespace. An
example that causes this error is below:
```compile_fail,E0412
use std::fs::File;
mod foo {
fn some_function(f: File) {}
}
```
```
use std::fs::File;
mod foo {
// either
use super::File;
// or
// use std::fs::File;
fn foo(f: File) {}
}
# fn main() {} // don't insert it for us; that'll break imports
```
"##,
E0415: r##"
More than one function parameter have the same name.
Erroneous code example:
```compile_fail,E0415
fn foo(f: i32, f: i32) {} // error: identifier `f` is bound more than
// once in this parameter list
```
Please verify you didn't misspell parameters' name. Example:
```
fn foo(f: i32, g: i32) {} // ok!
```
"##,
E0416: r##"
An identifier is bound more than once in a pattern.
Erroneous code example:
```compile_fail,E0416
match (1, 2) {
(x, x) => {} // error: identifier `x` is bound more than once in the
// same pattern
}
```
Please verify you didn't misspell identifiers' name. Example:
```
match (1, 2) {
(x, y) => {} // ok!
}
```
Or maybe did you mean to unify? Consider using a guard:
```
# let (A, B, C) = (1, 2, 3);
match (A, B, C) {
(x, x2, see) if x == x2 => { /* A and B are equal, do one thing */ }
(y, z, see) => { /* A and B unequal; do another thing */ }
}
```
"##,
E0422: r##"
You are trying to use an identifier that is either undefined or not a struct.
Erroneous code example:
```compile_fail,E0422
fn main () {
let x = Foo { x: 1, y: 2 };
}
```
In this case, `Foo` is undefined, so it inherently isn't anything, and
definitely not a struct.
```compile_fail
fn main () {
let foo = 1;
let x = foo { x: 1, y: 2 };
}
```
In this case, `foo` is defined, but is not a struct, so Rust can't use it as
one.
"##,
E0423: r##"
A `struct` variant name was used like a function name.
Erroneous code example:
```compile_fail,E0423
struct Foo { a: bool };
let f = Foo();
// error: `Foo` is a struct variant name, but this expression uses
// it like a function name
```
Please verify you didn't misspell the name of what you actually wanted to use
here. Example:
```
fn Foo() -> u32 { 0 }
let f = Foo(); // ok!
```
"##,
E0424: r##"
The `self` keyword was used in a static method.
Erroneous code example:
```compile_fail,E0424
struct Foo;
impl Foo {
fn bar(self) {}
fn foo() {
self.bar(); // error: `self` is not available in a static method.
}
}
```
Please check if the method's argument list should have contained `self`,
`&self`, or `&mut self` (in case you didn't want to create a static
method), and add it if so. Example:
```
struct Foo;
impl Foo {
fn bar(self) {}
fn foo(self) {
self.bar(); // ok!
}
}