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