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