]> git.lizzy.rs Git - rust.git/blob - src/librustc_typeck/error_codes.rs
Rollup merge of #63055 - Mark-Simulacrum:save-analysis-clean-2, r=Xanewok
[rust.git] / src / librustc_typeck / error_codes.rs
1 // ignore-tidy-filelength
2
3 register_long_diagnostics! {
4
5 E0023: r##"
6 A pattern used to match against an enum variant must provide a sub-pattern for
7 each field of the enum variant. This error indicates that a pattern attempted to
8 extract an incorrect number of fields from a variant.
9
10 ```
11 enum Fruit {
12     Apple(String, String),
13     Pear(u32),
14 }
15 ```
16
17 Here the `Apple` variant has two fields, and should be matched against like so:
18
19 ```
20 enum Fruit {
21     Apple(String, String),
22     Pear(u32),
23 }
24
25 let x = Fruit::Apple(String::new(), String::new());
26
27 // Correct.
28 match x {
29     Fruit::Apple(a, b) => {},
30     _ => {}
31 }
32 ```
33
34 Matching with the wrong number of fields has no sensible interpretation:
35
36 ```compile_fail,E0023
37 enum Fruit {
38     Apple(String, String),
39     Pear(u32),
40 }
41
42 let x = Fruit::Apple(String::new(), String::new());
43
44 // Incorrect.
45 match x {
46     Fruit::Apple(a) => {},
47     Fruit::Apple(a, b, c) => {},
48 }
49 ```
50
51 Check how many fields the enum was declared with and ensure that your pattern
52 uses the same number.
53 "##,
54
55 E0025: r##"
56 Each field of a struct can only be bound once in a pattern. Erroneous code
57 example:
58
59 ```compile_fail,E0025
60 struct Foo {
61     a: u8,
62     b: u8,
63 }
64
65 fn main(){
66     let x = Foo { a:1, b:2 };
67
68     let Foo { a: x, a: y } = x;
69     // error: field `a` bound multiple times in the pattern
70 }
71 ```
72
73 Each occurrence of a field name binds the value of that field, so to fix this
74 error you will have to remove or alter the duplicate uses of the field name.
75 Perhaps you misspelled another field name? Example:
76
77 ```
78 struct Foo {
79     a: u8,
80     b: u8,
81 }
82
83 fn main(){
84     let x = Foo { a:1, b:2 };
85
86     let Foo { a: x, b: y } = x; // ok!
87 }
88 ```
89 "##,
90
91 E0026: r##"
92 This error indicates that a struct pattern attempted to extract a non-existent
93 field from a struct. Struct fields are identified by the name used before the
94 colon `:` so struct patterns should resemble the declaration of the struct type
95 being matched.
96
97 ```
98 // Correct matching.
99 struct Thing {
100     x: u32,
101     y: u32
102 }
103
104 let thing = Thing { x: 1, y: 2 };
105
106 match thing {
107     Thing { x: xfield, y: yfield } => {}
108 }
109 ```
110
111 If you are using shorthand field patterns but want to refer to the struct field
112 by a different name, you should rename it explicitly.
113
114 Change this:
115
116 ```compile_fail,E0026
117 struct Thing {
118     x: u32,
119     y: u32
120 }
121
122 let thing = Thing { x: 0, y: 0 };
123
124 match thing {
125     Thing { x, z } => {}
126 }
127 ```
128
129 To this:
130
131 ```
132 struct Thing {
133     x: u32,
134     y: u32
135 }
136
137 let thing = Thing { x: 0, y: 0 };
138
139 match thing {
140     Thing { x, y: z } => {}
141 }
142 ```
143 "##,
144
145 E0027: r##"
146 This error indicates that a pattern for a struct fails to specify a sub-pattern
147 for every one of the struct's fields. Ensure that each field from the struct's
148 definition is mentioned in the pattern, or use `..` to ignore unwanted fields.
149
150 For example:
151
152 ```compile_fail,E0027
153 struct Dog {
154     name: String,
155     age: u32,
156 }
157
158 let d = Dog { name: "Rusty".to_string(), age: 8 };
159
160 // This is incorrect.
161 match d {
162     Dog { age: x } => {}
163 }
164 ```
165
166 This is correct (explicit):
167
168 ```
169 struct Dog {
170     name: String,
171     age: u32,
172 }
173
174 let d = Dog { name: "Rusty".to_string(), age: 8 };
175
176 match d {
177     Dog { name: ref n, age: x } => {}
178 }
179
180 // This is also correct (ignore unused fields).
181 match d {
182     Dog { age: x, .. } => {}
183 }
184 ```
185 "##,
186
187 E0029: r##"
188 In a match expression, only numbers and characters can be matched against a
189 range. This is because the compiler checks that the range is non-empty at
190 compile-time, and is unable to evaluate arbitrary comparison functions. If you
191 want to capture values of an orderable type between two end-points, you can use
192 a guard.
193
194 ```compile_fail,E0029
195 let string = "salutations !";
196
197 // The ordering relation for strings can't be evaluated at compile time,
198 // so this doesn't work:
199 match string {
200     "hello" ..= "world" => {}
201     _ => {}
202 }
203
204 // This is a more general version, using a guard:
205 match string {
206     s if s >= "hello" && s <= "world" => {}
207     _ => {}
208 }
209 ```
210 "##,
211
212 E0033: r##"
213 This error indicates that a pointer to a trait type cannot be implicitly
214 dereferenced by a pattern. Every trait defines a type, but because the
215 size of trait implementors isn't fixed, this type has no compile-time size.
216 Therefore, all accesses to trait types must be through pointers. If you
217 encounter this error you should try to avoid dereferencing the pointer.
218
219 ```compile_fail,E0033
220 # trait SomeTrait { fn method_one(&self){} fn method_two(&self){} }
221 # impl<T> SomeTrait for T {}
222 let trait_obj: &SomeTrait = &"some_value";
223
224 // This tries to implicitly dereference to create an unsized local variable.
225 let &invalid = trait_obj;
226
227 // You can call methods without binding to the value being pointed at.
228 trait_obj.method_one();
229 trait_obj.method_two();
230 ```
231
232 You can read more about trait objects in the [Trait Objects] section of the
233 Reference.
234
235 [Trait Objects]: https://doc.rust-lang.org/reference/types.html#trait-objects
236 "##,
237
238 E0034: r##"
239 The compiler doesn't know what method to call because more than one method
240 has the same prototype. Erroneous code example:
241
242 ```compile_fail,E0034
243 struct Test;
244
245 trait Trait1 {
246     fn foo();
247 }
248
249 trait Trait2 {
250     fn foo();
251 }
252
253 impl Trait1 for Test { fn foo() {} }
254 impl Trait2 for Test { fn foo() {} }
255
256 fn main() {
257     Test::foo() // error, which foo() to call?
258 }
259 ```
260
261 To avoid this error, you have to keep only one of them and remove the others.
262 So let's take our example and fix it:
263
264 ```
265 struct Test;
266
267 trait Trait1 {
268     fn foo();
269 }
270
271 impl Trait1 for Test { fn foo() {} }
272
273 fn main() {
274     Test::foo() // and now that's good!
275 }
276 ```
277
278 However, a better solution would be using fully explicit naming of type and
279 trait:
280
281 ```
282 struct Test;
283
284 trait Trait1 {
285     fn foo();
286 }
287
288 trait Trait2 {
289     fn foo();
290 }
291
292 impl Trait1 for Test { fn foo() {} }
293 impl Trait2 for Test { fn foo() {} }
294
295 fn main() {
296     <Test as Trait1>::foo()
297 }
298 ```
299
300 One last example:
301
302 ```
303 trait F {
304     fn m(&self);
305 }
306
307 trait G {
308     fn m(&self);
309 }
310
311 struct X;
312
313 impl F for X { fn m(&self) { println!("I am F"); } }
314 impl G for X { fn m(&self) { println!("I am G"); } }
315
316 fn main() {
317     let f = X;
318
319     F::m(&f); // it displays "I am F"
320     G::m(&f); // it displays "I am G"
321 }
322 ```
323 "##,
324
325 E0040: r##"
326 It is not allowed to manually call destructors in Rust. It is also not
327 necessary to do this since `drop` is called automatically whenever a value goes
328 out of scope.
329
330 Here's an example of this error:
331
332 ```compile_fail,E0040
333 struct Foo {
334     x: i32,
335 }
336
337 impl Drop for Foo {
338     fn drop(&mut self) {
339         println!("kaboom");
340     }
341 }
342
343 fn main() {
344     let mut x = Foo { x: -7 };
345     x.drop(); // error: explicit use of destructor method
346 }
347 ```
348 "##,
349
350 E0044: r##"
351 You can't use type or const parameters on foreign items.
352 Example of erroneous code:
353
354 ```compile_fail,E0044
355 extern { fn some_func<T>(x: T); }
356 ```
357
358 To fix this, replace the generic parameter with the specializations that you
359 need:
360
361 ```
362 extern { fn some_func_i32(x: i32); }
363 extern { fn some_func_i64(x: i64); }
364 ```
365 "##,
366
367 E0045: r##"
368 Rust only supports variadic parameters for interoperability with C code in its
369 FFI. As such, variadic parameters can only be used with functions which are
370 using the C ABI. Examples of erroneous code:
371
372 ```compile_fail
373 #![feature(unboxed_closures)]
374
375 extern "rust-call" { fn foo(x: u8, ...); }
376
377 // or
378
379 fn foo(x: u8, ...) {}
380 ```
381
382 To fix such code, put them in an extern "C" block:
383
384 ```
385 extern "C" {
386     fn foo (x: u8, ...);
387 }
388 ```
389 "##,
390
391 E0046: r##"
392 Items are missing in a trait implementation. Erroneous code example:
393
394 ```compile_fail,E0046
395 trait Foo {
396     fn foo();
397 }
398
399 struct Bar;
400
401 impl Foo for Bar {}
402 // error: not all trait items implemented, missing: `foo`
403 ```
404
405 When trying to make some type implement a trait `Foo`, you must, at minimum,
406 provide implementations for all of `Foo`'s required methods (meaning the
407 methods that do not have default implementations), as well as any required
408 trait items like associated types or constants. Example:
409
410 ```
411 trait Foo {
412     fn foo();
413 }
414
415 struct Bar;
416
417 impl Foo for Bar {
418     fn foo() {} // ok!
419 }
420 ```
421 "##,
422
423 E0049: r##"
424 This error indicates that an attempted implementation of a trait method
425 has the wrong number of type or const parameters.
426
427 For example, the trait below has a method `foo` with a type parameter `T`,
428 but the implementation of `foo` for the type `Bar` is missing this parameter:
429
430 ```compile_fail,E0049
431 trait Foo {
432     fn foo<T: Default>(x: T) -> Self;
433 }
434
435 struct Bar;
436
437 // error: method `foo` has 0 type parameters but its trait declaration has 1
438 // type parameter
439 impl Foo for Bar {
440     fn foo(x: bool) -> Self { Bar }
441 }
442 ```
443 "##,
444
445 E0050: r##"
446 This error indicates that an attempted implementation of a trait method
447 has the wrong number of function parameters.
448
449 For example, the trait below has a method `foo` with two function parameters
450 (`&self` and `u8`), but the implementation of `foo` for the type `Bar` omits
451 the `u8` parameter:
452
453 ```compile_fail,E0050
454 trait Foo {
455     fn foo(&self, x: u8) -> bool;
456 }
457
458 struct Bar;
459
460 // error: method `foo` has 1 parameter but the declaration in trait `Foo::foo`
461 // has 2
462 impl Foo for Bar {
463     fn foo(&self) -> bool { true }
464 }
465 ```
466 "##,
467
468 E0053: r##"
469 The parameters of any trait method must match between a trait implementation
470 and the trait definition.
471
472 Here are a couple examples of this error:
473
474 ```compile_fail,E0053
475 trait Foo {
476     fn foo(x: u16);
477     fn bar(&self);
478 }
479
480 struct Bar;
481
482 impl Foo for Bar {
483     // error, expected u16, found i16
484     fn foo(x: i16) { }
485
486     // error, types differ in mutability
487     fn bar(&mut self) { }
488 }
489 ```
490 "##,
491
492 E0054: r##"
493 It is not allowed to cast to a bool. If you are trying to cast a numeric type
494 to a bool, you can compare it with zero instead:
495
496 ```compile_fail,E0054
497 let x = 5;
498
499 // Not allowed, won't compile
500 let x_is_nonzero = x as bool;
501 ```
502
503 ```
504 let x = 5;
505
506 // Ok
507 let x_is_nonzero = x != 0;
508 ```
509 "##,
510
511 E0055: r##"
512 During a method call, a value is automatically dereferenced as many times as
513 needed to make the value's type match the method's receiver. The catch is that
514 the compiler will only attempt to dereference a number of times up to the
515 recursion limit (which can be set via the `recursion_limit` attribute).
516
517 For a somewhat artificial example:
518
519 ```compile_fail,E0055
520 #![recursion_limit="5"]
521
522 struct Foo;
523
524 impl Foo {
525     fn foo(&self) {}
526 }
527
528 fn main() {
529     let foo = Foo;
530     let ref_foo = &&&&&Foo;
531
532     // error, reached the recursion limit while auto-dereferencing `&&&&&Foo`
533     ref_foo.foo();
534 }
535 ```
536
537 One fix may be to increase the recursion limit. Note that it is possible to
538 create an infinite recursion of dereferencing, in which case the only fix is to
539 somehow break the recursion.
540 "##,
541
542 E0057: r##"
543 When invoking closures or other implementations of the function traits `Fn`,
544 `FnMut` or `FnOnce` using call notation, the number of parameters passed to the
545 function must match its definition.
546
547 An example using a closure:
548
549 ```compile_fail,E0057
550 let f = |x| x * 3;
551 let a = f();        // invalid, too few parameters
552 let b = f(4);       // this works!
553 let c = f(2, 3);    // invalid, too many parameters
554 ```
555
556 A generic function must be treated similarly:
557
558 ```
559 fn foo<F: Fn()>(f: F) {
560     f(); // this is valid, but f(3) would not work
561 }
562 ```
563 "##,
564
565 E0059: r##"
566 The built-in function traits are generic over a tuple of the function arguments.
567 If one uses angle-bracket notation (`Fn<(T,), Output=U>`) instead of parentheses
568 (`Fn(T) -> U`) to denote the function trait, the type parameter should be a
569 tuple. Otherwise function call notation cannot be used and the trait will not be
570 implemented by closures.
571
572 The most likely source of this error is using angle-bracket notation without
573 wrapping the function argument type into a tuple, for example:
574
575 ```compile_fail,E0059
576 #![feature(unboxed_closures)]
577
578 fn foo<F: Fn<i32>>(f: F) -> F::Output { f(3) }
579 ```
580
581 It can be fixed by adjusting the trait bound like this:
582
583 ```
584 #![feature(unboxed_closures)]
585
586 fn foo<F: Fn<(i32,)>>(f: F) -> F::Output { f(3) }
587 ```
588
589 Note that `(T,)` always denotes the type of a 1-tuple containing an element of
590 type `T`. The comma is necessary for syntactic disambiguation.
591 "##,
592
593 E0060: r##"
594 External C functions are allowed to be variadic. However, a variadic function
595 takes a minimum number of arguments. For example, consider C's variadic `printf`
596 function:
597
598 ```
599 use std::os::raw::{c_char, c_int};
600
601 extern "C" {
602     fn printf(_: *const c_char, ...) -> c_int;
603 }
604 ```
605
606 Using this declaration, it must be called with at least one argument, so
607 simply calling `printf()` is invalid. But the following uses are allowed:
608
609 ```
610 # #![feature(static_nobundle)]
611 # use std::os::raw::{c_char, c_int};
612 # #[cfg_attr(all(windows, target_env = "msvc"),
613 #            link(name = "legacy_stdio_definitions", kind = "static-nobundle"))]
614 # extern "C" { fn printf(_: *const c_char, ...) -> c_int; }
615 # fn main() {
616 unsafe {
617     use std::ffi::CString;
618
619     let fmt = CString::new("test\n").unwrap();
620     printf(fmt.as_ptr());
621
622     let fmt = CString::new("number = %d\n").unwrap();
623     printf(fmt.as_ptr(), 3);
624
625     let fmt = CString::new("%d, %d\n").unwrap();
626     printf(fmt.as_ptr(), 10, 5);
627 }
628 # }
629 ```
630 "##,
631 // ^ Note: On MSVC 2015, the `printf` function is "inlined" in the C code, and
632 // the C runtime does not contain the `printf` definition. This leads to linker
633 // error from the doc test (issue #42830).
634 // This can be fixed by linking to the static library
635 // `legacy_stdio_definitions.lib` (see https://stackoverflow.com/a/36504365/).
636 // If this compatibility library is removed in the future, consider changing
637 // `printf` in this example to another well-known variadic function.
638
639 E0061: r##"
640 The number of arguments passed to a function must match the number of arguments
641 specified in the function signature.
642
643 For example, a function like:
644
645 ```
646 fn f(a: u16, b: &str) {}
647 ```
648
649 Must always be called with exactly two arguments, e.g., `f(2, "test")`.
650
651 Note that Rust does not have a notion of optional function arguments or
652 variadic functions (except for its C-FFI).
653 "##,
654
655 E0062: r##"
656 This error indicates that during an attempt to build a struct or struct-like
657 enum variant, one of the fields was specified more than once. Erroneous code
658 example:
659
660 ```compile_fail,E0062
661 struct Foo {
662     x: i32,
663 }
664
665 fn main() {
666     let x = Foo {
667                 x: 0,
668                 x: 0, // error: field `x` specified more than once
669             };
670 }
671 ```
672
673 Each field should be specified exactly one time. Example:
674
675 ```
676 struct Foo {
677     x: i32,
678 }
679
680 fn main() {
681     let x = Foo { x: 0 }; // ok!
682 }
683 ```
684 "##,
685
686 E0063: r##"
687 This error indicates that during an attempt to build a struct or struct-like
688 enum variant, one of the fields was not provided. Erroneous code example:
689
690 ```compile_fail,E0063
691 struct Foo {
692     x: i32,
693     y: i32,
694 }
695
696 fn main() {
697     let x = Foo { x: 0 }; // error: missing field: `y`
698 }
699 ```
700
701 Each field should be specified exactly once. Example:
702
703 ```
704 struct Foo {
705     x: i32,
706     y: i32,
707 }
708
709 fn main() {
710     let x = Foo { x: 0, y: 0 }; // ok!
711 }
712 ```
713 "##,
714
715 E0067: r##"
716 The left-hand side of a compound assignment expression must be a place
717 expression. A place expression represents a memory location and includes
718 item paths (ie, namespaced variables), dereferences, indexing expressions,
719 and field references.
720
721 Let's start with some erroneous code examples:
722
723 ```compile_fail,E0067
724 use std::collections::LinkedList;
725
726 // Bad: assignment to non-place expression
727 LinkedList::new() += 1;
728
729 // ...
730
731 fn some_func(i: &mut i32) {
732     i += 12; // Error : '+=' operation cannot be applied on a reference !
733 }
734 ```
735
736 And now some working examples:
737
738 ```
739 let mut i : i32 = 0;
740
741 i += 12; // Good !
742
743 // ...
744
745 fn some_func(i: &mut i32) {
746     *i += 12; // Good !
747 }
748 ```
749 "##,
750
751 E0069: r##"
752 The compiler found a function whose body contains a `return;` statement but
753 whose return type is not `()`. An example of this is:
754
755 ```compile_fail,E0069
756 // error
757 fn foo() -> u8 {
758     return;
759 }
760 ```
761
762 Since `return;` is just like `return ();`, there is a mismatch between the
763 function's return type and the value being returned.
764 "##,
765
766 E0070: r##"
767 The left-hand side of an assignment operator must be a place expression. A
768 place expression represents a memory location and can be a variable (with
769 optional namespacing), a dereference, an indexing expression or a field
770 reference.
771
772 More details can be found in the [Expressions] section of the Reference.
773
774 [Expressions]: https://doc.rust-lang.org/reference/expressions.html#places-rvalues-and-temporaries
775
776 Now, we can go further. Here are some erroneous code examples:
777
778 ```compile_fail,E0070
779 struct SomeStruct {
780     x: i32,
781     y: i32
782 }
783
784 const SOME_CONST : i32 = 12;
785
786 fn some_other_func() {}
787
788 fn some_function() {
789     SOME_CONST = 14; // error : a constant value cannot be changed!
790     1 = 3; // error : 1 isn't a valid place!
791     some_other_func() = 4; // error : we can't assign value to a function!
792     SomeStruct.x = 12; // error : SomeStruct a structure name but it is used
793                        // like a variable!
794 }
795 ```
796
797 And now let's give working examples:
798
799 ```
800 struct SomeStruct {
801     x: i32,
802     y: i32
803 }
804 let mut s = SomeStruct {x: 0, y: 0};
805
806 s.x = 3; // that's good !
807
808 // ...
809
810 fn some_func(x: &mut i32) {
811     *x = 12; // that's good !
812 }
813 ```
814 "##,
815
816 E0071: r##"
817 You tried to use structure-literal syntax to create an item that is
818 not a structure or enum variant.
819
820 Example of erroneous code:
821
822 ```compile_fail,E0071
823 type U32 = u32;
824 let t = U32 { value: 4 }; // error: expected struct, variant or union type,
825                           // found builtin type `u32`
826 ```
827
828 To fix this, ensure that the name was correctly spelled, and that
829 the correct form of initializer was used.
830
831 For example, the code above can be fixed to:
832
833 ```
834 enum Foo {
835     FirstValue(i32)
836 }
837
838 fn main() {
839     let u = Foo::FirstValue(0i32);
840
841     let t = 4;
842 }
843 ```
844 "##,
845
846 E0073: r##"
847 #### Note: this error code is no longer emitted by the compiler.
848
849 You cannot define a struct (or enum) `Foo` that requires an instance of `Foo`
850 in order to make a new `Foo` value. This is because there would be no way a
851 first instance of `Foo` could be made to initialize another instance!
852
853 Here's an example of a struct that has this problem:
854
855 ```
856 struct Foo { x: Box<Foo> } // error
857 ```
858
859 One fix is to use `Option`, like so:
860
861 ```
862 struct Foo { x: Option<Box<Foo>> }
863 ```
864
865 Now it's possible to create at least one instance of `Foo`: `Foo { x: None }`.
866 "##,
867
868 E0074: r##"
869 #### Note: this error code is no longer emitted by the compiler.
870
871 When using the `#[simd]` attribute on a tuple struct, the components of the
872 tuple struct must all be of a concrete, nongeneric type so the compiler can
873 reason about how to use SIMD with them. This error will occur if the types
874 are generic.
875
876 This will cause an error:
877
878 ```
879 #![feature(repr_simd)]
880
881 #[repr(simd)]
882 struct Bad<T>(T, T, T);
883 ```
884
885 This will not:
886
887 ```
888 #![feature(repr_simd)]
889
890 #[repr(simd)]
891 struct Good(u32, u32, u32);
892 ```
893 "##,
894
895 E0075: r##"
896 The `#[simd]` attribute can only be applied to non empty tuple structs, because
897 it doesn't make sense to try to use SIMD operations when there are no values to
898 operate on.
899
900 This will cause an error:
901
902 ```compile_fail,E0075
903 #![feature(repr_simd)]
904
905 #[repr(simd)]
906 struct Bad;
907 ```
908
909 This will not:
910
911 ```
912 #![feature(repr_simd)]
913
914 #[repr(simd)]
915 struct Good(u32);
916 ```
917 "##,
918
919 E0076: r##"
920 When using the `#[simd]` attribute to automatically use SIMD operations in tuple
921 struct, the types in the struct must all be of the same type, or the compiler
922 will trigger this error.
923
924 This will cause an error:
925
926 ```compile_fail,E0076
927 #![feature(repr_simd)]
928
929 #[repr(simd)]
930 struct Bad(u16, u32, u32);
931 ```
932
933 This will not:
934
935 ```
936 #![feature(repr_simd)]
937
938 #[repr(simd)]
939 struct Good(u32, u32, u32);
940 ```
941 "##,
942
943 E0077: r##"
944 When using the `#[simd]` attribute on a tuple struct, the elements in the tuple
945 must be machine types so SIMD operations can be applied to them.
946
947 This will cause an error:
948
949 ```compile_fail,E0077
950 #![feature(repr_simd)]
951
952 #[repr(simd)]
953 struct Bad(String);
954 ```
955
956 This will not:
957
958 ```
959 #![feature(repr_simd)]
960
961 #[repr(simd)]
962 struct Good(u32, u32, u32);
963 ```
964 "##,
965
966 E0081: r##"
967 Enum discriminants are used to differentiate enum variants stored in memory.
968 This error indicates that the same value was used for two or more variants,
969 making them impossible to tell apart.
970
971 ```compile_fail,E0081
972 // Bad.
973 enum Enum {
974     P = 3,
975     X = 3,
976     Y = 5,
977 }
978 ```
979
980 ```
981 // Good.
982 enum Enum {
983     P,
984     X = 3,
985     Y = 5,
986 }
987 ```
988
989 Note that variants without a manually specified discriminant are numbered from
990 top to bottom starting from 0, so clashes can occur with seemingly unrelated
991 variants.
992
993 ```compile_fail,E0081
994 enum Bad {
995     X,
996     Y = 0
997 }
998 ```
999
1000 Here `X` will have already been specified the discriminant 0 by the time `Y` is
1001 encountered, so a conflict occurs.
1002 "##,
1003
1004 E0084: r##"
1005 An unsupported representation was attempted on a zero-variant enum.
1006
1007 Erroneous code example:
1008
1009 ```compile_fail,E0084
1010 #[repr(i32)]
1011 enum NightsWatch {} // error: unsupported representation for zero-variant enum
1012 ```
1013
1014 It is impossible to define an integer type to be used to represent zero-variant
1015 enum values because there are no zero-variant enum values. There is no way to
1016 construct an instance of the following type using only safe code. So you have
1017 two solutions. Either you add variants in your enum:
1018
1019 ```
1020 #[repr(i32)]
1021 enum NightsWatch {
1022     JonSnow,
1023     Commander,
1024 }
1025 ```
1026
1027 or you remove the integer represention of your enum:
1028
1029 ```
1030 enum NightsWatch {}
1031 ```
1032 "##,
1033
1034 // FIXME(const_generics:docs): example of inferring const parameter.
1035 E0087: r##"
1036 #### Note: this error code is no longer emitted by the compiler.
1037
1038 Too many type arguments were supplied for a function. For example:
1039
1040 ```compile_fail,E0107
1041 fn foo<T>() {}
1042
1043 fn main() {
1044     foo::<f64, bool>(); // error: wrong number of type arguments:
1045                         //        expected 1, found 2
1046 }
1047 ```
1048
1049 The number of supplied arguments must exactly match the number of defined type
1050 parameters.
1051 "##,
1052
1053 E0088: r##"
1054 #### Note: this error code is no longer emitted by the compiler.
1055
1056 You gave too many lifetime arguments. Erroneous code example:
1057
1058 ```compile_fail,E0107
1059 fn f() {}
1060
1061 fn main() {
1062     f::<'static>() // error: wrong number of lifetime arguments:
1063                    //        expected 0, found 1
1064 }
1065 ```
1066
1067 Please check you give the right number of lifetime arguments. Example:
1068
1069 ```
1070 fn f() {}
1071
1072 fn main() {
1073     f() // ok!
1074 }
1075 ```
1076
1077 It's also important to note that the Rust compiler can generally
1078 determine the lifetime by itself. Example:
1079
1080 ```
1081 struct Foo {
1082     value: String
1083 }
1084
1085 impl Foo {
1086     // it can be written like this
1087     fn get_value<'a>(&'a self) -> &'a str { &self.value }
1088     // but the compiler works fine with this too:
1089     fn without_lifetime(&self) -> &str { &self.value }
1090 }
1091
1092 fn main() {
1093     let f = Foo { value: "hello".to_owned() };
1094
1095     println!("{}", f.get_value());
1096     println!("{}", f.without_lifetime());
1097 }
1098 ```
1099 "##,
1100
1101 E0089: r##"
1102 #### Note: this error code is no longer emitted by the compiler.
1103
1104 Too few type arguments were supplied for a function. For example:
1105
1106 ```compile_fail,E0107
1107 fn foo<T, U>() {}
1108
1109 fn main() {
1110     foo::<f64>(); // error: wrong number of type arguments: expected 2, found 1
1111 }
1112 ```
1113
1114 Note that if a function takes multiple type arguments but you want the compiler
1115 to infer some of them, you can use type placeholders:
1116
1117 ```compile_fail,E0107
1118 fn foo<T, U>(x: T) {}
1119
1120 fn main() {
1121     let x: bool = true;
1122     foo::<f64>(x);    // error: wrong number of type arguments:
1123                       //        expected 2, found 1
1124     foo::<_, f64>(x); // same as `foo::<bool, f64>(x)`
1125 }
1126 ```
1127 "##,
1128
1129 E0090: r##"
1130 #### Note: this error code is no longer emitted by the compiler.
1131
1132 You gave too few lifetime arguments. Example:
1133
1134 ```compile_fail,E0107
1135 fn foo<'a: 'b, 'b: 'a>() {}
1136
1137 fn main() {
1138     foo::<'static>(); // error: wrong number of lifetime arguments:
1139                       //        expected 2, found 1
1140 }
1141 ```
1142
1143 Please check you give the right number of lifetime arguments. Example:
1144
1145 ```
1146 fn foo<'a: 'b, 'b: 'a>() {}
1147
1148 fn main() {
1149     foo::<'static, 'static>();
1150 }
1151 ```
1152 "##,
1153
1154 E0091: r##"
1155 You gave an unnecessary type or const parameter in a type alias. Erroneous
1156 code example:
1157
1158 ```compile_fail,E0091
1159 type Foo<T> = u32; // error: type parameter `T` is unused
1160 // or:
1161 type Foo<A,B> = Box<A>; // error: type parameter `B` is unused
1162 ```
1163
1164 Please check you didn't write too many parameters. Example:
1165
1166 ```
1167 type Foo = u32; // ok!
1168 type Foo2<A> = Box<A>; // ok!
1169 ```
1170 "##,
1171
1172 E0092: r##"
1173 You tried to declare an undefined atomic operation function.
1174 Erroneous code example:
1175
1176 ```compile_fail,E0092
1177 #![feature(intrinsics)]
1178
1179 extern "rust-intrinsic" {
1180     fn atomic_foo(); // error: unrecognized atomic operation
1181                      //        function
1182 }
1183 ```
1184
1185 Please check you didn't make a mistake in the function's name. All intrinsic
1186 functions are defined in librustc_codegen_llvm/intrinsic.rs and in
1187 libcore/intrinsics.rs in the Rust source code. Example:
1188
1189 ```
1190 #![feature(intrinsics)]
1191
1192 extern "rust-intrinsic" {
1193     fn atomic_fence(); // ok!
1194 }
1195 ```
1196 "##,
1197
1198 E0093: r##"
1199 You declared an unknown intrinsic function. Erroneous code example:
1200
1201 ```compile_fail,E0093
1202 #![feature(intrinsics)]
1203
1204 extern "rust-intrinsic" {
1205     fn foo(); // error: unrecognized intrinsic function: `foo`
1206 }
1207
1208 fn main() {
1209     unsafe {
1210         foo();
1211     }
1212 }
1213 ```
1214
1215 Please check you didn't make a mistake in the function's name. All intrinsic
1216 functions are defined in librustc_codegen_llvm/intrinsic.rs and in
1217 libcore/intrinsics.rs in the Rust source code. Example:
1218
1219 ```
1220 #![feature(intrinsics)]
1221
1222 extern "rust-intrinsic" {
1223     fn atomic_fence(); // ok!
1224 }
1225
1226 fn main() {
1227     unsafe {
1228         atomic_fence();
1229     }
1230 }
1231 ```
1232 "##,
1233
1234 E0094: r##"
1235 You gave an invalid number of type parameters to an intrinsic function.
1236 Erroneous code example:
1237
1238 ```compile_fail,E0094
1239 #![feature(intrinsics)]
1240
1241 extern "rust-intrinsic" {
1242     fn size_of<T, U>() -> usize; // error: intrinsic has wrong number
1243                                  //        of type parameters
1244 }
1245 ```
1246
1247 Please check that you provided the right number of type parameters
1248 and verify with the function declaration in the Rust source code.
1249 Example:
1250
1251 ```
1252 #![feature(intrinsics)]
1253
1254 extern "rust-intrinsic" {
1255     fn size_of<T>() -> usize; // ok!
1256 }
1257 ```
1258 "##,
1259
1260 E0107: r##"
1261 This error means that an incorrect number of generic arguments were provided:
1262
1263 ```compile_fail,E0107
1264 struct Foo<T> { x: T }
1265
1266 struct Bar { x: Foo }             // error: wrong number of type arguments:
1267                                   //        expected 1, found 0
1268 struct Baz<S, T> { x: Foo<S, T> } // error: wrong number of type arguments:
1269                                   //        expected 1, found 2
1270
1271 fn foo<T, U>(x: T, y: U) {}
1272
1273 fn main() {
1274     let x: bool = true;
1275     foo::<bool>(x);                 // error: wrong number of type arguments:
1276                                     //        expected 2, found 1
1277     foo::<bool, i32, i32>(x, 2, 4); // error: wrong number of type arguments:
1278                                     //        expected 2, found 3
1279 }
1280
1281 fn f() {}
1282
1283 fn main() {
1284     f::<'static>(); // error: wrong number of lifetime arguments:
1285                     //        expected 0, found 1
1286 }
1287 ```
1288
1289 "##,
1290
1291 E0109: r##"
1292 You tried to provide a generic argument to a type which doesn't need it.
1293 Erroneous code example:
1294
1295 ```compile_fail,E0109
1296 type X = u32<i32>; // error: type arguments are not allowed for this type
1297 type Y = bool<'static>; // error: lifetime parameters are not allowed on
1298                         //        this type
1299 ```
1300
1301 Check that you used the correct argument and that the definition is correct.
1302
1303 Example:
1304
1305 ```
1306 type X = u32; // ok!
1307 type Y = bool; // ok!
1308 ```
1309
1310 Note that generic arguments for enum variant constructors go after the variant,
1311 not after the enum. For example, you would write `Option::None::<u32>`,
1312 rather than `Option::<u32>::None`.
1313 "##,
1314
1315 E0110: r##"
1316 #### Note: this error code is no longer emitted by the compiler.
1317
1318 You tried to provide a lifetime to a type which doesn't need it.
1319 See `E0109` for more details.
1320 "##,
1321
1322 E0116: r##"
1323 You can only define an inherent implementation for a type in the same crate
1324 where the type was defined. For example, an `impl` block as below is not allowed
1325 since `Vec` is defined in the standard library:
1326
1327 ```compile_fail,E0116
1328 impl Vec<u8> { } // error
1329 ```
1330
1331 To fix this problem, you can do either of these things:
1332
1333  - define a trait that has the desired associated functions/types/constants and
1334    implement the trait for the type in question
1335  - define a new type wrapping the type and define an implementation on the new
1336    type
1337
1338 Note that using the `type` keyword does not work here because `type` only
1339 introduces a type alias:
1340
1341 ```compile_fail,E0116
1342 type Bytes = Vec<u8>;
1343
1344 impl Bytes { } // error, same as above
1345 ```
1346 "##,
1347
1348 E0117: r##"
1349 This error indicates a violation of one of Rust's orphan rules for trait
1350 implementations. The rule prohibits any implementation of a foreign trait (a
1351 trait defined in another crate) where
1352
1353  - the type that is implementing the trait is foreign
1354  - all of the parameters being passed to the trait (if there are any) are also
1355    foreign.
1356
1357 Here's one example of this error:
1358
1359 ```compile_fail,E0117
1360 impl Drop for u32 {}
1361 ```
1362
1363 To avoid this kind of error, ensure that at least one local type is referenced
1364 by the `impl`:
1365
1366 ```
1367 pub struct Foo; // you define your type in your crate
1368
1369 impl Drop for Foo { // and you can implement the trait on it!
1370     // code of trait implementation here
1371 #   fn drop(&mut self) { }
1372 }
1373
1374 impl From<Foo> for i32 { // or you use a type from your crate as
1375                          // a type parameter
1376     fn from(i: Foo) -> i32 {
1377         0
1378     }
1379 }
1380 ```
1381
1382 Alternatively, define a trait locally and implement that instead:
1383
1384 ```
1385 trait Bar {
1386     fn get(&self) -> usize;
1387 }
1388
1389 impl Bar for u32 {
1390     fn get(&self) -> usize { 0 }
1391 }
1392 ```
1393
1394 For information on the design of the orphan rules, see [RFC 1023].
1395
1396 [RFC 1023]: https://github.com/rust-lang/rfcs/blob/master/text/1023-rebalancing-coherence.md
1397 "##,
1398
1399 E0118: r##"
1400 You're trying to write an inherent implementation for something which isn't a
1401 struct nor an enum. Erroneous code example:
1402
1403 ```compile_fail,E0118
1404 impl (u8, u8) { // error: no base type found for inherent implementation
1405     fn get_state(&self) -> String {
1406         // ...
1407     }
1408 }
1409 ```
1410
1411 To fix this error, please implement a trait on the type or wrap it in a struct.
1412 Example:
1413
1414 ```
1415 // we create a trait here
1416 trait LiveLongAndProsper {
1417     fn get_state(&self) -> String;
1418 }
1419
1420 // and now you can implement it on (u8, u8)
1421 impl LiveLongAndProsper for (u8, u8) {
1422     fn get_state(&self) -> String {
1423         "He's dead, Jim!".to_owned()
1424     }
1425 }
1426 ```
1427
1428 Alternatively, you can create a newtype. A newtype is a wrapping tuple-struct.
1429 For example, `NewType` is a newtype over `Foo` in `struct NewType(Foo)`.
1430 Example:
1431
1432 ```
1433 struct TypeWrapper((u8, u8));
1434
1435 impl TypeWrapper {
1436     fn get_state(&self) -> String {
1437         "Fascinating!".to_owned()
1438     }
1439 }
1440 ```
1441 "##,
1442
1443 E0120: r##"
1444 An attempt was made to implement Drop on a trait, which is not allowed: only
1445 structs and enums can implement Drop. An example causing this error:
1446
1447 ```compile_fail,E0120
1448 trait MyTrait {}
1449
1450 impl Drop for MyTrait {
1451     fn drop(&mut self) {}
1452 }
1453 ```
1454
1455 A workaround for this problem is to wrap the trait up in a struct, and implement
1456 Drop on that. An example is shown below:
1457
1458 ```
1459 trait MyTrait {}
1460 struct MyWrapper<T: MyTrait> { foo: T }
1461
1462 impl <T: MyTrait> Drop for MyWrapper<T> {
1463     fn drop(&mut self) {}
1464 }
1465
1466 ```
1467
1468 Alternatively, wrapping trait objects requires something like the following:
1469
1470 ```
1471 trait MyTrait {}
1472
1473 //or Box<MyTrait>, if you wanted an owned trait object
1474 struct MyWrapper<'a> { foo: &'a MyTrait }
1475
1476 impl <'a> Drop for MyWrapper<'a> {
1477     fn drop(&mut self) {}
1478 }
1479 ```
1480 "##,
1481
1482 E0121: r##"
1483 In order to be consistent with Rust's lack of global type inference,
1484 type and const placeholders are disallowed by design in item signatures.
1485
1486 Examples of this error include:
1487
1488 ```compile_fail,E0121
1489 fn foo() -> _ { 5 } // error, explicitly write out the return type instead
1490
1491 static BAR: _ = "test"; // error, explicitly write out the type instead
1492 ```
1493 "##,
1494
1495 E0124: r##"
1496 You declared two fields of a struct with the same name. Erroneous code
1497 example:
1498
1499 ```compile_fail,E0124
1500 struct Foo {
1501     field1: i32,
1502     field1: i32, // error: field is already declared
1503 }
1504 ```
1505
1506 Please verify that the field names have been correctly spelled. Example:
1507
1508 ```
1509 struct Foo {
1510     field1: i32,
1511     field2: i32, // ok!
1512 }
1513 ```
1514 "##,
1515
1516 E0131: r##"
1517 It is not possible to define `main` with generic parameters.
1518 When `main` is present, it must take no arguments and return `()`.
1519 Erroneous code example:
1520
1521 ```compile_fail,E0131
1522 fn main<T>() { // error: main function is not allowed to have generic parameters
1523 }
1524 ```
1525 "##,
1526
1527 E0132: r##"
1528 A function with the `start` attribute was declared with type parameters.
1529
1530 Erroneous code example:
1531
1532 ```compile_fail,E0132
1533 #![feature(start)]
1534
1535 #[start]
1536 fn f<T>() {}
1537 ```
1538
1539 It is not possible to declare type parameters on a function that has the `start`
1540 attribute. Such a function must have the following type signature (for more
1541 information, view [the unstable book][1]):
1542
1543 [1]: https://doc.rust-lang.org/unstable-book/language-features/lang-items.html#writing-an-executable-without-stdlib
1544
1545 ```
1546 # let _:
1547 fn(isize, *const *const u8) -> isize;
1548 ```
1549
1550 Example:
1551
1552 ```
1553 #![feature(start)]
1554
1555 #[start]
1556 fn my_start(argc: isize, argv: *const *const u8) -> isize {
1557     0
1558 }
1559 ```
1560 "##,
1561
1562 E0164: r##"
1563 This error means that an attempt was made to match a struct type enum
1564 variant as a non-struct type:
1565
1566 ```compile_fail,E0164
1567 enum Foo { B { i: u32 } }
1568
1569 fn bar(foo: Foo) -> u32 {
1570     match foo {
1571         Foo::B(i) => i, // error E0164
1572     }
1573 }
1574 ```
1575
1576 Try using `{}` instead:
1577
1578 ```
1579 enum Foo { B { i: u32 } }
1580
1581 fn bar(foo: Foo) -> u32 {
1582     match foo {
1583         Foo::B{i} => i,
1584     }
1585 }
1586 ```
1587 "##,
1588
1589 E0184: r##"
1590 Explicitly implementing both Drop and Copy for a type is currently disallowed.
1591 This feature can make some sense in theory, but the current implementation is
1592 incorrect and can lead to memory unsafety (see [issue #20126][iss20126]), so
1593 it has been disabled for now.
1594
1595 [iss20126]: https://github.com/rust-lang/rust/issues/20126
1596 "##,
1597
1598 E0185: r##"
1599 An associated function for a trait was defined to be static, but an
1600 implementation of the trait declared the same function to be a method (i.e., to
1601 take a `self` parameter).
1602
1603 Here's an example of this error:
1604
1605 ```compile_fail,E0185
1606 trait Foo {
1607     fn foo();
1608 }
1609
1610 struct Bar;
1611
1612 impl Foo for Bar {
1613     // error, method `foo` has a `&self` declaration in the impl, but not in
1614     // the trait
1615     fn foo(&self) {}
1616 }
1617 ```
1618 "##,
1619
1620 E0186: r##"
1621 An associated function for a trait was defined to be a method (i.e., to take a
1622 `self` parameter), but an implementation of the trait declared the same function
1623 to be static.
1624
1625 Here's an example of this error:
1626
1627 ```compile_fail,E0186
1628 trait Foo {
1629     fn foo(&self);
1630 }
1631
1632 struct Bar;
1633
1634 impl Foo for Bar {
1635     // error, method `foo` has a `&self` declaration in the trait, but not in
1636     // the impl
1637     fn foo() {}
1638 }
1639 ```
1640 "##,
1641
1642 E0191: r##"
1643 Trait objects need to have all associated types specified. Erroneous code
1644 example:
1645
1646 ```compile_fail,E0191
1647 trait Trait {
1648     type Bar;
1649 }
1650
1651 type Foo = Trait; // error: the value of the associated type `Bar` (from
1652                   //        the trait `Trait`) must be specified
1653 ```
1654
1655 Please verify you specified all associated types of the trait and that you
1656 used the right trait. Example:
1657
1658 ```
1659 trait Trait {
1660     type Bar;
1661 }
1662
1663 type Foo = Trait<Bar=i32>; // ok!
1664 ```
1665 "##,
1666
1667 E0192: r##"
1668 Negative impls are only allowed for auto traits. For more
1669 information see the [opt-in builtin traits RFC][RFC 19].
1670
1671 [RFC 19]: https://github.com/rust-lang/rfcs/blob/master/text/0019-opt-in-builtin-traits.md
1672 "##,
1673
1674 E0193: r##"
1675 #### Note: this error code is no longer emitted by the compiler.
1676
1677 `where` clauses must use generic type parameters: it does not make sense to use
1678 them otherwise. An example causing this error:
1679
1680 ```
1681 trait Foo {
1682     fn bar(&self);
1683 }
1684
1685 #[derive(Copy,Clone)]
1686 struct Wrapper<T> {
1687     Wrapped: T
1688 }
1689
1690 impl Foo for Wrapper<u32> where Wrapper<u32>: Clone {
1691     fn bar(&self) { }
1692 }
1693 ```
1694
1695 This use of a `where` clause is strange - a more common usage would look
1696 something like the following:
1697
1698 ```
1699 trait Foo {
1700     fn bar(&self);
1701 }
1702
1703 #[derive(Copy,Clone)]
1704 struct Wrapper<T> {
1705     Wrapped: T
1706 }
1707 impl <T> Foo for Wrapper<T> where Wrapper<T>: Clone {
1708     fn bar(&self) { }
1709 }
1710 ```
1711
1712 Here, we're saying that the implementation exists on Wrapper only when the
1713 wrapped type `T` implements `Clone`. The `where` clause is important because
1714 some types will not implement `Clone`, and thus will not get this method.
1715
1716 In our erroneous example, however, we're referencing a single concrete type.
1717 Since we know for certain that `Wrapper<u32>` implements `Clone`, there's no
1718 reason to also specify it in a `where` clause.
1719 "##,
1720
1721 E0194: r##"
1722 A type parameter was declared which shadows an existing one. An example of this
1723 error:
1724
1725 ```compile_fail,E0194
1726 trait Foo<T> {
1727     fn do_something(&self) -> T;
1728     fn do_something_else<T: Clone>(&self, bar: T);
1729 }
1730 ```
1731
1732 In this example, the trait `Foo` and the trait method `do_something_else` both
1733 define a type parameter `T`. This is not allowed: if the method wishes to
1734 define a type parameter, it must use a different name for it.
1735 "##,
1736
1737 E0195: r##"
1738 Your method's lifetime parameters do not match the trait declaration.
1739 Erroneous code example:
1740
1741 ```compile_fail,E0195
1742 trait Trait {
1743     fn bar<'a,'b:'a>(x: &'a str, y: &'b str);
1744 }
1745
1746 struct Foo;
1747
1748 impl Trait for Foo {
1749     fn bar<'a,'b>(x: &'a str, y: &'b str) {
1750     // error: lifetime parameters or bounds on method `bar`
1751     // do not match the trait declaration
1752     }
1753 }
1754 ```
1755
1756 The lifetime constraint `'b` for bar() implementation does not match the
1757 trait declaration. Ensure lifetime declarations match exactly in both trait
1758 declaration and implementation. Example:
1759
1760 ```
1761 trait Trait {
1762     fn t<'a,'b:'a>(x: &'a str, y: &'b str);
1763 }
1764
1765 struct Foo;
1766
1767 impl Trait for Foo {
1768     fn t<'a,'b:'a>(x: &'a str, y: &'b str) { // ok!
1769     }
1770 }
1771 ```
1772 "##,
1773
1774 E0199: r##"
1775 Safe traits should not have unsafe implementations, therefore marking an
1776 implementation for a safe trait unsafe will cause a compiler error. Removing
1777 the unsafe marker on the trait noted in the error will resolve this problem.
1778
1779 ```compile_fail,E0199
1780 struct Foo;
1781
1782 trait Bar { }
1783
1784 // this won't compile because Bar is safe
1785 unsafe impl Bar for Foo { }
1786 // this will compile
1787 impl Bar for Foo { }
1788 ```
1789 "##,
1790
1791 E0200: r##"
1792 Unsafe traits must have unsafe implementations. This error occurs when an
1793 implementation for an unsafe trait isn't marked as unsafe. This may be resolved
1794 by marking the unsafe implementation as unsafe.
1795
1796 ```compile_fail,E0200
1797 struct Foo;
1798
1799 unsafe trait Bar { }
1800
1801 // this won't compile because Bar is unsafe and impl isn't unsafe
1802 impl Bar for Foo { }
1803 // this will compile
1804 unsafe impl Bar for Foo { }
1805 ```
1806 "##,
1807
1808 E0201: r##"
1809 It is an error to define two associated items (like methods, associated types,
1810 associated functions, etc.) with the same identifier.
1811
1812 For example:
1813
1814 ```compile_fail,E0201
1815 struct Foo(u8);
1816
1817 impl Foo {
1818     fn bar(&self) -> bool { self.0 > 5 }
1819     fn bar() {} // error: duplicate associated function
1820 }
1821
1822 trait Baz {
1823     type Quux;
1824     fn baz(&self) -> bool;
1825 }
1826
1827 impl Baz for Foo {
1828     type Quux = u32;
1829
1830     fn baz(&self) -> bool { true }
1831
1832     // error: duplicate method
1833     fn baz(&self) -> bool { self.0 > 5 }
1834
1835     // error: duplicate associated type
1836     type Quux = u32;
1837 }
1838 ```
1839
1840 Note, however, that items with the same name are allowed for inherent `impl`
1841 blocks that don't overlap:
1842
1843 ```
1844 struct Foo<T>(T);
1845
1846 impl Foo<u8> {
1847     fn bar(&self) -> bool { self.0 > 5 }
1848 }
1849
1850 impl Foo<bool> {
1851     fn bar(&self) -> bool { self.0 }
1852 }
1853 ```
1854 "##,
1855
1856 E0202: r##"
1857 Inherent associated types were part of [RFC 195] but are not yet implemented.
1858 See [the tracking issue][iss8995] for the status of this implementation.
1859
1860 [RFC 195]: https://github.com/rust-lang/rfcs/blob/master/text/0195-associated-items.md
1861 [iss8995]: https://github.com/rust-lang/rust/issues/8995
1862 "##,
1863
1864 E0204: r##"
1865 An attempt to implement the `Copy` trait for a struct failed because one of the
1866 fields does not implement `Copy`. To fix this, you must implement `Copy` for the
1867 mentioned field. Note that this may not be possible, as in the example of
1868
1869 ```compile_fail,E0204
1870 struct Foo {
1871     foo : Vec<u32>,
1872 }
1873
1874 impl Copy for Foo { }
1875 ```
1876
1877 This fails because `Vec<T>` does not implement `Copy` for any `T`.
1878
1879 Here's another example that will fail:
1880
1881 ```compile_fail,E0204
1882 #[derive(Copy)]
1883 struct Foo<'a> {
1884     ty: &'a mut bool,
1885 }
1886 ```
1887
1888 This fails because `&mut T` is not `Copy`, even when `T` is `Copy` (this
1889 differs from the behavior for `&T`, which is always `Copy`).
1890 "##,
1891
1892 /*
1893 E0205: r##"
1894 An attempt to implement the `Copy` trait for an enum failed because one of the
1895 variants does not implement `Copy`. To fix this, you must implement `Copy` for
1896 the mentioned variant. Note that this may not be possible, as in the example of
1897
1898 ```compile_fail,E0205
1899 enum Foo {
1900     Bar(Vec<u32>),
1901     Baz,
1902 }
1903
1904 impl Copy for Foo { }
1905 ```
1906
1907 This fails because `Vec<T>` does not implement `Copy` for any `T`.
1908
1909 Here's another example that will fail:
1910
1911 ```compile_fail,E0205
1912 #[derive(Copy)]
1913 enum Foo<'a> {
1914     Bar(&'a mut bool),
1915     Baz,
1916 }
1917 ```
1918
1919 This fails because `&mut T` is not `Copy`, even when `T` is `Copy` (this
1920 differs from the behavior for `&T`, which is always `Copy`).
1921 "##,
1922 */
1923
1924 E0206: r##"
1925 You can only implement `Copy` for a struct or enum. Both of the following
1926 examples will fail, because neither `[u8; 256]` nor `&'static mut Bar`
1927 (mutable reference to `Bar`) is a struct or enum:
1928
1929 ```compile_fail,E0206
1930 type Foo = [u8; 256];
1931 impl Copy for Foo { } // error
1932
1933 #[derive(Copy, Clone)]
1934 struct Bar;
1935 impl Copy for &'static mut Bar { } // error
1936 ```
1937 "##,
1938
1939 E0207: r##"
1940 Any type parameter or lifetime parameter of an `impl` must meet at least one of
1941 the following criteria:
1942
1943  - it appears in the _implementing type_ of the impl, e.g. `impl<T> Foo<T>`
1944  - for a trait impl, it appears in the _implemented trait_, e.g.
1945    `impl<T> SomeTrait<T> for Foo`
1946  - it is bound as an associated type, e.g. `impl<T, U> SomeTrait for T
1947    where T: AnotherTrait<AssocType=U>`
1948
1949 ### Error example 1
1950
1951 Suppose we have a struct `Foo` and we would like to define some methods for it.
1952 The following definition leads to a compiler error:
1953
1954 ```compile_fail,E0207
1955 struct Foo;
1956
1957 impl<T: Default> Foo {
1958 // error: the type parameter `T` is not constrained by the impl trait, self
1959 // type, or predicates [E0207]
1960     fn get(&self) -> T {
1961         <T as Default>::default()
1962     }
1963 }
1964 ```
1965
1966 The problem is that the parameter `T` does not appear in the implementing type
1967 (`Foo`) of the impl. In this case, we can fix the error by moving the type
1968 parameter from the `impl` to the method `get`:
1969
1970
1971 ```
1972 struct Foo;
1973
1974 // Move the type parameter from the impl to the method
1975 impl Foo {
1976     fn get<T: Default>(&self) -> T {
1977         <T as Default>::default()
1978     }
1979 }
1980 ```
1981
1982 ### Error example 2
1983
1984 As another example, suppose we have a `Maker` trait and want to establish a
1985 type `FooMaker` that makes `Foo`s:
1986
1987 ```compile_fail,E0207
1988 trait Maker {
1989     type Item;
1990     fn make(&mut self) -> Self::Item;
1991 }
1992
1993 struct Foo<T> {
1994     foo: T
1995 }
1996
1997 struct FooMaker;
1998
1999 impl<T: Default> Maker for FooMaker {
2000 // error: the type parameter `T` is not constrained by the impl trait, self
2001 // type, or predicates [E0207]
2002     type Item = Foo<T>;
2003
2004     fn make(&mut self) -> Foo<T> {
2005         Foo { foo: <T as Default>::default() }
2006     }
2007 }
2008 ```
2009
2010 This fails to compile because `T` does not appear in the trait or in the
2011 implementing type.
2012
2013 One way to work around this is to introduce a phantom type parameter into
2014 `FooMaker`, like so:
2015
2016 ```
2017 use std::marker::PhantomData;
2018
2019 trait Maker {
2020     type Item;
2021     fn make(&mut self) -> Self::Item;
2022 }
2023
2024 struct Foo<T> {
2025     foo: T
2026 }
2027
2028 // Add a type parameter to `FooMaker`
2029 struct FooMaker<T> {
2030     phantom: PhantomData<T>,
2031 }
2032
2033 impl<T: Default> Maker for FooMaker<T> {
2034     type Item = Foo<T>;
2035
2036     fn make(&mut self) -> Foo<T> {
2037         Foo {
2038             foo: <T as Default>::default(),
2039         }
2040     }
2041 }
2042 ```
2043
2044 Another way is to do away with the associated type in `Maker` and use an input
2045 type parameter instead:
2046
2047 ```
2048 // Use a type parameter instead of an associated type here
2049 trait Maker<Item> {
2050     fn make(&mut self) -> Item;
2051 }
2052
2053 struct Foo<T> {
2054     foo: T
2055 }
2056
2057 struct FooMaker;
2058
2059 impl<T: Default> Maker<Foo<T>> for FooMaker {
2060     fn make(&mut self) -> Foo<T> {
2061         Foo { foo: <T as Default>::default() }
2062     }
2063 }
2064 ```
2065
2066 ### Additional information
2067
2068 For more information, please see [RFC 447].
2069
2070 [RFC 447]: https://github.com/rust-lang/rfcs/blob/master/text/0447-no-unused-impl-parameters.md
2071 "##,
2072
2073 E0210: r##"
2074 This error indicates a violation of one of Rust's orphan rules for trait
2075 implementations. The rule concerns the use of type parameters in an
2076 implementation of a foreign trait (a trait defined in another crate), and
2077 states that type parameters must be "covered" by a local type. To understand
2078 what this means, it is perhaps easiest to consider a few examples.
2079
2080 If `ForeignTrait` is a trait defined in some external crate `foo`, then the
2081 following trait `impl` is an error:
2082
2083 ```compile_fail,E0210
2084 # #[cfg(for_demonstration_only)]
2085 extern crate foo;
2086 # #[cfg(for_demonstration_only)]
2087 use foo::ForeignTrait;
2088 # use std::panic::UnwindSafe as ForeignTrait;
2089
2090 impl<T> ForeignTrait for T { } // error
2091 # fn main() {}
2092 ```
2093
2094 To work around this, it can be covered with a local type, `MyType`:
2095
2096 ```
2097 # use std::panic::UnwindSafe as ForeignTrait;
2098 struct MyType<T>(T);
2099 impl<T> ForeignTrait for MyType<T> { } // Ok
2100 ```
2101
2102 Please note that a type alias is not sufficient.
2103
2104 For another example of an error, suppose there's another trait defined in `foo`
2105 named `ForeignTrait2` that takes two type parameters. Then this `impl` results
2106 in the same rule violation:
2107
2108 ```ignore (cannot-doctest-multicrate-project)
2109 struct MyType2;
2110 impl<T> ForeignTrait2<T, MyType<T>> for MyType2 { } // error
2111 ```
2112
2113 The reason for this is that there are two appearances of type parameter `T` in
2114 the `impl` header, both as parameters for `ForeignTrait2`. The first appearance
2115 is uncovered, and so runs afoul of the orphan rule.
2116
2117 Consider one more example:
2118
2119 ```ignore (cannot-doctest-multicrate-project)
2120 impl<T> ForeignTrait2<MyType<T>, T> for MyType2 { } // Ok
2121 ```
2122
2123 This only differs from the previous `impl` in that the parameters `T` and
2124 `MyType<T>` for `ForeignTrait2` have been swapped. This example does *not*
2125 violate the orphan rule; it is permitted.
2126
2127 To see why that last example was allowed, you need to understand the general
2128 rule. Unfortunately this rule is a bit tricky to state. Consider an `impl`:
2129
2130 ```ignore (only-for-syntax-highlight)
2131 impl<P1, ..., Pm> ForeignTrait<T1, ..., Tn> for T0 { ... }
2132 ```
2133
2134 where `P1, ..., Pm` are the type parameters of the `impl` and `T0, ..., Tn`
2135 are types. One of the types `T0, ..., Tn` must be a local type (this is another
2136 orphan rule, see the explanation for E0117). Let `i` be the smallest integer
2137 such that `Ti` is a local type. Then no type parameter can appear in any of the
2138 `Tj` for `j < i`.
2139
2140 For information on the design of the orphan rules, see [RFC 1023].
2141
2142 [RFC 1023]: https://github.com/rust-lang/rfcs/blob/master/text/1023-rebalancing-coherence.md
2143 "##,
2144
2145 /*
2146 E0211: r##"
2147 You used a function or type which doesn't fit the requirements for where it was
2148 used. Erroneous code examples:
2149
2150 ```compile_fail
2151 #![feature(intrinsics)]
2152
2153 extern "rust-intrinsic" {
2154     fn size_of<T>(); // error: intrinsic has wrong type
2155 }
2156
2157 // or:
2158
2159 fn main() -> i32 { 0 }
2160 // error: main function expects type: `fn() {main}`: expected (), found i32
2161
2162 // or:
2163
2164 let x = 1u8;
2165 match x {
2166     0u8..=3i8 => (),
2167     // error: mismatched types in range: expected u8, found i8
2168     _ => ()
2169 }
2170
2171 // or:
2172
2173 use std::rc::Rc;
2174 struct Foo;
2175
2176 impl Foo {
2177     fn x(self: Rc<Foo>) {}
2178     // error: mismatched self type: expected `Foo`: expected struct
2179     //        `Foo`, found struct `alloc::rc::Rc`
2180 }
2181 ```
2182
2183 For the first code example, please check the function definition. Example:
2184
2185 ```
2186 #![feature(intrinsics)]
2187
2188 extern "rust-intrinsic" {
2189     fn size_of<T>() -> usize; // ok!
2190 }
2191 ```
2192
2193 The second case example is a bit particular : the main function must always
2194 have this definition:
2195
2196 ```compile_fail
2197 fn main();
2198 ```
2199
2200 They never take parameters and never return types.
2201
2202 For the third example, when you match, all patterns must have the same type
2203 as the type you're matching on. Example:
2204
2205 ```
2206 let x = 1u8;
2207
2208 match x {
2209     0u8..=3u8 => (), // ok!
2210     _ => ()
2211 }
2212 ```
2213
2214 And finally, for the last example, only `Box<Self>`, `&Self`, `Self`,
2215 or `&mut Self` work as explicit self parameters. Example:
2216
2217 ```
2218 struct Foo;
2219
2220 impl Foo {
2221     fn x(self: Box<Foo>) {} // ok!
2222 }
2223 ```
2224 "##,
2225      */
2226
2227 E0220: r##"
2228 You used an associated type which isn't defined in the trait.
2229 Erroneous code example:
2230
2231 ```compile_fail,E0220
2232 trait T1 {
2233     type Bar;
2234 }
2235
2236 type Foo = T1<F=i32>; // error: associated type `F` not found for `T1`
2237
2238 // or:
2239
2240 trait T2 {
2241     type Bar;
2242
2243     // error: Baz is used but not declared
2244     fn return_bool(&self, _: &Self::Bar, _: &Self::Baz) -> bool;
2245 }
2246 ```
2247
2248 Make sure that you have defined the associated type in the trait body.
2249 Also, verify that you used the right trait or you didn't misspell the
2250 associated type name. Example:
2251
2252 ```
2253 trait T1 {
2254     type Bar;
2255 }
2256
2257 type Foo = T1<Bar=i32>; // ok!
2258
2259 // or:
2260
2261 trait T2 {
2262     type Bar;
2263     type Baz; // we declare `Baz` in our trait.
2264
2265     // and now we can use it here:
2266     fn return_bool(&self, _: &Self::Bar, _: &Self::Baz) -> bool;
2267 }
2268 ```
2269 "##,
2270
2271 E0221: r##"
2272 An attempt was made to retrieve an associated type, but the type was ambiguous.
2273 For example:
2274
2275 ```compile_fail,E0221
2276 trait T1 {}
2277 trait T2 {}
2278
2279 trait Foo {
2280     type A: T1;
2281 }
2282
2283 trait Bar : Foo {
2284     type A: T2;
2285     fn do_something() {
2286         let _: Self::A;
2287     }
2288 }
2289 ```
2290
2291 In this example, `Foo` defines an associated type `A`. `Bar` inherits that type
2292 from `Foo`, and defines another associated type of the same name. As a result,
2293 when we attempt to use `Self::A`, it's ambiguous whether we mean the `A` defined
2294 by `Foo` or the one defined by `Bar`.
2295
2296 There are two options to work around this issue. The first is simply to rename
2297 one of the types. Alternatively, one can specify the intended type using the
2298 following syntax:
2299
2300 ```
2301 trait T1 {}
2302 trait T2 {}
2303
2304 trait Foo {
2305     type A: T1;
2306 }
2307
2308 trait Bar : Foo {
2309     type A: T2;
2310     fn do_something() {
2311         let _: <Self as Bar>::A;
2312     }
2313 }
2314 ```
2315 "##,
2316
2317 E0223: r##"
2318 An attempt was made to retrieve an associated type, but the type was ambiguous.
2319 For example:
2320
2321 ```compile_fail,E0223
2322 trait MyTrait {type X; }
2323
2324 fn main() {
2325     let foo: MyTrait::X;
2326 }
2327 ```
2328
2329 The problem here is that we're attempting to take the type of X from MyTrait.
2330 Unfortunately, the type of X is not defined, because it's only made concrete in
2331 implementations of the trait. A working version of this code might look like:
2332
2333 ```
2334 trait MyTrait {type X; }
2335 struct MyStruct;
2336
2337 impl MyTrait for MyStruct {
2338     type X = u32;
2339 }
2340
2341 fn main() {
2342     let foo: <MyStruct as MyTrait>::X;
2343 }
2344 ```
2345
2346 This syntax specifies that we want the X type from MyTrait, as made concrete in
2347 MyStruct. The reason that we cannot simply use `MyStruct::X` is that MyStruct
2348 might implement two different traits with identically-named associated types.
2349 This syntax allows disambiguation between the two.
2350 "##,
2351
2352 E0225: r##"
2353 You attempted to use multiple types as bounds for a closure or trait object.
2354 Rust does not currently support this. A simple example that causes this error:
2355
2356 ```compile_fail,E0225
2357 fn main() {
2358     let _: Box<dyn std::io::Read + std::io::Write>;
2359 }
2360 ```
2361
2362 Auto traits such as Send and Sync are an exception to this rule:
2363 It's possible to have bounds of one non-builtin trait, plus any number of
2364 auto traits. For example, the following compiles correctly:
2365
2366 ```
2367 fn main() {
2368     let _: Box<dyn std::io::Read + Send + Sync>;
2369 }
2370 ```
2371 "##,
2372
2373 E0229: r##"
2374 An associated type binding was done outside of the type parameter declaration
2375 and `where` clause. Erroneous code example:
2376
2377 ```compile_fail,E0229
2378 pub trait Foo {
2379     type A;
2380     fn boo(&self) -> <Self as Foo>::A;
2381 }
2382
2383 struct Bar;
2384
2385 impl Foo for isize {
2386     type A = usize;
2387     fn boo(&self) -> usize { 42 }
2388 }
2389
2390 fn baz<I>(x: &<I as Foo<A=Bar>>::A) {}
2391 // error: associated type bindings are not allowed here
2392 ```
2393
2394 To solve this error, please move the type bindings in the type parameter
2395 declaration:
2396
2397 ```
2398 # struct Bar;
2399 # trait Foo { type A; }
2400 fn baz<I: Foo<A=Bar>>(x: &<I as Foo>::A) {} // ok!
2401 ```
2402
2403 Or in the `where` clause:
2404
2405 ```
2406 # struct Bar;
2407 # trait Foo { type A; }
2408 fn baz<I>(x: &<I as Foo>::A) where I: Foo<A=Bar> {}
2409 ```
2410 "##,
2411
2412 E0243: r##"
2413 #### Note: this error code is no longer emitted by the compiler.
2414
2415 This error indicates that not enough type parameters were found in a type or
2416 trait.
2417
2418 For example, the `Foo` struct below is defined to be generic in `T`, but the
2419 type parameter is missing in the definition of `Bar`:
2420
2421 ```compile_fail,E0107
2422 struct Foo<T> { x: T }
2423
2424 struct Bar { x: Foo }
2425 ```
2426 "##,
2427
2428 E0244: r##"
2429 #### Note: this error code is no longer emitted by the compiler.
2430
2431 This error indicates that too many type parameters were found in a type or
2432 trait.
2433
2434 For example, the `Foo` struct below has no type parameters, but is supplied
2435 with two in the definition of `Bar`:
2436
2437 ```compile_fail,E0107
2438 struct Foo { x: bool }
2439
2440 struct Bar<S, T> { x: Foo<S, T> }
2441 ```
2442 "##,
2443
2444 E0321: r##"
2445 A cross-crate opt-out trait was implemented on something which wasn't a struct
2446 or enum type. Erroneous code example:
2447
2448 ```compile_fail,E0321
2449 #![feature(optin_builtin_traits)]
2450
2451 struct Foo;
2452
2453 impl !Sync for Foo {}
2454
2455 unsafe impl Send for &'static Foo {}
2456 // error: cross-crate traits with a default impl, like `core::marker::Send`,
2457 //        can only be implemented for a struct/enum type, not
2458 //        `&'static Foo`
2459 ```
2460
2461 Only structs and enums are permitted to impl Send, Sync, and other opt-out
2462 trait, and the struct or enum must be local to the current crate. So, for
2463 example, `unsafe impl Send for Rc<Foo>` is not allowed.
2464 "##,
2465
2466 E0322: r##"
2467 The `Sized` trait is a special trait built-in to the compiler for types with a
2468 constant size known at compile-time. This trait is automatically implemented
2469 for types as needed by the compiler, and it is currently disallowed to
2470 explicitly implement it for a type.
2471 "##,
2472
2473 E0323: r##"
2474 An associated const was implemented when another trait item was expected.
2475 Erroneous code example:
2476
2477 ```compile_fail,E0323
2478 trait Foo {
2479     type N;
2480 }
2481
2482 struct Bar;
2483
2484 impl Foo for Bar {
2485     const N : u32 = 0;
2486     // error: item `N` is an associated const, which doesn't match its
2487     //        trait `<Bar as Foo>`
2488 }
2489 ```
2490
2491 Please verify that the associated const wasn't misspelled and the correct trait
2492 was implemented. Example:
2493
2494 ```
2495 struct Bar;
2496
2497 trait Foo {
2498     type N;
2499 }
2500
2501 impl Foo for Bar {
2502     type N = u32; // ok!
2503 }
2504 ```
2505
2506 Or:
2507
2508 ```
2509 struct Bar;
2510
2511 trait Foo {
2512     const N : u32;
2513 }
2514
2515 impl Foo for Bar {
2516     const N : u32 = 0; // ok!
2517 }
2518 ```
2519 "##,
2520
2521 E0324: r##"
2522 A method was implemented when another trait item was expected. Erroneous
2523 code example:
2524
2525 ```compile_fail,E0324
2526 struct Bar;
2527
2528 trait Foo {
2529     const N : u32;
2530
2531     fn M();
2532 }
2533
2534 impl Foo for Bar {
2535     fn N() {}
2536     // error: item `N` is an associated method, which doesn't match its
2537     //        trait `<Bar as Foo>`
2538 }
2539 ```
2540
2541 To fix this error, please verify that the method name wasn't misspelled and
2542 verify that you are indeed implementing the correct trait items. Example:
2543
2544 ```
2545 struct Bar;
2546
2547 trait Foo {
2548     const N : u32;
2549
2550     fn M();
2551 }
2552
2553 impl Foo for Bar {
2554     const N : u32 = 0;
2555
2556     fn M() {} // ok!
2557 }
2558 ```
2559 "##,
2560
2561 E0325: r##"
2562 An associated type was implemented when another trait item was expected.
2563 Erroneous code example:
2564
2565 ```compile_fail,E0325
2566 struct Bar;
2567
2568 trait Foo {
2569     const N : u32;
2570 }
2571
2572 impl Foo for Bar {
2573     type N = u32;
2574     // error: item `N` is an associated type, which doesn't match its
2575     //        trait `<Bar as Foo>`
2576 }
2577 ```
2578
2579 Please verify that the associated type name wasn't misspelled and your
2580 implementation corresponds to the trait definition. Example:
2581
2582 ```
2583 struct Bar;
2584
2585 trait Foo {
2586     type N;
2587 }
2588
2589 impl Foo for Bar {
2590     type N = u32; // ok!
2591 }
2592 ```
2593
2594 Or:
2595
2596 ```
2597 struct Bar;
2598
2599 trait Foo {
2600     const N : u32;
2601 }
2602
2603 impl Foo for Bar {
2604     const N : u32 = 0; // ok!
2605 }
2606 ```
2607 "##,
2608
2609 E0326: r##"
2610 The types of any associated constants in a trait implementation must match the
2611 types in the trait definition. This error indicates that there was a mismatch.
2612
2613 Here's an example of this error:
2614
2615 ```compile_fail,E0326
2616 trait Foo {
2617     const BAR: bool;
2618 }
2619
2620 struct Bar;
2621
2622 impl Foo for Bar {
2623     const BAR: u32 = 5; // error, expected bool, found u32
2624 }
2625 ```
2626 "##,
2627
2628 E0328: r##"
2629 The Unsize trait should not be implemented directly. All implementations of
2630 Unsize are provided automatically by the compiler.
2631
2632 Erroneous code example:
2633
2634 ```compile_fail,E0328
2635 #![feature(unsize)]
2636
2637 use std::marker::Unsize;
2638
2639 pub struct MyType;
2640
2641 impl<T> Unsize<T> for MyType {}
2642 ```
2643
2644 If you are defining your own smart pointer type and would like to enable
2645 conversion from a sized to an unsized type with the
2646 [DST coercion system][RFC 982], use [`CoerceUnsized`] instead.
2647
2648 ```
2649 #![feature(coerce_unsized)]
2650
2651 use std::ops::CoerceUnsized;
2652
2653 pub struct MyType<T: ?Sized> {
2654     field_with_unsized_type: T,
2655 }
2656
2657 impl<T, U> CoerceUnsized<MyType<U>> for MyType<T>
2658     where T: CoerceUnsized<U> {}
2659 ```
2660
2661 [RFC 982]: https://github.com/rust-lang/rfcs/blob/master/text/0982-dst-coercion.md
2662 [`CoerceUnsized`]: https://doc.rust-lang.org/std/ops/trait.CoerceUnsized.html
2663 "##,
2664
2665 /*
2666 // Associated consts can now be accessed through generic type parameters, and
2667 // this error is no longer emitted.
2668 //
2669 // FIXME: consider whether to leave it in the error index, or remove it entirely
2670 //        as associated consts is not stabilized yet.
2671
2672 E0329: r##"
2673 An attempt was made to access an associated constant through either a generic
2674 type parameter or `Self`. This is not supported yet. An example causing this
2675 error is shown below:
2676
2677 ```
2678 trait Foo {
2679     const BAR: f64;
2680 }
2681
2682 struct MyStruct;
2683
2684 impl Foo for MyStruct {
2685     const BAR: f64 = 0f64;
2686 }
2687
2688 fn get_bar_bad<F: Foo>(t: F) -> f64 {
2689     F::BAR
2690 }
2691 ```
2692
2693 Currently, the value of `BAR` for a particular type can only be accessed
2694 through a concrete type, as shown below:
2695
2696 ```
2697 trait Foo {
2698     const BAR: f64;
2699 }
2700
2701 struct MyStruct;
2702
2703 fn get_bar_good() -> f64 {
2704     <MyStruct as Foo>::BAR
2705 }
2706 ```
2707 "##,
2708 */
2709
2710 E0366: r##"
2711 An attempt was made to implement `Drop` on a concrete specialization of a
2712 generic type. An example is shown below:
2713
2714 ```compile_fail,E0366
2715 struct Foo<T> {
2716     t: T
2717 }
2718
2719 impl Drop for Foo<u32> {
2720     fn drop(&mut self) {}
2721 }
2722 ```
2723
2724 This code is not legal: it is not possible to specialize `Drop` to a subset of
2725 implementations of a generic type. One workaround for this is to wrap the
2726 generic type, as shown below:
2727
2728 ```
2729 struct Foo<T> {
2730     t: T
2731 }
2732
2733 struct Bar {
2734     t: Foo<u32>
2735 }
2736
2737 impl Drop for Bar {
2738     fn drop(&mut self) {}
2739 }
2740 ```
2741 "##,
2742
2743 E0367: r##"
2744 An attempt was made to implement `Drop` on a specialization of a generic type.
2745 An example is shown below:
2746
2747 ```compile_fail,E0367
2748 trait Foo{}
2749
2750 struct MyStruct<T> {
2751     t: T
2752 }
2753
2754 impl<T: Foo> Drop for MyStruct<T> {
2755     fn drop(&mut self) {}
2756 }
2757 ```
2758
2759 This code is not legal: it is not possible to specialize `Drop` to a subset of
2760 implementations of a generic type. In order for this code to work, `MyStruct`
2761 must also require that `T` implements `Foo`. Alternatively, another option is
2762 to wrap the generic type in another that specializes appropriately:
2763
2764 ```
2765 trait Foo{}
2766
2767 struct MyStruct<T> {
2768     t: T
2769 }
2770
2771 struct MyStructWrapper<T: Foo> {
2772     t: MyStruct<T>
2773 }
2774
2775 impl <T: Foo> Drop for MyStructWrapper<T> {
2776     fn drop(&mut self) {}
2777 }
2778 ```
2779 "##,
2780
2781 E0368: r##"
2782 This error indicates that a binary assignment operator like `+=` or `^=` was
2783 applied to a type that doesn't support it. For example:
2784
2785 ```compile_fail,E0368
2786 let mut x = 12f32; // error: binary operation `<<` cannot be applied to
2787                    //        type `f32`
2788
2789 x <<= 2;
2790 ```
2791
2792 To fix this error, please check that this type implements this binary
2793 operation. Example:
2794
2795 ```
2796 let mut x = 12u32; // the `u32` type does implement the `ShlAssign` trait
2797
2798 x <<= 2; // ok!
2799 ```
2800
2801 It is also possible to overload most operators for your own type by
2802 implementing the `[OP]Assign` traits from `std::ops`.
2803
2804 Another problem you might be facing is this: suppose you've overloaded the `+`
2805 operator for some type `Foo` by implementing the `std::ops::Add` trait for
2806 `Foo`, but you find that using `+=` does not work, as in this example:
2807
2808 ```compile_fail,E0368
2809 use std::ops::Add;
2810
2811 struct Foo(u32);
2812
2813 impl Add for Foo {
2814     type Output = Foo;
2815
2816     fn add(self, rhs: Foo) -> Foo {
2817         Foo(self.0 + rhs.0)
2818     }
2819 }
2820
2821 fn main() {
2822     let mut x: Foo = Foo(5);
2823     x += Foo(7); // error, `+= cannot be applied to the type `Foo`
2824 }
2825 ```
2826
2827 This is because `AddAssign` is not automatically implemented, so you need to
2828 manually implement it for your type.
2829 "##,
2830
2831 E0369: r##"
2832 A binary operation was attempted on a type which doesn't support it.
2833 Erroneous code example:
2834
2835 ```compile_fail,E0369
2836 let x = 12f32; // error: binary operation `<<` cannot be applied to
2837                //        type `f32`
2838
2839 x << 2;
2840 ```
2841
2842 To fix this error, please check that this type implements this binary
2843 operation. Example:
2844
2845 ```
2846 let x = 12u32; // the `u32` type does implement it:
2847                // https://doc.rust-lang.org/stable/std/ops/trait.Shl.html
2848
2849 x << 2; // ok!
2850 ```
2851
2852 It is also possible to overload most operators for your own type by
2853 implementing traits from `std::ops`.
2854
2855 String concatenation appends the string on the right to the string on the
2856 left and may require reallocation. This requires ownership of the string
2857 on the left. If something should be added to a string literal, move the
2858 literal to the heap by allocating it with `to_owned()` like in
2859 `"Your text".to_owned()`.
2860
2861 "##,
2862
2863 E0370: r##"
2864 The maximum value of an enum was reached, so it cannot be automatically
2865 set in the next enum value. Erroneous code example:
2866
2867 ```compile_fail,E0370
2868 #[repr(i64)]
2869 enum Foo {
2870     X = 0x7fffffffffffffff,
2871     Y, // error: enum discriminant overflowed on value after
2872        //        9223372036854775807: i64; set explicitly via
2873        //        Y = -9223372036854775808 if that is desired outcome
2874 }
2875 ```
2876
2877 To fix this, please set manually the next enum value or put the enum variant
2878 with the maximum value at the end of the enum. Examples:
2879
2880 ```
2881 #[repr(i64)]
2882 enum Foo {
2883     X = 0x7fffffffffffffff,
2884     Y = 0, // ok!
2885 }
2886 ```
2887
2888 Or:
2889
2890 ```
2891 #[repr(i64)]
2892 enum Foo {
2893     Y = 0, // ok!
2894     X = 0x7fffffffffffffff,
2895 }
2896 ```
2897 "##,
2898
2899 E0371: r##"
2900 When `Trait2` is a subtrait of `Trait1` (for example, when `Trait2` has a
2901 definition like `trait Trait2: Trait1 { ... }`), it is not allowed to implement
2902 `Trait1` for `Trait2`. This is because `Trait2` already implements `Trait1` by
2903 definition, so it is not useful to do this.
2904
2905 Example:
2906
2907 ```compile_fail,E0371
2908 trait Foo { fn foo(&self) { } }
2909 trait Bar: Foo { }
2910 trait Baz: Bar { }
2911
2912 impl Bar for Baz { } // error, `Baz` implements `Bar` by definition
2913 impl Foo for Baz { } // error, `Baz` implements `Bar` which implements `Foo`
2914 impl Baz for Baz { } // error, `Baz` (trivially) implements `Baz`
2915 impl Baz for Bar { } // Note: This is OK
2916 ```
2917 "##,
2918
2919 E0374: r##"
2920 A struct without a field containing an unsized type cannot implement
2921 `CoerceUnsized`. An [unsized type][1] is any type that the compiler
2922 doesn't know the length or alignment of at compile time. Any struct
2923 containing an unsized type is also unsized.
2924
2925 [1]: https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait
2926
2927 Example of erroneous code:
2928
2929 ```compile_fail,E0374
2930 #![feature(coerce_unsized)]
2931 use std::ops::CoerceUnsized;
2932
2933 struct Foo<T: ?Sized> {
2934     a: i32,
2935 }
2936
2937 // error: Struct `Foo` has no unsized fields that need `CoerceUnsized`.
2938 impl<T, U> CoerceUnsized<Foo<U>> for Foo<T>
2939     where T: CoerceUnsized<U> {}
2940 ```
2941
2942 `CoerceUnsized` is used to coerce one struct containing an unsized type
2943 into another struct containing a different unsized type. If the struct
2944 doesn't have any fields of unsized types then you don't need explicit
2945 coercion to get the types you want. To fix this you can either
2946 not try to implement `CoerceUnsized` or you can add a field that is
2947 unsized to the struct.
2948
2949 Example:
2950
2951 ```
2952 #![feature(coerce_unsized)]
2953 use std::ops::CoerceUnsized;
2954
2955 // We don't need to impl `CoerceUnsized` here.
2956 struct Foo {
2957     a: i32,
2958 }
2959
2960 // We add the unsized type field to the struct.
2961 struct Bar<T: ?Sized> {
2962     a: i32,
2963     b: T,
2964 }
2965
2966 // The struct has an unsized field so we can implement
2967 // `CoerceUnsized` for it.
2968 impl<T, U> CoerceUnsized<Bar<U>> for Bar<T>
2969     where T: CoerceUnsized<U> {}
2970 ```
2971
2972 Note that `CoerceUnsized` is mainly used by smart pointers like `Box`, `Rc`
2973 and `Arc` to be able to mark that they can coerce unsized types that they
2974 are pointing at.
2975 "##,
2976
2977 E0375: r##"
2978 A struct with more than one field containing an unsized type cannot implement
2979 `CoerceUnsized`. This only occurs when you are trying to coerce one of the
2980 types in your struct to another type in the struct. In this case we try to
2981 impl `CoerceUnsized` from `T` to `U` which are both types that the struct
2982 takes. An [unsized type][1] is any type that the compiler doesn't know the
2983 length or alignment of at compile time. Any struct containing an unsized type
2984 is also unsized.
2985
2986 Example of erroneous code:
2987
2988 ```compile_fail,E0375
2989 #![feature(coerce_unsized)]
2990 use std::ops::CoerceUnsized;
2991
2992 struct Foo<T: ?Sized, U: ?Sized> {
2993     a: i32,
2994     b: T,
2995     c: U,
2996 }
2997
2998 // error: Struct `Foo` has more than one unsized field.
2999 impl<T, U> CoerceUnsized<Foo<U, T>> for Foo<T, U> {}
3000 ```
3001
3002 `CoerceUnsized` only allows for coercion from a structure with a single
3003 unsized type field to another struct with a single unsized type field.
3004 In fact Rust only allows for a struct to have one unsized type in a struct
3005 and that unsized type must be the last field in the struct. So having two
3006 unsized types in a single struct is not allowed by the compiler. To fix this
3007 use only one field containing an unsized type in the struct and then use
3008 multiple structs to manage each unsized type field you need.
3009
3010 Example:
3011
3012 ```
3013 #![feature(coerce_unsized)]
3014 use std::ops::CoerceUnsized;
3015
3016 struct Foo<T: ?Sized> {
3017     a: i32,
3018     b: T,
3019 }
3020
3021 impl <T, U> CoerceUnsized<Foo<U>> for Foo<T>
3022     where T: CoerceUnsized<U> {}
3023
3024 fn coerce_foo<T: CoerceUnsized<U>, U>(t: T) -> Foo<U> {
3025     Foo { a: 12i32, b: t } // we use coercion to get the `Foo<U>` type we need
3026 }
3027 ```
3028
3029 [1]: https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait
3030 "##,
3031
3032 E0376: r##"
3033 The type you are trying to impl `CoerceUnsized` for is not a struct.
3034 `CoerceUnsized` can only be implemented for a struct. Unsized types are
3035 already able to be coerced without an implementation of `CoerceUnsized`
3036 whereas a struct containing an unsized type needs to know the unsized type
3037 field it's containing is able to be coerced. An [unsized type][1]
3038 is any type that the compiler doesn't know the length or alignment of at
3039 compile time. Any struct containing an unsized type is also unsized.
3040
3041 [1]: https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait
3042
3043 Example of erroneous code:
3044
3045 ```compile_fail,E0376
3046 #![feature(coerce_unsized)]
3047 use std::ops::CoerceUnsized;
3048
3049 struct Foo<T: ?Sized> {
3050     a: T,
3051 }
3052
3053 // error: The type `U` is not a struct
3054 impl<T, U> CoerceUnsized<U> for Foo<T> {}
3055 ```
3056
3057 The `CoerceUnsized` trait takes a struct type. Make sure the type you are
3058 providing to `CoerceUnsized` is a struct with only the last field containing an
3059 unsized type.
3060
3061 Example:
3062
3063 ```
3064 #![feature(coerce_unsized)]
3065 use std::ops::CoerceUnsized;
3066
3067 struct Foo<T> {
3068     a: T,
3069 }
3070
3071 // The `Foo<U>` is a struct so `CoerceUnsized` can be implemented
3072 impl<T, U> CoerceUnsized<Foo<U>> for Foo<T> where T: CoerceUnsized<U> {}
3073 ```
3074
3075 Note that in Rust, structs can only contain an unsized type if the field
3076 containing the unsized type is the last and only unsized type field in the
3077 struct.
3078 "##,
3079
3080 E0378: r##"
3081 The `DispatchFromDyn` trait currently can only be implemented for
3082 builtin pointer types and structs that are newtype wrappers around them
3083 — that is, the struct must have only one field (except for`PhantomData`),
3084 and that field must itself implement `DispatchFromDyn`.
3085
3086 Examples:
3087
3088 ```
3089 #![feature(dispatch_from_dyn, unsize)]
3090 use std::{
3091     marker::Unsize,
3092     ops::DispatchFromDyn,
3093 };
3094
3095 struct Ptr<T: ?Sized>(*const T);
3096
3097 impl<T: ?Sized, U: ?Sized> DispatchFromDyn<Ptr<U>> for Ptr<T>
3098 where
3099     T: Unsize<U>,
3100 {}
3101 ```
3102
3103 ```
3104 #![feature(dispatch_from_dyn)]
3105 use std::{
3106     ops::DispatchFromDyn,
3107     marker::PhantomData,
3108 };
3109
3110 struct Wrapper<T> {
3111     ptr: T,
3112     _phantom: PhantomData<()>,
3113 }
3114
3115 impl<T, U> DispatchFromDyn<Wrapper<U>> for Wrapper<T>
3116 where
3117     T: DispatchFromDyn<U>,
3118 {}
3119 ```
3120
3121 Example of illegal `DispatchFromDyn` implementation
3122 (illegal because of extra field)
3123
3124 ```compile-fail,E0378
3125 #![feature(dispatch_from_dyn)]
3126 use std::ops::DispatchFromDyn;
3127
3128 struct WrapperExtraField<T> {
3129     ptr: T,
3130     extra_stuff: i32,
3131 }
3132
3133 impl<T, U> DispatchFromDyn<WrapperExtraField<U>> for WrapperExtraField<T>
3134 where
3135     T: DispatchFromDyn<U>,
3136 {}
3137 ```
3138 "##,
3139
3140 E0390: r##"
3141 You tried to implement methods for a primitive type. Erroneous code example:
3142
3143 ```compile_fail,E0390
3144 struct Foo {
3145     x: i32
3146 }
3147
3148 impl *mut Foo {}
3149 // error: only a single inherent implementation marked with
3150 //        `#[lang = "mut_ptr"]` is allowed for the `*mut T` primitive
3151 ```
3152
3153 This isn't allowed, but using a trait to implement a method is a good solution.
3154 Example:
3155
3156 ```
3157 struct Foo {
3158     x: i32
3159 }
3160
3161 trait Bar {
3162     fn bar();
3163 }
3164
3165 impl Bar for *mut Foo {
3166     fn bar() {} // ok!
3167 }
3168 ```
3169 "##,
3170
3171 E0392: r##"
3172 This error indicates that a type or lifetime parameter has been declared
3173 but not actually used. Here is an example that demonstrates the error:
3174
3175 ```compile_fail,E0392
3176 enum Foo<T> {
3177     Bar,
3178 }
3179 ```
3180
3181 If the type parameter was included by mistake, this error can be fixed
3182 by simply removing the type parameter, as shown below:
3183
3184 ```
3185 enum Foo {
3186     Bar,
3187 }
3188 ```
3189
3190 Alternatively, if the type parameter was intentionally inserted, it must be
3191 used. A simple fix is shown below:
3192
3193 ```
3194 enum Foo<T> {
3195     Bar(T),
3196 }
3197 ```
3198
3199 This error may also commonly be found when working with unsafe code. For
3200 example, when using raw pointers one may wish to specify the lifetime for
3201 which the pointed-at data is valid. An initial attempt (below) causes this
3202 error:
3203
3204 ```compile_fail,E0392
3205 struct Foo<'a, T> {
3206     x: *const T,
3207 }
3208 ```
3209
3210 We want to express the constraint that Foo should not outlive `'a`, because
3211 the data pointed to by `T` is only valid for that lifetime. The problem is
3212 that there are no actual uses of `'a`. It's possible to work around this
3213 by adding a PhantomData type to the struct, using it to tell the compiler
3214 to act as if the struct contained a borrowed reference `&'a T`:
3215
3216 ```
3217 use std::marker::PhantomData;
3218
3219 struct Foo<'a, T: 'a> {
3220     x: *const T,
3221     phantom: PhantomData<&'a T>
3222 }
3223 ```
3224
3225 [PhantomData] can also be used to express information about unused type
3226 parameters.
3227
3228 [PhantomData]: https://doc.rust-lang.org/std/marker/struct.PhantomData.html
3229 "##,
3230
3231 E0393: r##"
3232 A type parameter which references `Self` in its default value was not specified.
3233 Example of erroneous code:
3234
3235 ```compile_fail,E0393
3236 trait A<T=Self> {}
3237
3238 fn together_we_will_rule_the_galaxy(son: &A) {}
3239 // error: the type parameter `T` must be explicitly specified in an
3240 //        object type because its default value `Self` references the
3241 //        type `Self`
3242 ```
3243
3244 A trait object is defined over a single, fully-defined trait. With a regular
3245 default parameter, this parameter can just be substituted in. However, if the
3246 default parameter is `Self`, the trait changes for each concrete type; i.e.
3247 `i32` will be expected to implement `A<i32>`, `bool` will be expected to
3248 implement `A<bool>`, etc... These types will not share an implementation of a
3249 fully-defined trait; instead they share implementations of a trait with
3250 different parameters substituted in for each implementation. This is
3251 irreconcilable with what we need to make a trait object work, and is thus
3252 disallowed. Making the trait concrete by explicitly specifying the value of the
3253 defaulted parameter will fix this issue. Fixed example:
3254
3255 ```
3256 trait A<T=Self> {}
3257
3258 fn together_we_will_rule_the_galaxy(son: &A<i32>) {} // Ok!
3259 ```
3260 "##,
3261
3262 E0399: r##"
3263 You implemented a trait, overriding one or more of its associated types but did
3264 not reimplement its default methods.
3265
3266 Example of erroneous code:
3267
3268 ```compile_fail,E0399
3269 #![feature(associated_type_defaults)]
3270
3271 pub trait Foo {
3272     type Assoc = u8;
3273     fn bar(&self) {}
3274 }
3275
3276 impl Foo for i32 {
3277     // error - the following trait items need to be reimplemented as
3278     //         `Assoc` was overridden: `bar`
3279     type Assoc = i32;
3280 }
3281 ```
3282
3283 To fix this, add an implementation for each default method from the trait:
3284
3285 ```
3286 #![feature(associated_type_defaults)]
3287
3288 pub trait Foo {
3289     type Assoc = u8;
3290     fn bar(&self) {}
3291 }
3292
3293 impl Foo for i32 {
3294     type Assoc = i32;
3295     fn bar(&self) {} // ok!
3296 }
3297 ```
3298 "##,
3299
3300 E0436: r##"
3301 The functional record update syntax is only allowed for structs. (Struct-like
3302 enum variants don't qualify, for example.)
3303
3304 Erroneous code example:
3305
3306 ```compile_fail,E0436
3307 enum PublicationFrequency {
3308     Weekly,
3309     SemiMonthly { days: (u8, u8), annual_special: bool },
3310 }
3311
3312 fn one_up_competitor(competitor_frequency: PublicationFrequency)
3313                      -> PublicationFrequency {
3314     match competitor_frequency {
3315         PublicationFrequency::Weekly => PublicationFrequency::SemiMonthly {
3316             days: (1, 15), annual_special: false
3317         },
3318         c @ PublicationFrequency::SemiMonthly{ .. } =>
3319             PublicationFrequency::SemiMonthly {
3320                 annual_special: true, ..c // error: functional record update
3321                                           //        syntax requires a struct
3322         }
3323     }
3324 }
3325 ```
3326
3327 Rewrite the expression without functional record update syntax:
3328
3329 ```
3330 enum PublicationFrequency {
3331     Weekly,
3332     SemiMonthly { days: (u8, u8), annual_special: bool },
3333 }
3334
3335 fn one_up_competitor(competitor_frequency: PublicationFrequency)
3336                      -> PublicationFrequency {
3337     match competitor_frequency {
3338         PublicationFrequency::Weekly => PublicationFrequency::SemiMonthly {
3339             days: (1, 15), annual_special: false
3340         },
3341         PublicationFrequency::SemiMonthly{ days, .. } =>
3342             PublicationFrequency::SemiMonthly {
3343                 days, annual_special: true // ok!
3344         }
3345     }
3346 }
3347 ```
3348 "##,
3349
3350 E0439: r##"
3351 The length of the platform-intrinsic function `simd_shuffle`
3352 wasn't specified. Erroneous code example:
3353
3354 ```compile_fail,E0439
3355 #![feature(platform_intrinsics)]
3356
3357 extern "platform-intrinsic" {
3358     fn simd_shuffle<A,B>(a: A, b: A, c: [u32; 8]) -> B;
3359     // error: invalid `simd_shuffle`, needs length: `simd_shuffle`
3360 }
3361 ```
3362
3363 The `simd_shuffle` function needs the length of the array passed as
3364 last parameter in its name. Example:
3365
3366 ```
3367 #![feature(platform_intrinsics)]
3368
3369 extern "platform-intrinsic" {
3370     fn simd_shuffle8<A,B>(a: A, b: A, c: [u32; 8]) -> B;
3371 }
3372 ```
3373 "##,
3374
3375 E0516: r##"
3376 The `typeof` keyword is currently reserved but unimplemented.
3377 Erroneous code example:
3378
3379 ```compile_fail,E0516
3380 fn main() {
3381     let x: typeof(92) = 92;
3382 }
3383 ```
3384
3385 Try using type inference instead. Example:
3386
3387 ```
3388 fn main() {
3389     let x = 92;
3390 }
3391 ```
3392 "##,
3393
3394 E0520: r##"
3395 A non-default implementation was already made on this type so it cannot be
3396 specialized further. Erroneous code example:
3397
3398 ```compile_fail,E0520
3399 #![feature(specialization)]
3400
3401 trait SpaceLlama {
3402     fn fly(&self);
3403 }
3404
3405 // applies to all T
3406 impl<T> SpaceLlama for T {
3407     default fn fly(&self) {}
3408 }
3409
3410 // non-default impl
3411 // applies to all `Clone` T and overrides the previous impl
3412 impl<T: Clone> SpaceLlama for T {
3413     fn fly(&self) {}
3414 }
3415
3416 // since `i32` is clone, this conflicts with the previous implementation
3417 impl SpaceLlama for i32 {
3418     default fn fly(&self) {}
3419     // error: item `fly` is provided by an `impl` that specializes
3420     //        another, but the item in the parent `impl` is not marked
3421     //        `default` and so it cannot be specialized.
3422 }
3423 ```
3424
3425 Specialization only allows you to override `default` functions in
3426 implementations.
3427
3428 To fix this error, you need to mark all the parent implementations as default.
3429 Example:
3430
3431 ```
3432 #![feature(specialization)]
3433
3434 trait SpaceLlama {
3435     fn fly(&self);
3436 }
3437
3438 // applies to all T
3439 impl<T> SpaceLlama for T {
3440     default fn fly(&self) {} // This is a parent implementation.
3441 }
3442
3443 // applies to all `Clone` T; overrides the previous impl
3444 impl<T: Clone> SpaceLlama for T {
3445     default fn fly(&self) {} // This is a parent implementation but was
3446                              // previously not a default one, causing the error
3447 }
3448
3449 // applies to i32, overrides the previous two impls
3450 impl SpaceLlama for i32 {
3451     fn fly(&self) {} // And now that's ok!
3452 }
3453 ```
3454 "##,
3455
3456 E0527: r##"
3457 The number of elements in an array or slice pattern differed from the number of
3458 elements in the array being matched.
3459
3460 Example of erroneous code:
3461
3462 ```compile_fail,E0527
3463 let r = &[1, 2, 3, 4];
3464 match r {
3465     &[a, b] => { // error: pattern requires 2 elements but array
3466                  //        has 4
3467         println!("a={}, b={}", a, b);
3468     }
3469 }
3470 ```
3471
3472 Ensure that the pattern is consistent with the size of the matched
3473 array. Additional elements can be matched with `..`:
3474
3475 ```
3476 #![feature(slice_patterns)]
3477
3478 let r = &[1, 2, 3, 4];
3479 match r {
3480     &[a, b, ..] => { // ok!
3481         println!("a={}, b={}", a, b);
3482     }
3483 }
3484 ```
3485 "##,
3486
3487 E0528: r##"
3488 An array or slice pattern required more elements than were present in the
3489 matched array.
3490
3491 Example of erroneous code:
3492
3493 ```compile_fail,E0528
3494 #![feature(slice_patterns)]
3495
3496 let r = &[1, 2];
3497 match r {
3498     &[a, b, c, rest @ ..] => { // error: pattern requires at least 3
3499                                //        elements but array has 2
3500         println!("a={}, b={}, c={} rest={:?}", a, b, c, rest);
3501     }
3502 }
3503 ```
3504
3505 Ensure that the matched array has at least as many elements as the pattern
3506 requires. You can match an arbitrary number of remaining elements with `..`:
3507
3508 ```
3509 #![feature(slice_patterns)]
3510
3511 let r = &[1, 2, 3, 4, 5];
3512 match r {
3513     &[a, b, c, rest @ ..] => { // ok!
3514         // prints `a=1, b=2, c=3 rest=[4, 5]`
3515         println!("a={}, b={}, c={} rest={:?}", a, b, c, rest);
3516     }
3517 }
3518 ```
3519 "##,
3520
3521 E0529: r##"
3522 An array or slice pattern was matched against some other type.
3523
3524 Example of erroneous code:
3525
3526 ```compile_fail,E0529
3527 let r: f32 = 1.0;
3528 match r {
3529     [a, b] => { // error: expected an array or slice, found `f32`
3530         println!("a={}, b={}", a, b);
3531     }
3532 }
3533 ```
3534
3535 Ensure that the pattern and the expression being matched on are of consistent
3536 types:
3537
3538 ```
3539 let r = [1.0, 2.0];
3540 match r {
3541     [a, b] => { // ok!
3542         println!("a={}, b={}", a, b);
3543     }
3544 }
3545 ```
3546 "##,
3547
3548 E0534: r##"
3549 The `inline` attribute was malformed.
3550
3551 Erroneous code example:
3552
3553 ```ignore (compile_fail not working here; see Issue #43707)
3554 #[inline()] // error: expected one argument
3555 pub fn something() {}
3556
3557 fn main() {}
3558 ```
3559
3560 The parenthesized `inline` attribute requires the parameter to be specified:
3561
3562 ```
3563 #[inline(always)]
3564 fn something() {}
3565 ```
3566
3567 or:
3568
3569 ```
3570 #[inline(never)]
3571 fn something() {}
3572 ```
3573
3574 Alternatively, a paren-less version of the attribute may be used to hint the
3575 compiler about inlining opportunity:
3576
3577 ```
3578 #[inline]
3579 fn something() {}
3580 ```
3581
3582 For more information about the inline attribute, read:
3583 https://doc.rust-lang.org/reference.html#inline-attributes
3584 "##,
3585
3586 E0535: r##"
3587 An unknown argument was given to the `inline` attribute.
3588
3589 Erroneous code example:
3590
3591 ```ignore (compile_fail not working here; see Issue #43707)
3592 #[inline(unknown)] // error: invalid argument
3593 pub fn something() {}
3594
3595 fn main() {}
3596 ```
3597
3598 The `inline` attribute only supports two arguments:
3599
3600  * always
3601  * never
3602
3603 All other arguments given to the `inline` attribute will return this error.
3604 Example:
3605
3606 ```
3607 #[inline(never)] // ok!
3608 pub fn something() {}
3609
3610 fn main() {}
3611 ```
3612
3613 For more information about the inline attribute, https:
3614 read://doc.rust-lang.org/reference.html#inline-attributes
3615 "##,
3616
3617 E0559: r##"
3618 An unknown field was specified into an enum's structure variant.
3619
3620 Erroneous code example:
3621
3622 ```compile_fail,E0559
3623 enum Field {
3624     Fool { x: u32 },
3625 }
3626
3627 let s = Field::Fool { joke: 0 };
3628 // error: struct variant `Field::Fool` has no field named `joke`
3629 ```
3630
3631 Verify you didn't misspell the field's name or that the field exists. Example:
3632
3633 ```
3634 enum Field {
3635     Fool { joke: u32 },
3636 }
3637
3638 let s = Field::Fool { joke: 0 }; // ok!
3639 ```
3640 "##,
3641
3642 E0560: r##"
3643 An unknown field was specified into a structure.
3644
3645 Erroneous code example:
3646
3647 ```compile_fail,E0560
3648 struct Simba {
3649     mother: u32,
3650 }
3651
3652 let s = Simba { mother: 1, father: 0 };
3653 // error: structure `Simba` has no field named `father`
3654 ```
3655
3656 Verify you didn't misspell the field's name or that the field exists. Example:
3657
3658 ```
3659 struct Simba {
3660     mother: u32,
3661     father: u32,
3662 }
3663
3664 let s = Simba { mother: 1, father: 0 }; // ok!
3665 ```
3666 "##,
3667
3668 E0569: r##"
3669 If an impl has a generic parameter with the `#[may_dangle]` attribute, then
3670 that impl must be declared as an `unsafe impl.
3671
3672 Erroneous code example:
3673
3674 ```compile_fail,E0569
3675 #![feature(dropck_eyepatch)]
3676
3677 struct Foo<X>(X);
3678 impl<#[may_dangle] X> Drop for Foo<X> {
3679     fn drop(&mut self) { }
3680 }
3681 ```
3682
3683 In this example, we are asserting that the destructor for `Foo` will not
3684 access any data of type `X`, and require this assertion to be true for
3685 overall safety in our program. The compiler does not currently attempt to
3686 verify this assertion; therefore we must tag this `impl` as unsafe.
3687 "##,
3688
3689 E0570: r##"
3690 The requested ABI is unsupported by the current target.
3691
3692 The rust compiler maintains for each target a blacklist of ABIs unsupported on
3693 that target. If an ABI is present in such a list this usually means that the
3694 target / ABI combination is currently unsupported by llvm.
3695
3696 If necessary, you can circumvent this check using custom target specifications.
3697 "##,
3698
3699 E0572: r##"
3700 A return statement was found outside of a function body.
3701
3702 Erroneous code example:
3703
3704 ```compile_fail,E0572
3705 const FOO: u32 = return 0; // error: return statement outside of function body
3706
3707 fn main() {}
3708 ```
3709
3710 To fix this issue, just remove the return keyword or move the expression into a
3711 function. Example:
3712
3713 ```
3714 const FOO: u32 = 0;
3715
3716 fn some_fn() -> u32 {
3717     return FOO;
3718 }
3719
3720 fn main() {
3721     some_fn();
3722 }
3723 ```
3724 "##,
3725
3726 E0581: r##"
3727 In a `fn` type, a lifetime appears only in the return type,
3728 and not in the arguments types.
3729
3730 Erroneous code example:
3731
3732 ```compile_fail,E0581
3733 fn main() {
3734     // Here, `'a` appears only in the return type:
3735     let x: for<'a> fn() -> &'a i32;
3736 }
3737 ```
3738
3739 To fix this issue, either use the lifetime in the arguments, or use
3740 `'static`. Example:
3741
3742 ```
3743 fn main() {
3744     // Here, `'a` appears only in the return type:
3745     let x: for<'a> fn(&'a i32) -> &'a i32;
3746     let y: fn() -> &'static i32;
3747 }
3748 ```
3749
3750 Note: The examples above used to be (erroneously) accepted by the
3751 compiler, but this was since corrected. See [issue #33685] for more
3752 details.
3753
3754 [issue #33685]: https://github.com/rust-lang/rust/issues/33685
3755 "##,
3756
3757 E0582: r##"
3758 A lifetime appears only in an associated-type binding,
3759 and not in the input types to the trait.
3760
3761 Erroneous code example:
3762
3763 ```compile_fail,E0582
3764 fn bar<F>(t: F)
3765     // No type can satisfy this requirement, since `'a` does not
3766     // appear in any of the input types (here, `i32`):
3767     where F: for<'a> Fn(i32) -> Option<&'a i32>
3768 {
3769 }
3770
3771 fn main() { }
3772 ```
3773
3774 To fix this issue, either use the lifetime in the inputs, or use
3775 `'static`. Example:
3776
3777 ```
3778 fn bar<F, G>(t: F, u: G)
3779     where F: for<'a> Fn(&'a i32) -> Option<&'a i32>,
3780           G: Fn(i32) -> Option<&'static i32>,
3781 {
3782 }
3783
3784 fn main() { }
3785 ```
3786
3787 Note: The examples above used to be (erroneously) accepted by the
3788 compiler, but this was since corrected. See [issue #33685] for more
3789 details.
3790
3791 [issue #33685]: https://github.com/rust-lang/rust/issues/33685
3792 "##,
3793
3794 E0592: r##"
3795 This error occurs when you defined methods or associated functions with same
3796 name.
3797
3798 Erroneous code example:
3799
3800 ```compile_fail,E0592
3801 struct Foo;
3802
3803 impl Foo {
3804     fn bar() {} // previous definition here
3805 }
3806
3807 impl Foo {
3808     fn bar() {} // duplicate definition here
3809 }
3810 ```
3811
3812 A similar error is E0201. The difference is whether there is one declaration
3813 block or not. To avoid this error, you must give each `fn` a unique name.
3814
3815 ```
3816 struct Foo;
3817
3818 impl Foo {
3819     fn bar() {}
3820 }
3821
3822 impl Foo {
3823     fn baz() {} // define with different name
3824 }
3825 ```
3826 "##,
3827
3828 E0599: r##"
3829 This error occurs when a method is used on a type which doesn't implement it:
3830
3831 Erroneous code example:
3832
3833 ```compile_fail,E0599
3834 struct Mouth;
3835
3836 let x = Mouth;
3837 x.chocolate(); // error: no method named `chocolate` found for type `Mouth`
3838                //        in the current scope
3839 ```
3840 "##,
3841
3842 E0600: r##"
3843 An unary operator was used on a type which doesn't implement it.
3844
3845 Example of erroneous code:
3846
3847 ```compile_fail,E0600
3848 enum Question {
3849     Yes,
3850     No,
3851 }
3852
3853 !Question::Yes; // error: cannot apply unary operator `!` to type `Question`
3854 ```
3855
3856 In this case, `Question` would need to implement the `std::ops::Not` trait in
3857 order to be able to use `!` on it. Let's implement it:
3858
3859 ```
3860 use std::ops::Not;
3861
3862 enum Question {
3863     Yes,
3864     No,
3865 }
3866
3867 // We implement the `Not` trait on the enum.
3868 impl Not for Question {
3869     type Output = bool;
3870
3871     fn not(self) -> bool {
3872         match self {
3873             Question::Yes => false, // If the `Answer` is `Yes`, then it
3874                                     // returns false.
3875             Question::No => true, // And here we do the opposite.
3876         }
3877     }
3878 }
3879
3880 assert_eq!(!Question::Yes, false);
3881 assert_eq!(!Question::No, true);
3882 ```
3883 "##,
3884
3885 E0608: r##"
3886 An attempt to index into a type which doesn't implement the `std::ops::Index`
3887 trait was performed.
3888
3889 Erroneous code example:
3890
3891 ```compile_fail,E0608
3892 0u8[2]; // error: cannot index into a value of type `u8`
3893 ```
3894
3895 To be able to index into a type it needs to implement the `std::ops::Index`
3896 trait. Example:
3897
3898 ```
3899 let v: Vec<u8> = vec![0, 1, 2, 3];
3900
3901 // The `Vec` type implements the `Index` trait so you can do:
3902 println!("{}", v[2]);
3903 ```
3904 "##,
3905
3906 E0604: r##"
3907 A cast to `char` was attempted on a type other than `u8`.
3908
3909 Erroneous code example:
3910
3911 ```compile_fail,E0604
3912 0u32 as char; // error: only `u8` can be cast as `char`, not `u32`
3913 ```
3914
3915 As the error message indicates, only `u8` can be cast into `char`. Example:
3916
3917 ```
3918 let c = 86u8 as char; // ok!
3919 assert_eq!(c, 'V');
3920 ```
3921
3922 For more information about casts, take a look at the Type cast section in
3923 [The Reference Book][1].
3924
3925 [1]: https://doc.rust-lang.org/reference/expressions/operator-expr.html#type-cast-expressions
3926 "##,
3927
3928 E0605: r##"
3929 An invalid cast was attempted.
3930
3931 Erroneous code examples:
3932
3933 ```compile_fail,E0605
3934 let x = 0u8;
3935 x as Vec<u8>; // error: non-primitive cast: `u8` as `std::vec::Vec<u8>`
3936
3937 // Another example
3938
3939 let v = core::ptr::null::<u8>(); // So here, `v` is a `*const u8`.
3940 v as &u8; // error: non-primitive cast: `*const u8` as `&u8`
3941 ```
3942
3943 Only primitive types can be cast into each other. Examples:
3944
3945 ```
3946 let x = 0u8;
3947 x as u32; // ok!
3948
3949 let v = core::ptr::null::<u8>();
3950 v as *const i8; // ok!
3951 ```
3952
3953 For more information about casts, take a look at the Type cast section in
3954 [The Reference Book][1].
3955
3956 [1]: https://doc.rust-lang.org/reference/expressions/operator-expr.html#type-cast-expressions
3957 "##,
3958
3959 E0606: r##"
3960 An incompatible cast was attempted.
3961
3962 Erroneous code example:
3963
3964 ```compile_fail,E0606
3965 let x = &0u8; // Here, `x` is a `&u8`.
3966 let y: u32 = x as u32; // error: casting `&u8` as `u32` is invalid
3967 ```
3968
3969 When casting, keep in mind that only primitive types can be cast into each
3970 other. Example:
3971
3972 ```
3973 let x = &0u8;
3974 let y: u32 = *x as u32; // We dereference it first and then cast it.
3975 ```
3976
3977 For more information about casts, take a look at the Type cast section in
3978 [The Reference Book][1].
3979
3980 [1]: https://doc.rust-lang.org/reference/expressions/operator-expr.html#type-cast-expressions
3981 "##,
3982
3983 E0607: r##"
3984 A cast between a thin and a fat pointer was attempted.
3985
3986 Erroneous code example:
3987
3988 ```compile_fail,E0607
3989 let v = core::ptr::null::<u8>();
3990 v as *const [u8];
3991 ```
3992
3993 First: what are thin and fat pointers?
3994
3995 Thin pointers are "simple" pointers: they are purely a reference to a memory
3996 address.
3997
3998 Fat pointers are pointers referencing Dynamically Sized Types (also called DST).
3999 DST don't have a statically known size, therefore they can only exist behind
4000 some kind of pointers that contain additional information. Slices and trait
4001 objects are DSTs. In the case of slices, the additional information the fat
4002 pointer holds is their size.
4003
4004 To fix this error, don't try to cast directly between thin and fat pointers.
4005
4006 For more information about casts, take a look at the Type cast section in
4007 [The Reference Book][1].
4008
4009 [1]: https://doc.rust-lang.org/reference/expressions/operator-expr.html#type-cast-expressions
4010 "##,
4011
4012 E0609: r##"
4013 Attempted to access a non-existent field in a struct.
4014
4015 Erroneous code example:
4016
4017 ```compile_fail,E0609
4018 struct StructWithFields {
4019     x: u32,
4020 }
4021
4022 let s = StructWithFields { x: 0 };
4023 println!("{}", s.foo); // error: no field `foo` on type `StructWithFields`
4024 ```
4025
4026 To fix this error, check that you didn't misspell the field's name or that the
4027 field actually exists. Example:
4028
4029 ```
4030 struct StructWithFields {
4031     x: u32,
4032 }
4033
4034 let s = StructWithFields { x: 0 };
4035 println!("{}", s.x); // ok!
4036 ```
4037 "##,
4038
4039 E0610: r##"
4040 Attempted to access a field on a primitive type.
4041
4042 Erroneous code example:
4043
4044 ```compile_fail,E0610
4045 let x: u32 = 0;
4046 println!("{}", x.foo); // error: `{integer}` is a primitive type, therefore
4047                        //        doesn't have fields
4048 ```
4049
4050 Primitive types are the most basic types available in Rust and don't have
4051 fields. To access data via named fields, struct types are used. Example:
4052
4053 ```
4054 // We declare struct called `Foo` containing two fields:
4055 struct Foo {
4056     x: u32,
4057     y: i64,
4058 }
4059
4060 // We create an instance of this struct:
4061 let variable = Foo { x: 0, y: -12 };
4062 // And we can now access its fields:
4063 println!("x: {}, y: {}", variable.x, variable.y);
4064 ```
4065
4066 For more information about primitives and structs, take a look at The Book:
4067 https://doc.rust-lang.org/book/ch03-02-data-types.html
4068 https://doc.rust-lang.org/book/ch05-00-structs.html
4069 "##,
4070
4071 E0614: r##"
4072 Attempted to dereference a variable which cannot be dereferenced.
4073
4074 Erroneous code example:
4075
4076 ```compile_fail,E0614
4077 let y = 0u32;
4078 *y; // error: type `u32` cannot be dereferenced
4079 ```
4080
4081 Only types implementing `std::ops::Deref` can be dereferenced (such as `&T`).
4082 Example:
4083
4084 ```
4085 let y = 0u32;
4086 let x = &y;
4087 // So here, `x` is a `&u32`, so we can dereference it:
4088 *x; // ok!
4089 ```
4090 "##,
4091
4092 E0615: r##"
4093 Attempted to access a method like a field.
4094
4095 Erroneous code example:
4096
4097 ```compile_fail,E0615
4098 struct Foo {
4099     x: u32,
4100 }
4101
4102 impl Foo {
4103     fn method(&self) {}
4104 }
4105
4106 let f = Foo { x: 0 };
4107 f.method; // error: attempted to take value of method `method` on type `Foo`
4108 ```
4109
4110 If you want to use a method, add `()` after it:
4111
4112 ```
4113 # struct Foo { x: u32 }
4114 # impl Foo { fn method(&self) {} }
4115 # let f = Foo { x: 0 };
4116 f.method();
4117 ```
4118
4119 However, if you wanted to access a field of a struct check that the field name
4120 is spelled correctly. Example:
4121
4122 ```
4123 # struct Foo { x: u32 }
4124 # impl Foo { fn method(&self) {} }
4125 # let f = Foo { x: 0 };
4126 println!("{}", f.x);
4127 ```
4128 "##,
4129
4130 E0616: r##"
4131 Attempted to access a private field on a struct.
4132
4133 Erroneous code example:
4134
4135 ```compile_fail,E0616
4136 mod some_module {
4137     pub struct Foo {
4138         x: u32, // So `x` is private in here.
4139     }
4140
4141     impl Foo {
4142         pub fn new() -> Foo { Foo { x: 0 } }
4143     }
4144 }
4145
4146 let f = some_module::Foo::new();
4147 println!("{}", f.x); // error: field `x` of struct `some_module::Foo` is private
4148 ```
4149
4150 If you want to access this field, you have two options:
4151
4152 1) Set the field public:
4153
4154 ```
4155 mod some_module {
4156     pub struct Foo {
4157         pub x: u32, // `x` is now public.
4158     }
4159
4160     impl Foo {
4161         pub fn new() -> Foo { Foo { x: 0 } }
4162     }
4163 }
4164
4165 let f = some_module::Foo::new();
4166 println!("{}", f.x); // ok!
4167 ```
4168
4169 2) Add a getter function:
4170
4171 ```
4172 mod some_module {
4173     pub struct Foo {
4174         x: u32, // So `x` is still private in here.
4175     }
4176
4177     impl Foo {
4178         pub fn new() -> Foo { Foo { x: 0 } }
4179
4180         // We create the getter function here:
4181         pub fn get_x(&self) -> &u32 { &self.x }
4182     }
4183 }
4184
4185 let f = some_module::Foo::new();
4186 println!("{}", f.get_x()); // ok!
4187 ```
4188 "##,
4189
4190 E0617: r##"
4191 Attempted to pass an invalid type of variable into a variadic function.
4192
4193 Erroneous code example:
4194
4195 ```compile_fail,E0617
4196 extern {
4197     fn printf(c: *const i8, ...);
4198 }
4199
4200 unsafe {
4201     printf(::std::ptr::null(), 0f32);
4202     // error: can't pass an `f32` to variadic function, cast to `c_double`
4203 }
4204 ```
4205
4206 Certain Rust types must be cast before passing them to a variadic function,
4207 because of arcane ABI rules dictated by the C standard. To fix the error,
4208 cast the value to the type specified by the error message (which you may need
4209 to import from `std::os::raw`).
4210 "##,
4211
4212 E0618: r##"
4213 Attempted to call something which isn't a function nor a method.
4214
4215 Erroneous code examples:
4216
4217 ```compile_fail,E0618
4218 enum X {
4219     Entry,
4220 }
4221
4222 X::Entry(); // error: expected function, found `X::Entry`
4223
4224 // Or even simpler:
4225 let x = 0i32;
4226 x(); // error: expected function, found `i32`
4227 ```
4228
4229 Only functions and methods can be called using `()`. Example:
4230
4231 ```
4232 // We declare a function:
4233 fn i_am_a_function() {}
4234
4235 // And we call it:
4236 i_am_a_function();
4237 ```
4238 "##,
4239
4240 E0619: r##"
4241 #### Note: this error code is no longer emitted by the compiler.
4242 The type-checker needed to know the type of an expression, but that type had not
4243 yet been inferred.
4244
4245 Erroneous code example:
4246
4247 ```compile_fail
4248 let mut x = vec![];
4249 match x.pop() {
4250     Some(v) => {
4251         // Here, the type of `v` is not (yet) known, so we
4252         // cannot resolve this method call:
4253         v.to_uppercase(); // error: the type of this value must be known in
4254                           //        this context
4255     }
4256     None => {}
4257 }
4258 ```
4259
4260 Type inference typically proceeds from the top of the function to the bottom,
4261 figuring out types as it goes. In some cases -- notably method calls and
4262 overloadable operators like `*` -- the type checker may not have enough
4263 information *yet* to make progress. This can be true even if the rest of the
4264 function provides enough context (because the type-checker hasn't looked that
4265 far ahead yet). In this case, type annotations can be used to help it along.
4266
4267 To fix this error, just specify the type of the variable. Example:
4268
4269 ```
4270 let mut x: Vec<String> = vec![]; // We precise the type of the vec elements.
4271 match x.pop() {
4272     Some(v) => {
4273         v.to_uppercase(); // Since rustc now knows the type of the vec elements,
4274                           // we can use `v`'s methods.
4275     }
4276     None => {}
4277 }
4278 ```
4279 "##,
4280
4281 E0620: r##"
4282 A cast to an unsized type was attempted.
4283
4284 Erroneous code example:
4285
4286 ```compile_fail,E0620
4287 let x = &[1_usize, 2] as [usize]; // error: cast to unsized type: `&[usize; 2]`
4288                                   //        as `[usize]`
4289 ```
4290
4291 In Rust, some types don't have a known size at compile-time. For example, in a
4292 slice type like `[u32]`, the number of elements is not known at compile-time and
4293 hence the overall size cannot be computed. As a result, such types can only be
4294 manipulated through a reference (e.g., `&T` or `&mut T`) or other pointer-type
4295 (e.g., `Box` or `Rc`). Try casting to a reference instead:
4296
4297 ```
4298 let x = &[1_usize, 2] as &[usize]; // ok!
4299 ```
4300 "##,
4301
4302 E0622: r##"
4303 An intrinsic was declared without being a function.
4304
4305 Erroneous code example:
4306
4307 ```compile_fail,E0622
4308 #![feature(intrinsics)]
4309 extern "rust-intrinsic" {
4310     pub static breakpoint : unsafe extern "rust-intrinsic" fn();
4311     // error: intrinsic must be a function
4312 }
4313
4314 fn main() { unsafe { breakpoint(); } }
4315 ```
4316
4317 An intrinsic is a function available for use in a given programming language
4318 whose implementation is handled specially by the compiler. In order to fix this
4319 error, just declare a function.
4320 "##,
4321
4322 E0624: r##"
4323 A private item was used outside of its scope.
4324
4325 Erroneous code example:
4326
4327 ```compile_fail,E0624
4328 mod inner {
4329     pub struct Foo;
4330
4331     impl Foo {
4332         fn method(&self) {}
4333     }
4334 }
4335
4336 let foo = inner::Foo;
4337 foo.method(); // error: method `method` is private
4338 ```
4339
4340 Two possibilities are available to solve this issue:
4341
4342 1. Only use the item in the scope it has been defined:
4343
4344 ```
4345 mod inner {
4346     pub struct Foo;
4347
4348     impl Foo {
4349         fn method(&self) {}
4350     }
4351
4352     pub fn call_method(foo: &Foo) { // We create a public function.
4353         foo.method(); // Which calls the item.
4354     }
4355 }
4356
4357 let foo = inner::Foo;
4358 inner::call_method(&foo); // And since the function is public, we can call the
4359                           // method through it.
4360 ```
4361
4362 2. Make the item public:
4363
4364 ```
4365 mod inner {
4366     pub struct Foo;
4367
4368     impl Foo {
4369         pub fn method(&self) {} // It's now public.
4370     }
4371 }
4372
4373 let foo = inner::Foo;
4374 foo.method(); // Ok!
4375 ```
4376 "##,
4377
4378 E0638: r##"
4379 This error indicates that the struct, enum or enum variant must be matched
4380 non-exhaustively as it has been marked as `non_exhaustive`.
4381
4382 When applied within a crate, downstream users of the crate will need to use the
4383 `_` pattern when matching enums and use the `..` pattern when matching structs.
4384 Downstream crates cannot match against non-exhaustive enum variants.
4385
4386 For example, in the below example, since the enum is marked as
4387 `non_exhaustive`, it is required that downstream crates match non-exhaustively
4388 on it.
4389
4390 ```rust,ignore (pseudo-Rust)
4391 use std::error::Error as StdError;
4392
4393 #[non_exhaustive] pub enum Error {
4394    Message(String),
4395    Other,
4396 }
4397
4398 impl StdError for Error {
4399    fn description(&self) -> &str {
4400         // This will not error, despite being marked as non_exhaustive, as this
4401         // enum is defined within the current crate, it can be matched
4402         // exhaustively.
4403         match *self {
4404            Message(ref s) => s,
4405            Other => "other or unknown error",
4406         }
4407    }
4408 }
4409 ```
4410
4411 An example of matching non-exhaustively on the above enum is provided below:
4412
4413 ```rust,ignore (pseudo-Rust)
4414 use mycrate::Error;
4415
4416 // This will not error as the non_exhaustive Error enum has been matched with a
4417 // wildcard.
4418 match error {
4419    Message(ref s) => ...,
4420    Other => ...,
4421    _ => ...,
4422 }
4423 ```
4424
4425 Similarly, for structs, match with `..` to avoid this error.
4426 "##,
4427
4428 E0639: r##"
4429 This error indicates that the struct, enum or enum variant cannot be
4430 instantiated from outside of the defining crate as it has been marked
4431 as `non_exhaustive` and as such more fields/variants may be added in
4432 future that could cause adverse side effects for this code.
4433
4434 It is recommended that you look for a `new` function or equivalent in the
4435 crate's documentation.
4436 "##,
4437
4438 E0643: r##"
4439 This error indicates that there is a mismatch between generic parameters and
4440 impl Trait parameters in a trait declaration versus its impl.
4441
4442 ```compile_fail,E0643
4443 trait Foo {
4444     fn foo(&self, _: &impl Iterator);
4445 }
4446 impl Foo for () {
4447     fn foo<U: Iterator>(&self, _: &U) { } // error method `foo` has incompatible
4448                                           // signature for trait
4449 }
4450 ```
4451 "##,
4452
4453 E0646: r##"
4454 It is not possible to define `main` with a where clause.
4455 Erroneous code example:
4456
4457 ```compile_fail,E0646
4458 fn main() where i32: Copy { // error: main function is not allowed to have
4459                             // a where clause
4460 }
4461 ```
4462 "##,
4463
4464 E0647: r##"
4465 It is not possible to define `start` with a where clause.
4466 Erroneous code example:
4467
4468 ```compile_fail,E0647
4469 #![feature(start)]
4470
4471 #[start]
4472 fn start(_: isize, _: *const *const u8) -> isize where (): Copy {
4473     //^ error: start function is not allowed to have a where clause
4474     0
4475 }
4476 ```
4477 "##,
4478
4479 E0648: r##"
4480 `export_name` attributes may not contain null characters (`\0`).
4481
4482 ```compile_fail,E0648
4483 #[export_name="\0foo"] // error: `export_name` may not contain null characters
4484 pub fn bar() {}
4485 ```
4486 "##,
4487
4488 E0689: r##"
4489 This error indicates that the numeric value for the method being passed exists
4490 but the type of the numeric value or binding could not be identified.
4491
4492 The error happens on numeric literals:
4493
4494 ```compile_fail,E0689
4495 2.0.neg();
4496 ```
4497
4498 and on numeric bindings without an identified concrete type:
4499
4500 ```compile_fail,E0689
4501 let x = 2.0;
4502 x.neg();  // same error as above
4503 ```
4504
4505 Because of this, you must give the numeric literal or binding a type:
4506
4507 ```
4508 use std::ops::Neg;
4509
4510 let _ = 2.0_f32.neg();
4511 let x: f32 = 2.0;
4512 let _ = x.neg();
4513 let _ = (2.0 as f32).neg();
4514 ```
4515 "##,
4516
4517 E0690: r##"
4518 A struct with the representation hint `repr(transparent)` had zero or more than
4519 one fields that were not guaranteed to be zero-sized.
4520
4521 Erroneous code example:
4522
4523 ```compile_fail,E0690
4524 #[repr(transparent)]
4525 struct LengthWithUnit<U> { // error: transparent struct needs exactly one
4526     value: f32,            //        non-zero-sized field, but has 2
4527     unit: U,
4528 }
4529 ```
4530
4531 Because transparent structs are represented exactly like one of their fields at
4532 run time, said field must be uniquely determined. If there is no field, or if
4533 there are multiple fields, it is not clear how the struct should be represented.
4534 Note that fields of zero-typed types (e.g., `PhantomData`) can also exist
4535 alongside the field that contains the actual data, they do not count for this
4536 error. When generic types are involved (as in the above example), an error is
4537 reported because the type parameter could be non-zero-sized.
4538
4539 To combine `repr(transparent)` with type parameters, `PhantomData` may be
4540 useful:
4541
4542 ```
4543 use std::marker::PhantomData;
4544
4545 #[repr(transparent)]
4546 struct LengthWithUnit<U> {
4547     value: f32,
4548     unit: PhantomData<U>,
4549 }
4550 ```
4551 "##,
4552
4553 E0691: r##"
4554 A struct, enum, or union with the `repr(transparent)` representation hint
4555 contains a zero-sized field that requires non-trivial alignment.
4556
4557 Erroneous code example:
4558
4559 ```compile_fail,E0691
4560 #![feature(repr_align)]
4561
4562 #[repr(align(32))]
4563 struct ForceAlign32;
4564
4565 #[repr(transparent)]
4566 struct Wrapper(f32, ForceAlign32); // error: zero-sized field in transparent
4567                                    //        struct has alignment larger than 1
4568 ```
4569
4570 A transparent struct, enum, or union is supposed to be represented exactly like
4571 the piece of data it contains. Zero-sized fields with different alignment
4572 requirements potentially conflict with this property. In the example above,
4573 `Wrapper` would have to be aligned to 32 bytes even though `f32` has a smaller
4574 alignment requirement.
4575
4576 Consider removing the over-aligned zero-sized field:
4577
4578 ```
4579 #[repr(transparent)]
4580 struct Wrapper(f32);
4581 ```
4582
4583 Alternatively, `PhantomData<T>` has alignment 1 for all `T`, so you can use it
4584 if you need to keep the field for some reason:
4585
4586 ```
4587 #![feature(repr_align)]
4588
4589 use std::marker::PhantomData;
4590
4591 #[repr(align(32))]
4592 struct ForceAlign32;
4593
4594 #[repr(transparent)]
4595 struct Wrapper(f32, PhantomData<ForceAlign32>);
4596 ```
4597
4598 Note that empty arrays `[T; 0]` have the same alignment requirement as the
4599 element type `T`. Also note that the error is conservatively reported even when
4600 the alignment of the zero-sized type is less than or equal to the data field's
4601 alignment.
4602 "##,
4603
4604 E0699: r##"
4605 A method was called on a raw pointer whose inner type wasn't completely known.
4606
4607 For example, you may have done something like:
4608
4609 ```compile_fail
4610 # #![deny(warnings)]
4611 let foo = &1;
4612 let bar = foo as *const _;
4613 if bar.is_null() {
4614     // ...
4615 }
4616 ```
4617
4618 Here, the type of `bar` isn't known; it could be a pointer to anything. Instead,
4619 specify a type for the pointer (preferably something that makes sense for the
4620 thing you're pointing to):
4621
4622 ```
4623 let foo = &1;
4624 let bar = foo as *const i32;
4625 if bar.is_null() {
4626     // ...
4627 }
4628 ```
4629
4630 Even though `is_null()` exists as a method on any raw pointer, Rust shows this
4631 error because  Rust allows for `self` to have arbitrary types (behind the
4632 arbitrary_self_types feature flag).
4633
4634 This means that someone can specify such a function:
4635
4636 ```ignore (cannot-doctest-feature-doesnt-exist-yet)
4637 impl Foo {
4638     fn is_null(self: *const Self) -> bool {
4639         // do something else
4640     }
4641 }
4642 ```
4643
4644 and now when you call `.is_null()` on a raw pointer to `Foo`, there's ambiguity.
4645
4646 Given that we don't know what type the pointer is, and there's potential
4647 ambiguity for some types, we disallow calling methods on raw pointers when
4648 the type is unknown.
4649 "##,
4650
4651 E0714: r##"
4652 A `#[marker]` trait contained an associated item.
4653
4654 The items of marker traits cannot be overridden, so there's no need to have them
4655 when they cannot be changed per-type anyway.  If you wanted them for ergonomic
4656 reasons, consider making an extension trait instead.
4657 "##,
4658
4659 E0715: r##"
4660 An `impl` for a `#[marker]` trait tried to override an associated item.
4661
4662 Because marker traits are allowed to have multiple implementations for the same
4663 type, it's not allowed to override anything in those implementations, as it
4664 would be ambiguous which override should actually be used.
4665 "##,
4666
4667
4668 E0720: r##"
4669 An `impl Trait` type expands to a recursive type.
4670
4671 An `impl Trait` type must be expandable to a concrete type that contains no
4672 `impl Trait` types. For example the following example tries to create an
4673 `impl Trait` type `T` that is equal to `[T, T]`:
4674
4675 ```compile_fail,E0720
4676 fn make_recursive_type() -> impl Sized {
4677     [make_recursive_type(), make_recursive_type()]
4678 }
4679 ```
4680 "##,
4681
4682 E0730: r##"
4683 An array without a fixed length was pattern-matched.
4684
4685 Example of erroneous code:
4686
4687 ```compile_fail,E0730
4688 #![feature(const_generics)]
4689
4690 fn is_123<const N: usize>(x: [u32; N]) -> bool {
4691     match x {
4692         [1, 2, 3] => true, // error: cannot pattern-match on an
4693                            //        array without a fixed length
4694         _ => false
4695     }
4696 }
4697 ```
4698
4699 Ensure that the pattern is consistent with the size of the matched
4700 array. Additional elements can be matched with `..`:
4701
4702 ```
4703 #![feature(slice_patterns)]
4704
4705 let r = &[1, 2, 3, 4];
4706 match r {
4707     &[a, b, ..] => { // ok!
4708         println!("a={}, b={}", a, b);
4709     }
4710 }
4711 ```
4712 "##,
4713
4714 E0731: r##"
4715 An enum with the representation hint `repr(transparent)` had zero or more than
4716 one variants.
4717
4718 Erroneous code example:
4719
4720 ```compile_fail,E0731
4721 #[repr(transparent)]
4722 enum Status { // error: transparent enum needs exactly one variant, but has 2
4723     Errno(u32),
4724     Ok,
4725 }
4726 ```
4727
4728 Because transparent enums are represented exactly like one of their variants at
4729 run time, said variant must be uniquely determined. If there is no variant, or
4730 if there are multiple variants, it is not clear how the enum should be
4731 represented.
4732 "##,
4733
4734 E0732: r##"
4735 An `enum` with a discriminant must specify a `#[repr(inttype)]`.
4736
4737 A `#[repr(inttype)]` must be provided on an `enum` if it has a non-unit
4738 variant with a discriminant, or where there are both unit variants with
4739 discriminants and non-unit variants. This restriction ensures that there
4740 is a well-defined way to extract a variant's discriminant from a value;
4741 for instance:
4742
4743 ```
4744 #![feature(arbitrary_enum_discriminant)]
4745
4746 #[repr(u8)]
4747 enum Enum {
4748     Unit = 3,
4749     Tuple(u16) = 2,
4750     Struct {
4751         a: u8,
4752         b: u16,
4753     } = 1,
4754 }
4755
4756 fn discriminant(v : &Enum) -> u8 {
4757     unsafe { *(v as *const Enum as *const u8) }
4758 }
4759
4760 assert_eq!(3, discriminant(&Enum::Unit));
4761 assert_eq!(2, discriminant(&Enum::Tuple(5)));
4762 assert_eq!(1, discriminant(&Enum::Struct{a: 7, b: 11}));
4763 ```
4764 "##,
4765
4766 E0733: r##"
4767 Recursion in an `async fn` requires boxing. For example, this will not compile:
4768
4769 ```edition2018,compile_fail,E0733
4770 #![feature(async_await)]
4771 async fn foo(n: usize) {
4772     if n > 0 {
4773         foo(n - 1).await;
4774     }
4775 }
4776 ```
4777
4778 To achieve async recursion, the `async fn` needs to be desugared
4779 such that the `Future` is explicit in the return type:
4780
4781 ```edition2018,compile_fail,E0720
4782 # #![feature(async_await)]
4783 use std::future::Future;
4784 fn foo_desugered(n: usize) -> impl Future<Output = ()> {
4785     async move {
4786         if n > 0 {
4787             foo_desugered(n - 1).await;
4788         }
4789     }
4790 }
4791 ```
4792
4793 Finally, the future is wrapped in a pinned box:
4794
4795 ```edition2018
4796 # #![feature(async_await)]
4797 use std::future::Future;
4798 use std::pin::Pin;
4799 fn foo_recursive(n: usize) -> Pin<Box<dyn Future<Output = ()>>> {
4800     Box::pin(async move {
4801         if n > 0 {
4802             foo_recursive(n - 1).await;
4803         }
4804     })
4805 }
4806 ```
4807
4808 The `Box<...>` ensures that the result is of known size,
4809 and the pin is required to keep it in the same place in memory.
4810 "##,
4811
4812 }  // (end of detailed error messages)
4813
4814 register_diagnostics! {
4815 //  E0035, merged into E0087/E0089
4816 //  E0036, merged into E0087/E0089
4817 //  E0068,
4818 //  E0085,
4819 //  E0086,
4820 //  E0103,
4821 //  E0104,
4822 //  E0122, // bounds in type aliases are ignored, turned into proper lint
4823 //  E0123,
4824 //  E0127,
4825 //  E0129,
4826 //  E0141,
4827 //  E0159, // use of trait `{}` as struct constructor
4828 //  E0163, // merged into E0071
4829 //  E0167,
4830 //  E0168,
4831 //  E0172, // non-trait found in a type sum, moved to resolve
4832 //  E0173, // manual implementations of unboxed closure traits are experimental
4833 //  E0174,
4834 //  E0182, // merged into E0229
4835     E0183,
4836 //  E0187, // can't infer the kind of the closure
4837 //  E0188, // can not cast an immutable reference to a mutable pointer
4838 //  E0189, // deprecated: can only cast a boxed pointer to a boxed object
4839 //  E0190, // deprecated: can only cast a &-pointer to an &-object
4840 //  E0196, // cannot determine a type for this closure
4841     E0203, // type parameter has more than one relaxed default bound,
4842            // and only one is supported
4843     E0208,
4844 //  E0209, // builtin traits can only be implemented on structs or enums
4845     E0212, // cannot extract an associated type from a higher-ranked trait bound
4846 //  E0213, // associated types are not accepted in this context
4847 //  E0215, // angle-bracket notation is not stable with `Fn`
4848 //  E0216, // parenthetical notation is only stable with `Fn`
4849 //  E0217, // ambiguous associated type, defined in multiple supertraits
4850 //  E0218, // no associated type defined
4851 //  E0219, // associated type defined in higher-ranked supertrait
4852 //  E0222, // Error code E0045 (variadic function must have C or cdecl calling
4853            // convention) duplicate
4854     E0224, // at least one non-builtin train is required for an object type
4855     E0227, // ambiguous lifetime bound, explicit lifetime bound required
4856     E0228, // explicit lifetime bound required
4857 //  E0233,
4858 //  E0234,
4859 //  E0235, // structure constructor specifies a structure of type but
4860 //  E0236, // no lang item for range syntax
4861 //  E0237, // no lang item for range syntax
4862 //  E0238, // parenthesized parameters may only be used with a trait
4863 //  E0239, // `next` method of `Iterator` trait has unexpected type
4864 //  E0240,
4865 //  E0241,
4866 //  E0242,
4867 //  E0245, // not a trait
4868 //  E0246, // invalid recursive type
4869 //  E0247,
4870 //  E0248, // value used as a type, now reported earlier during resolution as E0412
4871 //  E0249,
4872     E0307, // invalid method `self` type
4873 //  E0319, // trait impls for defaulted traits allowed just for structs/enums
4874 //  E0372, // coherence not object safe
4875     E0377, // the trait `CoerceUnsized` may only be implemented for a coercion
4876            // between structures with the same definition
4877 //  E0558, // replaced with a generic attribute input check
4878     E0533, // `{}` does not name a unit variant, unit struct or a constant
4879 //  E0563, // cannot determine a type for this `impl Trait`: {} // removed in 6383de15
4880     E0564, // only named lifetimes are allowed in `impl Trait`,
4881            // but `{}` was found in the type `{}`
4882     E0587, // type has conflicting packed and align representation hints
4883     E0588, // packed type cannot transitively contain a `[repr(align)]` type
4884 //  E0611, // merged into E0616
4885 //  E0612, // merged into E0609
4886 //  E0613, // Removed (merged with E0609)
4887     E0627, // yield statement outside of generator literal
4888     E0632, // cannot provide explicit type parameters when `impl Trait` is used in
4889            // argument position.
4890     E0634, // type has conflicting packed representaton hints
4891     E0640, // infer outlives requirements
4892     E0641, // cannot cast to/from a pointer with an unknown kind
4893     E0645, // trait aliases not finished
4894     E0719, // duplicate values for associated type binding
4895     E0722, // Malformed `#[optimize]` attribute
4896     E0724, // `#[ffi_returns_twice]` is only allowed in foreign functions
4897 }