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