]> git.proxmox.com Git - rustc.git/blame - src/librustc_resolve/diagnostics.rs
New upstream version 1.26.0+dfsg1
[rustc.git] / src / librustc_resolve / diagnostics.rs
CommitLineData
85aaf69f
SL
1// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2// file at the top-level directory of this distribution and at
3// http://rust-lang.org/COPYRIGHT.
4//
5// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8// option. This file may not be copied, modified, or distributed
9// except according to those terms.
10
11#![allow(non_snake_case)]
12
92a42be0
SL
13// Error messages for EXXXX errors. Each message should start and end with a
14// new line, and be wrapped to 80 characters. In vim you can `:set tw=80` and
15// use `gq` to wrap paragraphs. Use `:set tw=0` to disable.
d9579d0f
AL
16register_long_diagnostics! {
17
8bb4bdeb
XL
18E0128: r##"
19Type parameter defaults can only use parameters that occur before them.
20Erroneous code example:
21
22```compile_fail,E0128
23struct Foo<T=U, U=()> {
24 field1: T,
25 filed2: U,
26}
27// error: type parameters with a default cannot use forward declared
28// identifiers
29```
30
31Since type parameters are evaluated in-order, you may be able to fix this issue
32by doing:
33
34```
35struct Foo<U=(), T=U> {
36 field1: T,
37 filed2: U,
38}
39```
40
41Please also verify that this wasn't because of a name-clash and rename the type
42parameter if so.
43"##,
44
d9579d0f 45E0154: r##"
abe05a73 46#### Note: this error code is no longer emitted by the compiler.
3157f602 47
d9579d0f
AL
48Imports (`use` statements) are not allowed after non-item statements, such as
49variable declarations and expression statements.
50
51Here is an example that demonstrates the error:
52
041b39d2 53```
d9579d0f
AL
54fn f() {
55 // Variable declaration before import
56 let x = 0;
57 use std::io::Read;
7453a54e 58 // ...
d9579d0f
AL
59}
60```
61
62The solution is to declare the imports at the top of the block, function, or
63file.
64
65Here is the previous example again, with the correct order:
66
67```
68fn f() {
69 use std::io::Read;
70 let x = 0;
7453a54e 71 // ...
d9579d0f
AL
72}
73```
74
75See the Declaration Statements section of the reference for more information
76about what constitutes an Item declaration and what does not:
77
e9174d1e 78https://doc.rust-lang.org/reference.html#statements
d9579d0f
AL
79"##,
80
81E0251: r##"
abe05a73 82#### Note: this error code is no longer emitted by the compiler.
3157f602 83
d9579d0f
AL
84Two items of the same name cannot be imported without rebinding one of the
85items under a new local name.
86
87An example of this error:
88
041b39d2 89```
d9579d0f
AL
90use foo::baz;
91use bar::*; // error, do `use foo::baz as quux` instead on the previous line
92
93fn main() {}
94
95mod foo {
96 pub struct baz;
97}
98
99mod bar {
100 pub mod baz {}
101}
102```
103"##,
104
105E0252: r##"
106Two items of the same name cannot be imported without rebinding one of the
107items under a new local name.
108
3157f602 109Erroneous code example:
d9579d0f 110
3157f602 111```compile_fail,E0252
d9579d0f
AL
112use foo::baz;
113use bar::baz; // error, do `use bar::baz as quux` instead
114
115fn main() {}
116
117mod foo {
118 pub struct baz;
119}
120
3157f602
XL
121mod bar {
122 pub mod baz {}
123}
124```
125
126You can use aliases in order to fix this error. Example:
127
128```
129use foo::baz as foo_baz;
130use bar::baz; // ok!
131
132fn main() {}
133
134mod foo {
135 pub struct baz;
136}
137
138mod bar {
139 pub mod baz {}
140}
141```
142
143Or you can reference the item with its parent:
144
145```
146use bar::baz;
147
148fn main() {
149 let x = foo::baz; // ok!
150}
151
152mod foo {
153 pub struct baz;
154}
155
d9579d0f
AL
156mod bar {
157 pub mod baz {}
158}
159```
160"##,
161
c1a9b12d 162E0253: r##"
7453a54e 163Attempt was made to import an unimportable value. This can happen when trying
3157f602 164to import a method from a trait.
c1a9b12d 165
3157f602
XL
166Erroneous code example:
167
168```compile_fail,E0253
c1a9b12d
SL
169mod foo {
170 pub trait MyTrait {
171 fn do_something();
172 }
173}
7453a54e 174
c1a9b12d 175use foo::MyTrait::do_something;
5bcae85e 176// error: `do_something` is not directly importable
3157f602
XL
177
178fn main() {}
c1a9b12d
SL
179```
180
181It's invalid to directly import methods belonging to a trait or concrete type.
182"##,
183
5bcae85e
SL
184E0254: r##"
185Attempt was made to import an item whereas an extern crate with this name has
186already been imported.
187
188Erroneous code example:
189
190```compile_fail,E0254
041b39d2 191extern crate core;
5bcae85e
SL
192
193mod foo {
041b39d2 194 pub trait core {
5bcae85e
SL
195 fn do_something();
196 }
197}
198
041b39d2
XL
199use foo::core; // error: an extern crate named `core` has already
200 // been imported in this module
5bcae85e
SL
201
202fn main() {}
203```
204
205To fix issue issue, you have to rename at least one of the two imports.
206Example:
207
041b39d2
XL
208```
209extern crate core as libcore; // ok!
5bcae85e
SL
210
211mod foo {
041b39d2 212 pub trait core {
5bcae85e
SL
213 fn do_something();
214 }
215}
216
041b39d2 217use foo::core;
5bcae85e
SL
218
219fn main() {}
220```
221"##,
222
d9579d0f
AL
223E0255: r##"
224You can't import a value whose name is the same as another value defined in the
225module.
226
3157f602 227Erroneous code example:
d9579d0f 228
3157f602
XL
229```compile_fail,E0255
230use bar::foo; // error: an item named `foo` is already in scope
d9579d0f
AL
231
232fn foo() {}
233
234mod bar {
235 pub fn foo() {}
236}
237
238fn main() {}
239```
3157f602
XL
240
241You can use aliases in order to fix this error. Example:
242
243```
244use bar::foo as bar_foo; // ok!
245
246fn foo() {}
247
248mod bar {
249 pub fn foo() {}
250}
251
252fn main() {}
253```
254
255Or you can reference the item with its parent:
256
257```
258fn foo() {}
259
260mod bar {
261 pub fn foo() {}
262}
263
264fn main() {
265 bar::foo(); // we get the item by referring to its parent
266}
267```
d9579d0f
AL
268"##,
269
270E0256: r##"
abe05a73 271#### Note: this error code is no longer emitted by the compiler.
3157f602 272
d9579d0f
AL
273You can't import a type or module when the name of the item being imported is
274the same as another type or submodule defined in the module.
275
276An example of this error:
277
7453a54e 278```compile_fail
d9579d0f
AL
279use foo::Bar; // error
280
281type Bar = u32;
282
283mod foo {
284 pub mod Bar { }
285}
286
287fn main() {}
288```
289"##,
290
291E0259: r##"
7453a54e
SL
292The name chosen for an external crate conflicts with another external crate
293that has been imported into the current module.
d9579d0f 294
7453a54e 295Erroneous code example:
d9579d0f 296
3157f602 297```compile_fail,E0259
041b39d2
XL
298# #![feature(libc)]
299extern crate core;
300extern crate libc as core;
3157f602
XL
301
302fn main() {}
d9579d0f
AL
303```
304
305The solution is to choose a different name that doesn't conflict with any
306external crate imported into the current module.
307
308Correct example:
309
041b39d2
XL
310```
311# #![feature(libc)]
312extern crate core;
3157f602 313extern crate libc as other_name;
041b39d2
XL
314
315fn main() {}
d9579d0f
AL
316```
317"##,
318
319E0260: r##"
320The name for an item declaration conflicts with an external crate's name.
321
3157f602 322Erroneous code example:
d9579d0f 323
041b39d2
XL
324```compile_fail,E0260
325extern crate core;
d9579d0f 326
041b39d2 327struct core;
0531ce1d
XL
328
329fn main() {}
d9579d0f
AL
330```
331
332There are two possible solutions:
333
334Solution #1: Rename the item.
335
041b39d2
XL
336```
337extern crate core;
d9579d0f
AL
338
339struct xyz;
340```
341
342Solution #2: Import the crate with a different name.
343
041b39d2
XL
344```
345extern crate core as xyz;
d9579d0f
AL
346
347struct abc;
348```
349
350See the Declaration Statements section of the reference for more information
351about what constitutes an Item declaration and what does not:
352
e9174d1e 353https://doc.rust-lang.org/reference.html#statements
d9579d0f
AL
354"##,
355
c1a9b12d 356E0364: r##"
3157f602 357Private items cannot be publicly re-exported. This error indicates that you
7453a54e 358attempted to `pub use` a type or value that was not itself public.
c1a9b12d 359
3157f602 360Erroneous code example:
c1a9b12d 361
7453a54e 362```compile_fail
c1a9b12d
SL
363mod foo {
364 const X: u32 = 1;
365}
7453a54e 366
c1a9b12d 367pub use foo::X;
3157f602
XL
368
369fn main() {}
c1a9b12d
SL
370```
371
372The solution to this problem is to ensure that the items that you are
373re-exporting are themselves marked with `pub`:
374
3157f602 375```
c1a9b12d
SL
376mod foo {
377 pub const X: u32 = 1;
378}
7453a54e 379
c1a9b12d 380pub use foo::X;
3157f602
XL
381
382fn main() {}
c1a9b12d
SL
383```
384
7453a54e
SL
385See the 'Use Declarations' section of the reference for more information on
386this topic:
c1a9b12d 387
e9174d1e 388https://doc.rust-lang.org/reference.html#use-declarations
c1a9b12d
SL
389"##,
390
391E0365: r##"
7453a54e
SL
392Private modules cannot be publicly re-exported. This error indicates that you
393attempted to `pub use` a module that was not itself public.
c1a9b12d 394
3157f602 395Erroneous code example:
c1a9b12d 396
3157f602 397```compile_fail,E0365
c1a9b12d
SL
398mod foo {
399 pub const X: u32 = 1;
400}
c1a9b12d 401
7453a54e 402pub use foo as foo2;
3157f602
XL
403
404fn main() {}
c1a9b12d 405```
7453a54e 406
c1a9b12d
SL
407The solution to this problem is to ensure that the module that you are
408re-exporting is itself marked with `pub`:
409
3157f602 410```
c1a9b12d
SL
411pub mod foo {
412 pub const X: u32 = 1;
413}
7453a54e 414
c1a9b12d 415pub use foo as foo2;
3157f602
XL
416
417fn main() {}
c1a9b12d
SL
418```
419
420See the 'Use Declarations' section of the reference for more information
421on this topic:
422
e9174d1e 423https://doc.rust-lang.org/reference.html#use-declarations
c1a9b12d
SL
424"##,
425
92a42be0 426E0401: r##"
7453a54e 427Inner items do not inherit type parameters from the functions they are embedded
3157f602 428in.
92a42be0 429
3157f602
XL
430Erroneous code example:
431
432```compile_fail,E0401
92a42be0
SL
433fn foo<T>(x: T) {
434 fn bar(y: T) { // T is defined in the "outer" function
435 // ..
436 }
437 bar(x);
438}
439```
440
7453a54e 441Nor will this:
9cc50fc6 442
3157f602 443```compile_fail,E0401
9cc50fc6
SL
444fn foo<T>(x: T) {
445 type MaybeT = Option<T>;
446 // ...
447}
448```
449
7453a54e 450Or this:
9cc50fc6 451
3157f602 452```compile_fail,E0401
9cc50fc6
SL
453fn foo<T>(x: T) {
454 struct Foo {
455 x: T,
456 }
457 // ...
458}
459```
460
461Items inside functions are basically just like top-level items, except
462that they can only be used from the function they are in.
92a42be0
SL
463
464There are a couple of solutions for this.
465
9cc50fc6 466If the item is a function, you may use a closure:
92a42be0
SL
467
468```
469fn foo<T>(x: T) {
470 let bar = |y: T| { // explicit type annotation may not be necessary
471 // ..
7453a54e 472 };
92a42be0
SL
473 bar(x);
474}
475```
476
9cc50fc6 477For a generic item, you can copy over the parameters:
92a42be0
SL
478
479```
480fn foo<T>(x: T) {
481 fn bar<T>(y: T) {
482 // ..
483 }
484 bar(x);
485}
486```
487
9cc50fc6
SL
488```
489fn foo<T>(x: T) {
490 type MaybeT<T> = Option<T>;
491}
492```
493
92a42be0
SL
494Be sure to copy over any bounds as well:
495
496```
497fn foo<T: Copy>(x: T) {
498 fn bar<T: Copy>(y: T) {
499 // ..
500 }
501 bar(x);
502}
503```
504
9cc50fc6
SL
505```
506fn foo<T: Copy>(x: T) {
507 struct Foo<T: Copy> {
508 x: T,
509 }
510}
511```
512
92a42be0
SL
513This may require additional type hints in the function body.
514
9cc50fc6
SL
515In case the item is a function inside an `impl`, defining a private helper
516function might be easier:
92a42be0 517
041b39d2
XL
518```
519# struct Foo<T>(T);
92a42be0
SL
520impl<T> Foo<T> {
521 pub fn foo(&self, x: T) {
522 self.bar(x);
523 }
7453a54e 524
92a42be0
SL
525 fn bar(&self, y: T) {
526 // ..
527 }
528}
529```
530
531For default impls in traits, the private helper solution won't work, however
532closures or copying the parameters should still work.
533"##,
534
c1a9b12d 535E0403: r##"
3157f602 536Some type parameters have the same name.
c1a9b12d 537
3157f602
XL
538Erroneous code example:
539
540```compile_fail,E0403
c1a9b12d
SL
541fn foo<T, T>(s: T, u: T) {} // error: the name `T` is already used for a type
542 // parameter in this type parameter list
543```
544
0531ce1d 545Please verify that none of the type parameters are misspelled, and rename any
c1a9b12d
SL
546clashing parameters. Example:
547
548```
549fn foo<T, Y>(s: T, u: Y) {} // ok!
550```
551"##,
552
553E0404: r##"
0531ce1d
XL
554You tried to use something which is not a trait in a trait position, such as
555a bound or `impl`.
c1a9b12d 556
3157f602
XL
557Erroneous code example:
558
559```compile_fail,E0404
c1a9b12d
SL
560struct Foo;
561struct Bar;
562
563impl Foo for Bar {} // error: `Foo` is not a trait
564```
565
0531ce1d
XL
566Another erroneous code example:
567
568```compile_fail,E0404
569struct Foo;
570
571fn bar<T: Foo>(t: T) {} // error: `Foo` is not a trait
572```
573
c1a9b12d
SL
574Please verify that you didn't misspell the trait's name or otherwise use the
575wrong identifier. Example:
576
577```
578trait Foo {
579 // some functions
580}
581struct Bar;
582
583impl Foo for Bar { // ok!
584 // functions implementation
585}
586```
0531ce1d
XL
587
588or
589
590```
591trait Foo {
592 // some functions
593}
594
595fn bar<T: Foo>(t: T) {} // ok!
596```
597
c1a9b12d
SL
598"##,
599
600E0405: r##"
3157f602 601The code refers to a trait that is not in scope.
c1a9b12d 602
3157f602
XL
603Erroneous code example:
604
605```compile_fail,E0405
c1a9b12d
SL
606struct Foo;
607
7453a54e 608impl SomeTrait for Foo {} // error: trait `SomeTrait` is not in scope
c1a9b12d
SL
609```
610
611Please verify that the name of the trait wasn't misspelled and ensure that it
612was imported. Example:
613
041b39d2
XL
614```
615# #[cfg(for_demonstration_only)]
c1a9b12d
SL
616// solution 1:
617use some_file::SomeTrait;
618
619// solution 2:
620trait SomeTrait {
621 // some functions
622}
623
624struct Foo;
625
626impl SomeTrait for Foo { // ok!
627 // implements functions
628}
629```
630"##,
631
632E0407: r##"
633A definition of a method not in the implemented trait was given in a trait
3157f602 634implementation.
c1a9b12d 635
3157f602
XL
636Erroneous code example:
637
638```compile_fail,E0407
c1a9b12d
SL
639trait Foo {
640 fn a();
641}
642
643struct Bar;
644
645impl Foo for Bar {
646 fn a() {}
647 fn b() {} // error: method `b` is not a member of trait `Foo`
648}
649```
650
651Please verify you didn't misspell the method name and you used the correct
652trait. First example:
653
654```
655trait Foo {
656 fn a();
657 fn b();
658}
659
660struct Bar;
661
662impl Foo for Bar {
663 fn a() {}
664 fn b() {} // ok!
665}
666```
667
668Second example:
669
670```
671trait Foo {
672 fn a();
673}
674
675struct Bar;
676
677impl Foo for Bar {
678 fn a() {}
679}
680
681impl Bar {
682 fn b() {}
683}
684```
685"##,
686
a7813a04
XL
687E0408: r##"
688An "or" pattern was used where the variable bindings are not consistently bound
689across patterns.
690
3157f602 691Erroneous code example:
a7813a04 692
3157f602 693```compile_fail,E0408
a7813a04
XL
694match x {
695 Some(y) | None => { /* use y */ } // error: variable `y` from pattern #1 is
696 // not bound in pattern #2
697 _ => ()
698}
699```
700
701Here, `y` is bound to the contents of the `Some` and can be used within the
702block corresponding to the match arm. However, in case `x` is `None`, we have
703not specified what `y` is, and the block will use a nonexistent variable.
704
705To fix this error, either split into multiple match arms:
706
707```
708let x = Some(1);
709match x {
710 Some(y) => { /* use y */ }
711 None => { /* ... */ }
712}
713```
714
715or, bind the variable to a field of the same type in all sub-patterns of the
716or pattern:
717
718```
719let x = (0, 2);
720match x {
721 (0, y) | (y, 0) => { /* use y */}
722 _ => {}
723}
724```
725
726In this example, if `x` matches the pattern `(0, _)`, the second field is set
727to `y`. If it matches `(_, 0)`, the first field is set to `y`; so in all
728cases `y` is set to some value.
729"##,
730
731E0409: r##"
732An "or" pattern was used where the variable bindings are not consistently bound
733across patterns.
734
3157f602 735Erroneous code example:
a7813a04 736
3157f602 737```compile_fail,E0409
a7813a04
XL
738let x = (0, 2);
739match x {
740 (0, ref y) | (y, 0) => { /* use y */} // error: variable `y` is bound with
741 // different mode in pattern #2
742 // than in pattern #1
743 _ => ()
744}
745```
746
747Here, `y` is bound by-value in one case and by-reference in the other.
748
749To fix this error, just use the same mode in both cases.
750Generally using `ref` or `ref mut` where not already used will fix this:
751
041b39d2 752```
a7813a04
XL
753let x = (0, 2);
754match x {
755 (0, ref y) | (ref y, 0) => { /* use y */}
756 _ => ()
757}
758```
759
760Alternatively, split the pattern:
761
762```
763let x = (0, 2);
764match x {
765 (y, 0) => { /* use y */ }
766 (0, ref y) => { /* use y */}
767 _ => ()
768}
769```
770"##,
771
e9174d1e 772E0411: r##"
3157f602 773The `Self` keyword was used outside an impl or a trait.
e9174d1e 774
3157f602
XL
775Erroneous code example:
776
777```compile_fail,E0411
e9174d1e
SL
778<Self>::foo; // error: use of `Self` outside of an impl or trait
779```
780
7453a54e
SL
781The `Self` keyword represents the current type, which explains why it can only
782be used inside an impl or a trait. It gives access to the associated items of a
783type:
e9174d1e
SL
784
785```
786trait Foo {
787 type Bar;
788}
789
790trait Baz : Foo {
791 fn bar() -> Self::Bar; // like this
792}
793```
794
7453a54e 795However, be careful when two types have a common associated type:
e9174d1e 796
7453a54e 797```compile_fail
e9174d1e
SL
798trait Foo {
799 type Bar;
800}
801
802trait Foo2 {
803 type Bar;
804}
805
806trait Baz : Foo + Foo2 {
807 fn bar() -> Self::Bar;
808 // error: ambiguous associated type `Bar` in bounds of `Self`
809}
810```
811
7453a54e
SL
812This problem can be solved by specifying from which trait we want to use the
813`Bar` type:
e9174d1e
SL
814
815```
7453a54e
SL
816trait Foo {
817 type Bar;
818}
819
820trait Foo2 {
821 type Bar;
822}
823
e9174d1e
SL
824trait Baz : Foo + Foo2 {
825 fn bar() -> <Self as Foo>::Bar; // ok!
826}
827```
828"##,
829
830E0412: r##"
3157f602 831The type name used is not in scope.
7453a54e 832
3157f602
XL
833Erroneous code examples:
834
835```compile_fail,E0412
7453a54e 836impl Something {} // error: type name `Something` is not in scope
e9174d1e 837
e9174d1e 838// or:
7453a54e 839
e9174d1e 840trait Foo {
7453a54e 841 fn bar(N); // error: type name `N` is not in scope
e9174d1e 842}
7453a54e 843
e9174d1e 844// or:
7453a54e
SL
845
846fn foo(x: T) {} // type name `T` is not in scope
e9174d1e
SL
847```
848
7453a54e
SL
849To fix this error, please verify you didn't misspell the type name, you did
850declare it or imported it into the scope. Examples:
e9174d1e
SL
851
852```
853struct Something;
854
855impl Something {} // ok!
7453a54e 856
e9174d1e 857// or:
7453a54e 858
e9174d1e
SL
859trait Foo {
860 type N;
861
041b39d2 862 fn bar(_: Self::N); // ok!
e9174d1e 863}
7453a54e
SL
864
865// or:
866
e9174d1e
SL
867fn foo<T>(x: T) {} // ok!
868```
7cac9316
XL
869
870Another case that causes this error is when a type is imported into a parent
871module. To fix this, you can follow the suggestion and use File directly or
872`use super::File;` which will import the types from the parent namespace. An
873example that causes this error is below:
874
875```compile_fail,E0412
876use std::fs::File;
877
878mod foo {
879 fn some_function(f: File) {}
880}
881```
882
883```
884use std::fs::File;
885
886mod foo {
887 // either
888 use super::File;
889 // or
890 // use std::fs::File;
891 fn foo(f: File) {}
892}
893# fn main() {} // don't insert it for us; that'll break imports
894```
e9174d1e
SL
895"##,
896
e9174d1e 897E0415: r##"
3157f602 898More than one function parameter have the same name.
e9174d1e 899
3157f602
XL
900Erroneous code example:
901
902```compile_fail,E0415
e9174d1e
SL
903fn foo(f: i32, f: i32) {} // error: identifier `f` is bound more than
904 // once in this parameter list
905```
906
907Please verify you didn't misspell parameters' name. Example:
908
909```
910fn foo(f: i32, g: i32) {} // ok!
911```
912"##,
913
914E0416: r##"
3157f602 915An identifier is bound more than once in a pattern.
e9174d1e 916
3157f602
XL
917Erroneous code example:
918
919```compile_fail,E0416
e9174d1e
SL
920match (1, 2) {
921 (x, x) => {} // error: identifier `x` is bound more than once in the
922 // same pattern
923}
924```
925
926Please verify you didn't misspell identifiers' name. Example:
927
928```
929match (1, 2) {
930 (x, y) => {} // ok!
931}
932```
933
934Or maybe did you mean to unify? Consider using a guard:
935
041b39d2
XL
936```
937# let (A, B, C) = (1, 2, 3);
e9174d1e
SL
938match (A, B, C) {
939 (x, x2, see) if x == x2 => { /* A and B are equal, do one thing */ }
940 (y, z, see) => { /* A and B unequal; do another thing */ }
941}
942```
943"##,
944
32a655c1
SL
945E0422: r##"
946You are trying to use an identifier that is either undefined or not a struct.
947Erroneous code example:
cc61c64b
XL
948
949```compile_fail,E0422
32a655c1
SL
950fn main () {
951 let x = Foo { x: 1, y: 2 };
952}
953```
cc61c64b 954
32a655c1
SL
955In this case, `Foo` is undefined, so it inherently isn't anything, and
956definitely not a struct.
cc61c64b 957
32a655c1
SL
958```compile_fail
959fn main () {
960 let foo = 1;
961 let x = foo { x: 1, y: 2 };
962}
963```
cc61c64b 964
32a655c1
SL
965In this case, `foo` is defined, but is not a struct, so Rust can't use it as
966one.
967"##,
968
e9174d1e 969E0423: r##"
3157f602 970A `struct` variant name was used like a function name.
e9174d1e 971
3157f602
XL
972Erroneous code example:
973
974```compile_fail,E0423
9e0c209e 975struct Foo { a: bool };
e9174d1e
SL
976
977let f = Foo();
978// error: `Foo` is a struct variant name, but this expression uses
979// it like a function name
980```
981
7453a54e
SL
982Please verify you didn't misspell the name of what you actually wanted to use
983here. Example:
e9174d1e
SL
984
985```
986fn Foo() -> u32 { 0 }
987
988let f = Foo(); // ok!
989```
990"##,
991
c1a9b12d 992E0424: r##"
3157f602 993The `self` keyword was used in a static method.
c1a9b12d 994
3157f602
XL
995Erroneous code example:
996
997```compile_fail,E0424
c1a9b12d
SL
998struct Foo;
999
1000impl Foo {
1001 fn bar(self) {}
1002
1003 fn foo() {
1004 self.bar(); // error: `self` is not available in a static method.
1005 }
1006}
1007```
1008
1009Please check if the method's argument list should have contained `self`,
1010`&self`, or `&mut self` (in case you didn't want to create a static
1011method), and add it if so. Example:
1012
1013```
1014struct Foo;
1015
1016impl Foo {
1017 fn bar(self) {}
1018
1019 fn foo(self) {
1020 self.bar(); // ok!
1021 }
1022}
1023```
1024"##,
1025
1026E0425: r##"
3157f602 1027An unresolved name was used.
c1a9b12d 1028
3157f602
XL
1029Erroneous code examples:
1030
1031```compile_fail,E0425
c1a9b12d
SL
1032something_that_doesnt_exist::foo;
1033// error: unresolved name `something_that_doesnt_exist::foo`
1034
1035// or:
7453a54e 1036
c1a9b12d
SL
1037trait Foo {
1038 fn bar() {
1039 Self; // error: unresolved name `Self`
1040 }
1041}
e9174d1e
SL
1042
1043// or:
7453a54e 1044
e9174d1e 1045let x = unknown_variable; // error: unresolved name `unknown_variable`
c1a9b12d
SL
1046```
1047
e9174d1e
SL
1048Please verify that the name wasn't misspelled and ensure that the
1049identifier being referred to is valid for the given situation. Example:
c1a9b12d
SL
1050
1051```
1052enum something_that_does_exist {
7453a54e 1053 Foo,
c1a9b12d 1054}
7453a54e 1055```
e9174d1e 1056
7453a54e
SL
1057Or:
1058
1059```
c1a9b12d
SL
1060mod something_that_does_exist {
1061 pub static foo : i32 = 0i32;
1062}
1063
1064something_that_does_exist::foo; // ok!
7453a54e 1065```
e9174d1e 1066
7453a54e
SL
1067Or:
1068
1069```
e9174d1e
SL
1070let unknown_variable = 12u32;
1071let x = unknown_variable; // ok!
c1a9b12d 1072```
3157f602
XL
1073
1074If the item is not defined in the current module, it must be imported using a
1075`use` statement, like so:
1076
041b39d2
XL
1077```
1078# mod foo { pub fn bar() {} }
1079# fn main() {
3157f602
XL
1080use foo::bar;
1081bar();
041b39d2 1082# }
3157f602
XL
1083```
1084
1085If the item you are importing is not defined in some super-module of the
1086current module, then it must also be declared as public (e.g., `pub fn`).
c1a9b12d
SL
1087"##,
1088
1089E0426: r##"
3157f602 1090An undeclared label was used.
c1a9b12d 1091
3157f602
XL
1092Erroneous code example:
1093
1094```compile_fail,E0426
c1a9b12d
SL
1095loop {
1096 break 'a; // error: use of undeclared label `'a`
1097}
1098```
1099
1100Please verify you spelt or declare the label correctly. Example:
1101
1102```
1103'a: loop {
1104 break 'a; // ok!
1105}
1106```
1107"##,
1108
1109E0428: r##"
3157f602 1110A type or module has been defined more than once.
c1a9b12d 1111
3157f602
XL
1112Erroneous code example:
1113
1114```compile_fail,E0428
c1a9b12d
SL
1115struct Bar;
1116struct Bar; // error: duplicate definition of value `Bar`
1117```
1118
1119Please verify you didn't misspell the type/module's name or remove/rename the
1120duplicated one. Example:
1121
1122```
1123struct Bar;
1124struct Bar2; // ok!
1125```
1126"##,
1127
3157f602
XL
1128E0429: r##"
1129The `self` keyword cannot appear alone as the last segment in a `use`
1130declaration.
1131
1132Erroneous code example:
1133
1134```compile_fail,E0429
1135use std::fmt::self; // error: `self` imports are only allowed within a { } list
1136```
1137
1138To use a namespace itself in addition to some of its members, `self` may appear
1139as part of a brace-enclosed list of imports:
1140
1141```
1142use std::fmt::{self, Debug};
1143```
1144
1145If you only want to import the namespace, do so directly:
1146
1147```
1148use std::fmt;
1149```
1150"##,
1151
c1a9b12d 1152E0430: r##"
3157f602 1153The `self` import appears more than once in the list.
c1a9b12d 1154
3157f602
XL
1155Erroneous code example:
1156
1157```compile_fail,E0430
c1a9b12d
SL
1158use something::{self, self}; // error: `self` import can only appear once in
1159 // the list
1160```
1161
1162Please verify you didn't misspell the import name or remove the duplicated
1163`self` import. Example:
1164
041b39d2
XL
1165```
1166# mod something {}
1167# fn main() {
1168use something::{self}; // ok!
1169# }
c1a9b12d
SL
1170```
1171"##,
1172
1173E0431: r##"
3157f602 1174An invalid `self` import was made.
c1a9b12d 1175
3157f602
XL
1176Erroneous code example:
1177
1178```compile_fail,E0431
c1a9b12d
SL
1179use {self}; // error: `self` import can only appear in an import list with a
1180 // non-empty prefix
1181```
1182
1183You cannot import the current module into itself, please remove this import
1184or verify you didn't misspell it.
1185"##,
1186
1187E0432: r##"
3157f602 1188An import was unresolved.
c1a9b12d 1189
3157f602
XL
1190Erroneous code example:
1191
1192```compile_fail,E0432
c1a9b12d
SL
1193use something::Foo; // error: unresolved import `something::Foo`.
1194```
1195
a7813a04
XL
1196Paths in `use` statements are relative to the crate root. To import items
1197relative to the current and parent modules, use the `self::` and `super::`
1198prefixes, respectively. Also verify that you didn't misspell the import
1199name and that the import exists in the module from where you tried to
1200import it. Example:
c1a9b12d 1201
041b39d2 1202```
a7813a04 1203use self::something::Foo; // ok!
c1a9b12d
SL
1204
1205mod something {
1206 pub struct Foo;
1207}
041b39d2 1208# fn main() {}
c1a9b12d 1209```
92a42be0
SL
1210
1211Or, if you tried to use a module from an external crate, you may have missed
a7813a04 1212the `extern crate` declaration (which is usually placed in the crate root):
92a42be0 1213
041b39d2
XL
1214```
1215extern crate core; // Required to use the `core` crate
92a42be0 1216
041b39d2
XL
1217use core::any;
1218# fn main() {}
92a42be0 1219```
c1a9b12d
SL
1220"##,
1221
1222E0433: r##"
3157f602 1223An undeclared type or module was used.
c1a9b12d 1224
3157f602
XL
1225Erroneous code example:
1226
1227```compile_fail,E0433
1228let map = HashMap::new();
1229// error: failed to resolve. Use of undeclared type or module `HashMap`
c1a9b12d
SL
1230```
1231
3157f602
XL
1232Please verify you didn't misspell the type/module's name or that you didn't
1233forgot to import it:
1234
1235
1236```
1237use std::collections::HashMap; // HashMap has been imported.
1238let map: HashMap<u32, u32> = HashMap::new(); // So it can be used!
1239```
c1a9b12d
SL
1240"##,
1241
a7813a04
XL
1242E0434: r##"
1243This error indicates that a variable usage inside an inner function is invalid
1244because the variable comes from a dynamic environment. Inner functions do not
1245have access to their containing environment.
1246
3157f602 1247Erroneous code example:
a7813a04 1248
3157f602 1249```compile_fail,E0434
a7813a04
XL
1250fn foo() {
1251 let y = 5;
1252 fn bar() -> u32 {
1253 y // error: can't capture dynamic environment in a fn item; use the
1254 // || { ... } closure form instead.
1255 }
1256}
1257```
1258
1259Functions do not capture local variables. To fix this error, you can replace the
1260function with a closure:
1261
1262```
1263fn foo() {
1264 let y = 5;
1265 let bar = || {
1266 y
1267 };
1268}
1269```
1270
1271or replace the captured variable with a constant or a static item:
1272
1273```
1274fn foo() {
1275 static mut X: u32 = 4;
1276 const Y: u32 = 5;
1277 fn bar() -> u32 {
1278 unsafe {
1279 X = 3;
1280 }
1281 Y
1282 }
1283}
1284```
1285"##,
1286
e9174d1e 1287E0435: r##"
7cac9316 1288A non-constant value was used in a constant expression.
e9174d1e 1289
3157f602
XL
1290Erroneous code example:
1291
1292```compile_fail,E0435
7cac9316
XL
1293let foo = 42;
1294let a: [u8; foo]; // error: attempt to use a non-constant value in a constant
e9174d1e
SL
1295```
1296
1297To fix this error, please replace the value with a constant. Example:
1298
1299```
7cac9316 1300let a: [u8; 42]; // ok!
7453a54e 1301```
e9174d1e 1302
7453a54e
SL
1303Or:
1304
1305```
7cac9316
XL
1306const FOO: usize = 42;
1307let a: [u8; FOO]; // ok!
e9174d1e
SL
1308```
1309"##,
1310
c1a9b12d
SL
1311E0437: r##"
1312Trait implementations can only implement associated types that are members of
1313the trait in question. This error indicates that you attempted to implement
1314an associated type whose name does not match the name of any associated type
1315in the trait.
1316
3157f602 1317Erroneous code example:
c1a9b12d 1318
3157f602 1319```compile_fail,E0437
c1a9b12d
SL
1320trait Foo {}
1321
1322impl Foo for i32 {
1323 type Bar = bool;
1324}
1325```
1326
1327The solution to this problem is to remove the extraneous associated type:
1328
1329```
1330trait Foo {}
1331
1332impl Foo for i32 {}
1333```
1334"##,
1335
1336E0438: r##"
1337Trait implementations can only implement associated constants that are
1338members of the trait in question. This error indicates that you
1339attempted to implement an associated constant whose name does not
1340match the name of any associated constant in the trait.
1341
3157f602 1342Erroneous code example:
c1a9b12d 1343
3157f602 1344```compile_fail,E0438
c1a9b12d
SL
1345trait Foo {}
1346
1347impl Foo for i32 {
1348 const BAR: bool = true;
1349}
1350```
1351
1352The solution to this problem is to remove the extraneous associated constant:
1353
1354```
1355trait Foo {}
1356
1357impl Foo for i32 {}
1358```
9e0c209e
SL
1359"##,
1360
c30ab7b3
SL
1361E0466: r##"
1362Macro import declarations were malformed.
1363
1364Erroneous code examples:
1365
1366```compile_fail,E0466
1367#[macro_use(a_macro(another_macro))] // error: invalid import declaration
1368extern crate core as some_crate;
1369
1370#[macro_use(i_want = "some_macros")] // error: invalid import declaration
1371extern crate core as another_crate;
1372```
1373
1374This is a syntax error at the level of attribute declarations. The proper
1375syntax for macro imports is the following:
1376
041b39d2 1377```ignore (cannot-doctest-multicrate-project)
c30ab7b3
SL
1378// In some_crate:
1379#[macro_export]
1380macro_rules! get_tacos {
1381 ...
1382}
1383
1384#[macro_export]
1385macro_rules! get_pimientos {
1386 ...
1387}
1388
1389// In your crate:
1390#[macro_use(get_tacos, get_pimientos)] // It imports `get_tacos` and
1391extern crate some_crate; // `get_pimientos` macros from some_crate
1392```
1393
1394If you would like to import all exported macros, write `macro_use` with no
1395arguments.
1396"##,
1397
1398E0467: r##"
2c00a5a8 1399Macro re-export declarations were empty or malformed.
c30ab7b3
SL
1400
1401Erroneous code examples:
1402
1403```compile_fail,E0467
1404#[macro_reexport] // error: no macros listed for export
1405extern crate core as macros_for_good;
1406
1407#[macro_reexport(fun_macro = "foo")] // error: not a macro identifier
1408extern crate core as other_macros_for_good;
1409```
1410
1411This is a syntax error at the level of attribute declarations.
1412
1413Currently, `macro_reexport` requires at least one macro name to be listed.
2c00a5a8 1414Unlike `macro_use`, listing no names does not re-export all macros from the
c30ab7b3
SL
1415given crate.
1416
1417Decide which macros you would like to export and list them properly.
1418
2c00a5a8 1419These are proper re-export declarations:
c30ab7b3 1420
041b39d2 1421```ignore (cannot-doctest-multicrate-project)
c30ab7b3
SL
1422#[macro_reexport(some_macro, another_macro)]
1423extern crate macros_for_good;
1424```
1425"##,
1426
1427E0468: r##"
1428A non-root module attempts to import macros from another crate.
1429
1430Example of erroneous code:
1431
1432```compile_fail,E0468
1433mod foo {
041b39d2 1434 #[macro_use(debug_assert)] // error: must be at crate root to import
c30ab7b3 1435 extern crate core; // macros from another crate
041b39d2 1436 fn run_macro() { debug_assert!(true); }
c30ab7b3
SL
1437}
1438```
1439
1440Only `extern crate` imports at the crate root level are allowed to import
1441macros.
1442
1443Either move the macro import to crate root or do without the foreign macros.
1444This will work:
1445
041b39d2
XL
1446```
1447#[macro_use(debug_assert)]
1448extern crate core;
c30ab7b3
SL
1449
1450mod foo {
041b39d2 1451 fn run_macro() { debug_assert!(true); }
c30ab7b3 1452}
041b39d2 1453# fn main() {}
c30ab7b3
SL
1454```
1455"##,
1456
1457E0469: r##"
1458A macro listed for import was not found.
1459
1460Erroneous code example:
1461
1462```compile_fail,E0469
1463#[macro_use(drink, be_merry)] // error: imported macro not found
041b39d2 1464extern crate alloc;
c30ab7b3
SL
1465
1466fn main() {
1467 // ...
1468}
1469```
1470
1471Either the listed macro is not contained in the imported crate, or it is not
1472exported from the given crate.
1473
1474This could be caused by a typo. Did you misspell the macro's name?
1475
1476Double-check the names of the macros listed for import, and that the crate
1477in question exports them.
1478
1479A working version would be:
1480
041b39d2 1481```ignore (cannot-doctest-multicrate-project)
c30ab7b3
SL
1482// In some_crate crate:
1483#[macro_export]
1484macro_rules! eat {
1485 ...
1486}
1487
1488#[macro_export]
1489macro_rules! drink {
1490 ...
1491}
1492
1493// In your crate:
1494#[macro_use(eat, drink)]
1495extern crate some_crate; //ok!
1496```
1497"##,
1498
1499E0470: r##"
2c00a5a8 1500A macro listed for re-export was not found.
c30ab7b3
SL
1501
1502Erroneous code example:
1503
1504```compile_fail,E0470
1505#[macro_reexport(drink, be_merry)]
041b39d2 1506extern crate alloc;
c30ab7b3
SL
1507
1508fn main() {
1509 // ...
1510}
1511```
1512
1513Either the listed macro is not contained in the imported crate, or it is not
1514exported from the given crate.
1515
1516This could be caused by a typo. Did you misspell the macro's name?
1517
2c00a5a8 1518Double-check the names of the macros listed for re-export, and that the crate
c30ab7b3
SL
1519in question exports them.
1520
1521A working version:
1522
041b39d2 1523```ignore (cannot-doctest-multicrate-project)
c30ab7b3
SL
1524// In some_crate crate:
1525#[macro_export]
1526macro_rules! eat {
1527 ...
1528}
1529
1530#[macro_export]
1531macro_rules! drink {
1532 ...
1533}
1534
1535// In your_crate:
1536#[macro_reexport(eat, drink)]
1537extern crate some_crate;
1538```
1539"##,
1540
9e0c209e
SL
1541E0530: r##"
1542A binding shadowed something it shouldn't.
1543
1544Erroneous code example:
1545
1546```compile_fail,E0530
1547static TEST: i32 = 0;
1548
1549let r: (i32, i32) = (0, 0);
1550match r {
1551 TEST => {} // error: match bindings cannot shadow statics
1552}
1553```
1554
1555To fix this error, just change the binding's name in order to avoid shadowing
1556one of the following:
1557
1558* struct name
1559* struct/enum variant
1560* static
1561* const
1562* associated const
1563
1564Fixed example:
1565
1566```
1567static TEST: i32 = 0;
1568
1569let r: (i32, i32) = (0, 0);
1570match r {
1571 something => {} // ok!
1572}
1573```
1574"##,
d9579d0f 1575
c30ab7b3
SL
1576E0532: r##"
1577Pattern arm did not match expected kind.
1578
1579Erroneous code example:
1580
1581```compile_fail,E0532
1582enum State {
1583 Succeeded,
1584 Failed(String),
1585}
1586
1587fn print_on_failure(state: &State) {
1588 match *state {
1589 // error: expected unit struct/variant or constant, found tuple
1590 // variant `State::Failed`
1591 State::Failed => println!("Failed"),
1592 _ => ()
1593 }
1594}
1595```
1596
1597To fix this error, ensure the match arm kind is the same as the expression
1598matched.
1599
1600Fixed example:
1601
1602```
1603enum State {
1604 Succeeded,
1605 Failed(String),
1606}
1607
1608fn print_on_failure(state: &State) {
1609 match *state {
1610 State::Failed(ref msg) => println!("Failed with {}", msg),
1611 _ => ()
1612 }
1613}
1614```
1615"##,
1616
7cac9316
XL
1617E0603: r##"
1618A private item was used outside its scope.
1619
1620Erroneous code example:
1621
1622```compile_fail,E0603
1623mod SomeModule {
1624 const PRIVATE: u32 = 0x_a_bad_1dea_u32; // This const is private, so we
1625 // can't use it outside of the
1626 // `SomeModule` module.
1627}
1628
1629println!("const value: {}", SomeModule::PRIVATE); // error: constant `CONSTANT`
1630 // is private
1631```
1632
1633In order to fix this error, you need to make the item public by using the `pub`
1634keyword. Example:
1635
1636```
1637mod SomeModule {
1638 pub const PRIVATE: u32 = 0x_a_bad_1dea_u32; // We set it public by using the
1639 // `pub` keyword.
1640}
1641
1642println!("const value: {}", SomeModule::PRIVATE); // ok!
1643```
1644"##,
1645
2c00a5a8
XL
1646E0659: r##"
1647An item usage is ambiguous.
1648
1649Erroneous code example:
1650
1651```compile_fail,E0659
1652pub mod moon {
1653 pub fn foo() {}
1654}
1655
1656pub mod earth {
1657 pub fn foo() {}
1658}
1659
1660mod collider {
1661 pub use moon::*;
1662 pub use earth::*;
1663}
1664
1665fn main() {
1666 collider::foo(); // ERROR: `foo` is ambiguous
1667}
1668```
1669
1670This error generally appears when two items with the same name are imported into
1671a module. Here, the `foo` functions are imported and reexported from the
1672`collider` module and therefore, when we're using `collider::foo()`, both
1673functions collide.
1674
1675To solve this error, the best solution is generally to keep the path before the
1676item when using it. Example:
1677
1678```
1679pub mod moon {
1680 pub fn foo() {}
1681}
1682
1683pub mod earth {
1684 pub fn foo() {}
1685}
1686
1687mod collider {
1688 pub use moon;
1689 pub use earth;
1690}
1691
1692fn main() {
1693 collider::moon::foo(); // ok!
1694 collider::earth::foo(); // ok!
1695}
1696```
1697"##,
1698
d9579d0f
AL
1699}
1700
85aaf69f 1701register_diagnostics! {
e9174d1e
SL
1702// E0153, unused error code
1703// E0157, unused error code
7453a54e
SL
1704// E0257,
1705// E0258,
7cac9316 1706// E0402, // cannot use an outer type parameter in this context
3157f602 1707// E0406, merged into 420
a7813a04 1708// E0410, merged into 408
3157f602
XL
1709// E0413, merged into 530
1710// E0414, merged into 530
1711// E0417, merged into 532
1712// E0418, merged into 532
1713// E0419, merged into 531
1714// E0420, merged into 532
1715// E0421, merged into 531
3157f602 1716 E0531, // unresolved pattern path kind `name`
3157f602 1717// E0427, merged into 530
32a655c1
SL
1718 E0573,
1719 E0574,
1720 E0575,
1721 E0576,
1722 E0577,
1723 E0578,
85aaf69f 1724}