]> git.lizzy.rs Git - rust.git/blob - src/librustc_typeck/diagnostics.rs
Auto merge of #35856 - phimuemue:master, r=brson
[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 ```ignore
230 let trait_obj: &SomeTrait = ...;
231
232 // This tries to implicitly dereference to create an unsized local variable.
233 let &invalid = trait_obj;
234
235 // You can call methods without binding to the value being pointed at.
236 trait_obj.method_one();
237 trait_obj.method_two();
238 ```
239
240 You can read more about trait objects in the Trait Object section of the
241 Reference:
242
243 https://doc.rust-lang.org/reference.html#trait-objects
244 "##,
245
246 E0034: r##"
247 The compiler doesn't know what method to call because more than one method
248 has the same prototype. Erroneous code example:
249
250 ```compile_fail,E0034
251 struct Test;
252
253 trait Trait1 {
254     fn foo();
255 }
256
257 trait Trait2 {
258     fn foo();
259 }
260
261 impl Trait1 for Test { fn foo() {} }
262 impl Trait2 for Test { fn foo() {} }
263
264 fn main() {
265     Test::foo() // error, which foo() to call?
266 }
267 ```
268
269 To avoid this error, you have to keep only one of them and remove the others.
270 So let's take our example and fix it:
271
272 ```
273 struct Test;
274
275 trait Trait1 {
276     fn foo();
277 }
278
279 impl Trait1 for Test { fn foo() {} }
280
281 fn main() {
282     Test::foo() // and now that's good!
283 }
284 ```
285
286 However, a better solution would be using fully explicit naming of type and
287 trait:
288
289 ```
290 struct Test;
291
292 trait Trait1 {
293     fn foo();
294 }
295
296 trait Trait2 {
297     fn foo();
298 }
299
300 impl Trait1 for Test { fn foo() {} }
301 impl Trait2 for Test { fn foo() {} }
302
303 fn main() {
304     <Test as Trait1>::foo()
305 }
306 ```
307
308 One last example:
309
310 ```
311 trait F {
312     fn m(&self);
313 }
314
315 trait G {
316     fn m(&self);
317 }
318
319 struct X;
320
321 impl F for X { fn m(&self) { println!("I am F"); } }
322 impl G for X { fn m(&self) { println!("I am G"); } }
323
324 fn main() {
325     let f = X;
326
327     F::m(&f); // it displays "I am F"
328     G::m(&f); // it displays "I am G"
329 }
330 ```
331 "##,
332
333 E0035: r##"
334 You tried to give a type parameter where it wasn't needed. Erroneous code
335 example:
336
337 ```compile_fail,E0035
338 struct Test;
339
340 impl Test {
341     fn method(&self) {}
342 }
343
344 fn main() {
345     let x = Test;
346
347     x.method::<i32>(); // Error: Test::method doesn't need type parameter!
348 }
349 ```
350
351 To fix this error, just remove the type parameter:
352
353 ```
354 struct Test;
355
356 impl Test {
357     fn method(&self) {}
358 }
359
360 fn main() {
361     let x = Test;
362
363     x.method(); // OK, we're good!
364 }
365 ```
366 "##,
367
368 E0036: r##"
369 This error occurrs when you pass too many or not enough type parameters to
370 a method. Erroneous code example:
371
372 ```compile_fail,E0036
373 struct Test;
374
375 impl Test {
376     fn method<T>(&self, v: &[T]) -> usize {
377         v.len()
378     }
379 }
380
381 fn main() {
382     let x = Test;
383     let v = &[0];
384
385     x.method::<i32, i32>(v); // error: only one type parameter is expected!
386 }
387 ```
388
389 To fix it, just specify a correct number of type parameters:
390
391 ```
392 struct Test;
393
394 impl Test {
395     fn method<T>(&self, v: &[T]) -> usize {
396         v.len()
397     }
398 }
399
400 fn main() {
401     let x = Test;
402     let v = &[0];
403
404     x.method::<i32>(v); // OK, we're good!
405 }
406 ```
407
408 Please note on the last example that we could have called `method` like this:
409
410 ```ignore
411 x.method(v);
412 ```
413 "##,
414
415 E0040: r##"
416 It is not allowed to manually call destructors in Rust. It is also not
417 necessary to do this since `drop` is called automatically whenever a value goes
418 out of scope.
419
420 Here's an example of this error:
421
422 ```compile_fail,E0040
423 struct Foo {
424     x: i32,
425 }
426
427 impl Drop for Foo {
428     fn drop(&mut self) {
429         println!("kaboom");
430     }
431 }
432
433 fn main() {
434     let mut x = Foo { x: -7 };
435     x.drop(); // error: explicit use of destructor method
436 }
437 ```
438 "##,
439
440 E0044: r##"
441 You can't use type parameters on foreign items. Example of erroneous code:
442
443 ```compile_fail,E0044
444 extern { fn some_func<T>(x: T); }
445 ```
446
447 To fix this, replace the type parameter with the specializations that you
448 need:
449
450 ```
451 extern { fn some_func_i32(x: i32); }
452 extern { fn some_func_i64(x: i64); }
453 ```
454 "##,
455
456 E0045: r##"
457 Rust only supports variadic parameters for interoperability with C code in its
458 FFI. As such, variadic parameters can only be used with functions which are
459 using the C ABI. Examples of erroneous code:
460
461 ```compile_fail,E0045
462 #![feature(unboxed_closures)]
463
464 extern "rust-call" { fn foo(x: u8, ...); }
465
466 // or
467
468 fn foo(x: u8, ...) {}
469 ```
470
471 To fix such code, put them in an extern "C" block:
472
473 ```
474 extern "C" {
475     fn foo (x: u8, ...);
476 }
477 ```
478 "##,
479
480 E0046: r##"
481 Items are missing in a trait implementation. Erroneous code example:
482
483 ```compile_fail,E0046
484 trait Foo {
485     fn foo();
486 }
487
488 struct Bar;
489
490 impl Foo for Bar {}
491 // error: not all trait items implemented, missing: `foo`
492 ```
493
494 When trying to make some type implement a trait `Foo`, you must, at minimum,
495 provide implementations for all of `Foo`'s required methods (meaning the
496 methods that do not have default implementations), as well as any required
497 trait items like associated types or constants. Example:
498
499 ```
500 trait Foo {
501     fn foo();
502 }
503
504 struct Bar;
505
506 impl Foo for Bar {
507     fn foo() {} // ok!
508 }
509 ```
510 "##,
511
512 E0049: r##"
513 This error indicates that an attempted implementation of a trait method
514 has the wrong number of type parameters.
515
516 For example, the trait below has a method `foo` with a type parameter `T`,
517 but the implementation of `foo` for the type `Bar` is missing this parameter:
518
519 ```compile_fail,E0049
520 trait Foo {
521     fn foo<T: Default>(x: T) -> Self;
522 }
523
524 struct Bar;
525
526 // error: method `foo` has 0 type parameters but its trait declaration has 1
527 // type parameter
528 impl Foo for Bar {
529     fn foo(x: bool) -> Self { Bar }
530 }
531 ```
532 "##,
533
534 E0050: r##"
535 This error indicates that an attempted implementation of a trait method
536 has the wrong number of function parameters.
537
538 For example, the trait below has a method `foo` with two function parameters
539 (`&self` and `u8`), but the implementation of `foo` for the type `Bar` omits
540 the `u8` parameter:
541
542 ```compile_fail,E0050
543 trait Foo {
544     fn foo(&self, x: u8) -> bool;
545 }
546
547 struct Bar;
548
549 // error: method `foo` has 1 parameter but the declaration in trait `Foo::foo`
550 // has 2
551 impl Foo for Bar {
552     fn foo(&self) -> bool { true }
553 }
554 ```
555 "##,
556
557 E0053: r##"
558 The parameters of any trait method must match between a trait implementation
559 and the trait definition.
560
561 Here are a couple examples of this error:
562
563 ```compile_fail,E0053
564 trait Foo {
565     fn foo(x: u16);
566     fn bar(&self);
567 }
568
569 struct Bar;
570
571 impl Foo for Bar {
572     // error, expected u16, found i16
573     fn foo(x: i16) { }
574
575     // error, types differ in mutability
576     fn bar(&mut self) { }
577 }
578 ```
579 "##,
580
581 E0054: r##"
582 It is not allowed to cast to a bool. If you are trying to cast a numeric type
583 to a bool, you can compare it with zero instead:
584
585 ```compile_fail,E0054
586 let x = 5;
587
588 // Not allowed, won't compile
589 let x_is_nonzero = x as bool;
590 ```
591
592 ```
593 let x = 5;
594
595 // Ok
596 let x_is_nonzero = x != 0;
597 ```
598 "##,
599
600 E0055: r##"
601 During a method call, a value is automatically dereferenced as many times as
602 needed to make the value's type match the method's receiver. The catch is that
603 the compiler will only attempt to dereference a number of times up to the
604 recursion limit (which can be set via the `recursion_limit` attribute).
605
606 For a somewhat artificial example:
607
608 ```compile_fail,E0055
609 #![recursion_limit="2"]
610
611 struct Foo;
612
613 impl Foo {
614     fn foo(&self) {}
615 }
616
617 fn main() {
618     let foo = Foo;
619     let ref_foo = &&Foo;
620
621     // error, reached the recursion limit while auto-dereferencing &&Foo
622     ref_foo.foo();
623 }
624 ```
625
626 One fix may be to increase the recursion limit. Note that it is possible to
627 create an infinite recursion of dereferencing, in which case the only fix is to
628 somehow break the recursion.
629 "##,
630
631 E0057: r##"
632 When invoking closures or other implementations of the function traits `Fn`,
633 `FnMut` or `FnOnce` using call notation, the number of parameters passed to the
634 function must match its definition.
635
636 An example using a closure:
637
638 ```compile_fail,E0057
639 let f = |x| x * 3;
640 let a = f();        // invalid, too few parameters
641 let b = f(4);       // this works!
642 let c = f(2, 3);    // invalid, too many parameters
643 ```
644
645 A generic function must be treated similarly:
646
647 ```
648 fn foo<F: Fn()>(f: F) {
649     f(); // this is valid, but f(3) would not work
650 }
651 ```
652 "##,
653
654 E0059: r##"
655 The built-in function traits are generic over a tuple of the function arguments.
656 If one uses angle-bracket notation (`Fn<(T,), Output=U>`) instead of parentheses
657 (`Fn(T) -> U`) to denote the function trait, the type parameter should be a
658 tuple. Otherwise function call notation cannot be used and the trait will not be
659 implemented by closures.
660
661 The most likely source of this error is using angle-bracket notation without
662 wrapping the function argument type into a tuple, for example:
663
664 ```compile_fail,E0059
665 #![feature(unboxed_closures)]
666
667 fn foo<F: Fn<i32>>(f: F) -> F::Output { f(3) }
668 ```
669
670 It can be fixed by adjusting the trait bound like this:
671
672 ```
673 #![feature(unboxed_closures)]
674
675 fn foo<F: Fn<(i32,)>>(f: F) -> F::Output { f(3) }
676 ```
677
678 Note that `(T,)` always denotes the type of a 1-tuple containing an element of
679 type `T`. The comma is necessary for syntactic disambiguation.
680 "##,
681
682 E0060: r##"
683 External C functions are allowed to be variadic. However, a variadic function
684 takes a minimum number of arguments. For example, consider C's variadic `printf`
685 function:
686
687 ```ignore
688 extern crate libc;
689 use libc::{ c_char, c_int };
690
691 extern "C" {
692     fn printf(_: *const c_char, ...) -> c_int;
693 }
694 ```
695
696 Using this declaration, it must be called with at least one argument, so
697 simply calling `printf()` is invalid. But the following uses are allowed:
698
699 ```ignore
700 unsafe {
701     use std::ffi::CString;
702
703     printf(CString::new("test\n").unwrap().as_ptr());
704     printf(CString::new("number = %d\n").unwrap().as_ptr(), 3);
705     printf(CString::new("%d, %d\n").unwrap().as_ptr(), 10, 5);
706 }
707 ```
708 "##,
709
710 E0061: r##"
711 The number of arguments passed to a function must match the number of arguments
712 specified in the function signature.
713
714 For example, a function like:
715
716 ```
717 fn f(a: u16, b: &str) {}
718 ```
719
720 Must always be called with exactly two arguments, e.g. `f(2, "test")`.
721
722 Note that Rust does not have a notion of optional function arguments or
723 variadic functions (except for its C-FFI).
724 "##,
725
726 E0062: r##"
727 This error indicates that during an attempt to build a struct or struct-like
728 enum variant, one of the fields was specified more than once. Erroneous code
729 example:
730
731 ```compile_fail,E0062
732 struct Foo {
733     x: i32,
734 }
735
736 fn main() {
737     let x = Foo {
738                 x: 0,
739                 x: 0, // error: field `x` specified more than once
740             };
741 }
742 ```
743
744 Each field should be specified exactly one time. Example:
745
746 ```
747 struct Foo {
748     x: i32,
749 }
750
751 fn main() {
752     let x = Foo { x: 0 }; // ok!
753 }
754 ```
755 "##,
756
757 E0063: r##"
758 This error indicates that during an attempt to build a struct or struct-like
759 enum variant, one of the fields was not provided. Erroneous code example:
760
761 ```compile_fail,E0063
762 struct Foo {
763     x: i32,
764     y: i32,
765 }
766
767 fn main() {
768     let x = Foo { x: 0 }; // error: missing field: `y`
769 }
770 ```
771
772 Each field should be specified exactly once. Example:
773
774 ```
775 struct Foo {
776     x: i32,
777     y: i32,
778 }
779
780 fn main() {
781     let x = Foo { x: 0, y: 0 }; // ok!
782 }
783 ```
784 "##,
785
786 E0066: r##"
787 Box placement expressions (like C++'s "placement new") do not yet support any
788 place expression except the exchange heap (i.e. `std::boxed::HEAP`).
789 Furthermore, the syntax is changing to use `in` instead of `box`. See [RFC 470]
790 and [RFC 809] for more details.
791
792 [RFC 470]: https://github.com/rust-lang/rfcs/pull/470
793 [RFC 809]: https://github.com/rust-lang/rfcs/pull/809
794 "##,
795
796 E0067: r##"
797 The left-hand side of a compound assignment expression must be an lvalue
798 expression. An lvalue expression represents a memory location and includes
799 item paths (ie, namespaced variables), dereferences, indexing expressions,
800 and field references.
801
802 Let's start with some erroneous code examples:
803
804 ```compile_fail,E0067
805 use std::collections::LinkedList;
806
807 // Bad: assignment to non-lvalue expression
808 LinkedList::new() += 1;
809
810 // ...
811
812 fn some_func(i: &mut i32) {
813     i += 12; // Error : '+=' operation cannot be applied on a reference !
814 }
815 ```
816
817 And now some working examples:
818
819 ```
820 let mut i : i32 = 0;
821
822 i += 12; // Good !
823
824 // ...
825
826 fn some_func(i: &mut i32) {
827     *i += 12; // Good !
828 }
829 ```
830 "##,
831
832 E0069: r##"
833 The compiler found a function whose body contains a `return;` statement but
834 whose return type is not `()`. An example of this is:
835
836 ```compile_fail,E0069
837 // error
838 fn foo() -> u8 {
839     return;
840 }
841 ```
842
843 Since `return;` is just like `return ();`, there is a mismatch between the
844 function's return type and the value being returned.
845 "##,
846
847 E0070: r##"
848 The left-hand side of an assignment operator must be an lvalue expression. An
849 lvalue expression represents a memory location and can be a variable (with
850 optional namespacing), a dereference, an indexing expression or a field
851 reference.
852
853 More details can be found here:
854 https://doc.rust-lang.org/reference.html#lvalues-rvalues-and-temporaries
855
856 Now, we can go further. Here are some erroneous code examples:
857
858 ```compile_fail,E0070
859 struct SomeStruct {
860     x: i32,
861     y: i32
862 }
863
864 const SOME_CONST : i32 = 12;
865
866 fn some_other_func() {}
867
868 fn some_function() {
869     SOME_CONST = 14; // error : a constant value cannot be changed!
870     1 = 3; // error : 1 isn't a valid lvalue!
871     some_other_func() = 4; // error : we can't assign value to a function!
872     SomeStruct.x = 12; // error : SomeStruct a structure name but it is used
873                        // like a variable!
874 }
875 ```
876
877 And now let's give working examples:
878
879 ```
880 struct SomeStruct {
881     x: i32,
882     y: i32
883 }
884 let mut s = SomeStruct {x: 0, y: 0};
885
886 s.x = 3; // that's good !
887
888 // ...
889
890 fn some_func(x: &mut i32) {
891     *x = 12; // that's good !
892 }
893 ```
894 "##,
895
896 E0071: r##"
897 You tried to use structure-literal syntax to create an item that is
898 not a struct-style structure or enum variant.
899
900 Example of erroneous code:
901
902 ```compile_fail,E0071
903 enum Foo { FirstValue(i32) };
904
905 let u = Foo::FirstValue { value: 0 }; // error: Foo::FirstValue
906                                          // isn't a structure!
907 // or even simpler, if the name doesn't refer to a structure at all.
908 let t = u32 { value: 4 }; // error: `u32` does not name a structure.
909 ```
910
911 To fix this, ensure that the name was correctly spelled, and that
912 the correct form of initializer was used.
913
914 For example, the code above can be fixed to:
915
916 ```
917 enum Foo {
918     FirstValue(i32)
919 }
920
921 fn main() {
922     let u = Foo::FirstValue(0i32);
923
924     let t = 4;
925 }
926 ```
927 "##,
928
929 E0073: r##"
930 You cannot define a struct (or enum) `Foo` that requires an instance of `Foo`
931 in order to make a new `Foo` value. This is because there would be no way a
932 first instance of `Foo` could be made to initialize another instance!
933
934 Here's an example of a struct that has this problem:
935
936 ```ignore
937 struct Foo { x: Box<Foo> } // error
938 ```
939
940 One fix is to use `Option`, like so:
941
942 ```
943 struct Foo { x: Option<Box<Foo>> }
944 ```
945
946 Now it's possible to create at least one instance of `Foo`: `Foo { x: None }`.
947 "##,
948
949 E0074: r##"
950 When using the `#[simd]` attribute on a tuple struct, the components of the
951 tuple struct must all be of a concrete, nongeneric type so the compiler can
952 reason about how to use SIMD with them. This error will occur if the types
953 are generic.
954
955 This will cause an error:
956
957 ```ignore
958 #![feature(repr_simd)]
959
960 #[repr(simd)]
961 struct Bad<T>(T, T, T);
962 ```
963
964 This will not:
965
966 ```
967 #![feature(repr_simd)]
968
969 #[repr(simd)]
970 struct Good(u32, u32, u32);
971 ```
972 "##,
973
974 E0075: r##"
975 The `#[simd]` attribute can only be applied to non empty tuple structs, because
976 it doesn't make sense to try to use SIMD operations when there are no values to
977 operate on.
978
979 This will cause an error:
980
981 ```compile_fail,E0075
982 #![feature(repr_simd)]
983
984 #[repr(simd)]
985 struct Bad;
986 ```
987
988 This will not:
989
990 ```
991 #![feature(repr_simd)]
992
993 #[repr(simd)]
994 struct Good(u32);
995 ```
996 "##,
997
998 E0076: r##"
999 When using the `#[simd]` attribute to automatically use SIMD operations in tuple
1000 struct, the types in the struct must all be of the same type, or the compiler
1001 will trigger this error.
1002
1003 This will cause an error:
1004
1005 ```compile_fail,E0076
1006 #![feature(repr_simd)]
1007
1008 #[repr(simd)]
1009 struct Bad(u16, u32, u32);
1010 ```
1011
1012 This will not:
1013
1014 ```
1015 #![feature(repr_simd)]
1016
1017 #[repr(simd)]
1018 struct Good(u32, u32, u32);
1019 ```
1020 "##,
1021
1022 E0077: r##"
1023 When using the `#[simd]` attribute on a tuple struct, the elements in the tuple
1024 must be machine types so SIMD operations can be applied to them.
1025
1026 This will cause an error:
1027
1028 ```compile_fail,E0077
1029 #![feature(repr_simd)]
1030
1031 #[repr(simd)]
1032 struct Bad(String);
1033 ```
1034
1035 This will not:
1036
1037 ```
1038 #![feature(repr_simd)]
1039
1040 #[repr(simd)]
1041 struct Good(u32, u32, u32);
1042 ```
1043 "##,
1044
1045 E0079: r##"
1046 Enum variants which contain no data can be given a custom integer
1047 representation. This error indicates that the value provided is not an integer
1048 literal and is therefore invalid.
1049
1050 For example, in the following code:
1051
1052 ```compile_fail,E0079
1053 enum Foo {
1054     Q = "32",
1055 }
1056 ```
1057
1058 We try to set the representation to a string.
1059
1060 There's no general fix for this; if you can work with an integer then just set
1061 it to one:
1062
1063 ```
1064 enum Foo {
1065     Q = 32,
1066 }
1067 ```
1068
1069 However if you actually wanted a mapping between variants and non-integer
1070 objects, it may be preferable to use a method with a match instead:
1071
1072 ```
1073 enum Foo { Q }
1074 impl Foo {
1075     fn get_str(&self) -> &'static str {
1076         match *self {
1077             Foo::Q => "32",
1078         }
1079     }
1080 }
1081 ```
1082 "##,
1083
1084 E0081: r##"
1085 Enum discriminants are used to differentiate enum variants stored in memory.
1086 This error indicates that the same value was used for two or more variants,
1087 making them impossible to tell apart.
1088
1089 ```compile_fail,E0081
1090 // Bad.
1091 enum Enum {
1092     P = 3,
1093     X = 3,
1094     Y = 5,
1095 }
1096 ```
1097
1098 ```
1099 // Good.
1100 enum Enum {
1101     P,
1102     X = 3,
1103     Y = 5,
1104 }
1105 ```
1106
1107 Note that variants without a manually specified discriminant are numbered from
1108 top to bottom starting from 0, so clashes can occur with seemingly unrelated
1109 variants.
1110
1111 ```compile_fail,E0081
1112 enum Bad {
1113     X,
1114     Y = 0
1115 }
1116 ```
1117
1118 Here `X` will have already been specified the discriminant 0 by the time `Y` is
1119 encountered, so a conflict occurs.
1120 "##,
1121
1122 E0082: r##"
1123 When you specify enum discriminants with `=`, the compiler expects `isize`
1124 values by default. Or you can add the `repr` attibute to the enum declaration
1125 for an explicit choice of the discriminant type. In either cases, the
1126 discriminant values must fall within a valid range for the expected type;
1127 otherwise this error is raised. For example:
1128
1129 ```ignore
1130 #[repr(u8)]
1131 enum Thing {
1132     A = 1024,
1133     B = 5,
1134 }
1135 ```
1136
1137 Here, 1024 lies outside the valid range for `u8`, so the discriminant for `A` is
1138 invalid. Here is another, more subtle example which depends on target word size:
1139
1140 ```ignore
1141 enum DependsOnPointerSize {
1142     A = 1 << 32,
1143 }
1144 ```
1145
1146 Here, `1 << 32` is interpreted as an `isize` value. So it is invalid for 32 bit
1147 target (`target_pointer_width = "32"`) but valid for 64 bit target.
1148
1149 You may want to change representation types to fix this, or else change invalid
1150 discriminant values so that they fit within the existing type.
1151 "##,
1152
1153 E0084: r##"
1154 An unsupported representation was attempted on a zero-variant enum.
1155
1156 Erroneous code example:
1157
1158 ```compile_fail,E0084
1159 #[repr(i32)]
1160 enum NightsWatch {} // error: unsupported representation for zero-variant enum
1161 ```
1162
1163 It is impossible to define an integer type to be used to represent zero-variant
1164 enum values because there are no zero-variant enum values. There is no way to
1165 construct an instance of the following type using only safe code. So you have
1166 two solutions. Either you add variants in your enum:
1167
1168 ```
1169 #[repr(i32)]
1170 enum NightsWatch {
1171     JonSnow,
1172     Commander,
1173 }
1174 ```
1175
1176 or you remove the integer represention of your enum:
1177
1178 ```
1179 enum NightsWatch {}
1180 ```
1181 "##,
1182
1183 E0087: r##"
1184 Too many type parameters were supplied for a function. For example:
1185
1186 ```compile_fail,E0087
1187 fn foo<T>() {}
1188
1189 fn main() {
1190     foo::<f64, bool>(); // error, expected 1 parameter, found 2 parameters
1191 }
1192 ```
1193
1194 The number of supplied parameters must exactly match the number of defined type
1195 parameters.
1196 "##,
1197
1198 E0088: r##"
1199 You gave too many lifetime parameters. Erroneous code example:
1200
1201 ```compile_fail,E0088
1202 fn f() {}
1203
1204 fn main() {
1205     f::<'static>() // error: too many lifetime parameters provided
1206 }
1207 ```
1208
1209 Please check you give the right number of lifetime parameters. Example:
1210
1211 ```
1212 fn f() {}
1213
1214 fn main() {
1215     f() // ok!
1216 }
1217 ```
1218
1219 It's also important to note that the Rust compiler can generally
1220 determine the lifetime by itself. Example:
1221
1222 ```
1223 struct Foo {
1224     value: String
1225 }
1226
1227 impl Foo {
1228     // it can be written like this
1229     fn get_value<'a>(&'a self) -> &'a str { &self.value }
1230     // but the compiler works fine with this too:
1231     fn without_lifetime(&self) -> &str { &self.value }
1232 }
1233
1234 fn main() {
1235     let f = Foo { value: "hello".to_owned() };
1236
1237     println!("{}", f.get_value());
1238     println!("{}", f.without_lifetime());
1239 }
1240 ```
1241 "##,
1242
1243 E0089: r##"
1244 Not enough type parameters were supplied for a function. For example:
1245
1246 ```compile_fail,E0089
1247 fn foo<T, U>() {}
1248
1249 fn main() {
1250     foo::<f64>(); // error, expected 2 parameters, found 1 parameter
1251 }
1252 ```
1253
1254 Note that if a function takes multiple type parameters but you want the compiler
1255 to infer some of them, you can use type placeholders:
1256
1257 ```compile_fail,E0089
1258 fn foo<T, U>(x: T) {}
1259
1260 fn main() {
1261     let x: bool = true;
1262     foo::<f64>(x);    // error, expected 2 parameters, found 1 parameter
1263     foo::<_, f64>(x); // same as `foo::<bool, f64>(x)`
1264 }
1265 ```
1266 "##,
1267
1268 E0091: r##"
1269 You gave an unnecessary type parameter in a type alias. Erroneous code
1270 example:
1271
1272 ```compile_fail,E0091
1273 type Foo<T> = u32; // error: type parameter `T` is unused
1274 // or:
1275 type Foo<A,B> = Box<A>; // error: type parameter `B` is unused
1276 ```
1277
1278 Please check you didn't write too many type parameters. Example:
1279
1280 ```
1281 type Foo = u32; // ok!
1282 type Foo2<A> = Box<A>; // ok!
1283 ```
1284 "##,
1285
1286 E0092: r##"
1287 You tried to declare an undefined atomic operation function.
1288 Erroneous code example:
1289
1290 ```compile_fail,E0092
1291 #![feature(intrinsics)]
1292
1293 extern "rust-intrinsic" {
1294     fn atomic_foo(); // error: unrecognized atomic operation
1295                      //        function
1296 }
1297 ```
1298
1299 Please check you didn't make a mistake in the function's name. All intrinsic
1300 functions are defined in librustc_trans/trans/intrinsic.rs and in
1301 libcore/intrinsics.rs in the Rust source code. Example:
1302
1303 ```
1304 #![feature(intrinsics)]
1305
1306 extern "rust-intrinsic" {
1307     fn atomic_fence(); // ok!
1308 }
1309 ```
1310 "##,
1311
1312 E0093: r##"
1313 You declared an unknown intrinsic function. Erroneous code example:
1314
1315 ```compile_fail,E0093
1316 #![feature(intrinsics)]
1317
1318 extern "rust-intrinsic" {
1319     fn foo(); // error: unrecognized intrinsic function: `foo`
1320 }
1321
1322 fn main() {
1323     unsafe {
1324         foo();
1325     }
1326 }
1327 ```
1328
1329 Please check you didn't make a mistake in the function's name. All intrinsic
1330 functions are defined in librustc_trans/trans/intrinsic.rs and in
1331 libcore/intrinsics.rs in the Rust source code. Example:
1332
1333 ```
1334 #![feature(intrinsics)]
1335
1336 extern "rust-intrinsic" {
1337     fn atomic_fence(); // ok!
1338 }
1339
1340 fn main() {
1341     unsafe {
1342         atomic_fence();
1343     }
1344 }
1345 ```
1346 "##,
1347
1348 E0094: r##"
1349 You gave an invalid number of type parameters to an intrinsic function.
1350 Erroneous code example:
1351
1352 ```compile_fail,E0094
1353 #![feature(intrinsics)]
1354
1355 extern "rust-intrinsic" {
1356     fn size_of<T, U>() -> usize; // error: intrinsic has wrong number
1357                                  //        of type parameters
1358 }
1359 ```
1360
1361 Please check that you provided the right number of lifetime parameters
1362 and verify with the function declaration in the Rust source code.
1363 Example:
1364
1365 ```
1366 #![feature(intrinsics)]
1367
1368 extern "rust-intrinsic" {
1369     fn size_of<T>() -> usize; // ok!
1370 }
1371 ```
1372 "##,
1373
1374 E0101: r##"
1375 You hit this error because the compiler lacks the information to
1376 determine a type for this expression. Erroneous code example:
1377
1378 ```compile_fail,E0101
1379 let x = |_| {}; // error: cannot determine a type for this expression
1380 ```
1381
1382 You have two possibilities to solve this situation:
1383  * Give an explicit definition of the expression
1384  * Infer the expression
1385
1386 Examples:
1387
1388 ```
1389 let x = |_ : u32| {}; // ok!
1390 // or:
1391 let x = |_| {};
1392 x(0u32);
1393 ```
1394 "##,
1395
1396 E0102: r##"
1397 You hit this error because the compiler lacks the information to
1398 determine the type of this variable. Erroneous code example:
1399
1400 ```compile_fail,E0102
1401 // could be an array of anything
1402 let x = []; // error: cannot determine a type for this local variable
1403 ```
1404
1405 To solve this situation, constrain the type of the variable.
1406 Examples:
1407
1408 ```
1409 #![allow(unused_variables)]
1410
1411 fn main() {
1412     let x: [u8; 0] = [];
1413 }
1414 ```
1415 "##,
1416
1417 E0106: r##"
1418 This error indicates that a lifetime is missing from a type. If it is an error
1419 inside a function signature, the problem may be with failing to adhere to the
1420 lifetime elision rules (see below).
1421
1422 Here are some simple examples of where you'll run into this error:
1423
1424 ```compile_fail,E0106
1425 struct Foo { x: &bool }        // error
1426 struct Foo<'a> { x: &'a bool } // correct
1427
1428 enum Bar { A(u8), B(&bool), }        // error
1429 enum Bar<'a> { A(u8), B(&'a bool), } // correct
1430
1431 type MyStr = &str;        // error
1432 type MyStr<'a> = &'a str; // correct
1433 ```
1434
1435 Lifetime elision is a special, limited kind of inference for lifetimes in
1436 function signatures which allows you to leave out lifetimes in certain cases.
1437 For more background on lifetime elision see [the book][book-le].
1438
1439 The lifetime elision rules require that any function signature with an elided
1440 output lifetime must either have
1441
1442  - exactly one input lifetime
1443  - or, multiple input lifetimes, but the function must also be a method with a
1444    `&self` or `&mut self` receiver
1445
1446 In the first case, the output lifetime is inferred to be the same as the unique
1447 input lifetime. In the second case, the lifetime is instead inferred to be the
1448 same as the lifetime on `&self` or `&mut self`.
1449
1450 Here are some examples of elision errors:
1451
1452 ```compile_fail,E0106
1453 // error, no input lifetimes
1454 fn foo() -> &str { }
1455
1456 // error, `x` and `y` have distinct lifetimes inferred
1457 fn bar(x: &str, y: &str) -> &str { }
1458
1459 // error, `y`'s lifetime is inferred to be distinct from `x`'s
1460 fn baz<'a>(x: &'a str, y: &str) -> &str { }
1461 ```
1462
1463 [book-le]: https://doc.rust-lang.org/nightly/book/lifetimes.html#lifetime-elision
1464 "##,
1465
1466 E0107: r##"
1467 This error means that an incorrect number of lifetime parameters were provided
1468 for a type (like a struct or enum) or trait.
1469
1470 Some basic examples include:
1471
1472 ```compile_fail,E0107
1473 struct Foo<'a>(&'a str);
1474 enum Bar { A, B, C }
1475
1476 struct Baz<'a> {
1477     foo: Foo,     // error: expected 1, found 0
1478     bar: Bar<'a>, // error: expected 0, found 1
1479 }
1480 ```
1481
1482 Here's an example that is currently an error, but may work in a future version
1483 of Rust:
1484
1485 ```compile_fail,E0107
1486 struct Foo<'a>(&'a str);
1487
1488 trait Quux { }
1489 impl Quux for Foo { } // error: expected 1, found 0
1490 ```
1491
1492 Lifetime elision in implementation headers was part of the lifetime elision
1493 RFC. It is, however, [currently unimplemented][iss15872].
1494
1495 [iss15872]: https://github.com/rust-lang/rust/issues/15872
1496 "##,
1497
1498 E0116: r##"
1499 You can only define an inherent implementation for a type in the same crate
1500 where the type was defined. For example, an `impl` block as below is not allowed
1501 since `Vec` is defined in the standard library:
1502
1503 ```compile_fail,E0116
1504 impl Vec<u8> { } // error
1505 ```
1506
1507 To fix this problem, you can do either of these things:
1508
1509  - define a trait that has the desired associated functions/types/constants and
1510    implement the trait for the type in question
1511  - define a new type wrapping the type and define an implementation on the new
1512    type
1513
1514 Note that using the `type` keyword does not work here because `type` only
1515 introduces a type alias:
1516
1517 ```compile_fail,E0116
1518 type Bytes = Vec<u8>;
1519
1520 impl Bytes { } // error, same as above
1521 ```
1522 "##,
1523
1524 E0117: r##"
1525 This error indicates a violation of one of Rust's orphan rules for trait
1526 implementations. The rule prohibits any implementation of a foreign trait (a
1527 trait defined in another crate) where
1528
1529  - the type that is implementing the trait is foreign
1530  - all of the parameters being passed to the trait (if there are any) are also
1531    foreign.
1532
1533 Here's one example of this error:
1534
1535 ```compile_fail,E0117
1536 impl Drop for u32 {}
1537 ```
1538
1539 To avoid this kind of error, ensure that at least one local type is referenced
1540 by the `impl`:
1541
1542 ```ignore
1543 pub struct Foo; // you define your type in your crate
1544
1545 impl Drop for Foo { // and you can implement the trait on it!
1546     // code of trait implementation here
1547 }
1548
1549 impl From<Foo> for i32 { // or you use a type from your crate as
1550                          // a type parameter
1551     fn from(i: Foo) -> i32 {
1552         0
1553     }
1554 }
1555 ```
1556
1557 Alternatively, define a trait locally and implement that instead:
1558
1559 ```
1560 trait Bar {
1561     fn get(&self) -> usize;
1562 }
1563
1564 impl Bar for u32 {
1565     fn get(&self) -> usize { 0 }
1566 }
1567 ```
1568
1569 For information on the design of the orphan rules, see [RFC 1023].
1570
1571 [RFC 1023]: https://github.com/rust-lang/rfcs/pull/1023
1572 "##,
1573
1574 E0118: r##"
1575 You're trying to write an inherent implementation for something which isn't a
1576 struct nor an enum. Erroneous code example:
1577
1578 ```compile_fail,E0118
1579 impl (u8, u8) { // error: no base type found for inherent implementation
1580     fn get_state(&self) -> String {
1581         // ...
1582     }
1583 }
1584 ```
1585
1586 To fix this error, please implement a trait on the type or wrap it in a struct.
1587 Example:
1588
1589 ```
1590 // we create a trait here
1591 trait LiveLongAndProsper {
1592     fn get_state(&self) -> String;
1593 }
1594
1595 // and now you can implement it on (u8, u8)
1596 impl LiveLongAndProsper for (u8, u8) {
1597     fn get_state(&self) -> String {
1598         "He's dead, Jim!".to_owned()
1599     }
1600 }
1601 ```
1602
1603 Alternatively, you can create a newtype. A newtype is a wrapping tuple-struct.
1604 For example, `NewType` is a newtype over `Foo` in `struct NewType(Foo)`.
1605 Example:
1606
1607 ```
1608 struct TypeWrapper((u8, u8));
1609
1610 impl TypeWrapper {
1611     fn get_state(&self) -> String {
1612         "Fascinating!".to_owned()
1613     }
1614 }
1615 ```
1616 "##,
1617
1618 E0119: r##"
1619 There are conflicting trait implementations for the same type.
1620 Example of erroneous code:
1621
1622 ```compile_fail,E0119
1623 trait MyTrait {
1624     fn get(&self) -> usize;
1625 }
1626
1627 impl<T> MyTrait for T {
1628     fn get(&self) -> usize { 0 }
1629 }
1630
1631 struct Foo {
1632     value: usize
1633 }
1634
1635 impl MyTrait for Foo { // error: conflicting implementations of trait
1636                        //        `MyTrait` for type `Foo`
1637     fn get(&self) -> usize { self.value }
1638 }
1639 ```
1640
1641 When looking for the implementation for the trait, the compiler finds
1642 both the `impl<T> MyTrait for T` where T is all types and the `impl
1643 MyTrait for Foo`. Since a trait cannot be implemented multiple times,
1644 this is an error. So, when you write:
1645
1646 ```
1647 trait MyTrait {
1648     fn get(&self) -> usize;
1649 }
1650
1651 impl<T> MyTrait for T {
1652     fn get(&self) -> usize { 0 }
1653 }
1654 ```
1655
1656 This makes the trait implemented on all types in the scope. So if you
1657 try to implement it on another one after that, the implementations will
1658 conflict. Example:
1659
1660 ```
1661 trait MyTrait {
1662     fn get(&self) -> usize;
1663 }
1664
1665 impl<T> MyTrait for T {
1666     fn get(&self) -> usize { 0 }
1667 }
1668
1669 struct Foo;
1670
1671 fn main() {
1672     let f = Foo;
1673
1674     f.get(); // the trait is implemented so we can use it
1675 }
1676 ```
1677 "##,
1678
1679 E0120: r##"
1680 An attempt was made to implement Drop on a trait, which is not allowed: only
1681 structs and enums can implement Drop. An example causing this error:
1682
1683 ```compile_fail,E0120
1684 trait MyTrait {}
1685
1686 impl Drop for MyTrait {
1687     fn drop(&mut self) {}
1688 }
1689 ```
1690
1691 A workaround for this problem is to wrap the trait up in a struct, and implement
1692 Drop on that. An example is shown below:
1693
1694 ```
1695 trait MyTrait {}
1696 struct MyWrapper<T: MyTrait> { foo: T }
1697
1698 impl <T: MyTrait> Drop for MyWrapper<T> {
1699     fn drop(&mut self) {}
1700 }
1701
1702 ```
1703
1704 Alternatively, wrapping trait objects requires something like the following:
1705
1706 ```
1707 trait MyTrait {}
1708
1709 //or Box<MyTrait>, if you wanted an owned trait object
1710 struct MyWrapper<'a> { foo: &'a MyTrait }
1711
1712 impl <'a> Drop for MyWrapper<'a> {
1713     fn drop(&mut self) {}
1714 }
1715 ```
1716 "##,
1717
1718 E0121: r##"
1719 In order to be consistent with Rust's lack of global type inference, type
1720 placeholders are disallowed by design in item signatures.
1721
1722 Examples of this error include:
1723
1724 ```compile_fail,E0121
1725 fn foo() -> _ { 5 } // error, explicitly write out the return type instead
1726
1727 static BAR: _ = "test"; // error, explicitly write out the type instead
1728 ```
1729 "##,
1730
1731 E0122: r##"
1732 An attempt was made to add a generic constraint to a type alias. While Rust will
1733 allow this with a warning, it will not currently enforce the constraint.
1734 Consider the example below:
1735
1736 ```
1737 trait Foo{}
1738
1739 type MyType<R: Foo> = (R, ());
1740
1741 fn main() {
1742     let t: MyType<u32>;
1743 }
1744 ```
1745
1746 We're able to declare a variable of type `MyType<u32>`, despite the fact that
1747 `u32` does not implement `Foo`. As a result, one should avoid using generic
1748 constraints in concert with type aliases.
1749 "##,
1750
1751 E0124: r##"
1752 You declared two fields of a struct with the same name. Erroneous code
1753 example:
1754
1755 ```compile_fail,E0124
1756 struct Foo {
1757     field1: i32,
1758     field1: i32, // error: field is already declared
1759 }
1760 ```
1761
1762 Please verify that the field names have been correctly spelled. Example:
1763
1764 ```
1765 struct Foo {
1766     field1: i32,
1767     field2: i32, // ok!
1768 }
1769 ```
1770 "##,
1771
1772 E0128: r##"
1773 Type parameter defaults can only use parameters that occur before them.
1774 Erroneous code example:
1775
1776 ```compile_fail,E0128
1777 struct Foo<T=U, U=()> {
1778     field1: T,
1779     filed2: U,
1780 }
1781 // error: type parameters with a default cannot use forward declared
1782 // identifiers
1783 ```
1784
1785 Since type parameters are evaluated in-order, you may be able to fix this issue
1786 by doing:
1787
1788 ```
1789 struct Foo<U=(), T=U> {
1790     field1: T,
1791     filed2: U,
1792 }
1793 ```
1794
1795 Please also verify that this wasn't because of a name-clash and rename the type
1796 parameter if so.
1797 "##,
1798
1799 E0131: r##"
1800 It is not possible to define `main` with type parameters, or even with function
1801 parameters. When `main` is present, it must take no arguments and return `()`.
1802 Erroneous code example:
1803
1804 ```compile_fail,E0131
1805 fn main<T>() { // error: main function is not allowed to have type parameters
1806 }
1807 ```
1808 "##,
1809
1810 E0132: r##"
1811 A function with the `start` attribute was declared with type parameters.
1812
1813 Erroneous code example:
1814
1815 ```compile_fail,E0132
1816 #![feature(start)]
1817
1818 #[start]
1819 fn f<T>() {}
1820 ```
1821
1822 It is not possible to declare type parameters on a function that has the `start`
1823 attribute. Such a function must have the following type signature (for more
1824 information: http://doc.rust-lang.org/stable/book/no-stdlib.html):
1825
1826 ```ignore
1827 fn(isize, *const *const u8) -> isize;
1828 ```
1829
1830 Example:
1831
1832 ```
1833 #![feature(start)]
1834
1835 #[start]
1836 fn my_start(argc: isize, argv: *const *const u8) -> isize {
1837     0
1838 }
1839 ```
1840 "##,
1841
1842 E0164: r##"
1843 This error means that an attempt was made to match a struct type enum
1844 variant as a non-struct type:
1845
1846 ```compile_fail,E0164
1847 enum Foo { B { i: u32 } }
1848
1849 fn bar(foo: Foo) -> u32 {
1850     match foo {
1851         Foo::B(i) => i, // error E0164
1852     }
1853 }
1854 ```
1855
1856 Try using `{}` instead:
1857
1858 ```
1859 enum Foo { B { i: u32 } }
1860
1861 fn bar(foo: Foo) -> u32 {
1862     match foo {
1863         Foo::B{i} => i,
1864     }
1865 }
1866 ```
1867 "##,
1868
1869 E0172: r##"
1870 This error means that an attempt was made to specify the type of a variable with
1871 a combination of a concrete type and a trait. Consider the following example:
1872
1873 ```compile_fail,E0172
1874 fn foo(bar: i32+std::fmt::Display) {}
1875 ```
1876
1877 The code is trying to specify that we want to receive a signed 32-bit integer
1878 which also implements `Display`. This doesn't make sense: when we pass `i32`, a
1879 concrete type, it implicitly includes all of the traits that it implements.
1880 This includes `Display`, `Debug`, `Clone`, and a host of others.
1881
1882 If `i32` implements the trait we desire, there's no need to specify the trait
1883 separately. If it does not, then we need to `impl` the trait for `i32` before
1884 passing it into `foo`. Either way, a fixed definition for `foo` will look like
1885 the following:
1886
1887 ```
1888 fn foo(bar: i32) {}
1889 ```
1890
1891 To learn more about traits, take a look at the Book:
1892
1893 https://doc.rust-lang.org/book/traits.html
1894 "##,
1895
1896 E0178: r##"
1897 In types, the `+` type operator has low precedence, so it is often necessary
1898 to use parentheses.
1899
1900 For example:
1901
1902 ```compile_fail,E0178
1903 trait Foo {}
1904
1905 struct Bar<'a> {
1906     w: &'a Foo + Copy,   // error, use &'a (Foo + Copy)
1907     x: &'a Foo + 'a,     // error, use &'a (Foo + 'a)
1908     y: &'a mut Foo + 'a, // error, use &'a mut (Foo + 'a)
1909     z: fn() -> Foo + 'a, // error, use fn() -> (Foo + 'a)
1910 }
1911 ```
1912
1913 More details can be found in [RFC 438].
1914
1915 [RFC 438]: https://github.com/rust-lang/rfcs/pull/438
1916 "##,
1917
1918 E0184: r##"
1919 Explicitly implementing both Drop and Copy for a type is currently disallowed.
1920 This feature can make some sense in theory, but the current implementation is
1921 incorrect and can lead to memory unsafety (see [issue #20126][iss20126]), so
1922 it has been disabled for now.
1923
1924 [iss20126]: https://github.com/rust-lang/rust/issues/20126
1925 "##,
1926
1927 E0185: r##"
1928 An associated function for a trait was defined to be static, but an
1929 implementation of the trait declared the same function to be a method (i.e. to
1930 take a `self` parameter).
1931
1932 Here's an example of this error:
1933
1934 ```compile_fail,E0185
1935 trait Foo {
1936     fn foo();
1937 }
1938
1939 struct Bar;
1940
1941 impl Foo for Bar {
1942     // error, method `foo` has a `&self` declaration in the impl, but not in
1943     // the trait
1944     fn foo(&self) {}
1945 }
1946 ```
1947 "##,
1948
1949 E0186: r##"
1950 An associated function for a trait was defined to be a method (i.e. to take a
1951 `self` parameter), but an implementation of the trait declared the same function
1952 to be static.
1953
1954 Here's an example of this error:
1955
1956 ```compile_fail,E0186
1957 trait Foo {
1958     fn foo(&self);
1959 }
1960
1961 struct Bar;
1962
1963 impl Foo for Bar {
1964     // error, method `foo` has a `&self` declaration in the trait, but not in
1965     // the impl
1966     fn foo() {}
1967 }
1968 ```
1969 "##,
1970
1971 E0191: r##"
1972 Trait objects need to have all associated types specified. Erroneous code
1973 example:
1974
1975 ```compile_fail,E0191
1976 trait Trait {
1977     type Bar;
1978 }
1979
1980 type Foo = Trait; // error: the value of the associated type `Bar` (from
1981                   //        the trait `Trait`) must be specified
1982 ```
1983
1984 Please verify you specified all associated types of the trait and that you
1985 used the right trait. Example:
1986
1987 ```
1988 trait Trait {
1989     type Bar;
1990 }
1991
1992 type Foo = Trait<Bar=i32>; // ok!
1993 ```
1994 "##,
1995
1996 E0192: r##"
1997 Negative impls are only allowed for traits with default impls. For more
1998 information see the [opt-in builtin traits RFC](https://github.com/rust-lang/
1999 rfcs/blob/master/text/0019-opt-in-builtin-traits.md).
2000 "##,
2001
2002 E0193: r##"
2003 `where` clauses must use generic type parameters: it does not make sense to use
2004 them otherwise. An example causing this error:
2005
2006 ```ignore
2007 trait Foo {
2008     fn bar(&self);
2009 }
2010
2011 #[derive(Copy,Clone)]
2012 struct Wrapper<T> {
2013     Wrapped: T
2014 }
2015
2016 impl Foo for Wrapper<u32> where Wrapper<u32>: Clone {
2017     fn bar(&self) { }
2018 }
2019 ```
2020
2021 This use of a `where` clause is strange - a more common usage would look
2022 something like the following:
2023
2024 ```
2025 trait Foo {
2026     fn bar(&self);
2027 }
2028
2029 #[derive(Copy,Clone)]
2030 struct Wrapper<T> {
2031     Wrapped: T
2032 }
2033 impl <T> Foo for Wrapper<T> where Wrapper<T>: Clone {
2034     fn bar(&self) { }
2035 }
2036 ```
2037
2038 Here, we're saying that the implementation exists on Wrapper only when the
2039 wrapped type `T` implements `Clone`. The `where` clause is important because
2040 some types will not implement `Clone`, and thus will not get this method.
2041
2042 In our erroneous example, however, we're referencing a single concrete type.
2043 Since we know for certain that `Wrapper<u32>` implements `Clone`, there's no
2044 reason to also specify it in a `where` clause.
2045 "##,
2046
2047 E0194: r##"
2048 A type parameter was declared which shadows an existing one. An example of this
2049 error:
2050
2051 ```compile_fail,E0194
2052 trait Foo<T> {
2053     fn do_something(&self) -> T;
2054     fn do_something_else<T: Clone>(&self, bar: T);
2055 }
2056 ```
2057
2058 In this example, the trait `Foo` and the trait method `do_something_else` both
2059 define a type parameter `T`. This is not allowed: if the method wishes to
2060 define a type parameter, it must use a different name for it.
2061 "##,
2062
2063 E0195: r##"
2064 Your method's lifetime parameters do not match the trait declaration.
2065 Erroneous code example:
2066
2067 ```compile_fail,E0195
2068 trait Trait {
2069     fn bar<'a,'b:'a>(x: &'a str, y: &'b str);
2070 }
2071
2072 struct Foo;
2073
2074 impl Trait for Foo {
2075     fn bar<'a,'b>(x: &'a str, y: &'b str) {
2076     // error: lifetime parameters or bounds on method `bar`
2077     // do not match the trait declaration
2078     }
2079 }
2080 ```
2081
2082 The lifetime constraint `'b` for bar() implementation does not match the
2083 trait declaration. Ensure lifetime declarations match exactly in both trait
2084 declaration and implementation. Example:
2085
2086 ```
2087 trait Trait {
2088     fn t<'a,'b:'a>(x: &'a str, y: &'b str);
2089 }
2090
2091 struct Foo;
2092
2093 impl Trait for Foo {
2094     fn t<'a,'b:'a>(x: &'a str, y: &'b str) { // ok!
2095     }
2096 }
2097 ```
2098 "##,
2099
2100 E0197: r##"
2101 Inherent implementations (one that do not implement a trait but provide
2102 methods associated with a type) are always safe because they are not
2103 implementing an unsafe trait. Removing the `unsafe` keyword from the inherent
2104 implementation will resolve this error.
2105
2106 ```compile_fail,E0197
2107 struct Foo;
2108
2109 // this will cause this error
2110 unsafe impl Foo { }
2111 // converting it to this will fix it
2112 impl Foo { }
2113 ```
2114 "##,
2115
2116 E0198: r##"
2117 A negative implementation is one that excludes a type from implementing a
2118 particular trait. Not being able to use a trait is always a safe operation,
2119 so negative implementations are always safe and never need to be marked as
2120 unsafe.
2121
2122 ```compile_fail
2123 #![feature(optin_builtin_traits)]
2124
2125 struct Foo;
2126
2127 // unsafe is unnecessary
2128 unsafe impl !Clone for Foo { }
2129 ```
2130
2131 This will compile:
2132
2133 ```
2134 #![feature(optin_builtin_traits)]
2135
2136 struct Foo;
2137
2138 trait Enterprise {}
2139
2140 impl Enterprise for .. { }
2141
2142 impl !Enterprise for Foo { }
2143 ```
2144
2145 Please note that negative impls are only allowed for traits with default impls.
2146 "##,
2147
2148 E0199: r##"
2149 Safe traits should not have unsafe implementations, therefore marking an
2150 implementation for a safe trait unsafe will cause a compiler error. Removing
2151 the unsafe marker on the trait noted in the error will resolve this problem.
2152
2153 ```compile_fail,E0199
2154 struct Foo;
2155
2156 trait Bar { }
2157
2158 // this won't compile because Bar is safe
2159 unsafe impl Bar for Foo { }
2160 // this will compile
2161 impl Bar for Foo { }
2162 ```
2163 "##,
2164
2165 E0200: r##"
2166 Unsafe traits must have unsafe implementations. This error occurs when an
2167 implementation for an unsafe trait isn't marked as unsafe. This may be resolved
2168 by marking the unsafe implementation as unsafe.
2169
2170 ```compile_fail,E0200
2171 struct Foo;
2172
2173 unsafe trait Bar { }
2174
2175 // this won't compile because Bar is unsafe and impl isn't unsafe
2176 impl Bar for Foo { }
2177 // this will compile
2178 unsafe impl Bar for Foo { }
2179 ```
2180 "##,
2181
2182 E0201: r##"
2183 It is an error to define two associated items (like methods, associated types,
2184 associated functions, etc.) with the same identifier.
2185
2186 For example:
2187
2188 ```compile_fail,E0201
2189 struct Foo(u8);
2190
2191 impl Foo {
2192     fn bar(&self) -> bool { self.0 > 5 }
2193     fn bar() {} // error: duplicate associated function
2194 }
2195
2196 trait Baz {
2197     type Quux;
2198     fn baz(&self) -> bool;
2199 }
2200
2201 impl Baz for Foo {
2202     type Quux = u32;
2203
2204     fn baz(&self) -> bool { true }
2205
2206     // error: duplicate method
2207     fn baz(&self) -> bool { self.0 > 5 }
2208
2209     // error: duplicate associated type
2210     type Quux = u32;
2211 }
2212 ```
2213
2214 Note, however, that items with the same name are allowed for inherent `impl`
2215 blocks that don't overlap:
2216
2217 ```
2218 struct Foo<T>(T);
2219
2220 impl Foo<u8> {
2221     fn bar(&self) -> bool { self.0 > 5 }
2222 }
2223
2224 impl Foo<bool> {
2225     fn bar(&self) -> bool { self.0 }
2226 }
2227 ```
2228 "##,
2229
2230 E0202: r##"
2231 Inherent associated types were part of [RFC 195] but are not yet implemented.
2232 See [the tracking issue][iss8995] for the status of this implementation.
2233
2234 [RFC 195]: https://github.com/rust-lang/rfcs/pull/195
2235 [iss8995]: https://github.com/rust-lang/rust/issues/8995
2236 "##,
2237
2238 E0204: r##"
2239 An attempt to implement the `Copy` trait for a struct failed because one of the
2240 fields does not implement `Copy`. To fix this, you must implement `Copy` for the
2241 mentioned field. Note that this may not be possible, as in the example of
2242
2243 ```compile_fail,E0204
2244 struct Foo {
2245     foo : Vec<u32>,
2246 }
2247
2248 impl Copy for Foo { }
2249 ```
2250
2251 This fails because `Vec<T>` does not implement `Copy` for any `T`.
2252
2253 Here's another example that will fail:
2254
2255 ```compile_fail,E0204
2256 #[derive(Copy)]
2257 struct Foo<'a> {
2258     ty: &'a mut bool,
2259 }
2260 ```
2261
2262 This fails because `&mut T` is not `Copy`, even when `T` is `Copy` (this
2263 differs from the behavior for `&T`, which is always `Copy`).
2264 "##,
2265
2266 E0205: r##"
2267 An attempt to implement the `Copy` trait for an enum failed because one of the
2268 variants does not implement `Copy`. To fix this, you must implement `Copy` for
2269 the mentioned variant. Note that this may not be possible, as in the example of
2270
2271 ```compile_fail,E0205
2272 enum Foo {
2273     Bar(Vec<u32>),
2274     Baz,
2275 }
2276
2277 impl Copy for Foo { }
2278 ```
2279
2280 This fails because `Vec<T>` does not implement `Copy` for any `T`.
2281
2282 Here's another example that will fail:
2283
2284 ```compile_fail,E0205
2285 #[derive(Copy)]
2286 enum Foo<'a> {
2287     Bar(&'a mut bool),
2288     Baz,
2289 }
2290 ```
2291
2292 This fails because `&mut T` is not `Copy`, even when `T` is `Copy` (this
2293 differs from the behavior for `&T`, which is always `Copy`).
2294 "##,
2295
2296 E0206: r##"
2297 You can only implement `Copy` for a struct or enum. Both of the following
2298 examples will fail, because neither `i32` (primitive type) nor `&'static Bar`
2299 (reference to `Bar`) is a struct or enum:
2300
2301 ```compile_fail,E0206
2302 type Foo = i32;
2303 impl Copy for Foo { } // error
2304
2305 #[derive(Copy, Clone)]
2306 struct Bar;
2307 impl Copy for &'static Bar { } // error
2308 ```
2309 "##,
2310
2311 E0207: r##"
2312 Any type parameter or lifetime parameter of an `impl` must meet at least one of
2313 the following criteria:
2314
2315  - it appears in the self type of the impl
2316  - for a trait impl, it appears in the trait reference
2317  - it is bound as an associated type
2318
2319 ### Error example 1
2320
2321 Suppose we have a struct `Foo` and we would like to define some methods for it.
2322 The following definition leads to a compiler error:
2323
2324 ```compile_fail,E0207
2325 struct Foo;
2326
2327 impl<T: Default> Foo {
2328 // error: the type parameter `T` is not constrained by the impl trait, self
2329 // type, or predicates [E0207]
2330     fn get(&self) -> T {
2331         <T as Default>::default()
2332     }
2333 }
2334 ```
2335
2336 The problem is that the parameter `T` does not appear in the self type (`Foo`)
2337 of the impl. In this case, we can fix the error by moving the type parameter
2338 from the `impl` to the method `get`:
2339
2340
2341 ```
2342 struct Foo;
2343
2344 // Move the type parameter from the impl to the method
2345 impl Foo {
2346     fn get<T: Default>(&self) -> T {
2347         <T as Default>::default()
2348     }
2349 }
2350 ```
2351
2352 ### Error example 2
2353
2354 As another example, suppose we have a `Maker` trait and want to establish a
2355 type `FooMaker` that makes `Foo`s:
2356
2357 ```compile_fail,E0207
2358 trait Maker {
2359     type Item;
2360     fn make(&mut self) -> Self::Item;
2361 }
2362
2363 struct Foo<T> {
2364     foo: T
2365 }
2366
2367 struct FooMaker;
2368
2369 impl<T: Default> Maker for FooMaker {
2370 // error: the type parameter `T` is not constrained by the impl trait, self
2371 // type, or predicates [E0207]
2372     type Item = Foo<T>;
2373
2374     fn make(&mut self) -> Foo<T> {
2375         Foo { foo: <T as Default>::default() }
2376     }
2377 }
2378 ```
2379
2380 This fails to compile because `T` does not appear in the trait or in the
2381 implementing type.
2382
2383 One way to work around this is to introduce a phantom type parameter into
2384 `FooMaker`, like so:
2385
2386 ```
2387 use std::marker::PhantomData;
2388
2389 trait Maker {
2390     type Item;
2391     fn make(&mut self) -> Self::Item;
2392 }
2393
2394 struct Foo<T> {
2395     foo: T
2396 }
2397
2398 // Add a type parameter to `FooMaker`
2399 struct FooMaker<T> {
2400     phantom: PhantomData<T>,
2401 }
2402
2403 impl<T: Default> Maker for FooMaker<T> {
2404     type Item = Foo<T>;
2405
2406     fn make(&mut self) -> Foo<T> {
2407         Foo {
2408             foo: <T as Default>::default(),
2409         }
2410     }
2411 }
2412 ```
2413
2414 Another way is to do away with the associated type in `Maker` and use an input
2415 type parameter instead:
2416
2417 ```
2418 // Use a type parameter instead of an associated type here
2419 trait Maker<Item> {
2420     fn make(&mut self) -> Item;
2421 }
2422
2423 struct Foo<T> {
2424     foo: T
2425 }
2426
2427 struct FooMaker;
2428
2429 impl<T: Default> Maker<Foo<T>> for FooMaker {
2430     fn make(&mut self) -> Foo<T> {
2431         Foo { foo: <T as Default>::default() }
2432     }
2433 }
2434 ```
2435
2436 ### Additional information
2437
2438 For more information, please see [RFC 447].
2439
2440 [RFC 447]: https://github.com/rust-lang/rfcs/blob/master/text/0447-no-unused-impl-parameters.md
2441 "##,
2442
2443 E0210: r##"
2444 This error indicates a violation of one of Rust's orphan rules for trait
2445 implementations. The rule concerns the use of type parameters in an
2446 implementation of a foreign trait (a trait defined in another crate), and
2447 states that type parameters must be "covered" by a local type. To understand
2448 what this means, it is perhaps easiest to consider a few examples.
2449
2450 If `ForeignTrait` is a trait defined in some external crate `foo`, then the
2451 following trait `impl` is an error:
2452
2453 ```compile_fail,E0210
2454 extern crate collections;
2455 use collections::range::RangeArgument;
2456
2457 impl<T> RangeArgument<T> for T { } // error
2458
2459 fn main() {}
2460 ```
2461
2462 To work around this, it can be covered with a local type, `MyType`:
2463
2464 ```ignore
2465 struct MyType<T>(T);
2466 impl<T> ForeignTrait for MyType<T> { } // Ok
2467 ```
2468
2469 Please note that a type alias is not sufficient.
2470
2471 For another example of an error, suppose there's another trait defined in `foo`
2472 named `ForeignTrait2` that takes two type parameters. Then this `impl` results
2473 in the same rule violation:
2474
2475 ```compile_fail
2476 struct MyType2;
2477 impl<T> ForeignTrait2<T, MyType<T>> for MyType2 { } // error
2478 ```
2479
2480 The reason for this is that there are two appearances of type parameter `T` in
2481 the `impl` header, both as parameters for `ForeignTrait2`. The first appearance
2482 is uncovered, and so runs afoul of the orphan rule.
2483
2484 Consider one more example:
2485
2486 ```ignore
2487 impl<T> ForeignTrait2<MyType<T>, T> for MyType2 { } // Ok
2488 ```
2489
2490 This only differs from the previous `impl` in that the parameters `T` and
2491 `MyType<T>` for `ForeignTrait2` have been swapped. This example does *not*
2492 violate the orphan rule; it is permitted.
2493
2494 To see why that last example was allowed, you need to understand the general
2495 rule. Unfortunately this rule is a bit tricky to state. Consider an `impl`:
2496
2497 ```ignore
2498 impl<P1, ..., Pm> ForeignTrait<T1, ..., Tn> for T0 { ... }
2499 ```
2500
2501 where `P1, ..., Pm` are the type parameters of the `impl` and `T0, ..., Tn`
2502 are types. One of the types `T0, ..., Tn` must be a local type (this is another
2503 orphan rule, see the explanation for E0117). Let `i` be the smallest integer
2504 such that `Ti` is a local type. Then no type parameter can appear in any of the
2505 `Tj` for `j < i`.
2506
2507 For information on the design of the orphan rules, see [RFC 1023].
2508
2509 [RFC 1023]: https://github.com/rust-lang/rfcs/pull/1023
2510 "##,
2511
2512 /*
2513 E0211: r##"
2514 You used a function or type which doesn't fit the requirements for where it was
2515 used. Erroneous code examples:
2516
2517 ```compile_fail
2518 #![feature(intrinsics)]
2519
2520 extern "rust-intrinsic" {
2521     fn size_of<T>(); // error: intrinsic has wrong type
2522 }
2523
2524 // or:
2525
2526 fn main() -> i32 { 0 }
2527 // error: main function expects type: `fn() {main}`: expected (), found i32
2528
2529 // or:
2530
2531 let x = 1u8;
2532 match x {
2533     0u8...3i8 => (),
2534     // error: mismatched types in range: expected u8, found i8
2535     _ => ()
2536 }
2537
2538 // or:
2539
2540 use std::rc::Rc;
2541 struct Foo;
2542
2543 impl Foo {
2544     fn x(self: Rc<Foo>) {}
2545     // error: mismatched self type: expected `Foo`: expected struct
2546     //        `Foo`, found struct `alloc::rc::Rc`
2547 }
2548 ```
2549
2550 For the first code example, please check the function definition. Example:
2551
2552 ```
2553 #![feature(intrinsics)]
2554
2555 extern "rust-intrinsic" {
2556     fn size_of<T>() -> usize; // ok!
2557 }
2558 ```
2559
2560 The second case example is a bit particular : the main function must always
2561 have this definition:
2562
2563 ```compile_fail
2564 fn main();
2565 ```
2566
2567 They never take parameters and never return types.
2568
2569 For the third example, when you match, all patterns must have the same type
2570 as the type you're matching on. Example:
2571
2572 ```
2573 let x = 1u8;
2574
2575 match x {
2576     0u8...3u8 => (), // ok!
2577     _ => ()
2578 }
2579 ```
2580
2581 And finally, for the last example, only `Box<Self>`, `&Self`, `Self`,
2582 or `&mut Self` work as explicit self parameters. Example:
2583
2584 ```
2585 struct Foo;
2586
2587 impl Foo {
2588     fn x(self: Box<Foo>) {} // ok!
2589 }
2590 ```
2591 "##,
2592      */
2593
2594 E0214: r##"
2595 A generic type was described using parentheses rather than angle brackets. For
2596 example:
2597
2598 ```compile_fail,E0214
2599 fn main() {
2600     let v: Vec(&str) = vec!["foo"];
2601 }
2602 ```
2603
2604 This is not currently supported: `v` should be defined as `Vec<&str>`.
2605 Parentheses are currently only used with generic types when defining parameters
2606 for `Fn`-family traits.
2607 "##,
2608
2609 E0220: r##"
2610 You used an associated type which isn't defined in the trait.
2611 Erroneous code example:
2612
2613 ```compile_fail,E0220
2614 trait T1 {
2615     type Bar;
2616 }
2617
2618 type Foo = T1<F=i32>; // error: associated type `F` not found for `T1`
2619
2620 // or:
2621
2622 trait T2 {
2623     type Bar;
2624
2625     // error: Baz is used but not declared
2626     fn return_bool(&self, &Self::Bar, &Self::Baz) -> bool;
2627 }
2628 ```
2629
2630 Make sure that you have defined the associated type in the trait body.
2631 Also, verify that you used the right trait or you didn't misspell the
2632 associated type name. Example:
2633
2634 ```
2635 trait T1 {
2636     type Bar;
2637 }
2638
2639 type Foo = T1<Bar=i32>; // ok!
2640
2641 // or:
2642
2643 trait T2 {
2644     type Bar;
2645     type Baz; // we declare `Baz` in our trait.
2646
2647     // and now we can use it here:
2648     fn return_bool(&self, &Self::Bar, &Self::Baz) -> bool;
2649 }
2650 ```
2651 "##,
2652
2653 E0221: r##"
2654 An attempt was made to retrieve an associated type, but the type was ambiguous.
2655 For example:
2656
2657 ```compile_fail,E0221
2658 trait T1 {}
2659 trait T2 {}
2660
2661 trait Foo {
2662     type A: T1;
2663 }
2664
2665 trait Bar : Foo {
2666     type A: T2;
2667     fn do_something() {
2668         let _: Self::A;
2669     }
2670 }
2671 ```
2672
2673 In this example, `Foo` defines an associated type `A`. `Bar` inherits that type
2674 from `Foo`, and defines another associated type of the same name. As a result,
2675 when we attempt to use `Self::A`, it's ambiguous whether we mean the `A` defined
2676 by `Foo` or the one defined by `Bar`.
2677
2678 There are two options to work around this issue. The first is simply to rename
2679 one of the types. Alternatively, one can specify the intended type using the
2680 following syntax:
2681
2682 ```
2683 trait T1 {}
2684 trait T2 {}
2685
2686 trait Foo {
2687     type A: T1;
2688 }
2689
2690 trait Bar : Foo {
2691     type A: T2;
2692     fn do_something() {
2693         let _: <Self as Bar>::A;
2694     }
2695 }
2696 ```
2697 "##,
2698
2699 E0223: r##"
2700 An attempt was made to retrieve an associated type, but the type was ambiguous.
2701 For example:
2702
2703 ```compile_fail,E0223
2704 trait MyTrait {type X; }
2705
2706 fn main() {
2707     let foo: MyTrait::X;
2708 }
2709 ```
2710
2711 The problem here is that we're attempting to take the type of X from MyTrait.
2712 Unfortunately, the type of X is not defined, because it's only made concrete in
2713 implementations of the trait. A working version of this code might look like:
2714
2715 ```
2716 trait MyTrait {type X; }
2717 struct MyStruct;
2718
2719 impl MyTrait for MyStruct {
2720     type X = u32;
2721 }
2722
2723 fn main() {
2724     let foo: <MyStruct as MyTrait>::X;
2725 }
2726 ```
2727
2728 This syntax specifies that we want the X type from MyTrait, as made concrete in
2729 MyStruct. The reason that we cannot simply use `MyStruct::X` is that MyStruct
2730 might implement two different traits with identically-named associated types.
2731 This syntax allows disambiguation between the two.
2732 "##,
2733
2734 E0225: r##"
2735 You attempted to use multiple types as bounds for a closure or trait object.
2736 Rust does not currently support this. A simple example that causes this error:
2737
2738 ```compile_fail,E0225
2739 fn main() {
2740     let _: Box<std::io::Read + std::io::Write>;
2741 }
2742 ```
2743
2744 Builtin traits are an exception to this rule: it's possible to have bounds of
2745 one non-builtin type, plus any number of builtin types. For example, the
2746 following compiles correctly:
2747
2748 ```
2749 fn main() {
2750     let _: Box<std::io::Read + Send + Sync>;
2751 }
2752 ```
2753 "##,
2754
2755 E0232: r##"
2756 The attribute must have a value. Erroneous code example:
2757
2758 ```compile_fail,E0232
2759 #![feature(on_unimplemented)]
2760
2761 #[rustc_on_unimplemented] // error: this attribute must have a value
2762 trait Bar {}
2763 ```
2764
2765 Please supply the missing value of the attribute. Example:
2766
2767 ```
2768 #![feature(on_unimplemented)]
2769
2770 #[rustc_on_unimplemented = "foo"] // ok!
2771 trait Bar {}
2772 ```
2773 "##,
2774
2775 E0243: r##"
2776 This error indicates that not enough type parameters were found in a type or
2777 trait.
2778
2779 For example, the `Foo` struct below is defined to be generic in `T`, but the
2780 type parameter is missing in the definition of `Bar`:
2781
2782 ```compile_fail,E0243
2783 struct Foo<T> { x: T }
2784
2785 struct Bar { x: Foo }
2786 ```
2787 "##,
2788
2789 E0244: r##"
2790 This error indicates that too many type parameters were found in a type or
2791 trait.
2792
2793 For example, the `Foo` struct below has no type parameters, but is supplied
2794 with two in the definition of `Bar`:
2795
2796 ```compile_fail,E0244
2797 struct Foo { x: bool }
2798
2799 struct Bar<S, T> { x: Foo<S, T> }
2800 ```
2801 "##,
2802
2803 E0248: r##"
2804 This error indicates an attempt to use a value where a type is expected. For
2805 example:
2806
2807 ```compile_fail,E0248
2808 enum Foo {
2809     Bar(u32)
2810 }
2811
2812 fn do_something(x: Foo::Bar) { }
2813 ```
2814
2815 In this example, we're attempting to take a type of `Foo::Bar` in the
2816 do_something function. This is not legal: `Foo::Bar` is a value of type `Foo`,
2817 not a distinct static type. Likewise, it's not legal to attempt to
2818 `impl Foo::Bar`: instead, you must `impl Foo` and then pattern match to specify
2819 behavior for specific enum variants.
2820 "##,
2821
2822 E0318: r##"
2823 Default impls for a trait must be located in the same crate where the trait was
2824 defined. For more information see the [opt-in builtin traits RFC](https://github
2825 .com/rust-lang/rfcs/blob/master/text/0019-opt-in-builtin-traits.md).
2826 "##,
2827
2828 E0321: r##"
2829 A cross-crate opt-out trait was implemented on something which wasn't a struct
2830 or enum type. Erroneous code example:
2831
2832 ```compile_fail,E0321
2833 #![feature(optin_builtin_traits)]
2834
2835 struct Foo;
2836
2837 impl !Sync for Foo {}
2838
2839 unsafe impl Send for &'static Foo {}
2840 // error: cross-crate traits with a default impl, like `core::marker::Send`,
2841 //        can only be implemented for a struct/enum type, not
2842 //        `&'static Foo`
2843 ```
2844
2845 Only structs and enums are permitted to impl Send, Sync, and other opt-out
2846 trait, and the struct or enum must be local to the current crate. So, for
2847 example, `unsafe impl Send for Rc<Foo>` is not allowed.
2848 "##,
2849
2850 E0322: r##"
2851 The `Sized` trait is a special trait built-in to the compiler for types with a
2852 constant size known at compile-time. This trait is automatically implemented
2853 for types as needed by the compiler, and it is currently disallowed to
2854 explicitly implement it for a type.
2855 "##,
2856
2857 E0323: r##"
2858 An associated const was implemented when another trait item was expected.
2859 Erroneous code example:
2860
2861 ```compile_fail,E0323
2862 #![feature(associated_consts)]
2863
2864 trait Foo {
2865     type N;
2866 }
2867
2868 struct Bar;
2869
2870 impl Foo for Bar {
2871     const N : u32 = 0;
2872     // error: item `N` is an associated const, which doesn't match its
2873     //        trait `<Bar as Foo>`
2874 }
2875 ```
2876
2877 Please verify that the associated const wasn't misspelled and the correct trait
2878 was implemented. Example:
2879
2880 ```
2881 struct Bar;
2882
2883 trait Foo {
2884     type N;
2885 }
2886
2887 impl Foo for Bar {
2888     type N = u32; // ok!
2889 }
2890 ```
2891
2892 Or:
2893
2894 ```
2895 #![feature(associated_consts)]
2896
2897 struct Bar;
2898
2899 trait Foo {
2900     const N : u32;
2901 }
2902
2903 impl Foo for Bar {
2904     const N : u32 = 0; // ok!
2905 }
2906 ```
2907 "##,
2908
2909 E0324: r##"
2910 A method was implemented when another trait item was expected. Erroneous
2911 code example:
2912
2913 ```compile_fail,E0324
2914 #![feature(associated_consts)]
2915
2916 struct Bar;
2917
2918 trait Foo {
2919     const N : u32;
2920
2921     fn M();
2922 }
2923
2924 impl Foo for Bar {
2925     fn N() {}
2926     // error: item `N` is an associated method, which doesn't match its
2927     //        trait `<Bar as Foo>`
2928 }
2929 ```
2930
2931 To fix this error, please verify that the method name wasn't misspelled and
2932 verify that you are indeed implementing the correct trait items. Example:
2933
2934 ```
2935 #![feature(associated_consts)]
2936
2937 struct Bar;
2938
2939 trait Foo {
2940     const N : u32;
2941
2942     fn M();
2943 }
2944
2945 impl Foo for Bar {
2946     const N : u32 = 0;
2947
2948     fn M() {} // ok!
2949 }
2950 ```
2951 "##,
2952
2953 E0325: r##"
2954 An associated type was implemented when another trait item was expected.
2955 Erroneous code example:
2956
2957 ```compile_fail,E0325
2958 #![feature(associated_consts)]
2959
2960 struct Bar;
2961
2962 trait Foo {
2963     const N : u32;
2964 }
2965
2966 impl Foo for Bar {
2967     type N = u32;
2968     // error: item `N` is an associated type, which doesn't match its
2969     //        trait `<Bar as Foo>`
2970 }
2971 ```
2972
2973 Please verify that the associated type name wasn't misspelled and your
2974 implementation corresponds to the trait definition. Example:
2975
2976 ```
2977 struct Bar;
2978
2979 trait Foo {
2980     type N;
2981 }
2982
2983 impl Foo for Bar {
2984     type N = u32; // ok!
2985 }
2986 ```
2987
2988 Or:
2989
2990 ```
2991 #![feature(associated_consts)]
2992
2993 struct Bar;
2994
2995 trait Foo {
2996     const N : u32;
2997 }
2998
2999 impl Foo for Bar {
3000     const N : u32 = 0; // ok!
3001 }
3002 ```
3003 "##,
3004
3005 E0326: r##"
3006 The types of any associated constants in a trait implementation must match the
3007 types in the trait definition. This error indicates that there was a mismatch.
3008
3009 Here's an example of this error:
3010
3011 ```compile_fail,E0326
3012 #![feature(associated_consts)]
3013
3014 trait Foo {
3015     const BAR: bool;
3016 }
3017
3018 struct Bar;
3019
3020 impl Foo for Bar {
3021     const BAR: u32 = 5; // error, expected bool, found u32
3022 }
3023 ```
3024 "##,
3025
3026 E0329: r##"
3027 An attempt was made to access an associated constant through either a generic
3028 type parameter or `Self`. This is not supported yet. An example causing this
3029 error is shown below:
3030
3031 ```ignore
3032 #![feature(associated_consts)]
3033
3034 trait Foo {
3035     const BAR: f64;
3036 }
3037
3038 struct MyStruct;
3039
3040 impl Foo for MyStruct {
3041     const BAR: f64 = 0f64;
3042 }
3043
3044 fn get_bar_bad<F: Foo>(t: F) -> f64 {
3045     F::BAR
3046 }
3047 ```
3048
3049 Currently, the value of `BAR` for a particular type can only be accessed
3050 through a concrete type, as shown below:
3051
3052 ```ignore
3053 #![feature(associated_consts)]
3054
3055 trait Foo {
3056     const BAR: f64;
3057 }
3058
3059 struct MyStruct;
3060
3061 fn get_bar_good() -> f64 {
3062     <MyStruct as Foo>::BAR
3063 }
3064 ```
3065 "##,
3066
3067 E0366: r##"
3068 An attempt was made to implement `Drop` on a concrete specialization of a
3069 generic type. An example is shown below:
3070
3071 ```compile_fail,E0366
3072 struct Foo<T> {
3073     t: T
3074 }
3075
3076 impl Drop for Foo<u32> {
3077     fn drop(&mut self) {}
3078 }
3079 ```
3080
3081 This code is not legal: it is not possible to specialize `Drop` to a subset of
3082 implementations of a generic type. One workaround for this is to wrap the
3083 generic type, as shown below:
3084
3085 ```
3086 struct Foo<T> {
3087     t: T
3088 }
3089
3090 struct Bar {
3091     t: Foo<u32>
3092 }
3093
3094 impl Drop for Bar {
3095     fn drop(&mut self) {}
3096 }
3097 ```
3098 "##,
3099
3100 E0367: r##"
3101 An attempt was made to implement `Drop` on a specialization of a generic type.
3102 An example is shown below:
3103
3104 ```compile_fail,E0367
3105 trait Foo{}
3106
3107 struct MyStruct<T> {
3108     t: T
3109 }
3110
3111 impl<T: Foo> Drop for MyStruct<T> {
3112     fn drop(&mut self) {}
3113 }
3114 ```
3115
3116 This code is not legal: it is not possible to specialize `Drop` to a subset of
3117 implementations of a generic type. In order for this code to work, `MyStruct`
3118 must also require that `T` implements `Foo`. Alternatively, another option is
3119 to wrap the generic type in another that specializes appropriately:
3120
3121 ```
3122 trait Foo{}
3123
3124 struct MyStruct<T> {
3125     t: T
3126 }
3127
3128 struct MyStructWrapper<T: Foo> {
3129     t: MyStruct<T>
3130 }
3131
3132 impl <T: Foo> Drop for MyStructWrapper<T> {
3133     fn drop(&mut self) {}
3134 }
3135 ```
3136 "##,
3137
3138 E0368: r##"
3139 This error indicates that a binary assignment operator like `+=` or `^=` was
3140 applied to a type that doesn't support it. For example:
3141
3142 ```compile_fail,E0368
3143 let mut x = 12f32; // error: binary operation `<<` cannot be applied to
3144                    //        type `f32`
3145
3146 x <<= 2;
3147 ```
3148
3149 To fix this error, please check that this type implements this binary
3150 operation. Example:
3151
3152 ```
3153 let mut x = 12u32; // the `u32` type does implement the `ShlAssign` trait
3154
3155 x <<= 2; // ok!
3156 ```
3157
3158 It is also possible to overload most operators for your own type by
3159 implementing the `[OP]Assign` traits from `std::ops`.
3160
3161 Another problem you might be facing is this: suppose you've overloaded the `+`
3162 operator for some type `Foo` by implementing the `std::ops::Add` trait for
3163 `Foo`, but you find that using `+=` does not work, as in this example:
3164
3165 ```compile_fail,E0368
3166 use std::ops::Add;
3167
3168 struct Foo(u32);
3169
3170 impl Add for Foo {
3171     type Output = Foo;
3172
3173     fn add(self, rhs: Foo) -> Foo {
3174         Foo(self.0 + rhs.0)
3175     }
3176 }
3177
3178 fn main() {
3179     let mut x: Foo = Foo(5);
3180     x += Foo(7); // error, `+= cannot be applied to the type `Foo`
3181 }
3182 ```
3183
3184 This is because `AddAssign` is not automatically implemented, so you need to
3185 manually implement it for your type.
3186 "##,
3187
3188 E0369: r##"
3189 A binary operation was attempted on a type which doesn't support it.
3190 Erroneous code example:
3191
3192 ```compile_fail,E0369
3193 let x = 12f32; // error: binary operation `<<` cannot be applied to
3194                //        type `f32`
3195
3196 x << 2;
3197 ```
3198
3199 To fix this error, please check that this type implements this binary
3200 operation. Example:
3201
3202 ```
3203 let x = 12u32; // the `u32` type does implement it:
3204                // https://doc.rust-lang.org/stable/std/ops/trait.Shl.html
3205
3206 x << 2; // ok!
3207 ```
3208
3209 It is also possible to overload most operators for your own type by
3210 implementing traits from `std::ops`.
3211 "##,
3212
3213 E0370: r##"
3214 The maximum value of an enum was reached, so it cannot be automatically
3215 set in the next enum value. Erroneous code example:
3216
3217 ```compile_fail
3218 #[deny(overflowing_literals)]
3219 enum Foo {
3220     X = 0x7fffffffffffffff,
3221     Y, // error: enum discriminant overflowed on value after
3222        //        9223372036854775807: i64; set explicitly via
3223        //        Y = -9223372036854775808 if that is desired outcome
3224 }
3225 ```
3226
3227 To fix this, please set manually the next enum value or put the enum variant
3228 with the maximum value at the end of the enum. Examples:
3229
3230 ```
3231 enum Foo {
3232     X = 0x7fffffffffffffff,
3233     Y = 0, // ok!
3234 }
3235 ```
3236
3237 Or:
3238
3239 ```
3240 enum Foo {
3241     Y = 0, // ok!
3242     X = 0x7fffffffffffffff,
3243 }
3244 ```
3245 "##,
3246
3247 E0371: r##"
3248 When `Trait2` is a subtrait of `Trait1` (for example, when `Trait2` has a
3249 definition like `trait Trait2: Trait1 { ... }`), it is not allowed to implement
3250 `Trait1` for `Trait2`. This is because `Trait2` already implements `Trait1` by
3251 definition, so it is not useful to do this.
3252
3253 Example:
3254
3255 ```compile_fail,E0371
3256 trait Foo { fn foo(&self) { } }
3257 trait Bar: Foo { }
3258 trait Baz: Bar { }
3259
3260 impl Bar for Baz { } // error, `Baz` implements `Bar` by definition
3261 impl Foo for Baz { } // error, `Baz` implements `Bar` which implements `Foo`
3262 impl Baz for Baz { } // error, `Baz` (trivially) implements `Baz`
3263 impl Baz for Bar { } // Note: This is OK
3264 ```
3265 "##,
3266
3267 E0374: r##"
3268 A struct without a field containing an unsized type cannot implement
3269 `CoerceUnsized`. An
3270 [unsized type](https://doc.rust-lang.org/book/unsized-types.html)
3271 is any type that the compiler doesn't know the length or alignment of at
3272 compile time. Any struct containing an unsized type is also unsized.
3273
3274 Example of erroneous code:
3275
3276 ```compile_fail,E0374
3277 #![feature(coerce_unsized)]
3278 use std::ops::CoerceUnsized;
3279
3280 struct Foo<T: ?Sized> {
3281     a: i32,
3282 }
3283
3284 // error: Struct `Foo` has no unsized fields that need `CoerceUnsized`.
3285 impl<T, U> CoerceUnsized<Foo<U>> for Foo<T>
3286     where T: CoerceUnsized<U> {}
3287 ```
3288
3289 `CoerceUnsized` is used to coerce one struct containing an unsized type
3290 into another struct containing a different unsized type. If the struct
3291 doesn't have any fields of unsized types then you don't need explicit
3292 coercion to get the types you want. To fix this you can either
3293 not try to implement `CoerceUnsized` or you can add a field that is
3294 unsized to the struct.
3295
3296 Example:
3297
3298 ```
3299 #![feature(coerce_unsized)]
3300 use std::ops::CoerceUnsized;
3301
3302 // We don't need to impl `CoerceUnsized` here.
3303 struct Foo {
3304     a: i32,
3305 }
3306
3307 // We add the unsized type field to the struct.
3308 struct Bar<T: ?Sized> {
3309     a: i32,
3310     b: T,
3311 }
3312
3313 // The struct has an unsized field so we can implement
3314 // `CoerceUnsized` for it.
3315 impl<T, U> CoerceUnsized<Bar<U>> for Bar<T>
3316     where T: CoerceUnsized<U> {}
3317 ```
3318
3319 Note that `CoerceUnsized` is mainly used by smart pointers like `Box`, `Rc`
3320 and `Arc` to be able to mark that they can coerce unsized types that they
3321 are pointing at.
3322 "##,
3323
3324 E0375: r##"
3325 A struct with more than one field containing an unsized type cannot implement
3326 `CoerceUnsized`. This only occurs when you are trying to coerce one of the
3327 types in your struct to another type in the struct. In this case we try to
3328 impl `CoerceUnsized` from `T` to `U` which are both types that the struct
3329 takes. An [unsized type](https://doc.rust-lang.org/book/unsized-types.html)
3330 is any type that the compiler doesn't know the length or alignment of at
3331 compile time. Any struct containing an unsized type is also unsized.
3332
3333 Example of erroneous code:
3334
3335 ```compile_fail,E0375
3336 #![feature(coerce_unsized)]
3337 use std::ops::CoerceUnsized;
3338
3339 struct Foo<T: ?Sized, U: ?Sized> {
3340     a: i32,
3341     b: T,
3342     c: U,
3343 }
3344
3345 // error: Struct `Foo` has more than one unsized field.
3346 impl<T, U> CoerceUnsized<Foo<U, T>> for Foo<T, U> {}
3347 ```
3348
3349 `CoerceUnsized` only allows for coercion from a structure with a single
3350 unsized type field to another struct with a single unsized type field.
3351 In fact Rust only allows for a struct to have one unsized type in a struct
3352 and that unsized type must be the last field in the struct. So having two
3353 unsized types in a single struct is not allowed by the compiler. To fix this
3354 use only one field containing an unsized type in the struct and then use
3355 multiple structs to manage each unsized type field you need.
3356
3357 Example:
3358
3359 ```
3360 #![feature(coerce_unsized)]
3361 use std::ops::CoerceUnsized;
3362
3363 struct Foo<T: ?Sized> {
3364     a: i32,
3365     b: T,
3366 }
3367
3368 impl <T, U> CoerceUnsized<Foo<U>> for Foo<T>
3369     where T: CoerceUnsized<U> {}
3370
3371 fn coerce_foo<T: CoerceUnsized<U>, U>(t: T) -> Foo<U> {
3372     Foo { a: 12i32, b: t } // we use coercion to get the `Foo<U>` type we need
3373 }
3374 ```
3375
3376 "##,
3377
3378 E0376: r##"
3379 The type you are trying to impl `CoerceUnsized` for is not a struct.
3380 `CoerceUnsized` can only be implemented for a struct. Unsized types are
3381 already able to be coerced without an implementation of `CoerceUnsized`
3382 whereas a struct containing an unsized type needs to know the unsized type
3383 field it's containing is able to be coerced. An
3384 [unsized type](https://doc.rust-lang.org/book/unsized-types.html)
3385 is any type that the compiler doesn't know the length or alignment of at
3386 compile time. Any struct containing an unsized type is also unsized.
3387
3388 Example of erroneous code:
3389
3390 ```compile_fail,E0376
3391 #![feature(coerce_unsized)]
3392 use std::ops::CoerceUnsized;
3393
3394 struct Foo<T: ?Sized> {
3395     a: T,
3396 }
3397
3398 // error: The type `U` is not a struct
3399 impl<T, U> CoerceUnsized<U> for Foo<T> {}
3400 ```
3401
3402 The `CoerceUnsized` trait takes a struct type. Make sure the type you are
3403 providing to `CoerceUnsized` is a struct with only the last field containing an
3404 unsized type.
3405
3406 Example:
3407
3408 ```
3409 #![feature(coerce_unsized)]
3410 use std::ops::CoerceUnsized;
3411
3412 struct Foo<T> {
3413     a: T,
3414 }
3415
3416 // The `Foo<U>` is a struct so `CoerceUnsized` can be implemented
3417 impl<T, U> CoerceUnsized<Foo<U>> for Foo<T> where T: CoerceUnsized<U> {}
3418 ```
3419
3420 Note that in Rust, structs can only contain an unsized type if the field
3421 containing the unsized type is the last and only unsized type field in the
3422 struct.
3423 "##,
3424
3425 E0380: r##"
3426 Default impls are only allowed for traits with no methods or associated items.
3427 For more information see the [opt-in builtin traits RFC](https://github.com/rust
3428 -lang/rfcs/blob/master/text/0019-opt-in-builtin-traits.md).
3429 "##,
3430
3431 E0390: r##"
3432 You tried to implement methods for a primitive type. Erroneous code example:
3433
3434 ```compile_fail,E0390
3435 struct Foo {
3436     x: i32
3437 }
3438
3439 impl *mut Foo {}
3440 // error: only a single inherent implementation marked with
3441 //        `#[lang = "mut_ptr"]` is allowed for the `*mut T` primitive
3442 ```
3443
3444 This isn't allowed, but using a trait to implement a method is a good solution.
3445 Example:
3446
3447 ```
3448 struct Foo {
3449     x: i32
3450 }
3451
3452 trait Bar {
3453     fn bar();
3454 }
3455
3456 impl Bar for *mut Foo {
3457     fn bar() {} // ok!
3458 }
3459 ```
3460 "##,
3461
3462 E0391: r##"
3463 This error indicates that some types or traits depend on each other
3464 and therefore cannot be constructed.
3465
3466 The following example contains a circular dependency between two traits:
3467
3468 ```compile_fail,E0391
3469 trait FirstTrait : SecondTrait {
3470
3471 }
3472
3473 trait SecondTrait : FirstTrait {
3474
3475 }
3476 ```
3477 "##,
3478
3479 E0392: r##"
3480 This error indicates that a type or lifetime parameter has been declared
3481 but not actually used. Here is an example that demonstrates the error:
3482
3483 ```compile_fail,E0392
3484 enum Foo<T> {
3485     Bar,
3486 }
3487 ```
3488
3489 If the type parameter was included by mistake, this error can be fixed
3490 by simply removing the type parameter, as shown below:
3491
3492 ```
3493 enum Foo {
3494     Bar,
3495 }
3496 ```
3497
3498 Alternatively, if the type parameter was intentionally inserted, it must be
3499 used. A simple fix is shown below:
3500
3501 ```
3502 enum Foo<T> {
3503     Bar(T),
3504 }
3505 ```
3506
3507 This error may also commonly be found when working with unsafe code. For
3508 example, when using raw pointers one may wish to specify the lifetime for
3509 which the pointed-at data is valid. An initial attempt (below) causes this
3510 error:
3511
3512 ```compile_fail,E0392
3513 struct Foo<'a, T> {
3514     x: *const T,
3515 }
3516 ```
3517
3518 We want to express the constraint that Foo should not outlive `'a`, because
3519 the data pointed to by `T` is only valid for that lifetime. The problem is
3520 that there are no actual uses of `'a`. It's possible to work around this
3521 by adding a PhantomData type to the struct, using it to tell the compiler
3522 to act as if the struct contained a borrowed reference `&'a T`:
3523
3524 ```
3525 use std::marker::PhantomData;
3526
3527 struct Foo<'a, T: 'a> {
3528     x: *const T,
3529     phantom: PhantomData<&'a T>
3530 }
3531 ```
3532
3533 PhantomData can also be used to express information about unused type
3534 parameters. You can read more about it in the API documentation:
3535
3536 https://doc.rust-lang.org/std/marker/struct.PhantomData.html
3537 "##,
3538
3539 E0393: r##"
3540 A type parameter which references `Self` in its default value was not specified.
3541 Example of erroneous code:
3542
3543 ```compile_fail,E0393
3544 trait A<T=Self> {}
3545
3546 fn together_we_will_rule_the_galaxy(son: &A) {}
3547 // error: the type parameter `T` must be explicitly specified in an
3548 //        object type because its default value `Self` references the
3549 //        type `Self`
3550 ```
3551
3552 A trait object is defined over a single, fully-defined trait. With a regular
3553 default parameter, this parameter can just be substituted in. However, if the
3554 default parameter is `Self`, the trait changes for each concrete type; i.e.
3555 `i32` will be expected to implement `A<i32>`, `bool` will be expected to
3556 implement `A<bool>`, etc... These types will not share an implementation of a
3557 fully-defined trait; instead they share implementations of a trait with
3558 different parameters substituted in for each implementation. This is
3559 irreconcilable with what we need to make a trait object work, and is thus
3560 disallowed. Making the trait concrete by explicitly specifying the value of the
3561 defaulted parameter will fix this issue. Fixed example:
3562
3563 ```
3564 trait A<T=Self> {}
3565
3566 fn together_we_will_rule_the_galaxy(son: &A<i32>) {} // Ok!
3567 ```
3568 "##,
3569
3570 E0439: r##"
3571 The length of the platform-intrinsic function `simd_shuffle`
3572 wasn't specified. Erroneous code example:
3573
3574 ```compile_fail,E0439
3575 #![feature(platform_intrinsics)]
3576
3577 extern "platform-intrinsic" {
3578     fn simd_shuffle<A,B>(a: A, b: A, c: [u32; 8]) -> B;
3579     // error: invalid `simd_shuffle`, needs length: `simd_shuffle`
3580 }
3581 ```
3582
3583 The `simd_shuffle` function needs the length of the array passed as
3584 last parameter in its name. Example:
3585
3586 ```
3587 #![feature(platform_intrinsics)]
3588
3589 extern "platform-intrinsic" {
3590     fn simd_shuffle8<A,B>(a: A, b: A, c: [u32; 8]) -> B;
3591 }
3592 ```
3593 "##,
3594
3595 E0440: r##"
3596 A platform-specific intrinsic function has the wrong number of type
3597 parameters. Erroneous code example:
3598
3599 ```compile_fail,E0440
3600 #![feature(repr_simd)]
3601 #![feature(platform_intrinsics)]
3602
3603 #[repr(simd)]
3604 struct f64x2(f64, f64);
3605
3606 extern "platform-intrinsic" {
3607     fn x86_mm_movemask_pd<T>(x: f64x2) -> i32;
3608     // error: platform-specific intrinsic has wrong number of type
3609     //        parameters
3610 }
3611 ```
3612
3613 Please refer to the function declaration to see if it corresponds
3614 with yours. Example:
3615
3616 ```
3617 #![feature(repr_simd)]
3618 #![feature(platform_intrinsics)]
3619
3620 #[repr(simd)]
3621 struct f64x2(f64, f64);
3622
3623 extern "platform-intrinsic" {
3624     fn x86_mm_movemask_pd(x: f64x2) -> i32;
3625 }
3626 ```
3627 "##,
3628
3629 E0441: r##"
3630 An unknown platform-specific intrinsic function was used. Erroneous
3631 code example:
3632
3633 ```compile_fail,E0441
3634 #![feature(repr_simd)]
3635 #![feature(platform_intrinsics)]
3636
3637 #[repr(simd)]
3638 struct i16x8(i16, i16, i16, i16, i16, i16, i16, i16);
3639
3640 extern "platform-intrinsic" {
3641     fn x86_mm_adds_ep16(x: i16x8, y: i16x8) -> i16x8;
3642     // error: unrecognized platform-specific intrinsic function
3643 }
3644 ```
3645
3646 Please verify that the function name wasn't misspelled, and ensure
3647 that it is declared in the rust source code (in the file
3648 src/librustc_platform_intrinsics/x86.rs). Example:
3649
3650 ```
3651 #![feature(repr_simd)]
3652 #![feature(platform_intrinsics)]
3653
3654 #[repr(simd)]
3655 struct i16x8(i16, i16, i16, i16, i16, i16, i16, i16);
3656
3657 extern "platform-intrinsic" {
3658     fn x86_mm_adds_epi16(x: i16x8, y: i16x8) -> i16x8; // ok!
3659 }
3660 ```
3661 "##,
3662
3663 E0442: r##"
3664 Intrinsic argument(s) and/or return value have the wrong type.
3665 Erroneous code example:
3666
3667 ```compile_fail,E0442
3668 #![feature(repr_simd)]
3669 #![feature(platform_intrinsics)]
3670
3671 #[repr(simd)]
3672 struct i8x16(i8, i8, i8, i8, i8, i8, i8, i8,
3673              i8, i8, i8, i8, i8, i8, i8, i8);
3674 #[repr(simd)]
3675 struct i32x4(i32, i32, i32, i32);
3676 #[repr(simd)]
3677 struct i64x2(i64, i64);
3678
3679 extern "platform-intrinsic" {
3680     fn x86_mm_adds_epi16(x: i8x16, y: i32x4) -> i64x2;
3681     // error: intrinsic arguments/return value have wrong type
3682 }
3683 ```
3684
3685 To fix this error, please refer to the function declaration to give
3686 it the awaited types. Example:
3687
3688 ```
3689 #![feature(repr_simd)]
3690 #![feature(platform_intrinsics)]
3691
3692 #[repr(simd)]
3693 struct i16x8(i16, i16, i16, i16, i16, i16, i16, i16);
3694
3695 extern "platform-intrinsic" {
3696     fn x86_mm_adds_epi16(x: i16x8, y: i16x8) -> i16x8; // ok!
3697 }
3698 ```
3699 "##,
3700
3701 E0443: r##"
3702 Intrinsic argument(s) and/or return value have the wrong type.
3703 Erroneous code example:
3704
3705 ```compile_fail,E0443
3706 #![feature(repr_simd)]
3707 #![feature(platform_intrinsics)]
3708
3709 #[repr(simd)]
3710 struct i16x8(i16, i16, i16, i16, i16, i16, i16, i16);
3711 #[repr(simd)]
3712 struct i64x8(i64, i64, i64, i64, i64, i64, i64, i64);
3713
3714 extern "platform-intrinsic" {
3715     fn x86_mm_adds_epi16(x: i16x8, y: i16x8) -> i64x8;
3716     // error: intrinsic argument/return value has wrong type
3717 }
3718 ```
3719
3720 To fix this error, please refer to the function declaration to give
3721 it the awaited types. Example:
3722
3723 ```
3724 #![feature(repr_simd)]
3725 #![feature(platform_intrinsics)]
3726
3727 #[repr(simd)]
3728 struct i16x8(i16, i16, i16, i16, i16, i16, i16, i16);
3729
3730 extern "platform-intrinsic" {
3731     fn x86_mm_adds_epi16(x: i16x8, y: i16x8) -> i16x8; // ok!
3732 }
3733 ```
3734 "##,
3735
3736 E0444: r##"
3737 A platform-specific intrinsic function has wrong number of arguments.
3738 Erroneous code example:
3739
3740 ```compile_fail,E0444
3741 #![feature(repr_simd)]
3742 #![feature(platform_intrinsics)]
3743
3744 #[repr(simd)]
3745 struct f64x2(f64, f64);
3746
3747 extern "platform-intrinsic" {
3748     fn x86_mm_movemask_pd(x: f64x2, y: f64x2, z: f64x2) -> i32;
3749     // error: platform-specific intrinsic has invalid number of arguments
3750 }
3751 ```
3752
3753 Please refer to the function declaration to see if it corresponds
3754 with yours. Example:
3755
3756 ```
3757 #![feature(repr_simd)]
3758 #![feature(platform_intrinsics)]
3759
3760 #[repr(simd)]
3761 struct f64x2(f64, f64);
3762
3763 extern "platform-intrinsic" {
3764     fn x86_mm_movemask_pd(x: f64x2) -> i32; // ok!
3765 }
3766 ```
3767 "##,
3768
3769 E0516: r##"
3770 The `typeof` keyword is currently reserved but unimplemented.
3771 Erroneous code example:
3772
3773 ```compile_fail,E0516
3774 fn main() {
3775     let x: typeof(92) = 92;
3776 }
3777 ```
3778
3779 Try using type inference instead. Example:
3780
3781 ```
3782 fn main() {
3783     let x = 92;
3784 }
3785 ```
3786 "##,
3787
3788 E0520: r##"
3789 A non-default implementation was already made on this type so it cannot be
3790 specialized further. Erroneous code example:
3791
3792 ```compile_fail,E0520
3793 #![feature(specialization)]
3794
3795 trait SpaceLlama {
3796     fn fly(&self);
3797 }
3798
3799 // applies to all T
3800 impl<T> SpaceLlama for T {
3801     default fn fly(&self) {}
3802 }
3803
3804 // non-default impl
3805 // applies to all `Clone` T and overrides the previous impl
3806 impl<T: Clone> SpaceLlama for T {
3807     fn fly(&self) {}
3808 }
3809
3810 // since `i32` is clone, this conflicts with the previous implementation
3811 impl SpaceLlama for i32 {
3812     default fn fly(&self) {}
3813     // error: item `fly` is provided by an `impl` that specializes
3814     //        another, but the item in the parent `impl` is not marked
3815     //        `default` and so it cannot be specialized.
3816 }
3817 ```
3818
3819 Specialization only allows you to override `default` functions in
3820 implementations.
3821
3822 To fix this error, you need to mark all the parent implementations as default.
3823 Example:
3824
3825 ```
3826 #![feature(specialization)]
3827
3828 trait SpaceLlama {
3829     fn fly(&self);
3830 }
3831
3832 // applies to all T
3833 impl<T> SpaceLlama for T {
3834     default fn fly(&self) {} // This is a parent implementation.
3835 }
3836
3837 // applies to all `Clone` T; overrides the previous impl
3838 impl<T: Clone> SpaceLlama for T {
3839     default fn fly(&self) {} // This is a parent implementation but was
3840                              // previously not a default one, causing the error
3841 }
3842
3843 // applies to i32, overrides the previous two impls
3844 impl SpaceLlama for i32 {
3845     fn fly(&self) {} // And now that's ok!
3846 }
3847 ```
3848 "##,
3849
3850 E0527: r##"
3851 The number of elements in an array or slice pattern differed from the number of
3852 elements in the array being matched.
3853
3854 Example of erroneous code:
3855
3856 ```compile_fail,E0527
3857 #![feature(slice_patterns)]
3858
3859 let r = &[1, 2, 3, 4];
3860 match r {
3861     &[a, b] => { // error: pattern requires 2 elements but array
3862                  //        has 4
3863         println!("a={}, b={}", a, b);
3864     }
3865 }
3866 ```
3867
3868 Ensure that the pattern is consistent with the size of the matched
3869 array. Additional elements can be matched with `..`:
3870
3871 ```
3872 #![feature(slice_patterns)]
3873
3874 let r = &[1, 2, 3, 4];
3875 match r {
3876     &[a, b, ..] => { // ok!
3877         println!("a={}, b={}", a, b);
3878     }
3879 }
3880 ```
3881 "##,
3882
3883 E0528: r##"
3884 An array or slice pattern required more elements than were present in the
3885 matched array.
3886
3887 Example of erroneous code:
3888
3889 ```compile_fail,E0528
3890 #![feature(slice_patterns)]
3891
3892 let r = &[1, 2];
3893 match r {
3894     &[a, b, c, rest..] => { // error: pattern requires at least 3
3895                             //        elements but array has 2
3896         println!("a={}, b={}, c={} rest={:?}", a, b, c, rest);
3897     }
3898 }
3899 ```
3900
3901 Ensure that the matched array has at least as many elements as the pattern
3902 requires. You can match an arbitrary number of remaining elements with `..`:
3903
3904 ```
3905 #![feature(slice_patterns)]
3906
3907 let r = &[1, 2, 3, 4, 5];
3908 match r {
3909     &[a, b, c, rest..] => { // ok!
3910         // prints `a=1, b=2, c=3 rest=[4, 5]`
3911         println!("a={}, b={}, c={} rest={:?}", a, b, c, rest);
3912     }
3913 }
3914 ```
3915 "##,
3916
3917 E0529: r##"
3918 An array or slice pattern was matched against some other type.
3919
3920 Example of erroneous code:
3921
3922 ```compile_fail,E0529
3923 #![feature(slice_patterns)]
3924
3925 let r: f32 = 1.0;
3926 match r {
3927     [a, b] => { // error: expected an array or slice, found `f32`
3928         println!("a={}, b={}", a, b);
3929     }
3930 }
3931 ```
3932
3933 Ensure that the pattern and the expression being matched on are of consistent
3934 types:
3935
3936 ```
3937 #![feature(slice_patterns)]
3938
3939 let r = [1.0, 2.0];
3940 match r {
3941     [a, b] => { // ok!
3942         println!("a={}, b={}", a, b);
3943     }
3944 }
3945 ```
3946 "##,
3947
3948 E0559: r##"
3949 An unknown field was specified into an enum's structure variant.
3950
3951 Erroneous code example:
3952
3953 ```compile_fail,E0559
3954 enum Field {
3955     Fool { x: u32 },
3956 }
3957
3958 let s = Field::Fool { joke: 0 };
3959 // error: struct variant `Field::Fool` has no field named `joke`
3960 ```
3961
3962 Verify you didn't misspell the field's name or that the field exists. Example:
3963
3964 ```
3965 enum Field {
3966     Fool { joke: u32 },
3967 }
3968
3969 let s = Field::Fool { joke: 0 }; // ok!
3970 ```
3971 "##,
3972
3973 E0560: r##"
3974 An unknown field was specified into a structure.
3975
3976 Erroneous code example:
3977
3978 ```compile_fail,E0560
3979 struct Simba {
3980     mother: u32,
3981 }
3982
3983 let s = Simba { mother: 1, father: 0 };
3984 // error: structure `Simba` has no field named `father`
3985 ```
3986
3987 Verify you didn't misspell the field's name or that the field exists. Example:
3988
3989 ```
3990 struct Simba {
3991     mother: u32,
3992     father: u32,
3993 }
3994
3995 let s = Simba { mother: 1, father: 0 }; // ok!
3996 ```
3997 "##,
3998
3999 }
4000
4001 register_diagnostics! {
4002 //  E0068,
4003 //  E0085,
4004 //  E0086,
4005     E0090,
4006     E0103, // @GuillaumeGomez: I was unable to get this error, try your best!
4007     E0104,
4008 //  E0123,
4009 //  E0127,
4010 //  E0129,
4011 //  E0141,
4012 //  E0159, // use of trait `{}` as struct constructor
4013 //  E0163, // merged into E0071
4014 //  E0167,
4015 //  E0168,
4016 //  E0173, // manual implementations of unboxed closure traits are experimental
4017 //  E0174,
4018     E0182,
4019     E0183,
4020 //  E0187, // can't infer the kind of the closure
4021 //  E0188, // can not cast an immutable reference to a mutable pointer
4022 //  E0189, // deprecated: can only cast a boxed pointer to a boxed object
4023 //  E0190, // deprecated: can only cast a &-pointer to an &-object
4024     E0196, // cannot determine a type for this closure
4025     E0203, // type parameter has more than one relaxed default bound,
4026            // and only one is supported
4027     E0208,
4028 //  E0209, // builtin traits can only be implemented on structs or enums
4029     E0212, // cannot extract an associated type from a higher-ranked trait bound
4030 //  E0213, // associated types are not accepted in this context
4031 //  E0215, // angle-bracket notation is not stable with `Fn`
4032 //  E0216, // parenthetical notation is only stable with `Fn`
4033 //  E0217, // ambiguous associated type, defined in multiple supertraits
4034 //  E0218, // no associated type defined
4035 //  E0219, // associated type defined in higher-ranked supertrait
4036 //  E0222, // Error code E0045 (variadic function must have C calling
4037            // convention) duplicate
4038     E0224, // at least one non-builtin train is required for an object type
4039     E0226, // only a single explicit lifetime bound is permitted
4040     E0227, // ambiguous lifetime bound, explicit lifetime bound required
4041     E0228, // explicit lifetime bound required
4042     E0230, // there is no type parameter on trait
4043     E0231, // only named substitution parameters are allowed
4044 //  E0233,
4045 //  E0234,
4046 //  E0235, // structure constructor specifies a structure of type but
4047 //  E0236, // no lang item for range syntax
4048 //  E0237, // no lang item for range syntax
4049 //  E0238, // parenthesized parameters may only be used with a trait
4050 //  E0239, // `next` method of `Iterator` trait has unexpected type
4051 //  E0240,
4052 //  E0241,
4053 //  E0242,
4054     E0245, // not a trait
4055 //  E0246, // invalid recursive type
4056 //  E0247,
4057 //  E0249,
4058 //  E0319, // trait impls for defaulted traits allowed just for structs/enums
4059     E0320, // recursive overflow during dropck
4060     E0328, // cannot implement Unsize explicitly
4061 //  E0372, // coherence not object safe
4062     E0377, // the trait `CoerceUnsized` may only be implemented for a coercion
4063            // between structures with the same definition
4064     E0399, // trait items need to be implemented because the associated
4065            // type `{}` was overridden
4066     E0436, // functional record update requires a struct
4067     E0513, // no type for local variable ..
4068     E0521, // redundant default implementations of trait
4069     E0533, // `{}` does not name a unit variant, unit struct or a constant
4070     E0562, // `impl Trait` not allowed outside of function
4071            // and inherent method return types
4072     E0563, // cannot determine a type for this `impl Trait`: {}
4073     E0564, // only named lifetimes are allowed in `impl Trait`,
4074            // but `{}` was found in the type `{}`
4075 }