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