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