]> git.proxmox.com Git - rustc.git/blame - src/librustc_resolve/diagnostics.rs
New upstream version 1.23.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;
d9579d0f
AL
328```
329
330There are two possible solutions:
331
332Solution #1: Rename the item.
333
041b39d2
XL
334```
335extern crate core;
d9579d0f
AL
336
337struct xyz;
338```
339
340Solution #2: Import the crate with a different name.
341
041b39d2
XL
342```
343extern crate core as xyz;
d9579d0f
AL
344
345struct abc;
346```
347
348See the Declaration Statements section of the reference for more information
349about what constitutes an Item declaration and what does not:
350
e9174d1e 351https://doc.rust-lang.org/reference.html#statements
d9579d0f
AL
352"##,
353
c1a9b12d 354E0364: r##"
3157f602 355Private items cannot be publicly re-exported. This error indicates that you
7453a54e 356attempted to `pub use` a type or value that was not itself public.
c1a9b12d 357
3157f602 358Erroneous code example:
c1a9b12d 359
7453a54e 360```compile_fail
c1a9b12d
SL
361mod foo {
362 const X: u32 = 1;
363}
7453a54e 364
c1a9b12d 365pub use foo::X;
3157f602
XL
366
367fn main() {}
c1a9b12d
SL
368```
369
370The solution to this problem is to ensure that the items that you are
371re-exporting are themselves marked with `pub`:
372
3157f602 373```
c1a9b12d
SL
374mod foo {
375 pub const X: u32 = 1;
376}
7453a54e 377
c1a9b12d 378pub use foo::X;
3157f602
XL
379
380fn main() {}
c1a9b12d
SL
381```
382
7453a54e
SL
383See the 'Use Declarations' section of the reference for more information on
384this topic:
c1a9b12d 385
e9174d1e 386https://doc.rust-lang.org/reference.html#use-declarations
c1a9b12d
SL
387"##,
388
389E0365: r##"
7453a54e
SL
390Private modules cannot be publicly re-exported. This error indicates that you
391attempted to `pub use` a module that was not itself public.
c1a9b12d 392
3157f602 393Erroneous code example:
c1a9b12d 394
3157f602 395```compile_fail,E0365
c1a9b12d
SL
396mod foo {
397 pub const X: u32 = 1;
398}
c1a9b12d 399
7453a54e 400pub use foo as foo2;
3157f602
XL
401
402fn main() {}
c1a9b12d 403```
7453a54e 404
c1a9b12d
SL
405The solution to this problem is to ensure that the module that you are
406re-exporting is itself marked with `pub`:
407
3157f602 408```
c1a9b12d
SL
409pub mod foo {
410 pub const X: u32 = 1;
411}
7453a54e 412
c1a9b12d 413pub use foo as foo2;
3157f602
XL
414
415fn main() {}
c1a9b12d
SL
416```
417
418See the 'Use Declarations' section of the reference for more information
419on this topic:
420
e9174d1e 421https://doc.rust-lang.org/reference.html#use-declarations
c1a9b12d
SL
422"##,
423
92a42be0 424E0401: r##"
7453a54e 425Inner items do not inherit type parameters from the functions they are embedded
3157f602 426in.
92a42be0 427
3157f602
XL
428Erroneous code example:
429
430```compile_fail,E0401
92a42be0
SL
431fn foo<T>(x: T) {
432 fn bar(y: T) { // T is defined in the "outer" function
433 // ..
434 }
435 bar(x);
436}
437```
438
7453a54e 439Nor will this:
9cc50fc6 440
3157f602 441```compile_fail,E0401
9cc50fc6
SL
442fn foo<T>(x: T) {
443 type MaybeT = Option<T>;
444 // ...
445}
446```
447
7453a54e 448Or this:
9cc50fc6 449
3157f602 450```compile_fail,E0401
9cc50fc6
SL
451fn foo<T>(x: T) {
452 struct Foo {
453 x: T,
454 }
455 // ...
456}
457```
458
459Items inside functions are basically just like top-level items, except
460that they can only be used from the function they are in.
92a42be0
SL
461
462There are a couple of solutions for this.
463
9cc50fc6 464If the item is a function, you may use a closure:
92a42be0
SL
465
466```
467fn foo<T>(x: T) {
468 let bar = |y: T| { // explicit type annotation may not be necessary
469 // ..
7453a54e 470 };
92a42be0
SL
471 bar(x);
472}
473```
474
9cc50fc6 475For a generic item, you can copy over the parameters:
92a42be0
SL
476
477```
478fn foo<T>(x: T) {
479 fn bar<T>(y: T) {
480 // ..
481 }
482 bar(x);
483}
484```
485
9cc50fc6
SL
486```
487fn foo<T>(x: T) {
488 type MaybeT<T> = Option<T>;
489}
490```
491
92a42be0
SL
492Be sure to copy over any bounds as well:
493
494```
495fn foo<T: Copy>(x: T) {
496 fn bar<T: Copy>(y: T) {
497 // ..
498 }
499 bar(x);
500}
501```
502
9cc50fc6
SL
503```
504fn foo<T: Copy>(x: T) {
505 struct Foo<T: Copy> {
506 x: T,
507 }
508}
509```
510
92a42be0
SL
511This may require additional type hints in the function body.
512
9cc50fc6
SL
513In case the item is a function inside an `impl`, defining a private helper
514function might be easier:
92a42be0 515
041b39d2
XL
516```
517# struct Foo<T>(T);
92a42be0
SL
518impl<T> Foo<T> {
519 pub fn foo(&self, x: T) {
520 self.bar(x);
521 }
7453a54e 522
92a42be0
SL
523 fn bar(&self, y: T) {
524 // ..
525 }
526}
527```
528
529For default impls in traits, the private helper solution won't work, however
530closures or copying the parameters should still work.
531"##,
532
c1a9b12d 533E0403: r##"
3157f602 534Some type parameters have the same name.
c1a9b12d 535
3157f602
XL
536Erroneous code example:
537
538```compile_fail,E0403
c1a9b12d
SL
539fn foo<T, T>(s: T, u: T) {} // error: the name `T` is already used for a type
540 // parameter in this type parameter list
541```
542
543Please verify that none of the type parameterss are misspelled, and rename any
544clashing parameters. Example:
545
546```
547fn foo<T, Y>(s: T, u: Y) {} // ok!
548```
549"##,
550
551E0404: r##"
3157f602 552You tried to implement something which was not a trait on an object.
c1a9b12d 553
3157f602
XL
554Erroneous code example:
555
556```compile_fail,E0404
c1a9b12d
SL
557struct Foo;
558struct Bar;
559
560impl Foo for Bar {} // error: `Foo` is not a trait
561```
562
563Please verify that you didn't misspell the trait's name or otherwise use the
564wrong identifier. Example:
565
566```
567trait Foo {
568 // some functions
569}
570struct Bar;
571
572impl Foo for Bar { // ok!
573 // functions implementation
574}
575```
576"##,
577
578E0405: r##"
3157f602 579The code refers to a trait that is not in scope.
c1a9b12d 580
3157f602
XL
581Erroneous code example:
582
583```compile_fail,E0405
c1a9b12d
SL
584struct Foo;
585
7453a54e 586impl SomeTrait for Foo {} // error: trait `SomeTrait` is not in scope
c1a9b12d
SL
587```
588
589Please verify that the name of the trait wasn't misspelled and ensure that it
590was imported. Example:
591
041b39d2
XL
592```
593# #[cfg(for_demonstration_only)]
c1a9b12d
SL
594// solution 1:
595use some_file::SomeTrait;
596
597// solution 2:
598trait SomeTrait {
599 // some functions
600}
601
602struct Foo;
603
604impl SomeTrait for Foo { // ok!
605 // implements functions
606}
607```
608"##,
609
610E0407: r##"
611A definition of a method not in the implemented trait was given in a trait
3157f602 612implementation.
c1a9b12d 613
3157f602
XL
614Erroneous code example:
615
616```compile_fail,E0407
c1a9b12d
SL
617trait Foo {
618 fn a();
619}
620
621struct Bar;
622
623impl Foo for Bar {
624 fn a() {}
625 fn b() {} // error: method `b` is not a member of trait `Foo`
626}
627```
628
629Please verify you didn't misspell the method name and you used the correct
630trait. First example:
631
632```
633trait Foo {
634 fn a();
635 fn b();
636}
637
638struct Bar;
639
640impl Foo for Bar {
641 fn a() {}
642 fn b() {} // ok!
643}
644```
645
646Second example:
647
648```
649trait Foo {
650 fn a();
651}
652
653struct Bar;
654
655impl Foo for Bar {
656 fn a() {}
657}
658
659impl Bar {
660 fn b() {}
661}
662```
663"##,
664
a7813a04
XL
665E0408: r##"
666An "or" pattern was used where the variable bindings are not consistently bound
667across patterns.
668
3157f602 669Erroneous code example:
a7813a04 670
3157f602 671```compile_fail,E0408
a7813a04
XL
672match x {
673 Some(y) | None => { /* use y */ } // error: variable `y` from pattern #1 is
674 // not bound in pattern #2
675 _ => ()
676}
677```
678
679Here, `y` is bound to the contents of the `Some` and can be used within the
680block corresponding to the match arm. However, in case `x` is `None`, we have
681not specified what `y` is, and the block will use a nonexistent variable.
682
683To fix this error, either split into multiple match arms:
684
685```
686let x = Some(1);
687match x {
688 Some(y) => { /* use y */ }
689 None => { /* ... */ }
690}
691```
692
693or, bind the variable to a field of the same type in all sub-patterns of the
694or pattern:
695
696```
697let x = (0, 2);
698match x {
699 (0, y) | (y, 0) => { /* use y */}
700 _ => {}
701}
702```
703
704In this example, if `x` matches the pattern `(0, _)`, the second field is set
705to `y`. If it matches `(_, 0)`, the first field is set to `y`; so in all
706cases `y` is set to some value.
707"##,
708
709E0409: r##"
710An "or" pattern was used where the variable bindings are not consistently bound
711across patterns.
712
3157f602 713Erroneous code example:
a7813a04 714
3157f602 715```compile_fail,E0409
a7813a04
XL
716let x = (0, 2);
717match x {
718 (0, ref y) | (y, 0) => { /* use y */} // error: variable `y` is bound with
719 // different mode in pattern #2
720 // than in pattern #1
721 _ => ()
722}
723```
724
725Here, `y` is bound by-value in one case and by-reference in the other.
726
727To fix this error, just use the same mode in both cases.
728Generally using `ref` or `ref mut` where not already used will fix this:
729
041b39d2 730```
a7813a04
XL
731let x = (0, 2);
732match x {
733 (0, ref y) | (ref y, 0) => { /* use y */}
734 _ => ()
735}
736```
737
738Alternatively, split the pattern:
739
740```
741let x = (0, 2);
742match x {
743 (y, 0) => { /* use y */ }
744 (0, ref y) => { /* use y */}
745 _ => ()
746}
747```
748"##,
749
e9174d1e 750E0411: r##"
3157f602 751The `Self` keyword was used outside an impl or a trait.
e9174d1e 752
3157f602
XL
753Erroneous code example:
754
755```compile_fail,E0411
e9174d1e
SL
756<Self>::foo; // error: use of `Self` outside of an impl or trait
757```
758
7453a54e
SL
759The `Self` keyword represents the current type, which explains why it can only
760be used inside an impl or a trait. It gives access to the associated items of a
761type:
e9174d1e
SL
762
763```
764trait Foo {
765 type Bar;
766}
767
768trait Baz : Foo {
769 fn bar() -> Self::Bar; // like this
770}
771```
772
7453a54e 773However, be careful when two types have a common associated type:
e9174d1e 774
7453a54e 775```compile_fail
e9174d1e
SL
776trait Foo {
777 type Bar;
778}
779
780trait Foo2 {
781 type Bar;
782}
783
784trait Baz : Foo + Foo2 {
785 fn bar() -> Self::Bar;
786 // error: ambiguous associated type `Bar` in bounds of `Self`
787}
788```
789
7453a54e
SL
790This problem can be solved by specifying from which trait we want to use the
791`Bar` type:
e9174d1e
SL
792
793```
7453a54e
SL
794trait Foo {
795 type Bar;
796}
797
798trait Foo2 {
799 type Bar;
800}
801
e9174d1e
SL
802trait Baz : Foo + Foo2 {
803 fn bar() -> <Self as Foo>::Bar; // ok!
804}
805```
806"##,
807
808E0412: r##"
3157f602 809The type name used is not in scope.
7453a54e 810
3157f602
XL
811Erroneous code examples:
812
813```compile_fail,E0412
7453a54e 814impl Something {} // error: type name `Something` is not in scope
e9174d1e 815
e9174d1e 816// or:
7453a54e 817
e9174d1e 818trait Foo {
7453a54e 819 fn bar(N); // error: type name `N` is not in scope
e9174d1e 820}
7453a54e 821
e9174d1e 822// or:
7453a54e
SL
823
824fn foo(x: T) {} // type name `T` is not in scope
e9174d1e
SL
825```
826
7453a54e
SL
827To fix this error, please verify you didn't misspell the type name, you did
828declare it or imported it into the scope. Examples:
e9174d1e
SL
829
830```
831struct Something;
832
833impl Something {} // ok!
7453a54e 834
e9174d1e 835// or:
7453a54e 836
e9174d1e
SL
837trait Foo {
838 type N;
839
041b39d2 840 fn bar(_: Self::N); // ok!
e9174d1e 841}
7453a54e
SL
842
843// or:
844
e9174d1e
SL
845fn foo<T>(x: T) {} // ok!
846```
7cac9316
XL
847
848Another case that causes this error is when a type is imported into a parent
849module. To fix this, you can follow the suggestion and use File directly or
850`use super::File;` which will import the types from the parent namespace. An
851example that causes this error is below:
852
853```compile_fail,E0412
854use std::fs::File;
855
856mod foo {
857 fn some_function(f: File) {}
858}
859```
860
861```
862use std::fs::File;
863
864mod foo {
865 // either
866 use super::File;
867 // or
868 // use std::fs::File;
869 fn foo(f: File) {}
870}
871# fn main() {} // don't insert it for us; that'll break imports
872```
e9174d1e
SL
873"##,
874
e9174d1e 875E0415: r##"
3157f602 876More than one function parameter have the same name.
e9174d1e 877
3157f602
XL
878Erroneous code example:
879
880```compile_fail,E0415
e9174d1e
SL
881fn foo(f: i32, f: i32) {} // error: identifier `f` is bound more than
882 // once in this parameter list
883```
884
885Please verify you didn't misspell parameters' name. Example:
886
887```
888fn foo(f: i32, g: i32) {} // ok!
889```
890"##,
891
892E0416: r##"
3157f602 893An identifier is bound more than once in a pattern.
e9174d1e 894
3157f602
XL
895Erroneous code example:
896
897```compile_fail,E0416
e9174d1e
SL
898match (1, 2) {
899 (x, x) => {} // error: identifier `x` is bound more than once in the
900 // same pattern
901}
902```
903
904Please verify you didn't misspell identifiers' name. Example:
905
906```
907match (1, 2) {
908 (x, y) => {} // ok!
909}
910```
911
912Or maybe did you mean to unify? Consider using a guard:
913
041b39d2
XL
914```
915# let (A, B, C) = (1, 2, 3);
e9174d1e
SL
916match (A, B, C) {
917 (x, x2, see) if x == x2 => { /* A and B are equal, do one thing */ }
918 (y, z, see) => { /* A and B unequal; do another thing */ }
919}
920```
921"##,
922
32a655c1
SL
923E0422: r##"
924You are trying to use an identifier that is either undefined or not a struct.
925Erroneous code example:
cc61c64b
XL
926
927```compile_fail,E0422
32a655c1
SL
928fn main () {
929 let x = Foo { x: 1, y: 2 };
930}
931```
cc61c64b 932
32a655c1
SL
933In this case, `Foo` is undefined, so it inherently isn't anything, and
934definitely not a struct.
cc61c64b 935
32a655c1
SL
936```compile_fail
937fn main () {
938 let foo = 1;
939 let x = foo { x: 1, y: 2 };
940}
941```
cc61c64b 942
32a655c1
SL
943In this case, `foo` is defined, but is not a struct, so Rust can't use it as
944one.
945"##,
946
e9174d1e 947E0423: r##"
3157f602 948A `struct` variant name was used like a function name.
e9174d1e 949
3157f602
XL
950Erroneous code example:
951
952```compile_fail,E0423
9e0c209e 953struct Foo { a: bool };
e9174d1e
SL
954
955let f = Foo();
956// error: `Foo` is a struct variant name, but this expression uses
957// it like a function name
958```
959
7453a54e
SL
960Please verify you didn't misspell the name of what you actually wanted to use
961here. Example:
e9174d1e
SL
962
963```
964fn Foo() -> u32 { 0 }
965
966let f = Foo(); // ok!
967```
968"##,
969
c1a9b12d 970E0424: r##"
3157f602 971The `self` keyword was used in a static method.
c1a9b12d 972
3157f602
XL
973Erroneous code example:
974
975```compile_fail,E0424
c1a9b12d
SL
976struct Foo;
977
978impl Foo {
979 fn bar(self) {}
980
981 fn foo() {
982 self.bar(); // error: `self` is not available in a static method.
983 }
984}
985```
986
987Please check if the method's argument list should have contained `self`,
988`&self`, or `&mut self` (in case you didn't want to create a static
989method), and add it if so. Example:
990
991```
992struct Foo;
993
994impl Foo {
995 fn bar(self) {}
996
997 fn foo(self) {
998 self.bar(); // ok!
999 }
1000}
1001```
1002"##,
1003
1004E0425: r##"
3157f602 1005An unresolved name was used.
c1a9b12d 1006
3157f602
XL
1007Erroneous code examples:
1008
1009```compile_fail,E0425
c1a9b12d
SL
1010something_that_doesnt_exist::foo;
1011// error: unresolved name `something_that_doesnt_exist::foo`
1012
1013// or:
7453a54e 1014
c1a9b12d
SL
1015trait Foo {
1016 fn bar() {
1017 Self; // error: unresolved name `Self`
1018 }
1019}
e9174d1e
SL
1020
1021// or:
7453a54e 1022
e9174d1e 1023let x = unknown_variable; // error: unresolved name `unknown_variable`
c1a9b12d
SL
1024```
1025
e9174d1e
SL
1026Please verify that the name wasn't misspelled and ensure that the
1027identifier being referred to is valid for the given situation. Example:
c1a9b12d
SL
1028
1029```
1030enum something_that_does_exist {
7453a54e 1031 Foo,
c1a9b12d 1032}
7453a54e 1033```
e9174d1e 1034
7453a54e
SL
1035Or:
1036
1037```
c1a9b12d
SL
1038mod something_that_does_exist {
1039 pub static foo : i32 = 0i32;
1040}
1041
1042something_that_does_exist::foo; // ok!
7453a54e 1043```
e9174d1e 1044
7453a54e
SL
1045Or:
1046
1047```
e9174d1e
SL
1048let unknown_variable = 12u32;
1049let x = unknown_variable; // ok!
c1a9b12d 1050```
3157f602
XL
1051
1052If the item is not defined in the current module, it must be imported using a
1053`use` statement, like so:
1054
041b39d2
XL
1055```
1056# mod foo { pub fn bar() {} }
1057# fn main() {
3157f602
XL
1058use foo::bar;
1059bar();
041b39d2 1060# }
3157f602
XL
1061```
1062
1063If the item you are importing is not defined in some super-module of the
1064current module, then it must also be declared as public (e.g., `pub fn`).
c1a9b12d
SL
1065"##,
1066
1067E0426: r##"
3157f602 1068An undeclared label was used.
c1a9b12d 1069
3157f602
XL
1070Erroneous code example:
1071
1072```compile_fail,E0426
c1a9b12d
SL
1073loop {
1074 break 'a; // error: use of undeclared label `'a`
1075}
1076```
1077
1078Please verify you spelt or declare the label correctly. Example:
1079
1080```
1081'a: loop {
1082 break 'a; // ok!
1083}
1084```
1085"##,
1086
1087E0428: r##"
3157f602 1088A type or module has been defined more than once.
c1a9b12d 1089
3157f602
XL
1090Erroneous code example:
1091
1092```compile_fail,E0428
c1a9b12d
SL
1093struct Bar;
1094struct Bar; // error: duplicate definition of value `Bar`
1095```
1096
1097Please verify you didn't misspell the type/module's name or remove/rename the
1098duplicated one. Example:
1099
1100```
1101struct Bar;
1102struct Bar2; // ok!
1103```
1104"##,
1105
3157f602
XL
1106E0429: r##"
1107The `self` keyword cannot appear alone as the last segment in a `use`
1108declaration.
1109
1110Erroneous code example:
1111
1112```compile_fail,E0429
1113use std::fmt::self; // error: `self` imports are only allowed within a { } list
1114```
1115
1116To use a namespace itself in addition to some of its members, `self` may appear
1117as part of a brace-enclosed list of imports:
1118
1119```
1120use std::fmt::{self, Debug};
1121```
1122
1123If you only want to import the namespace, do so directly:
1124
1125```
1126use std::fmt;
1127```
1128"##,
1129
c1a9b12d 1130E0430: r##"
3157f602 1131The `self` import appears more than once in the list.
c1a9b12d 1132
3157f602
XL
1133Erroneous code example:
1134
1135```compile_fail,E0430
c1a9b12d
SL
1136use something::{self, self}; // error: `self` import can only appear once in
1137 // the list
1138```
1139
1140Please verify you didn't misspell the import name or remove the duplicated
1141`self` import. Example:
1142
041b39d2
XL
1143```
1144# mod something {}
1145# fn main() {
1146use something::{self}; // ok!
1147# }
c1a9b12d
SL
1148```
1149"##,
1150
1151E0431: r##"
3157f602 1152An invalid `self` import was made.
c1a9b12d 1153
3157f602
XL
1154Erroneous code example:
1155
1156```compile_fail,E0431
c1a9b12d
SL
1157use {self}; // error: `self` import can only appear in an import list with a
1158 // non-empty prefix
1159```
1160
1161You cannot import the current module into itself, please remove this import
1162or verify you didn't misspell it.
1163"##,
1164
1165E0432: r##"
3157f602 1166An import was unresolved.
c1a9b12d 1167
3157f602
XL
1168Erroneous code example:
1169
1170```compile_fail,E0432
c1a9b12d
SL
1171use something::Foo; // error: unresolved import `something::Foo`.
1172```
1173
a7813a04
XL
1174Paths in `use` statements are relative to the crate root. To import items
1175relative to the current and parent modules, use the `self::` and `super::`
1176prefixes, respectively. Also verify that you didn't misspell the import
1177name and that the import exists in the module from where you tried to
1178import it. Example:
c1a9b12d 1179
041b39d2 1180```
a7813a04 1181use self::something::Foo; // ok!
c1a9b12d
SL
1182
1183mod something {
1184 pub struct Foo;
1185}
041b39d2 1186# fn main() {}
c1a9b12d 1187```
92a42be0
SL
1188
1189Or, if you tried to use a module from an external crate, you may have missed
a7813a04 1190the `extern crate` declaration (which is usually placed in the crate root):
92a42be0 1191
041b39d2
XL
1192```
1193extern crate core; // Required to use the `core` crate
92a42be0 1194
041b39d2
XL
1195use core::any;
1196# fn main() {}
92a42be0 1197```
c1a9b12d
SL
1198"##,
1199
1200E0433: r##"
3157f602 1201An undeclared type or module was used.
c1a9b12d 1202
3157f602
XL
1203Erroneous code example:
1204
1205```compile_fail,E0433
1206let map = HashMap::new();
1207// error: failed to resolve. Use of undeclared type or module `HashMap`
c1a9b12d
SL
1208```
1209
3157f602
XL
1210Please verify you didn't misspell the type/module's name or that you didn't
1211forgot to import it:
1212
1213
1214```
1215use std::collections::HashMap; // HashMap has been imported.
1216let map: HashMap<u32, u32> = HashMap::new(); // So it can be used!
1217```
c1a9b12d
SL
1218"##,
1219
a7813a04
XL
1220E0434: r##"
1221This error indicates that a variable usage inside an inner function is invalid
1222because the variable comes from a dynamic environment. Inner functions do not
1223have access to their containing environment.
1224
3157f602 1225Erroneous code example:
a7813a04 1226
3157f602 1227```compile_fail,E0434
a7813a04
XL
1228fn foo() {
1229 let y = 5;
1230 fn bar() -> u32 {
1231 y // error: can't capture dynamic environment in a fn item; use the
1232 // || { ... } closure form instead.
1233 }
1234}
1235```
1236
1237Functions do not capture local variables. To fix this error, you can replace the
1238function with a closure:
1239
1240```
1241fn foo() {
1242 let y = 5;
1243 let bar = || {
1244 y
1245 };
1246}
1247```
1248
1249or replace the captured variable with a constant or a static item:
1250
1251```
1252fn foo() {
1253 static mut X: u32 = 4;
1254 const Y: u32 = 5;
1255 fn bar() -> u32 {
1256 unsafe {
1257 X = 3;
1258 }
1259 Y
1260 }
1261}
1262```
1263"##,
1264
e9174d1e 1265E0435: r##"
7cac9316 1266A non-constant value was used in a constant expression.
e9174d1e 1267
3157f602
XL
1268Erroneous code example:
1269
1270```compile_fail,E0435
7cac9316
XL
1271let foo = 42;
1272let a: [u8; foo]; // error: attempt to use a non-constant value in a constant
e9174d1e
SL
1273```
1274
1275To fix this error, please replace the value with a constant. Example:
1276
1277```
7cac9316 1278let a: [u8; 42]; // ok!
7453a54e 1279```
e9174d1e 1280
7453a54e
SL
1281Or:
1282
1283```
7cac9316
XL
1284const FOO: usize = 42;
1285let a: [u8; FOO]; // ok!
e9174d1e
SL
1286```
1287"##,
1288
c1a9b12d
SL
1289E0437: r##"
1290Trait implementations can only implement associated types that are members of
1291the trait in question. This error indicates that you attempted to implement
1292an associated type whose name does not match the name of any associated type
1293in the trait.
1294
3157f602 1295Erroneous code example:
c1a9b12d 1296
3157f602 1297```compile_fail,E0437
c1a9b12d
SL
1298trait Foo {}
1299
1300impl Foo for i32 {
1301 type Bar = bool;
1302}
1303```
1304
1305The solution to this problem is to remove the extraneous associated type:
1306
1307```
1308trait Foo {}
1309
1310impl Foo for i32 {}
1311```
1312"##,
1313
1314E0438: r##"
1315Trait implementations can only implement associated constants that are
1316members of the trait in question. This error indicates that you
1317attempted to implement an associated constant whose name does not
1318match the name of any associated constant in the trait.
1319
3157f602 1320Erroneous code example:
c1a9b12d 1321
3157f602 1322```compile_fail,E0438
c1a9b12d
SL
1323trait Foo {}
1324
1325impl Foo for i32 {
1326 const BAR: bool = true;
1327}
1328```
1329
1330The solution to this problem is to remove the extraneous associated constant:
1331
1332```
1333trait Foo {}
1334
1335impl Foo for i32 {}
1336```
9e0c209e
SL
1337"##,
1338
c30ab7b3
SL
1339E0466: r##"
1340Macro import declarations were malformed.
1341
1342Erroneous code examples:
1343
1344```compile_fail,E0466
1345#[macro_use(a_macro(another_macro))] // error: invalid import declaration
1346extern crate core as some_crate;
1347
1348#[macro_use(i_want = "some_macros")] // error: invalid import declaration
1349extern crate core as another_crate;
1350```
1351
1352This is a syntax error at the level of attribute declarations. The proper
1353syntax for macro imports is the following:
1354
041b39d2 1355```ignore (cannot-doctest-multicrate-project)
c30ab7b3
SL
1356// In some_crate:
1357#[macro_export]
1358macro_rules! get_tacos {
1359 ...
1360}
1361
1362#[macro_export]
1363macro_rules! get_pimientos {
1364 ...
1365}
1366
1367// In your crate:
1368#[macro_use(get_tacos, get_pimientos)] // It imports `get_tacos` and
1369extern crate some_crate; // `get_pimientos` macros from some_crate
1370```
1371
1372If you would like to import all exported macros, write `macro_use` with no
1373arguments.
1374"##,
1375
1376E0467: r##"
1377Macro reexport declarations were empty or malformed.
1378
1379Erroneous code examples:
1380
1381```compile_fail,E0467
1382#[macro_reexport] // error: no macros listed for export
1383extern crate core as macros_for_good;
1384
1385#[macro_reexport(fun_macro = "foo")] // error: not a macro identifier
1386extern crate core as other_macros_for_good;
1387```
1388
1389This is a syntax error at the level of attribute declarations.
1390
1391Currently, `macro_reexport` requires at least one macro name to be listed.
1392Unlike `macro_use`, listing no names does not reexport all macros from the
1393given crate.
1394
1395Decide which macros you would like to export and list them properly.
1396
1397These are proper reexport declarations:
1398
041b39d2 1399```ignore (cannot-doctest-multicrate-project)
c30ab7b3
SL
1400#[macro_reexport(some_macro, another_macro)]
1401extern crate macros_for_good;
1402```
1403"##,
1404
1405E0468: r##"
1406A non-root module attempts to import macros from another crate.
1407
1408Example of erroneous code:
1409
1410```compile_fail,E0468
1411mod foo {
041b39d2 1412 #[macro_use(debug_assert)] // error: must be at crate root to import
c30ab7b3 1413 extern crate core; // macros from another crate
041b39d2 1414 fn run_macro() { debug_assert!(true); }
c30ab7b3
SL
1415}
1416```
1417
1418Only `extern crate` imports at the crate root level are allowed to import
1419macros.
1420
1421Either move the macro import to crate root or do without the foreign macros.
1422This will work:
1423
041b39d2
XL
1424```
1425#[macro_use(debug_assert)]
1426extern crate core;
c30ab7b3
SL
1427
1428mod foo {
041b39d2 1429 fn run_macro() { debug_assert!(true); }
c30ab7b3 1430}
041b39d2 1431# fn main() {}
c30ab7b3
SL
1432```
1433"##,
1434
1435E0469: r##"
1436A macro listed for import was not found.
1437
1438Erroneous code example:
1439
1440```compile_fail,E0469
1441#[macro_use(drink, be_merry)] // error: imported macro not found
041b39d2 1442extern crate alloc;
c30ab7b3
SL
1443
1444fn main() {
1445 // ...
1446}
1447```
1448
1449Either the listed macro is not contained in the imported crate, or it is not
1450exported from the given crate.
1451
1452This could be caused by a typo. Did you misspell the macro's name?
1453
1454Double-check the names of the macros listed for import, and that the crate
1455in question exports them.
1456
1457A working version would be:
1458
041b39d2 1459```ignore (cannot-doctest-multicrate-project)
c30ab7b3
SL
1460// In some_crate crate:
1461#[macro_export]
1462macro_rules! eat {
1463 ...
1464}
1465
1466#[macro_export]
1467macro_rules! drink {
1468 ...
1469}
1470
1471// In your crate:
1472#[macro_use(eat, drink)]
1473extern crate some_crate; //ok!
1474```
1475"##,
1476
1477E0470: r##"
1478A macro listed for reexport was not found.
1479
1480Erroneous code example:
1481
1482```compile_fail,E0470
1483#[macro_reexport(drink, be_merry)]
041b39d2 1484extern crate alloc;
c30ab7b3
SL
1485
1486fn main() {
1487 // ...
1488}
1489```
1490
1491Either the listed macro is not contained in the imported crate, or it is not
1492exported from the given crate.
1493
1494This could be caused by a typo. Did you misspell the macro's name?
1495
1496Double-check the names of the macros listed for reexport, and that the crate
1497in question exports them.
1498
1499A working version:
1500
041b39d2 1501```ignore (cannot-doctest-multicrate-project)
c30ab7b3
SL
1502// In some_crate crate:
1503#[macro_export]
1504macro_rules! eat {
1505 ...
1506}
1507
1508#[macro_export]
1509macro_rules! drink {
1510 ...
1511}
1512
1513// In your_crate:
1514#[macro_reexport(eat, drink)]
1515extern crate some_crate;
1516```
1517"##,
1518
9e0c209e
SL
1519E0530: r##"
1520A binding shadowed something it shouldn't.
1521
1522Erroneous code example:
1523
1524```compile_fail,E0530
1525static TEST: i32 = 0;
1526
1527let r: (i32, i32) = (0, 0);
1528match r {
1529 TEST => {} // error: match bindings cannot shadow statics
1530}
1531```
1532
1533To fix this error, just change the binding's name in order to avoid shadowing
1534one of the following:
1535
1536* struct name
1537* struct/enum variant
1538* static
1539* const
1540* associated const
1541
1542Fixed example:
1543
1544```
1545static TEST: i32 = 0;
1546
1547let r: (i32, i32) = (0, 0);
1548match r {
1549 something => {} // ok!
1550}
1551```
1552"##,
d9579d0f 1553
c30ab7b3
SL
1554E0532: r##"
1555Pattern arm did not match expected kind.
1556
1557Erroneous code example:
1558
1559```compile_fail,E0532
1560enum State {
1561 Succeeded,
1562 Failed(String),
1563}
1564
1565fn print_on_failure(state: &State) {
1566 match *state {
1567 // error: expected unit struct/variant or constant, found tuple
1568 // variant `State::Failed`
1569 State::Failed => println!("Failed"),
1570 _ => ()
1571 }
1572}
1573```
1574
1575To fix this error, ensure the match arm kind is the same as the expression
1576matched.
1577
1578Fixed example:
1579
1580```
1581enum State {
1582 Succeeded,
1583 Failed(String),
1584}
1585
1586fn print_on_failure(state: &State) {
1587 match *state {
1588 State::Failed(ref msg) => println!("Failed with {}", msg),
1589 _ => ()
1590 }
1591}
1592```
1593"##,
1594
7cac9316
XL
1595E0603: r##"
1596A private item was used outside its scope.
1597
1598Erroneous code example:
1599
1600```compile_fail,E0603
1601mod SomeModule {
1602 const PRIVATE: u32 = 0x_a_bad_1dea_u32; // This const is private, so we
1603 // can't use it outside of the
1604 // `SomeModule` module.
1605}
1606
1607println!("const value: {}", SomeModule::PRIVATE); // error: constant `CONSTANT`
1608 // is private
1609```
1610
1611In order to fix this error, you need to make the item public by using the `pub`
1612keyword. Example:
1613
1614```
1615mod SomeModule {
1616 pub const PRIVATE: u32 = 0x_a_bad_1dea_u32; // We set it public by using the
1617 // `pub` keyword.
1618}
1619
1620println!("const value: {}", SomeModule::PRIVATE); // ok!
1621```
1622"##,
1623
d9579d0f
AL
1624}
1625
85aaf69f 1626register_diagnostics! {
e9174d1e
SL
1627// E0153, unused error code
1628// E0157, unused error code
7453a54e
SL
1629// E0257,
1630// E0258,
7cac9316 1631// E0402, // cannot use an outer type parameter in this context
3157f602 1632// E0406, merged into 420
a7813a04 1633// E0410, merged into 408
3157f602
XL
1634// E0413, merged into 530
1635// E0414, merged into 530
1636// E0417, merged into 532
1637// E0418, merged into 532
1638// E0419, merged into 531
1639// E0420, merged into 532
1640// E0421, merged into 531
3157f602 1641 E0531, // unresolved pattern path kind `name`
3157f602 1642// E0427, merged into 530
32a655c1
SL
1643 E0573,
1644 E0574,
1645 E0575,
1646 E0576,
1647 E0577,
1648 E0578,
85aaf69f 1649}