]> git.lizzy.rs Git - rust.git/blob - src/librustc_typeck/error_codes.rs
Rollup merge of #60256 - ethanboxx:master, r=alexcrichton
[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, type
1486 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 self type of the impl
1946  - for a trait impl, it appears in the trait reference
1947  - it is bound as an associated type
1948
1949 ### Error example 1
1950
1951 Suppose we have a struct `Foo` and we would like to define some methods for it.
1952 The following definition leads to a compiler error:
1953
1954 ```compile_fail,E0207
1955 struct Foo;
1956
1957 impl<T: Default> Foo {
1958 // error: the type parameter `T` is not constrained by the impl trait, self
1959 // type, or predicates [E0207]
1960     fn get(&self) -> T {
1961         <T as Default>::default()
1962     }
1963 }
1964 ```
1965
1966 The problem is that the parameter `T` does not appear in the self type (`Foo`)
1967 of the impl. In this case, we can fix the error by moving the type parameter
1968 from the `impl` to the method `get`:
1969
1970
1971 ```
1972 struct Foo;
1973
1974 // Move the type parameter from the impl to the method
1975 impl Foo {
1976     fn get<T: Default>(&self) -> T {
1977         <T as Default>::default()
1978     }
1979 }
1980 ```
1981
1982 ### Error example 2
1983
1984 As another example, suppose we have a `Maker` trait and want to establish a
1985 type `FooMaker` that makes `Foo`s:
1986
1987 ```compile_fail,E0207
1988 trait Maker {
1989     type Item;
1990     fn make(&mut self) -> Self::Item;
1991 }
1992
1993 struct Foo<T> {
1994     foo: T
1995 }
1996
1997 struct FooMaker;
1998
1999 impl<T: Default> Maker for FooMaker {
2000 // error: the type parameter `T` is not constrained by the impl trait, self
2001 // type, or predicates [E0207]
2002     type Item = Foo<T>;
2003
2004     fn make(&mut self) -> Foo<T> {
2005         Foo { foo: <T as Default>::default() }
2006     }
2007 }
2008 ```
2009
2010 This fails to compile because `T` does not appear in the trait or in the
2011 implementing type.
2012
2013 One way to work around this is to introduce a phantom type parameter into
2014 `FooMaker`, like so:
2015
2016 ```
2017 use std::marker::PhantomData;
2018
2019 trait Maker {
2020     type Item;
2021     fn make(&mut self) -> Self::Item;
2022 }
2023
2024 struct Foo<T> {
2025     foo: T
2026 }
2027
2028 // Add a type parameter to `FooMaker`
2029 struct FooMaker<T> {
2030     phantom: PhantomData<T>,
2031 }
2032
2033 impl<T: Default> Maker for FooMaker<T> {
2034     type Item = Foo<T>;
2035
2036     fn make(&mut self) -> Foo<T> {
2037         Foo {
2038             foo: <T as Default>::default(),
2039         }
2040     }
2041 }
2042 ```
2043
2044 Another way is to do away with the associated type in `Maker` and use an input
2045 type parameter instead:
2046
2047 ```
2048 // Use a type parameter instead of an associated type here
2049 trait Maker<Item> {
2050     fn make(&mut self) -> Item;
2051 }
2052
2053 struct Foo<T> {
2054     foo: T
2055 }
2056
2057 struct FooMaker;
2058
2059 impl<T: Default> Maker<Foo<T>> for FooMaker {
2060     fn make(&mut self) -> Foo<T> {
2061         Foo { foo: <T as Default>::default() }
2062     }
2063 }
2064 ```
2065
2066 ### Additional information
2067
2068 For more information, please see [RFC 447].
2069
2070 [RFC 447]: https://github.com/rust-lang/rfcs/blob/master/text/0447-no-unused-impl-parameters.md
2071 "##,
2072
2073 E0210: r##"
2074 This error indicates a violation of one of Rust's orphan rules for trait
2075 implementations. The rule concerns the use of type parameters in an
2076 implementation of a foreign trait (a trait defined in another crate), and
2077 states that type parameters must be "covered" by a local type. To understand
2078 what this means, it is perhaps easiest to consider a few examples.
2079
2080 If `ForeignTrait` is a trait defined in some external crate `foo`, then the
2081 following trait `impl` is an error:
2082
2083 ```compile_fail,E0210
2084 # #[cfg(for_demonstration_only)]
2085 extern crate foo;
2086 # #[cfg(for_demonstration_only)]
2087 use foo::ForeignTrait;
2088 # use std::panic::UnwindSafe as ForeignTrait;
2089
2090 impl<T> ForeignTrait for T { } // error
2091 # fn main() {}
2092 ```
2093
2094 To work around this, it can be covered with a local type, `MyType`:
2095
2096 ```
2097 # use std::panic::UnwindSafe as ForeignTrait;
2098 struct MyType<T>(T);
2099 impl<T> ForeignTrait for MyType<T> { } // Ok
2100 ```
2101
2102 Please note that a type alias is not sufficient.
2103
2104 For another example of an error, suppose there's another trait defined in `foo`
2105 named `ForeignTrait2` that takes two type parameters. Then this `impl` results
2106 in the same rule violation:
2107
2108 ```ignore (cannot-doctest-multicrate-project)
2109 struct MyType2;
2110 impl<T> ForeignTrait2<T, MyType<T>> for MyType2 { } // error
2111 ```
2112
2113 The reason for this is that there are two appearances of type parameter `T` in
2114 the `impl` header, both as parameters for `ForeignTrait2`. The first appearance
2115 is uncovered, and so runs afoul of the orphan rule.
2116
2117 Consider one more example:
2118
2119 ```ignore (cannot-doctest-multicrate-project)
2120 impl<T> ForeignTrait2<MyType<T>, T> for MyType2 { } // Ok
2121 ```
2122
2123 This only differs from the previous `impl` in that the parameters `T` and
2124 `MyType<T>` for `ForeignTrait2` have been swapped. This example does *not*
2125 violate the orphan rule; it is permitted.
2126
2127 To see why that last example was allowed, you need to understand the general
2128 rule. Unfortunately this rule is a bit tricky to state. Consider an `impl`:
2129
2130 ```ignore (only-for-syntax-highlight)
2131 impl<P1, ..., Pm> ForeignTrait<T1, ..., Tn> for T0 { ... }
2132 ```
2133
2134 where `P1, ..., Pm` are the type parameters of the `impl` and `T0, ..., Tn`
2135 are types. One of the types `T0, ..., Tn` must be a local type (this is another
2136 orphan rule, see the explanation for E0117). Let `i` be the smallest integer
2137 such that `Ti` is a local type. Then no type parameter can appear in any of the
2138 `Tj` for `j < i`.
2139
2140 For information on the design of the orphan rules, see [RFC 1023].
2141
2142 [RFC 1023]: https://github.com/rust-lang/rfcs/blob/master/text/1023-rebalancing-coherence.md
2143 "##,
2144
2145 /*
2146 E0211: r##"
2147 You used a function or type which doesn't fit the requirements for where it was
2148 used. Erroneous code examples:
2149
2150 ```compile_fail
2151 #![feature(intrinsics)]
2152
2153 extern "rust-intrinsic" {
2154     fn size_of<T>(); // error: intrinsic has wrong type
2155 }
2156
2157 // or:
2158
2159 fn main() -> i32 { 0 }
2160 // error: main function expects type: `fn() {main}`: expected (), found i32
2161
2162 // or:
2163
2164 let x = 1u8;
2165 match x {
2166     0u8..=3i8 => (),
2167     // error: mismatched types in range: expected u8, found i8
2168     _ => ()
2169 }
2170
2171 // or:
2172
2173 use std::rc::Rc;
2174 struct Foo;
2175
2176 impl Foo {
2177     fn x(self: Rc<Foo>) {}
2178     // error: mismatched self type: expected `Foo`: expected struct
2179     //        `Foo`, found struct `alloc::rc::Rc`
2180 }
2181 ```
2182
2183 For the first code example, please check the function definition. Example:
2184
2185 ```
2186 #![feature(intrinsics)]
2187
2188 extern "rust-intrinsic" {
2189     fn size_of<T>() -> usize; // ok!
2190 }
2191 ```
2192
2193 The second case example is a bit particular : the main function must always
2194 have this definition:
2195
2196 ```compile_fail
2197 fn main();
2198 ```
2199
2200 They never take parameters and never return types.
2201
2202 For the third example, when you match, all patterns must have the same type
2203 as the type you're matching on. Example:
2204
2205 ```
2206 let x = 1u8;
2207
2208 match x {
2209     0u8..=3u8 => (), // ok!
2210     _ => ()
2211 }
2212 ```
2213
2214 And finally, for the last example, only `Box<Self>`, `&Self`, `Self`,
2215 or `&mut Self` work as explicit self parameters. Example:
2216
2217 ```
2218 struct Foo;
2219
2220 impl Foo {
2221     fn x(self: Box<Foo>) {} // ok!
2222 }
2223 ```
2224 "##,
2225      */
2226
2227 E0220: r##"
2228 You used an associated type which isn't defined in the trait.
2229 Erroneous code example:
2230
2231 ```compile_fail,E0220
2232 trait T1 {
2233     type Bar;
2234 }
2235
2236 type Foo = T1<F=i32>; // error: associated type `F` not found for `T1`
2237
2238 // or:
2239
2240 trait T2 {
2241     type Bar;
2242
2243     // error: Baz is used but not declared
2244     fn return_bool(&self, _: &Self::Bar, _: &Self::Baz) -> bool;
2245 }
2246 ```
2247
2248 Make sure that you have defined the associated type in the trait body.
2249 Also, verify that you used the right trait or you didn't misspell the
2250 associated type name. Example:
2251
2252 ```
2253 trait T1 {
2254     type Bar;
2255 }
2256
2257 type Foo = T1<Bar=i32>; // ok!
2258
2259 // or:
2260
2261 trait T2 {
2262     type Bar;
2263     type Baz; // we declare `Baz` in our trait.
2264
2265     // and now we can use it here:
2266     fn return_bool(&self, _: &Self::Bar, _: &Self::Baz) -> bool;
2267 }
2268 ```
2269 "##,
2270
2271 E0221: r##"
2272 An attempt was made to retrieve an associated type, but the type was ambiguous.
2273 For example:
2274
2275 ```compile_fail,E0221
2276 trait T1 {}
2277 trait T2 {}
2278
2279 trait Foo {
2280     type A: T1;
2281 }
2282
2283 trait Bar : Foo {
2284     type A: T2;
2285     fn do_something() {
2286         let _: Self::A;
2287     }
2288 }
2289 ```
2290
2291 In this example, `Foo` defines an associated type `A`. `Bar` inherits that type
2292 from `Foo`, and defines another associated type of the same name. As a result,
2293 when we attempt to use `Self::A`, it's ambiguous whether we mean the `A` defined
2294 by `Foo` or the one defined by `Bar`.
2295
2296 There are two options to work around this issue. The first is simply to rename
2297 one of the types. Alternatively, one can specify the intended type using the
2298 following syntax:
2299
2300 ```
2301 trait T1 {}
2302 trait T2 {}
2303
2304 trait Foo {
2305     type A: T1;
2306 }
2307
2308 trait Bar : Foo {
2309     type A: T2;
2310     fn do_something() {
2311         let _: <Self as Bar>::A;
2312     }
2313 }
2314 ```
2315 "##,
2316
2317 E0223: r##"
2318 An attempt was made to retrieve an associated type, but the type was ambiguous.
2319 For example:
2320
2321 ```compile_fail,E0223
2322 trait MyTrait {type X; }
2323
2324 fn main() {
2325     let foo: MyTrait::X;
2326 }
2327 ```
2328
2329 The problem here is that we're attempting to take the type of X from MyTrait.
2330 Unfortunately, the type of X is not defined, because it's only made concrete in
2331 implementations of the trait. A working version of this code might look like:
2332
2333 ```
2334 trait MyTrait {type X; }
2335 struct MyStruct;
2336
2337 impl MyTrait for MyStruct {
2338     type X = u32;
2339 }
2340
2341 fn main() {
2342     let foo: <MyStruct as MyTrait>::X;
2343 }
2344 ```
2345
2346 This syntax specifies that we want the X type from MyTrait, as made concrete in
2347 MyStruct. The reason that we cannot simply use `MyStruct::X` is that MyStruct
2348 might implement two different traits with identically-named associated types.
2349 This syntax allows disambiguation between the two.
2350 "##,
2351
2352 E0225: r##"
2353 You attempted to use multiple types as bounds for a closure or trait object.
2354 Rust does not currently support this. A simple example that causes this error:
2355
2356 ```compile_fail,E0225
2357 fn main() {
2358     let _: Box<dyn std::io::Read + std::io::Write>;
2359 }
2360 ```
2361
2362 Auto traits such as Send and Sync are an exception to this rule:
2363 It's possible to have bounds of one non-builtin trait, plus any number of
2364 auto traits. For example, the following compiles correctly:
2365
2366 ```
2367 fn main() {
2368     let _: Box<dyn std::io::Read + Send + Sync>;
2369 }
2370 ```
2371 "##,
2372
2373 E0229: r##"
2374 An associated type binding was done outside of the type parameter declaration
2375 and `where` clause. Erroneous code example:
2376
2377 ```compile_fail,E0229
2378 pub trait Foo {
2379     type A;
2380     fn boo(&self) -> <Self as Foo>::A;
2381 }
2382
2383 struct Bar;
2384
2385 impl Foo for isize {
2386     type A = usize;
2387     fn boo(&self) -> usize { 42 }
2388 }
2389
2390 fn baz<I>(x: &<I as Foo<A=Bar>>::A) {}
2391 // error: associated type bindings are not allowed here
2392 ```
2393
2394 To solve this error, please move the type bindings in the type parameter
2395 declaration:
2396
2397 ```
2398 # struct Bar;
2399 # trait Foo { type A; }
2400 fn baz<I: Foo<A=Bar>>(x: &<I as Foo>::A) {} // ok!
2401 ```
2402
2403 Or in the `where` clause:
2404
2405 ```
2406 # struct Bar;
2407 # trait Foo { type A; }
2408 fn baz<I>(x: &<I as Foo>::A) where I: Foo<A=Bar> {}
2409 ```
2410 "##,
2411
2412 E0243: r##"
2413 #### Note: this error code is no longer emitted by the compiler.
2414
2415 This error indicates that not enough type parameters were found in a type or
2416 trait.
2417
2418 For example, the `Foo` struct below is defined to be generic in `T`, but the
2419 type parameter is missing in the definition of `Bar`:
2420
2421 ```compile_fail,E0107
2422 struct Foo<T> { x: T }
2423
2424 struct Bar { x: Foo }
2425 ```
2426 "##,
2427
2428 E0244: r##"
2429 #### Note: this error code is no longer emitted by the compiler.
2430
2431 This error indicates that too many type parameters were found in a type or
2432 trait.
2433
2434 For example, the `Foo` struct below has no type parameters, but is supplied
2435 with two in the definition of `Bar`:
2436
2437 ```compile_fail,E0107
2438 struct Foo { x: bool }
2439
2440 struct Bar<S, T> { x: Foo<S, T> }
2441 ```
2442 "##,
2443
2444 E0321: r##"
2445 A cross-crate opt-out trait was implemented on something which wasn't a struct
2446 or enum type. Erroneous code example:
2447
2448 ```compile_fail,E0321
2449 #![feature(optin_builtin_traits)]
2450
2451 struct Foo;
2452
2453 impl !Sync for Foo {}
2454
2455 unsafe impl Send for &'static Foo {}
2456 // error: cross-crate traits with a default impl, like `core::marker::Send`,
2457 //        can only be implemented for a struct/enum type, not
2458 //        `&'static Foo`
2459 ```
2460
2461 Only structs and enums are permitted to impl Send, Sync, and other opt-out
2462 trait, and the struct or enum must be local to the current crate. So, for
2463 example, `unsafe impl Send for Rc<Foo>` is not allowed.
2464 "##,
2465
2466 E0322: r##"
2467 The `Sized` trait is a special trait built-in to the compiler for types with a
2468 constant size known at compile-time. This trait is automatically implemented
2469 for types as needed by the compiler, and it is currently disallowed to
2470 explicitly implement it for a type.
2471 "##,
2472
2473 E0323: r##"
2474 An associated const was implemented when another trait item was expected.
2475 Erroneous code example:
2476
2477 ```compile_fail,E0323
2478 trait Foo {
2479     type N;
2480 }
2481
2482 struct Bar;
2483
2484 impl Foo for Bar {
2485     const N : u32 = 0;
2486     // error: item `N` is an associated const, which doesn't match its
2487     //        trait `<Bar as Foo>`
2488 }
2489 ```
2490
2491 Please verify that the associated const wasn't misspelled and the correct trait
2492 was implemented. Example:
2493
2494 ```
2495 struct Bar;
2496
2497 trait Foo {
2498     type N;
2499 }
2500
2501 impl Foo for Bar {
2502     type N = u32; // ok!
2503 }
2504 ```
2505
2506 Or:
2507
2508 ```
2509 struct Bar;
2510
2511 trait Foo {
2512     const N : u32;
2513 }
2514
2515 impl Foo for Bar {
2516     const N : u32 = 0; // ok!
2517 }
2518 ```
2519 "##,
2520
2521 E0324: r##"
2522 A method was implemented when another trait item was expected. Erroneous
2523 code example:
2524
2525 ```compile_fail,E0324
2526 struct Bar;
2527
2528 trait Foo {
2529     const N : u32;
2530
2531     fn M();
2532 }
2533
2534 impl Foo for Bar {
2535     fn N() {}
2536     // error: item `N` is an associated method, which doesn't match its
2537     //        trait `<Bar as Foo>`
2538 }
2539 ```
2540
2541 To fix this error, please verify that the method name wasn't misspelled and
2542 verify that you are indeed implementing the correct trait items. Example:
2543
2544 ```
2545 struct Bar;
2546
2547 trait Foo {
2548     const N : u32;
2549
2550     fn M();
2551 }
2552
2553 impl Foo for Bar {
2554     const N : u32 = 0;
2555
2556     fn M() {} // ok!
2557 }
2558 ```
2559 "##,
2560
2561 E0325: r##"
2562 An associated type was implemented when another trait item was expected.
2563 Erroneous code example:
2564
2565 ```compile_fail,E0325
2566 struct Bar;
2567
2568 trait Foo {
2569     const N : u32;
2570 }
2571
2572 impl Foo for Bar {
2573     type N = u32;
2574     // error: item `N` is an associated type, which doesn't match its
2575     //        trait `<Bar as Foo>`
2576 }
2577 ```
2578
2579 Please verify that the associated type name wasn't misspelled and your
2580 implementation corresponds to the trait definition. Example:
2581
2582 ```
2583 struct Bar;
2584
2585 trait Foo {
2586     type N;
2587 }
2588
2589 impl Foo for Bar {
2590     type N = u32; // ok!
2591 }
2592 ```
2593
2594 Or:
2595
2596 ```
2597 struct Bar;
2598
2599 trait Foo {
2600     const N : u32;
2601 }
2602
2603 impl Foo for Bar {
2604     const N : u32 = 0; // ok!
2605 }
2606 ```
2607 "##,
2608
2609 E0326: r##"
2610 The types of any associated constants in a trait implementation must match the
2611 types in the trait definition. This error indicates that there was a mismatch.
2612
2613 Here's an example of this error:
2614
2615 ```compile_fail,E0326
2616 trait Foo {
2617     const BAR: bool;
2618 }
2619
2620 struct Bar;
2621
2622 impl Foo for Bar {
2623     const BAR: u32 = 5; // error, expected bool, found u32
2624 }
2625 ```
2626 "##,
2627
2628 E0328: r##"
2629 The Unsize trait should not be implemented directly. All implementations of
2630 Unsize are provided automatically by the compiler.
2631
2632 Erroneous code example:
2633
2634 ```compile_fail,E0328
2635 #![feature(unsize)]
2636
2637 use std::marker::Unsize;
2638
2639 pub struct MyType;
2640
2641 impl<T> Unsize<T> for MyType {}
2642 ```
2643
2644 If you are defining your own smart pointer type and would like to enable
2645 conversion from a sized to an unsized type with the
2646 [DST coercion system][RFC 982], use [`CoerceUnsized`] instead.
2647
2648 ```
2649 #![feature(coerce_unsized)]
2650
2651 use std::ops::CoerceUnsized;
2652
2653 pub struct MyType<T: ?Sized> {
2654     field_with_unsized_type: T,
2655 }
2656
2657 impl<T, U> CoerceUnsized<MyType<U>> for MyType<T>
2658     where T: CoerceUnsized<U> {}
2659 ```
2660
2661 [RFC 982]: https://github.com/rust-lang/rfcs/blob/master/text/0982-dst-coercion.md
2662 [`CoerceUnsized`]: https://doc.rust-lang.org/std/ops/trait.CoerceUnsized.html
2663 "##,
2664
2665 /*
2666 // Associated consts can now be accessed through generic type parameters, and
2667 // this error is no longer emitted.
2668 //
2669 // FIXME: consider whether to leave it in the error index, or remove it entirely
2670 //        as associated consts is not stabilized yet.
2671
2672 E0329: r##"
2673 An attempt was made to access an associated constant through either a generic
2674 type parameter or `Self`. This is not supported yet. An example causing this
2675 error is shown below:
2676
2677 ```
2678 trait Foo {
2679     const BAR: f64;
2680 }
2681
2682 struct MyStruct;
2683
2684 impl Foo for MyStruct {
2685     const BAR: f64 = 0f64;
2686 }
2687
2688 fn get_bar_bad<F: Foo>(t: F) -> f64 {
2689     F::BAR
2690 }
2691 ```
2692
2693 Currently, the value of `BAR` for a particular type can only be accessed
2694 through a concrete type, as shown below:
2695
2696 ```
2697 trait Foo {
2698     const BAR: f64;
2699 }
2700
2701 struct MyStruct;
2702
2703 fn get_bar_good() -> f64 {
2704     <MyStruct as Foo>::BAR
2705 }
2706 ```
2707 "##,
2708 */
2709
2710 E0366: r##"
2711 An attempt was made to implement `Drop` on a concrete specialization of a
2712 generic type. An example is shown below:
2713
2714 ```compile_fail,E0366
2715 struct Foo<T> {
2716     t: T
2717 }
2718
2719 impl Drop for Foo<u32> {
2720     fn drop(&mut self) {}
2721 }
2722 ```
2723
2724 This code is not legal: it is not possible to specialize `Drop` to a subset of
2725 implementations of a generic type. One workaround for this is to wrap the
2726 generic type, as shown below:
2727
2728 ```
2729 struct Foo<T> {
2730     t: T
2731 }
2732
2733 struct Bar {
2734     t: Foo<u32>
2735 }
2736
2737 impl Drop for Bar {
2738     fn drop(&mut self) {}
2739 }
2740 ```
2741 "##,
2742
2743 E0367: r##"
2744 An attempt was made to implement `Drop` on a specialization of a generic type.
2745 An example is shown below:
2746
2747 ```compile_fail,E0367
2748 trait Foo{}
2749
2750 struct MyStruct<T> {
2751     t: T
2752 }
2753
2754 impl<T: Foo> Drop for MyStruct<T> {
2755     fn drop(&mut self) {}
2756 }
2757 ```
2758
2759 This code is not legal: it is not possible to specialize `Drop` to a subset of
2760 implementations of a generic type. In order for this code to work, `MyStruct`
2761 must also require that `T` implements `Foo`. Alternatively, another option is
2762 to wrap the generic type in another that specializes appropriately:
2763
2764 ```
2765 trait Foo{}
2766
2767 struct MyStruct<T> {
2768     t: T
2769 }
2770
2771 struct MyStructWrapper<T: Foo> {
2772     t: MyStruct<T>
2773 }
2774
2775 impl <T: Foo> Drop for MyStructWrapper<T> {
2776     fn drop(&mut self) {}
2777 }
2778 ```
2779 "##,
2780
2781 E0368: r##"
2782 This error indicates that a binary assignment operator like `+=` or `^=` was
2783 applied to a type that doesn't support it. For example:
2784
2785 ```compile_fail,E0368
2786 let mut x = 12f32; // error: binary operation `<<` cannot be applied to
2787                    //        type `f32`
2788
2789 x <<= 2;
2790 ```
2791
2792 To fix this error, please check that this type implements this binary
2793 operation. Example:
2794
2795 ```
2796 let mut x = 12u32; // the `u32` type does implement the `ShlAssign` trait
2797
2798 x <<= 2; // ok!
2799 ```
2800
2801 It is also possible to overload most operators for your own type by
2802 implementing the `[OP]Assign` traits from `std::ops`.
2803
2804 Another problem you might be facing is this: suppose you've overloaded the `+`
2805 operator for some type `Foo` by implementing the `std::ops::Add` trait for
2806 `Foo`, but you find that using `+=` does not work, as in this example:
2807
2808 ```compile_fail,E0368
2809 use std::ops::Add;
2810
2811 struct Foo(u32);
2812
2813 impl Add for Foo {
2814     type Output = Foo;
2815
2816     fn add(self, rhs: Foo) -> Foo {
2817         Foo(self.0 + rhs.0)
2818     }
2819 }
2820
2821 fn main() {
2822     let mut x: Foo = Foo(5);
2823     x += Foo(7); // error, `+= cannot be applied to the type `Foo`
2824 }
2825 ```
2826
2827 This is because `AddAssign` is not automatically implemented, so you need to
2828 manually implement it for your type.
2829 "##,
2830
2831 E0369: r##"
2832 A binary operation was attempted on a type which doesn't support it.
2833 Erroneous code example:
2834
2835 ```compile_fail,E0369
2836 let x = 12f32; // error: binary operation `<<` cannot be applied to
2837                //        type `f32`
2838
2839 x << 2;
2840 ```
2841
2842 To fix this error, please check that this type implements this binary
2843 operation. Example:
2844
2845 ```
2846 let x = 12u32; // the `u32` type does implement it:
2847                // https://doc.rust-lang.org/stable/std/ops/trait.Shl.html
2848
2849 x << 2; // ok!
2850 ```
2851
2852 It is also possible to overload most operators for your own type by
2853 implementing traits from `std::ops`.
2854
2855 String concatenation appends the string on the right to the string on the
2856 left and may require reallocation. This requires ownership of the string
2857 on the left. If something should be added to a string literal, move the
2858 literal to the heap by allocating it with `to_owned()` like in
2859 `"Your text".to_owned()`.
2860
2861 "##,
2862
2863 E0370: r##"
2864 The maximum value of an enum was reached, so it cannot be automatically
2865 set in the next enum value. Erroneous code example:
2866
2867 ```compile_fail,E0370
2868 #[repr(i64)]
2869 enum Foo {
2870     X = 0x7fffffffffffffff,
2871     Y, // error: enum discriminant overflowed on value after
2872        //        9223372036854775807: i64; set explicitly via
2873        //        Y = -9223372036854775808 if that is desired outcome
2874 }
2875 ```
2876
2877 To fix this, please set manually the next enum value or put the enum variant
2878 with the maximum value at the end of the enum. Examples:
2879
2880 ```
2881 #[repr(i64)]
2882 enum Foo {
2883     X = 0x7fffffffffffffff,
2884     Y = 0, // ok!
2885 }
2886 ```
2887
2888 Or:
2889
2890 ```
2891 #[repr(i64)]
2892 enum Foo {
2893     Y = 0, // ok!
2894     X = 0x7fffffffffffffff,
2895 }
2896 ```
2897 "##,
2898
2899 E0371: r##"
2900 When `Trait2` is a subtrait of `Trait1` (for example, when `Trait2` has a
2901 definition like `trait Trait2: Trait1 { ... }`), it is not allowed to implement
2902 `Trait1` for `Trait2`. This is because `Trait2` already implements `Trait1` by
2903 definition, so it is not useful to do this.
2904
2905 Example:
2906
2907 ```compile_fail,E0371
2908 trait Foo { fn foo(&self) { } }
2909 trait Bar: Foo { }
2910 trait Baz: Bar { }
2911
2912 impl Bar for Baz { } // error, `Baz` implements `Bar` by definition
2913 impl Foo for Baz { } // error, `Baz` implements `Bar` which implements `Foo`
2914 impl Baz for Baz { } // error, `Baz` (trivially) implements `Baz`
2915 impl Baz for Bar { } // Note: This is OK
2916 ```
2917 "##,
2918
2919 E0374: r##"
2920 A struct without a field containing an unsized type cannot implement
2921 `CoerceUnsized`. An [unsized type][1] is any type that the compiler
2922 doesn't know the length or alignment of at compile time. Any struct
2923 containing an unsized type is also unsized.
2924
2925 [1]: https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait
2926
2927 Example of erroneous code:
2928
2929 ```compile_fail,E0374
2930 #![feature(coerce_unsized)]
2931 use std::ops::CoerceUnsized;
2932
2933 struct Foo<T: ?Sized> {
2934     a: i32,
2935 }
2936
2937 // error: Struct `Foo` has no unsized fields that need `CoerceUnsized`.
2938 impl<T, U> CoerceUnsized<Foo<U>> for Foo<T>
2939     where T: CoerceUnsized<U> {}
2940 ```
2941
2942 `CoerceUnsized` is used to coerce one struct containing an unsized type
2943 into another struct containing a different unsized type. If the struct
2944 doesn't have any fields of unsized types then you don't need explicit
2945 coercion to get the types you want. To fix this you can either
2946 not try to implement `CoerceUnsized` or you can add a field that is
2947 unsized to the struct.
2948
2949 Example:
2950
2951 ```
2952 #![feature(coerce_unsized)]
2953 use std::ops::CoerceUnsized;
2954
2955 // We don't need to impl `CoerceUnsized` here.
2956 struct Foo {
2957     a: i32,
2958 }
2959
2960 // We add the unsized type field to the struct.
2961 struct Bar<T: ?Sized> {
2962     a: i32,
2963     b: T,
2964 }
2965
2966 // The struct has an unsized field so we can implement
2967 // `CoerceUnsized` for it.
2968 impl<T, U> CoerceUnsized<Bar<U>> for Bar<T>
2969     where T: CoerceUnsized<U> {}
2970 ```
2971
2972 Note that `CoerceUnsized` is mainly used by smart pointers like `Box`, `Rc`
2973 and `Arc` to be able to mark that they can coerce unsized types that they
2974 are pointing at.
2975 "##,
2976
2977 E0375: r##"
2978 A struct with more than one field containing an unsized type cannot implement
2979 `CoerceUnsized`. This only occurs when you are trying to coerce one of the
2980 types in your struct to another type in the struct. In this case we try to
2981 impl `CoerceUnsized` from `T` to `U` which are both types that the struct
2982 takes. An [unsized type][1] is any type that the compiler doesn't know the
2983 length or alignment of at compile time. Any struct containing an unsized type
2984 is also unsized.
2985
2986 Example of erroneous code:
2987
2988 ```compile_fail,E0375
2989 #![feature(coerce_unsized)]
2990 use std::ops::CoerceUnsized;
2991
2992 struct Foo<T: ?Sized, U: ?Sized> {
2993     a: i32,
2994     b: T,
2995     c: U,
2996 }
2997
2998 // error: Struct `Foo` has more than one unsized field.
2999 impl<T, U> CoerceUnsized<Foo<U, T>> for Foo<T, U> {}
3000 ```
3001
3002 `CoerceUnsized` only allows for coercion from a structure with a single
3003 unsized type field to another struct with a single unsized type field.
3004 In fact Rust only allows for a struct to have one unsized type in a struct
3005 and that unsized type must be the last field in the struct. So having two
3006 unsized types in a single struct is not allowed by the compiler. To fix this
3007 use only one field containing an unsized type in the struct and then use
3008 multiple structs to manage each unsized type field you need.
3009
3010 Example:
3011
3012 ```
3013 #![feature(coerce_unsized)]
3014 use std::ops::CoerceUnsized;
3015
3016 struct Foo<T: ?Sized> {
3017     a: i32,
3018     b: T,
3019 }
3020
3021 impl <T, U> CoerceUnsized<Foo<U>> for Foo<T>
3022     where T: CoerceUnsized<U> {}
3023
3024 fn coerce_foo<T: CoerceUnsized<U>, U>(t: T) -> Foo<U> {
3025     Foo { a: 12i32, b: t } // we use coercion to get the `Foo<U>` type we need
3026 }
3027 ```
3028
3029 [1]: https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait
3030 "##,
3031
3032 E0376: r##"
3033 The type you are trying to impl `CoerceUnsized` for is not a struct.
3034 `CoerceUnsized` can only be implemented for a struct. Unsized types are
3035 already able to be coerced without an implementation of `CoerceUnsized`
3036 whereas a struct containing an unsized type needs to know the unsized type
3037 field it's containing is able to be coerced. An [unsized type][1]
3038 is any type that the compiler doesn't know the length or alignment of at
3039 compile time. Any struct containing an unsized type is also unsized.
3040
3041 [1]: https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait
3042
3043 Example of erroneous code:
3044
3045 ```compile_fail,E0376
3046 #![feature(coerce_unsized)]
3047 use std::ops::CoerceUnsized;
3048
3049 struct Foo<T: ?Sized> {
3050     a: T,
3051 }
3052
3053 // error: The type `U` is not a struct
3054 impl<T, U> CoerceUnsized<U> for Foo<T> {}
3055 ```
3056
3057 The `CoerceUnsized` trait takes a struct type. Make sure the type you are
3058 providing to `CoerceUnsized` is a struct with only the last field containing an
3059 unsized type.
3060
3061 Example:
3062
3063 ```
3064 #![feature(coerce_unsized)]
3065 use std::ops::CoerceUnsized;
3066
3067 struct Foo<T> {
3068     a: T,
3069 }
3070
3071 // The `Foo<U>` is a struct so `CoerceUnsized` can be implemented
3072 impl<T, U> CoerceUnsized<Foo<U>> for Foo<T> where T: CoerceUnsized<U> {}
3073 ```
3074
3075 Note that in Rust, structs can only contain an unsized type if the field
3076 containing the unsized type is the last and only unsized type field in the
3077 struct.
3078 "##,
3079
3080 E0378: r##"
3081 The `DispatchFromDyn` trait currently can only be implemented for
3082 builtin pointer types and structs that are newtype wrappers around them
3083 — that is, the struct must have only one field (except for`PhantomData`),
3084 and that field must itself implement `DispatchFromDyn`.
3085
3086 Examples:
3087
3088 ```
3089 #![feature(dispatch_from_dyn, unsize)]
3090 use std::{
3091     marker::Unsize,
3092     ops::DispatchFromDyn,
3093 };
3094
3095 struct Ptr<T: ?Sized>(*const T);
3096
3097 impl<T: ?Sized, U: ?Sized> DispatchFromDyn<Ptr<U>> for Ptr<T>
3098 where
3099     T: Unsize<U>,
3100 {}
3101 ```
3102
3103 ```
3104 #![feature(dispatch_from_dyn)]
3105 use std::{
3106     ops::DispatchFromDyn,
3107     marker::PhantomData,
3108 };
3109
3110 struct Wrapper<T> {
3111     ptr: T,
3112     _phantom: PhantomData<()>,
3113 }
3114
3115 impl<T, U> DispatchFromDyn<Wrapper<U>> for Wrapper<T>
3116 where
3117     T: DispatchFromDyn<U>,
3118 {}
3119 ```
3120
3121 Example of illegal `DispatchFromDyn` implementation
3122 (illegal because of extra field)
3123
3124 ```compile-fail,E0378
3125 #![feature(dispatch_from_dyn)]
3126 use std::ops::DispatchFromDyn;
3127
3128 struct WrapperExtraField<T> {
3129     ptr: T,
3130     extra_stuff: i32,
3131 }
3132
3133 impl<T, U> DispatchFromDyn<WrapperExtraField<U>> for WrapperExtraField<T>
3134 where
3135     T: DispatchFromDyn<U>,
3136 {}
3137 ```
3138 "##,
3139
3140 E0390: r##"
3141 You tried to implement methods for a primitive type. Erroneous code example:
3142
3143 ```compile_fail,E0390
3144 struct Foo {
3145     x: i32
3146 }
3147
3148 impl *mut Foo {}
3149 // error: only a single inherent implementation marked with
3150 //        `#[lang = "mut_ptr"]` is allowed for the `*mut T` primitive
3151 ```
3152
3153 This isn't allowed, but using a trait to implement a method is a good solution.
3154 Example:
3155
3156 ```
3157 struct Foo {
3158     x: i32
3159 }
3160
3161 trait Bar {
3162     fn bar();
3163 }
3164
3165 impl Bar for *mut Foo {
3166     fn bar() {} // ok!
3167 }
3168 ```
3169 "##,
3170
3171 E0392: r##"
3172 This error indicates that a type or lifetime parameter has been declared
3173 but not actually used. Here is an example that demonstrates the error:
3174
3175 ```compile_fail,E0392
3176 enum Foo<T> {
3177     Bar,
3178 }
3179 ```
3180
3181 If the type parameter was included by mistake, this error can be fixed
3182 by simply removing the type parameter, as shown below:
3183
3184 ```
3185 enum Foo {
3186     Bar,
3187 }
3188 ```
3189
3190 Alternatively, if the type parameter was intentionally inserted, it must be
3191 used. A simple fix is shown below:
3192
3193 ```
3194 enum Foo<T> {
3195     Bar(T),
3196 }
3197 ```
3198
3199 This error may also commonly be found when working with unsafe code. For
3200 example, when using raw pointers one may wish to specify the lifetime for
3201 which the pointed-at data is valid. An initial attempt (below) causes this
3202 error:
3203
3204 ```compile_fail,E0392
3205 struct Foo<'a, T> {
3206     x: *const T,
3207 }
3208 ```
3209
3210 We want to express the constraint that Foo should not outlive `'a`, because
3211 the data pointed to by `T` is only valid for that lifetime. The problem is
3212 that there are no actual uses of `'a`. It's possible to work around this
3213 by adding a PhantomData type to the struct, using it to tell the compiler
3214 to act as if the struct contained a borrowed reference `&'a T`:
3215
3216 ```
3217 use std::marker::PhantomData;
3218
3219 struct Foo<'a, T: 'a> {
3220     x: *const T,
3221     phantom: PhantomData<&'a T>
3222 }
3223 ```
3224
3225 [PhantomData] can also be used to express information about unused type
3226 parameters.
3227
3228 [PhantomData]: https://doc.rust-lang.org/std/marker/struct.PhantomData.html
3229 "##,
3230
3231 E0393: r##"
3232 A type parameter which references `Self` in its default value was not specified.
3233 Example of erroneous code:
3234
3235 ```compile_fail,E0393
3236 trait A<T=Self> {}
3237
3238 fn together_we_will_rule_the_galaxy(son: &A) {}
3239 // error: the type parameter `T` must be explicitly specified in an
3240 //        object type because its default value `Self` references the
3241 //        type `Self`
3242 ```
3243
3244 A trait object is defined over a single, fully-defined trait. With a regular
3245 default parameter, this parameter can just be substituted in. However, if the
3246 default parameter is `Self`, the trait changes for each concrete type; i.e.
3247 `i32` will be expected to implement `A<i32>`, `bool` will be expected to
3248 implement `A<bool>`, etc... These types will not share an implementation of a
3249 fully-defined trait; instead they share implementations of a trait with
3250 different parameters substituted in for each implementation. This is
3251 irreconcilable with what we need to make a trait object work, and is thus
3252 disallowed. Making the trait concrete by explicitly specifying the value of the
3253 defaulted parameter will fix this issue. Fixed example:
3254
3255 ```
3256 trait A<T=Self> {}
3257
3258 fn together_we_will_rule_the_galaxy(son: &A<i32>) {} // Ok!
3259 ```
3260 "##,
3261
3262 E0399: r##"
3263 You implemented a trait, overriding one or more of its associated types but did
3264 not reimplement its default methods.
3265
3266 Example of erroneous code:
3267
3268 ```compile_fail,E0399
3269 #![feature(associated_type_defaults)]
3270
3271 pub trait Foo {
3272     type Assoc = u8;
3273     fn bar(&self) {}
3274 }
3275
3276 impl Foo for i32 {
3277     // error - the following trait items need to be reimplemented as
3278     //         `Assoc` was overridden: `bar`
3279     type Assoc = i32;
3280 }
3281 ```
3282
3283 To fix this, add an implementation for each default method from the trait:
3284
3285 ```
3286 #![feature(associated_type_defaults)]
3287
3288 pub trait Foo {
3289     type Assoc = u8;
3290     fn bar(&self) {}
3291 }
3292
3293 impl Foo for i32 {
3294     type Assoc = i32;
3295     fn bar(&self) {} // ok!
3296 }
3297 ```
3298 "##,
3299
3300 E0436: r##"
3301 The functional record update syntax is only allowed for structs. (Struct-like
3302 enum variants don't qualify, for example.)
3303
3304 Erroneous code example:
3305
3306 ```compile_fail,E0436
3307 enum PublicationFrequency {
3308     Weekly,
3309     SemiMonthly { days: (u8, u8), annual_special: bool },
3310 }
3311
3312 fn one_up_competitor(competitor_frequency: PublicationFrequency)
3313                      -> PublicationFrequency {
3314     match competitor_frequency {
3315         PublicationFrequency::Weekly => PublicationFrequency::SemiMonthly {
3316             days: (1, 15), annual_special: false
3317         },
3318         c @ PublicationFrequency::SemiMonthly{ .. } =>
3319             PublicationFrequency::SemiMonthly {
3320                 annual_special: true, ..c // error: functional record update
3321                                           //        syntax requires a struct
3322         }
3323     }
3324 }
3325 ```
3326
3327 Rewrite the expression without functional record update syntax:
3328
3329 ```
3330 enum PublicationFrequency {
3331     Weekly,
3332     SemiMonthly { days: (u8, u8), annual_special: bool },
3333 }
3334
3335 fn one_up_competitor(competitor_frequency: PublicationFrequency)
3336                      -> PublicationFrequency {
3337     match competitor_frequency {
3338         PublicationFrequency::Weekly => PublicationFrequency::SemiMonthly {
3339             days: (1, 15), annual_special: false
3340         },
3341         PublicationFrequency::SemiMonthly{ days, .. } =>
3342             PublicationFrequency::SemiMonthly {
3343                 days, annual_special: true // ok!
3344         }
3345     }
3346 }
3347 ```
3348 "##,
3349
3350 E0439: r##"
3351 The length of the platform-intrinsic function `simd_shuffle`
3352 wasn't specified. Erroneous code example:
3353
3354 ```compile_fail,E0439
3355 #![feature(platform_intrinsics)]
3356
3357 extern "platform-intrinsic" {
3358     fn simd_shuffle<A,B>(a: A, b: A, c: [u32; 8]) -> B;
3359     // error: invalid `simd_shuffle`, needs length: `simd_shuffle`
3360 }
3361 ```
3362
3363 The `simd_shuffle` function needs the length of the array passed as
3364 last parameter in its name. Example:
3365
3366 ```
3367 #![feature(platform_intrinsics)]
3368
3369 extern "platform-intrinsic" {
3370     fn simd_shuffle8<A,B>(a: A, b: A, c: [u32; 8]) -> B;
3371 }
3372 ```
3373 "##,
3374
3375 E0516: r##"
3376 The `typeof` keyword is currently reserved but unimplemented.
3377 Erroneous code example:
3378
3379 ```compile_fail,E0516
3380 fn main() {
3381     let x: typeof(92) = 92;
3382 }
3383 ```
3384
3385 Try using type inference instead. Example:
3386
3387 ```
3388 fn main() {
3389     let x = 92;
3390 }
3391 ```
3392 "##,
3393
3394 E0520: r##"
3395 A non-default implementation was already made on this type so it cannot be
3396 specialized further. Erroneous code example:
3397
3398 ```compile_fail,E0520
3399 #![feature(specialization)]
3400
3401 trait SpaceLlama {
3402     fn fly(&self);
3403 }
3404
3405 // applies to all T
3406 impl<T> SpaceLlama for T {
3407     default fn fly(&self) {}
3408 }
3409
3410 // non-default impl
3411 // applies to all `Clone` T and overrides the previous impl
3412 impl<T: Clone> SpaceLlama for T {
3413     fn fly(&self) {}
3414 }
3415
3416 // since `i32` is clone, this conflicts with the previous implementation
3417 impl SpaceLlama for i32 {
3418     default fn fly(&self) {}
3419     // error: item `fly` is provided by an `impl` that specializes
3420     //        another, but the item in the parent `impl` is not marked
3421     //        `default` and so it cannot be specialized.
3422 }
3423 ```
3424
3425 Specialization only allows you to override `default` functions in
3426 implementations.
3427
3428 To fix this error, you need to mark all the parent implementations as default.
3429 Example:
3430
3431 ```
3432 #![feature(specialization)]
3433
3434 trait SpaceLlama {
3435     fn fly(&self);
3436 }
3437
3438 // applies to all T
3439 impl<T> SpaceLlama for T {
3440     default fn fly(&self) {} // This is a parent implementation.
3441 }
3442
3443 // applies to all `Clone` T; overrides the previous impl
3444 impl<T: Clone> SpaceLlama for T {
3445     default fn fly(&self) {} // This is a parent implementation but was
3446                              // previously not a default one, causing the error
3447 }
3448
3449 // applies to i32, overrides the previous two impls
3450 impl SpaceLlama for i32 {
3451     fn fly(&self) {} // And now that's ok!
3452 }
3453 ```
3454 "##,
3455
3456 E0527: r##"
3457 The number of elements in an array or slice pattern differed from the number of
3458 elements in the array being matched.
3459
3460 Example of erroneous code:
3461
3462 ```compile_fail,E0527
3463 let r = &[1, 2, 3, 4];
3464 match r {
3465     &[a, b] => { // error: pattern requires 2 elements but array
3466                  //        has 4
3467         println!("a={}, b={}", a, b);
3468     }
3469 }
3470 ```
3471
3472 Ensure that the pattern is consistent with the size of the matched
3473 array. Additional elements can be matched with `..`:
3474
3475 ```
3476 #![feature(slice_patterns)]
3477
3478 let r = &[1, 2, 3, 4];
3479 match r {
3480     &[a, b, ..] => { // ok!
3481         println!("a={}, b={}", a, b);
3482     }
3483 }
3484 ```
3485 "##,
3486
3487 E0528: r##"
3488 An array or slice pattern required more elements than were present in the
3489 matched array.
3490
3491 Example of erroneous code:
3492
3493 ```compile_fail,E0528
3494 #![feature(slice_patterns)]
3495
3496 let r = &[1, 2];
3497 match r {
3498     &[a, b, c, rest..] => { // error: pattern requires at least 3
3499                             //        elements but array has 2
3500         println!("a={}, b={}, c={} rest={:?}", a, b, c, rest);
3501     }
3502 }
3503 ```
3504
3505 Ensure that the matched array has at least as many elements as the pattern
3506 requires. You can match an arbitrary number of remaining elements with `..`:
3507
3508 ```
3509 #![feature(slice_patterns)]
3510
3511 let r = &[1, 2, 3, 4, 5];
3512 match r {
3513     &[a, b, c, rest..] => { // ok!
3514         // prints `a=1, b=2, c=3 rest=[4, 5]`
3515         println!("a={}, b={}, c={} rest={:?}", a, b, c, rest);
3516     }
3517 }
3518 ```
3519 "##,
3520
3521 E0529: r##"
3522 An array or slice pattern was matched against some other type.
3523
3524 Example of erroneous code:
3525
3526 ```compile_fail,E0529
3527 let r: f32 = 1.0;
3528 match r {
3529     [a, b] => { // error: expected an array or slice, found `f32`
3530         println!("a={}, b={}", a, b);
3531     }
3532 }
3533 ```
3534
3535 Ensure that the pattern and the expression being matched on are of consistent
3536 types:
3537
3538 ```
3539 let r = [1.0, 2.0];
3540 match r {
3541     [a, b] => { // ok!
3542         println!("a={}, b={}", a, b);
3543     }
3544 }
3545 ```
3546 "##,
3547
3548 E0534: r##"
3549 The `inline` attribute was malformed.
3550
3551 Erroneous code example:
3552
3553 ```ignore (compile_fail not working here; see Issue #43707)
3554 #[inline()] // error: expected one argument
3555 pub fn something() {}
3556
3557 fn main() {}
3558 ```
3559
3560 The parenthesized `inline` attribute requires the parameter to be specified:
3561
3562 ```
3563 #[inline(always)]
3564 fn something() {}
3565 ```
3566
3567 or:
3568
3569 ```
3570 #[inline(never)]
3571 fn something() {}
3572 ```
3573
3574 Alternatively, a paren-less version of the attribute may be used to hint the
3575 compiler about inlining opportunity:
3576
3577 ```
3578 #[inline]
3579 fn something() {}
3580 ```
3581
3582 For more information about the inline attribute, read:
3583 https://doc.rust-lang.org/reference.html#inline-attributes
3584 "##,
3585
3586 E0535: r##"
3587 An unknown argument was given to the `inline` attribute.
3588
3589 Erroneous code example:
3590
3591 ```ignore (compile_fail not working here; see Issue #43707)
3592 #[inline(unknown)] // error: invalid argument
3593 pub fn something() {}
3594
3595 fn main() {}
3596 ```
3597
3598 The `inline` attribute only supports two arguments:
3599
3600  * always
3601  * never
3602
3603 All other arguments given to the `inline` attribute will return this error.
3604 Example:
3605
3606 ```
3607 #[inline(never)] // ok!
3608 pub fn something() {}
3609
3610 fn main() {}
3611 ```
3612
3613 For more information about the inline attribute, https:
3614 read://doc.rust-lang.org/reference.html#inline-attributes
3615 "##,
3616
3617 E0559: r##"
3618 An unknown field was specified into an enum's structure variant.
3619
3620 Erroneous code example:
3621
3622 ```compile_fail,E0559
3623 enum Field {
3624     Fool { x: u32 },
3625 }
3626
3627 let s = Field::Fool { joke: 0 };
3628 // error: struct variant `Field::Fool` has no field named `joke`
3629 ```
3630
3631 Verify you didn't misspell the field's name or that the field exists. Example:
3632
3633 ```
3634 enum Field {
3635     Fool { joke: u32 },
3636 }
3637
3638 let s = Field::Fool { joke: 0 }; // ok!
3639 ```
3640 "##,
3641
3642 E0560: r##"
3643 An unknown field was specified into a structure.
3644
3645 Erroneous code example:
3646
3647 ```compile_fail,E0560
3648 struct Simba {
3649     mother: u32,
3650 }
3651
3652 let s = Simba { mother: 1, father: 0 };
3653 // error: structure `Simba` has no field named `father`
3654 ```
3655
3656 Verify you didn't misspell the field's name or that the field exists. Example:
3657
3658 ```
3659 struct Simba {
3660     mother: u32,
3661     father: u32,
3662 }
3663
3664 let s = Simba { mother: 1, father: 0 }; // ok!
3665 ```
3666 "##,
3667
3668 E0569: r##"
3669 If an impl has a generic parameter with the `#[may_dangle]` attribute, then
3670 that impl must be declared as an `unsafe impl.
3671
3672 Erroneous code example:
3673
3674 ```compile_fail,E0569
3675 #![feature(dropck_eyepatch)]
3676
3677 struct Foo<X>(X);
3678 impl<#[may_dangle] X> Drop for Foo<X> {
3679     fn drop(&mut self) { }
3680 }
3681 ```
3682
3683 In this example, we are asserting that the destructor for `Foo` will not
3684 access any data of type `X`, and require this assertion to be true for
3685 overall safety in our program. The compiler does not currently attempt to
3686 verify this assertion; therefore we must tag this `impl` as unsafe.
3687 "##,
3688
3689 E0570: r##"
3690 The requested ABI is unsupported by the current target.
3691
3692 The rust compiler maintains for each target a blacklist of ABIs unsupported on
3693 that target. If an ABI is present in such a list this usually means that the
3694 target / ABI combination is currently unsupported by llvm.
3695
3696 If necessary, you can circumvent this check using custom target specifications.
3697 "##,
3698
3699 E0572: r##"
3700 A return statement was found outside of a function body.
3701
3702 Erroneous code example:
3703
3704 ```compile_fail,E0572
3705 const FOO: u32 = return 0; // error: return statement outside of function body
3706
3707 fn main() {}
3708 ```
3709
3710 To fix this issue, just remove the return keyword or move the expression into a
3711 function. Example:
3712
3713 ```
3714 const FOO: u32 = 0;
3715
3716 fn some_fn() -> u32 {
3717     return FOO;
3718 }
3719
3720 fn main() {
3721     some_fn();
3722 }
3723 ```
3724 "##,
3725
3726 E0581: r##"
3727 In a `fn` type, a lifetime appears only in the return type,
3728 and not in the arguments types.
3729
3730 Erroneous code example:
3731
3732 ```compile_fail,E0581
3733 fn main() {
3734     // Here, `'a` appears only in the return type:
3735     let x: for<'a> fn() -> &'a i32;
3736 }
3737 ```
3738
3739 To fix this issue, either use the lifetime in the arguments, or use
3740 `'static`. Example:
3741
3742 ```
3743 fn main() {
3744     // Here, `'a` appears only in the return type:
3745     let x: for<'a> fn(&'a i32) -> &'a i32;
3746     let y: fn() -> &'static i32;
3747 }
3748 ```
3749
3750 Note: The examples above used to be (erroneously) accepted by the
3751 compiler, but this was since corrected. See [issue #33685] for more
3752 details.
3753
3754 [issue #33685]: https://github.com/rust-lang/rust/issues/33685
3755 "##,
3756
3757 E0582: r##"
3758 A lifetime appears only in an associated-type binding,
3759 and not in the input types to the trait.
3760
3761 Erroneous code example:
3762
3763 ```compile_fail,E0582
3764 fn bar<F>(t: F)
3765     // No type can satisfy this requirement, since `'a` does not
3766     // appear in any of the input types (here, `i32`):
3767     where F: for<'a> Fn(i32) -> Option<&'a i32>
3768 {
3769 }
3770
3771 fn main() { }
3772 ```
3773
3774 To fix this issue, either use the lifetime in the inputs, or use
3775 `'static`. Example:
3776
3777 ```
3778 fn bar<F, G>(t: F, u: G)
3779     where F: for<'a> Fn(&'a i32) -> Option<&'a i32>,
3780           G: Fn(i32) -> Option<&'static i32>,
3781 {
3782 }
3783
3784 fn main() { }
3785 ```
3786
3787 Note: The examples above used to be (erroneously) accepted by the
3788 compiler, but this was since corrected. See [issue #33685] for more
3789 details.
3790
3791 [issue #33685]: https://github.com/rust-lang/rust/issues/33685
3792 "##,
3793
3794 E0599: r##"
3795 This error occurs when a method is used on a type which doesn't implement it:
3796
3797 Erroneous code example:
3798
3799 ```compile_fail,E0599
3800 struct Mouth;
3801
3802 let x = Mouth;
3803 x.chocolate(); // error: no method named `chocolate` found for type `Mouth`
3804                //        in the current scope
3805 ```
3806 "##,
3807
3808 E0600: r##"
3809 An unary operator was used on a type which doesn't implement it.
3810
3811 Example of erroneous code:
3812
3813 ```compile_fail,E0600
3814 enum Question {
3815     Yes,
3816     No,
3817 }
3818
3819 !Question::Yes; // error: cannot apply unary operator `!` to type `Question`
3820 ```
3821
3822 In this case, `Question` would need to implement the `std::ops::Not` trait in
3823 order to be able to use `!` on it. Let's implement it:
3824
3825 ```
3826 use std::ops::Not;
3827
3828 enum Question {
3829     Yes,
3830     No,
3831 }
3832
3833 // We implement the `Not` trait on the enum.
3834 impl Not for Question {
3835     type Output = bool;
3836
3837     fn not(self) -> bool {
3838         match self {
3839             Question::Yes => false, // If the `Answer` is `Yes`, then it
3840                                     // returns false.
3841             Question::No => true, // And here we do the opposite.
3842         }
3843     }
3844 }
3845
3846 assert_eq!(!Question::Yes, false);
3847 assert_eq!(!Question::No, true);
3848 ```
3849 "##,
3850
3851 E0608: r##"
3852 An attempt to index into a type which doesn't implement the `std::ops::Index`
3853 trait was performed.
3854
3855 Erroneous code example:
3856
3857 ```compile_fail,E0608
3858 0u8[2]; // error: cannot index into a value of type `u8`
3859 ```
3860
3861 To be able to index into a type it needs to implement the `std::ops::Index`
3862 trait. Example:
3863
3864 ```
3865 let v: Vec<u8> = vec![0, 1, 2, 3];
3866
3867 // The `Vec` type implements the `Index` trait so you can do:
3868 println!("{}", v[2]);
3869 ```
3870 "##,
3871
3872 E0604: r##"
3873 A cast to `char` was attempted on a type other than `u8`.
3874
3875 Erroneous code example:
3876
3877 ```compile_fail,E0604
3878 0u32 as char; // error: only `u8` can be cast as `char`, not `u32`
3879 ```
3880
3881 As the error message indicates, only `u8` can be cast into `char`. Example:
3882
3883 ```
3884 let c = 86u8 as char; // ok!
3885 assert_eq!(c, 'V');
3886 ```
3887
3888 For more information about casts, take a look at the Type cast section in
3889 [The Reference Book][1].
3890
3891 [1]: https://doc.rust-lang.org/reference/expressions/operator-expr.html#type-cast-expressions
3892 "##,
3893
3894 E0605: r##"
3895 An invalid cast was attempted.
3896
3897 Erroneous code examples:
3898
3899 ```compile_fail,E0605
3900 let x = 0u8;
3901 x as Vec<u8>; // error: non-primitive cast: `u8` as `std::vec::Vec<u8>`
3902
3903 // Another example
3904
3905 let v = 0 as *const u8; // So here, `v` is a `*const u8`.
3906 v as &u8; // error: non-primitive cast: `*const u8` as `&u8`
3907 ```
3908
3909 Only primitive types can be cast into each other. Examples:
3910
3911 ```
3912 let x = 0u8;
3913 x as u32; // ok!
3914
3915 let v = 0 as *const u8;
3916 v as *const i8; // ok!
3917 ```
3918
3919 For more information about casts, take a look at the Type cast section in
3920 [The Reference Book][1].
3921
3922 [1]: https://doc.rust-lang.org/reference/expressions/operator-expr.html#type-cast-expressions
3923 "##,
3924
3925 E0606: r##"
3926 An incompatible cast was attempted.
3927
3928 Erroneous code example:
3929
3930 ```compile_fail,E0606
3931 let x = &0u8; // Here, `x` is a `&u8`.
3932 let y: u32 = x as u32; // error: casting `&u8` as `u32` is invalid
3933 ```
3934
3935 When casting, keep in mind that only primitive types can be cast into each
3936 other. Example:
3937
3938 ```
3939 let x = &0u8;
3940 let y: u32 = *x as u32; // We dereference it first and then cast it.
3941 ```
3942
3943 For more information about casts, take a look at the Type cast section in
3944 [The Reference Book][1].
3945
3946 [1]: https://doc.rust-lang.org/reference/expressions/operator-expr.html#type-cast-expressions
3947 "##,
3948
3949 E0607: r##"
3950 A cast between a thin and a fat pointer was attempted.
3951
3952 Erroneous code example:
3953
3954 ```compile_fail,E0607
3955 let v = 0 as *const u8;
3956 v as *const [u8];
3957 ```
3958
3959 First: what are thin and fat pointers?
3960
3961 Thin pointers are "simple" pointers: they are purely a reference to a memory
3962 address.
3963
3964 Fat pointers are pointers referencing Dynamically Sized Types (also called DST).
3965 DST don't have a statically known size, therefore they can only exist behind
3966 some kind of pointers that contain additional information. Slices and trait
3967 objects are DSTs. In the case of slices, the additional information the fat
3968 pointer holds is their size.
3969
3970 To fix this error, don't try to cast directly between thin and fat pointers.
3971
3972 For more information about casts, take a look at the Type cast section in
3973 [The Reference Book][1].
3974
3975 [1]: https://doc.rust-lang.org/reference/expressions/operator-expr.html#type-cast-expressions
3976 "##,
3977
3978 E0609: r##"
3979 Attempted to access a non-existent field in a struct.
3980
3981 Erroneous code example:
3982
3983 ```compile_fail,E0609
3984 struct StructWithFields {
3985     x: u32,
3986 }
3987
3988 let s = StructWithFields { x: 0 };
3989 println!("{}", s.foo); // error: no field `foo` on type `StructWithFields`
3990 ```
3991
3992 To fix this error, check that you didn't misspell the field's name or that the
3993 field actually exists. Example:
3994
3995 ```
3996 struct StructWithFields {
3997     x: u32,
3998 }
3999
4000 let s = StructWithFields { x: 0 };
4001 println!("{}", s.x); // ok!
4002 ```
4003 "##,
4004
4005 E0610: r##"
4006 Attempted to access a field on a primitive type.
4007
4008 Erroneous code example:
4009
4010 ```compile_fail,E0610
4011 let x: u32 = 0;
4012 println!("{}", x.foo); // error: `{integer}` is a primitive type, therefore
4013                        //        doesn't have fields
4014 ```
4015
4016 Primitive types are the most basic types available in Rust and don't have
4017 fields. To access data via named fields, struct types are used. Example:
4018
4019 ```
4020 // We declare struct called `Foo` containing two fields:
4021 struct Foo {
4022     x: u32,
4023     y: i64,
4024 }
4025
4026 // We create an instance of this struct:
4027 let variable = Foo { x: 0, y: -12 };
4028 // And we can now access its fields:
4029 println!("x: {}, y: {}", variable.x, variable.y);
4030 ```
4031
4032 For more information about primitives and structs, take a look at The Book:
4033 https://doc.rust-lang.org/book/ch03-02-data-types.html
4034 https://doc.rust-lang.org/book/ch05-00-structs.html
4035 "##,
4036
4037 E0614: r##"
4038 Attempted to dereference a variable which cannot be dereferenced.
4039
4040 Erroneous code example:
4041
4042 ```compile_fail,E0614
4043 let y = 0u32;
4044 *y; // error: type `u32` cannot be dereferenced
4045 ```
4046
4047 Only types implementing `std::ops::Deref` can be dereferenced (such as `&T`).
4048 Example:
4049
4050 ```
4051 let y = 0u32;
4052 let x = &y;
4053 // So here, `x` is a `&u32`, so we can dereference it:
4054 *x; // ok!
4055 ```
4056 "##,
4057
4058 E0615: r##"
4059 Attempted to access a method like a field.
4060
4061 Erroneous code example:
4062
4063 ```compile_fail,E0615
4064 struct Foo {
4065     x: u32,
4066 }
4067
4068 impl Foo {
4069     fn method(&self) {}
4070 }
4071
4072 let f = Foo { x: 0 };
4073 f.method; // error: attempted to take value of method `method` on type `Foo`
4074 ```
4075
4076 If you want to use a method, add `()` after it:
4077
4078 ```
4079 # struct Foo { x: u32 }
4080 # impl Foo { fn method(&self) {} }
4081 # let f = Foo { x: 0 };
4082 f.method();
4083 ```
4084
4085 However, if you wanted to access a field of a struct check that the field name
4086 is spelled correctly. Example:
4087
4088 ```
4089 # struct Foo { x: u32 }
4090 # impl Foo { fn method(&self) {} }
4091 # let f = Foo { x: 0 };
4092 println!("{}", f.x);
4093 ```
4094 "##,
4095
4096 E0616: r##"
4097 Attempted to access a private field on a struct.
4098
4099 Erroneous code example:
4100
4101 ```compile_fail,E0616
4102 mod some_module {
4103     pub struct Foo {
4104         x: u32, // So `x` is private in here.
4105     }
4106
4107     impl Foo {
4108         pub fn new() -> Foo { Foo { x: 0 } }
4109     }
4110 }
4111
4112 let f = some_module::Foo::new();
4113 println!("{}", f.x); // error: field `x` of struct `some_module::Foo` is private
4114 ```
4115
4116 If you want to access this field, you have two options:
4117
4118 1) Set the field public:
4119
4120 ```
4121 mod some_module {
4122     pub struct Foo {
4123         pub x: u32, // `x` is now public.
4124     }
4125
4126     impl Foo {
4127         pub fn new() -> Foo { Foo { x: 0 } }
4128     }
4129 }
4130
4131 let f = some_module::Foo::new();
4132 println!("{}", f.x); // ok!
4133 ```
4134
4135 2) Add a getter function:
4136
4137 ```
4138 mod some_module {
4139     pub struct Foo {
4140         x: u32, // So `x` is still private in here.
4141     }
4142
4143     impl Foo {
4144         pub fn new() -> Foo { Foo { x: 0 } }
4145
4146         // We create the getter function here:
4147         pub fn get_x(&self) -> &u32 { &self.x }
4148     }
4149 }
4150
4151 let f = some_module::Foo::new();
4152 println!("{}", f.get_x()); // ok!
4153 ```
4154 "##,
4155
4156 E0617: r##"
4157 Attempted to pass an invalid type of variable into a variadic function.
4158
4159 Erroneous code example:
4160
4161 ```compile_fail,E0617
4162 extern {
4163     fn printf(c: *const i8, ...);
4164 }
4165
4166 unsafe {
4167     printf(::std::ptr::null(), 0f32);
4168     // error: can't pass an `f32` to variadic function, cast to `c_double`
4169 }
4170 ```
4171
4172 Certain Rust types must be cast before passing them to a variadic function,
4173 because of arcane ABI rules dictated by the C standard. To fix the error,
4174 cast the value to the type specified by the error message (which you may need
4175 to import from `std::os::raw`).
4176 "##,
4177
4178 E0618: r##"
4179 Attempted to call something which isn't a function nor a method.
4180
4181 Erroneous code examples:
4182
4183 ```compile_fail,E0618
4184 enum X {
4185     Entry,
4186 }
4187
4188 X::Entry(); // error: expected function, found `X::Entry`
4189
4190 // Or even simpler:
4191 let x = 0i32;
4192 x(); // error: expected function, found `i32`
4193 ```
4194
4195 Only functions and methods can be called using `()`. Example:
4196
4197 ```
4198 // We declare a function:
4199 fn i_am_a_function() {}
4200
4201 // And we call it:
4202 i_am_a_function();
4203 ```
4204 "##,
4205
4206 E0619: r##"
4207 #### Note: this error code is no longer emitted by the compiler.
4208 The type-checker needed to know the type of an expression, but that type had not
4209 yet been inferred.
4210
4211 Erroneous code example:
4212
4213 ```compile_fail
4214 let mut x = vec![];
4215 match x.pop() {
4216     Some(v) => {
4217         // Here, the type of `v` is not (yet) known, so we
4218         // cannot resolve this method call:
4219         v.to_uppercase(); // error: the type of this value must be known in
4220                           //        this context
4221     }
4222     None => {}
4223 }
4224 ```
4225
4226 Type inference typically proceeds from the top of the function to the bottom,
4227 figuring out types as it goes. In some cases -- notably method calls and
4228 overloadable operators like `*` -- the type checker may not have enough
4229 information *yet* to make progress. This can be true even if the rest of the
4230 function provides enough context (because the type-checker hasn't looked that
4231 far ahead yet). In this case, type annotations can be used to help it along.
4232
4233 To fix this error, just specify the type of the variable. Example:
4234
4235 ```
4236 let mut x: Vec<String> = vec![]; // We precise the type of the vec elements.
4237 match x.pop() {
4238     Some(v) => {
4239         v.to_uppercase(); // Since rustc now knows the type of the vec elements,
4240                           // we can use `v`'s methods.
4241     }
4242     None => {}
4243 }
4244 ```
4245 "##,
4246
4247 E0620: r##"
4248 A cast to an unsized type was attempted.
4249
4250 Erroneous code example:
4251
4252 ```compile_fail,E0620
4253 let x = &[1_usize, 2] as [usize]; // error: cast to unsized type: `&[usize; 2]`
4254                                   //        as `[usize]`
4255 ```
4256
4257 In Rust, some types don't have a known size at compile-time. For example, in a
4258 slice type like `[u32]`, the number of elements is not known at compile-time and
4259 hence the overall size cannot be computed. As a result, such types can only be
4260 manipulated through a reference (e.g., `&T` or `&mut T`) or other pointer-type
4261 (e.g., `Box` or `Rc`). Try casting to a reference instead:
4262
4263 ```
4264 let x = &[1_usize, 2] as &[usize]; // ok!
4265 ```
4266 "##,
4267
4268 E0622: r##"
4269 An intrinsic was declared without being a function.
4270
4271 Erroneous code example:
4272
4273 ```compile_fail,E0622
4274 #![feature(intrinsics)]
4275 extern "rust-intrinsic" {
4276     pub static breakpoint : unsafe extern "rust-intrinsic" fn();
4277     // error: intrinsic must be a function
4278 }
4279
4280 fn main() { unsafe { breakpoint(); } }
4281 ```
4282
4283 An intrinsic is a function available for use in a given programming language
4284 whose implementation is handled specially by the compiler. In order to fix this
4285 error, just declare a function.
4286 "##,
4287
4288 E0624: r##"
4289 A private item was used outside of its scope.
4290
4291 Erroneous code example:
4292
4293 ```compile_fail,E0624
4294 mod inner {
4295     pub struct Foo;
4296
4297     impl Foo {
4298         fn method(&self) {}
4299     }
4300 }
4301
4302 let foo = inner::Foo;
4303 foo.method(); // error: method `method` is private
4304 ```
4305
4306 Two possibilities are available to solve this issue:
4307
4308 1. Only use the item in the scope it has been defined:
4309
4310 ```
4311 mod inner {
4312     pub struct Foo;
4313
4314     impl Foo {
4315         fn method(&self) {}
4316     }
4317
4318     pub fn call_method(foo: &Foo) { // We create a public function.
4319         foo.method(); // Which calls the item.
4320     }
4321 }
4322
4323 let foo = inner::Foo;
4324 inner::call_method(&foo); // And since the function is public, we can call the
4325                           // method through it.
4326 ```
4327
4328 2. Make the item public:
4329
4330 ```
4331 mod inner {
4332     pub struct Foo;
4333
4334     impl Foo {
4335         pub fn method(&self) {} // It's now public.
4336     }
4337 }
4338
4339 let foo = inner::Foo;
4340 foo.method(); // Ok!
4341 ```
4342 "##,
4343
4344 E0638: r##"
4345 This error indicates that the struct, enum or enum variant must be matched
4346 non-exhaustively as it has been marked as `non_exhaustive`.
4347
4348 When applied within a crate, downstream users of the crate will need to use the
4349 `_` pattern when matching enums and use the `..` pattern when matching structs.
4350 Downstream crates cannot match against non-exhaustive enum variants.
4351
4352 For example, in the below example, since the enum is marked as
4353 `non_exhaustive`, it is required that downstream crates match non-exhaustively
4354 on it.
4355
4356 ```rust,ignore (pseudo-Rust)
4357 use std::error::Error as StdError;
4358
4359 #[non_exhaustive] pub enum Error {
4360    Message(String),
4361    Other,
4362 }
4363
4364 impl StdError for Error {
4365    fn description(&self) -> &str {
4366         // This will not error, despite being marked as non_exhaustive, as this
4367         // enum is defined within the current crate, it can be matched
4368         // exhaustively.
4369         match *self {
4370            Message(ref s) => s,
4371            Other => "other or unknown error",
4372         }
4373    }
4374 }
4375 ```
4376
4377 An example of matching non-exhaustively on the above enum is provided below:
4378
4379 ```rust,ignore (pseudo-Rust)
4380 use mycrate::Error;
4381
4382 // This will not error as the non_exhaustive Error enum has been matched with a
4383 // wildcard.
4384 match error {
4385    Message(ref s) => ...,
4386    Other => ...,
4387    _ => ...,
4388 }
4389 ```
4390
4391 Similarly, for structs, match with `..` to avoid this error.
4392 "##,
4393
4394 E0639: r##"
4395 This error indicates that the struct, enum or enum variant cannot be
4396 instantiated from outside of the defining crate as it has been marked
4397 as `non_exhaustive` and as such more fields/variants may be added in
4398 future that could cause adverse side effects for this code.
4399
4400 It is recommended that you look for a `new` function or equivalent in the
4401 crate's documentation.
4402 "##,
4403
4404 E0643: r##"
4405 This error indicates that there is a mismatch between generic parameters and
4406 impl Trait parameters in a trait declaration versus its impl.
4407
4408 ```compile_fail,E0643
4409 trait Foo {
4410     fn foo(&self, _: &impl Iterator);
4411 }
4412 impl Foo for () {
4413     fn foo<U: Iterator>(&self, _: &U) { } // error method `foo` has incompatible
4414                                           // signature for trait
4415 }
4416 ```
4417 "##,
4418
4419 E0646: r##"
4420 It is not possible to define `main` with a where clause.
4421 Erroneous code example:
4422
4423 ```compile_fail,E0646
4424 fn main() where i32: Copy { // error: main function is not allowed to have
4425                             // a where clause
4426 }
4427 ```
4428 "##,
4429
4430 E0647: r##"
4431 It is not possible to define `start` with a where clause.
4432 Erroneous code example:
4433
4434 ```compile_fail,E0647
4435 #![feature(start)]
4436
4437 #[start]
4438 fn start(_: isize, _: *const *const u8) -> isize where (): Copy {
4439     //^ error: start function is not allowed to have a where clause
4440     0
4441 }
4442 ```
4443 "##,
4444
4445 E0648: r##"
4446 `export_name` attributes may not contain null characters (`\0`).
4447
4448 ```compile_fail,E0648
4449 #[export_name="\0foo"] // error: `export_name` may not contain null characters
4450 pub fn bar() {}
4451 ```
4452 "##,
4453
4454 E0689: r##"
4455 This error indicates that the numeric value for the method being passed exists
4456 but the type of the numeric value or binding could not be identified.
4457
4458 The error happens on numeric literals:
4459
4460 ```compile_fail,E0689
4461 2.0.neg();
4462 ```
4463
4464 and on numeric bindings without an identified concrete type:
4465
4466 ```compile_fail,E0689
4467 let x = 2.0;
4468 x.neg();  // same error as above
4469 ```
4470
4471 Because of this, you must give the numeric literal or binding a type:
4472
4473 ```
4474 use std::ops::Neg;
4475
4476 let _ = 2.0_f32.neg();
4477 let x: f32 = 2.0;
4478 let _ = x.neg();
4479 let _ = (2.0 as f32).neg();
4480 ```
4481 "##,
4482
4483 E0690: r##"
4484 A struct with the representation hint `repr(transparent)` had zero or more than
4485 on fields that were not guaranteed to be zero-sized.
4486
4487 Erroneous code example:
4488
4489 ```compile_fail,E0690
4490 #[repr(transparent)]
4491 struct LengthWithUnit<U> { // error: transparent struct needs exactly one
4492     value: f32,            //        non-zero-sized field, but has 2
4493     unit: U,
4494 }
4495 ```
4496
4497 Because transparent structs are represented exactly like one of their fields at
4498 run time, said field must be uniquely determined. If there is no field, or if
4499 there are multiple fields, it is not clear how the struct should be represented.
4500 Note that fields of zero-typed types (e.g., `PhantomData`) can also exist
4501 alongside the field that contains the actual data, they do not count for this
4502 error. When generic types are involved (as in the above example), an error is
4503 reported because the type parameter could be non-zero-sized.
4504
4505 To combine `repr(transparent)` with type parameters, `PhantomData` may be
4506 useful:
4507
4508 ```
4509 use std::marker::PhantomData;
4510
4511 #[repr(transparent)]
4512 struct LengthWithUnit<U> {
4513     value: f32,
4514     unit: PhantomData<U>,
4515 }
4516 ```
4517 "##,
4518
4519 E0691: r##"
4520 A struct with the `repr(transparent)` representation hint contains a zero-sized
4521 field that requires non-trivial alignment.
4522
4523 Erroneous code example:
4524
4525 ```compile_fail,E0691
4526 #![feature(repr_align)]
4527
4528 #[repr(align(32))]
4529 struct ForceAlign32;
4530
4531 #[repr(transparent)]
4532 struct Wrapper(f32, ForceAlign32); // error: zero-sized field in transparent
4533                                    //        struct has alignment larger than 1
4534 ```
4535
4536 A transparent struct is supposed to be represented exactly like the piece of
4537 data it contains. Zero-sized fields with different alignment requirements
4538 potentially conflict with this property. In the example above, `Wrapper` would
4539 have to be aligned to 32 bytes even though `f32` has a smaller alignment
4540 requirement.
4541
4542 Consider removing the over-aligned zero-sized field:
4543
4544 ```
4545 #[repr(transparent)]
4546 struct Wrapper(f32);
4547 ```
4548
4549 Alternatively, `PhantomData<T>` has alignment 1 for all `T`, so you can use it
4550 if you need to keep the field for some reason:
4551
4552 ```
4553 #![feature(repr_align)]
4554
4555 use std::marker::PhantomData;
4556
4557 #[repr(align(32))]
4558 struct ForceAlign32;
4559
4560 #[repr(transparent)]
4561 struct Wrapper(f32, PhantomData<ForceAlign32>);
4562 ```
4563
4564 Note that empty arrays `[T; 0]` have the same alignment requirement as the
4565 element type `T`. Also note that the error is conservatively reported even when
4566 the alignment of the zero-sized type is less than or equal to the data field's
4567 alignment.
4568 "##,
4569
4570
4571 E0699: r##"
4572 A method was called on a raw pointer whose inner type wasn't completely known.
4573
4574 For example, you may have done something like:
4575
4576 ```compile_fail
4577 # #![deny(warnings)]
4578 let foo = &1;
4579 let bar = foo as *const _;
4580 if bar.is_null() {
4581     // ...
4582 }
4583 ```
4584
4585 Here, the type of `bar` isn't known; it could be a pointer to anything. Instead,
4586 specify a type for the pointer (preferably something that makes sense for the
4587 thing you're pointing to):
4588
4589 ```
4590 let foo = &1;
4591 let bar = foo as *const i32;
4592 if bar.is_null() {
4593     // ...
4594 }
4595 ```
4596
4597 Even though `is_null()` exists as a method on any raw pointer, Rust shows this
4598 error because  Rust allows for `self` to have arbitrary types (behind the
4599 arbitrary_self_types feature flag).
4600
4601 This means that someone can specify such a function:
4602
4603 ```ignore (cannot-doctest-feature-doesnt-exist-yet)
4604 impl Foo {
4605     fn is_null(self: *const Self) -> bool {
4606         // do something else
4607     }
4608 }
4609 ```
4610
4611 and now when you call `.is_null()` on a raw pointer to `Foo`, there's ambiguity.
4612
4613 Given that we don't know what type the pointer is, and there's potential
4614 ambiguity for some types, we disallow calling methods on raw pointers when
4615 the type is unknown.
4616 "##,
4617
4618 E0714: r##"
4619 A `#[marker]` trait contained an associated item.
4620
4621 The items of marker traits cannot be overridden, so there's no need to have them
4622 when they cannot be changed per-type anyway.  If you wanted them for ergonomic
4623 reasons, consider making an extension trait instead.
4624 "##,
4625
4626 E0715: r##"
4627 An `impl` for a `#[marker]` trait tried to override an associated item.
4628
4629 Because marker traits are allowed to have multiple implementations for the same
4630 type, it's not allowed to override anything in those implementations, as it
4631 would be ambiguous which override should actually be used.
4632 "##,
4633
4634
4635 E0720: r##"
4636 An `impl Trait` type expands to a recursive type.
4637
4638 An `impl Trait` type must be expandable to a concrete type that contains no
4639 `impl Trait` types. For example the following example tries to create an
4640 `impl Trait` type `T` that is equal to `[T, T]`:
4641
4642 ```compile_fail,E0720
4643 fn make_recursive_type() -> impl Sized {
4644     [make_recursive_type(), make_recursive_type()]
4645 }
4646 ```
4647 "##,
4648
4649 }
4650
4651 register_diagnostics! {
4652 //  E0035, merged into E0087/E0089
4653 //  E0036, merged into E0087/E0089
4654 //  E0068,
4655 //  E0085,
4656 //  E0086,
4657 //  E0103,
4658 //  E0104,
4659 //  E0122, // bounds in type aliases are ignored, turned into proper lint
4660 //  E0123,
4661 //  E0127,
4662 //  E0129,
4663 //  E0141,
4664 //  E0159, // use of trait `{}` as struct constructor
4665 //  E0163, // merged into E0071
4666 //  E0167,
4667 //  E0168,
4668 //  E0172, // non-trait found in a type sum, moved to resolve
4669 //  E0173, // manual implementations of unboxed closure traits are experimental
4670 //  E0174,
4671 //  E0182, // merged into E0229
4672     E0183,
4673 //  E0187, // can't infer the kind of the closure
4674 //  E0188, // can not cast an immutable reference to a mutable pointer
4675 //  E0189, // deprecated: can only cast a boxed pointer to a boxed object
4676 //  E0190, // deprecated: can only cast a &-pointer to an &-object
4677 //  E0196, // cannot determine a type for this closure
4678     E0203, // type parameter has more than one relaxed default bound,
4679            // and only one is supported
4680     E0208,
4681 //  E0209, // builtin traits can only be implemented on structs or enums
4682     E0212, // cannot extract an associated type from a higher-ranked trait bound
4683 //  E0213, // associated types are not accepted in this context
4684 //  E0215, // angle-bracket notation is not stable with `Fn`
4685 //  E0216, // parenthetical notation is only stable with `Fn`
4686 //  E0217, // ambiguous associated type, defined in multiple supertraits
4687 //  E0218, // no associated type defined
4688 //  E0219, // associated type defined in higher-ranked supertrait
4689 //  E0222, // Error code E0045 (variadic function must have C or cdecl calling
4690            // convention) duplicate
4691     E0224, // at least one non-builtin train is required for an object type
4692     E0227, // ambiguous lifetime bound, explicit lifetime bound required
4693     E0228, // explicit lifetime bound required
4694 //  E0233,
4695 //  E0234,
4696 //  E0235, // structure constructor specifies a structure of type but
4697 //  E0236, // no lang item for range syntax
4698 //  E0237, // no lang item for range syntax
4699 //  E0238, // parenthesized parameters may only be used with a trait
4700 //  E0239, // `next` method of `Iterator` trait has unexpected type
4701 //  E0240,
4702 //  E0241,
4703 //  E0242,
4704 //  E0245, // not a trait
4705 //  E0246, // invalid recursive type
4706 //  E0247,
4707 //  E0248, // value used as a type, now reported earlier during resolution as E0412
4708 //  E0249,
4709     E0307, // invalid method `self` type
4710 //  E0319, // trait impls for defaulted traits allowed just for structs/enums
4711 //  E0372, // coherence not object safe
4712     E0377, // the trait `CoerceUnsized` may only be implemented for a coercion
4713            // between structures with the same definition
4714 //  E0558, // replaced with a generic attribute input check
4715     E0533, // `{}` does not name a unit variant, unit struct or a constant
4716 //  E0563, // cannot determine a type for this `impl Trait`: {} // removed in 6383de15
4717     E0564, // only named lifetimes are allowed in `impl Trait`,
4718            // but `{}` was found in the type `{}`
4719     E0587, // type has conflicting packed and align representation hints
4720     E0588, // packed type cannot transitively contain a `[repr(align)]` type
4721     E0592, // duplicate definitions with name `{}`
4722 //  E0611, // merged into E0616
4723 //  E0612, // merged into E0609
4724 //  E0613, // Removed (merged with E0609)
4725     E0627, // yield statement outside of generator literal
4726     E0632, // cannot provide explicit type parameters when `impl Trait` is used in
4727            // argument position.
4728     E0634, // type has conflicting packed representaton hints
4729     E0640, // infer outlives requirements
4730     E0641, // cannot cast to/from a pointer with an unknown kind
4731     E0645, // trait aliases not finished
4732     E0719, // duplicate values for associated type binding
4733     E0722, // Malformed #[optimize] attribute
4734     E0724, // `#[ffi_returns_twice]` is only allowed in foreign functions
4735 }