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