]> git.lizzy.rs Git - rust.git/blob - src/librustc/diagnostics.rs
Add new error code
[rust.git] / src / librustc / 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 // Error messages for EXXXX errors.
14 // Each message should start and end with a new line, and be wrapped to 80 characters.
15 // In vim you can `:set tw=80` and use `gq` to wrap paragraphs. Use `:set tw=0` to disable.
16 register_long_diagnostics! {
17 E0020: r##"
18 This error indicates that an attempt was made to divide by zero (or take the
19 remainder of a zero divisor) in a static or constant expression. Erroneous
20 code example:
21
22 ```compile_fail
23 #[deny(const_err)]
24
25 const X: i32 = 42 / 0;
26 // error: attempt to divide by zero in a constant expression
27 ```
28 "##,
29
30 E0038: r##"
31 Trait objects like `Box<Trait>` can only be constructed when certain
32 requirements are satisfied by the trait in question.
33
34 Trait objects are a form of dynamic dispatch and use a dynamically sized type
35 for the inner type. So, for a given trait `Trait`, when `Trait` is treated as a
36 type, as in `Box<Trait>`, the inner type is 'unsized'. In such cases the boxed
37 pointer is a 'fat pointer' that contains an extra pointer to a table of methods
38 (among other things) for dynamic dispatch. This design mandates some
39 restrictions on the types of traits that are allowed to be used in trait
40 objects, which are collectively termed as 'object safety' rules.
41
42 Attempting to create a trait object for a non object-safe trait will trigger
43 this error.
44
45 There are various rules:
46
47 ### The trait cannot require `Self: Sized`
48
49 When `Trait` is treated as a type, the type does not implement the special
50 `Sized` trait, because the type does not have a known size at compile time and
51 can only be accessed behind a pointer. Thus, if we have a trait like the
52 following:
53
54 ```
55 trait Foo where Self: Sized {
56
57 }
58 ```
59
60 We cannot create an object of type `Box<Foo>` or `&Foo` since in this case
61 `Self` would not be `Sized`.
62
63 Generally, `Self : Sized` is used to indicate that the trait should not be used
64 as a trait object. If the trait comes from your own crate, consider removing
65 this restriction.
66
67 ### Method references the `Self` type in its arguments or return type
68
69 This happens when a trait has a method like the following:
70
71 ```
72 trait Trait {
73     fn foo(&self) -> Self;
74 }
75
76 impl Trait for String {
77     fn foo(&self) -> Self {
78         "hi".to_owned()
79     }
80 }
81
82 impl Trait for u8 {
83     fn foo(&self) -> Self {
84         1
85     }
86 }
87 ```
88
89 (Note that `&self` and `&mut self` are okay, it's additional `Self` types which
90 cause this problem.)
91
92 In such a case, the compiler cannot predict the return type of `foo()` in a
93 situation like the following:
94
95 ```compile_fail
96 trait Trait {
97     fn foo(&self) -> Self;
98 }
99
100 fn call_foo(x: Box<Trait>) {
101     let y = x.foo(); // What type is y?
102     // ...
103 }
104 ```
105
106 If only some methods aren't object-safe, you can add a `where Self: Sized` bound
107 on them to mark them as explicitly unavailable to trait objects. The
108 functionality will still be available to all other implementers, including
109 `Box<Trait>` which is itself sized (assuming you `impl Trait for Box<Trait>`).
110
111 ```
112 trait Trait {
113     fn foo(&self) -> Self where Self: Sized;
114     // more functions
115 }
116 ```
117
118 Now, `foo()` can no longer be called on a trait object, but you will now be
119 allowed to make a trait object, and that will be able to call any object-safe
120 methods. With such a bound, one can still call `foo()` on types implementing
121 that trait that aren't behind trait objects.
122
123 ### Method has generic type parameters
124
125 As mentioned before, trait objects contain pointers to method tables. So, if we
126 have:
127
128 ```
129 trait Trait {
130     fn foo(&self);
131 }
132
133 impl Trait for String {
134     fn foo(&self) {
135         // implementation 1
136     }
137 }
138
139 impl Trait for u8 {
140     fn foo(&self) {
141         // implementation 2
142     }
143 }
144 // ...
145 ```
146
147 At compile time each implementation of `Trait` will produce a table containing
148 the various methods (and other items) related to the implementation.
149
150 This works fine, but when the method gains generic parameters, we can have a
151 problem.
152
153 Usually, generic parameters get _monomorphized_. For example, if I have
154
155 ```
156 fn foo<T>(x: T) {
157     // ...
158 }
159 ```
160
161 The machine code for `foo::<u8>()`, `foo::<bool>()`, `foo::<String>()`, or any
162 other type substitution is different. Hence the compiler generates the
163 implementation on-demand. If you call `foo()` with a `bool` parameter, the
164 compiler will only generate code for `foo::<bool>()`. When we have additional
165 type parameters, the number of monomorphized implementations the compiler
166 generates does not grow drastically, since the compiler will only generate an
167 implementation if the function is called with unparametrized substitutions
168 (i.e., substitutions where none of the substituted types are themselves
169 parametrized).
170
171 However, with trait objects we have to make a table containing _every_ object
172 that implements the trait. Now, if it has type parameters, we need to add
173 implementations for every type that implements the trait, and there could
174 theoretically be an infinite number of types.
175
176 For example, with:
177
178 ```
179 trait Trait {
180     fn foo<T>(&self, on: T);
181     // more methods
182 }
183
184 impl Trait for String {
185     fn foo<T>(&self, on: T) {
186         // implementation 1
187     }
188 }
189
190 impl Trait for u8 {
191     fn foo<T>(&self, on: T) {
192         // implementation 2
193     }
194 }
195
196 // 8 more implementations
197 ```
198
199 Now, if we have the following code:
200
201 ```ignore
202 fn call_foo(thing: Box<Trait>) {
203     thing.foo(true); // this could be any one of the 8 types above
204     thing.foo(1);
205     thing.foo("hello");
206 }
207 ```
208
209 We don't just need to create a table of all implementations of all methods of
210 `Trait`, we need to create such a table, for each different type fed to
211 `foo()`. In this case this turns out to be (10 types implementing `Trait`)*(3
212 types being fed to `foo()`) = 30 implementations!
213
214 With real world traits these numbers can grow drastically.
215
216 To fix this, it is suggested to use a `where Self: Sized` bound similar to the
217 fix for the sub-error above if you do not intend to call the method with type
218 parameters:
219
220 ```
221 trait Trait {
222     fn foo<T>(&self, on: T) where Self: Sized;
223     // more methods
224 }
225 ```
226
227 If this is not an option, consider replacing the type parameter with another
228 trait object (e.g. if `T: OtherTrait`, use `on: Box<OtherTrait>`). If the number
229 of types you intend to feed to this method is limited, consider manually listing
230 out the methods of different types.
231
232 ### Method has no receiver
233
234 Methods that do not take a `self` parameter can't be called since there won't be
235 a way to get a pointer to the method table for them.
236
237 ```
238 trait Foo {
239     fn foo() -> u8;
240 }
241 ```
242
243 This could be called as `<Foo as Foo>::foo()`, which would not be able to pick
244 an implementation.
245
246 Adding a `Self: Sized` bound to these methods will generally make this compile.
247
248 ```
249 trait Foo {
250     fn foo() -> u8 where Self: Sized;
251 }
252 ```
253
254 ### The trait cannot use `Self` as a type parameter in the supertrait listing
255
256 This is similar to the second sub-error, but subtler. It happens in situations
257 like the following:
258
259 ```compile_fail
260 trait Super<A> {}
261
262 trait Trait: Super<Self> {
263 }
264
265 struct Foo;
266
267 impl Super<Foo> for Foo{}
268
269 impl Trait for Foo {}
270 ```
271
272 Here, the supertrait might have methods as follows:
273
274 ```
275 trait Super<A> {
276     fn get_a(&self) -> A; // note that this is object safe!
277 }
278 ```
279
280 If the trait `Foo` was deriving from something like `Super<String>` or
281 `Super<T>` (where `Foo` itself is `Foo<T>`), this is okay, because given a type
282 `get_a()` will definitely return an object of that type.
283
284 However, if it derives from `Super<Self>`, even though `Super` is object safe,
285 the method `get_a()` would return an object of unknown type when called on the
286 function. `Self` type parameters let us make object safe traits no longer safe,
287 so they are forbidden when specifying supertraits.
288
289 There's no easy fix for this, generally code will need to be refactored so that
290 you no longer need to derive from `Super<Self>`.
291 "##,
292
293 E0072: r##"
294 When defining a recursive struct or enum, any use of the type being defined
295 from inside the definition must occur behind a pointer (like `Box` or `&`).
296 This is because structs and enums must have a well-defined size, and without
297 the pointer, the size of the type would need to be unbounded.
298
299 Consider the following erroneous definition of a type for a list of bytes:
300
301 ```compile_fail,E0072
302 // error, invalid recursive struct type
303 struct ListNode {
304     head: u8,
305     tail: Option<ListNode>,
306 }
307 ```
308
309 This type cannot have a well-defined size, because it needs to be arbitrarily
310 large (since we would be able to nest `ListNode`s to any depth). Specifically,
311
312 ```plain
313 size of `ListNode` = 1 byte for `head`
314                    + 1 byte for the discriminant of the `Option`
315                    + size of `ListNode`
316 ```
317
318 One way to fix this is by wrapping `ListNode` in a `Box`, like so:
319
320 ```
321 struct ListNode {
322     head: u8,
323     tail: Option<Box<ListNode>>,
324 }
325 ```
326
327 This works because `Box` is a pointer, so its size is well-known.
328 "##,
329
330 E0080: r##"
331 This error indicates that the compiler was unable to sensibly evaluate an
332 constant expression that had to be evaluated. Attempting to divide by 0
333 or causing integer overflow are two ways to induce this error. For example:
334
335 ```compile_fail,E0080
336 enum Enum {
337     X = (1 << 500),
338     Y = (1 / 0)
339 }
340 ```
341
342 Ensure that the expressions given can be evaluated as the desired integer type.
343 See the FFI section of the Reference for more information about using a custom
344 integer type:
345
346 https://doc.rust-lang.org/reference.html#ffi-attributes
347 "##,
348
349 E0106: r##"
350 This error indicates that a lifetime is missing from a type. If it is an error
351 inside a function signature, the problem may be with failing to adhere to the
352 lifetime elision rules (see below).
353
354 Here are some simple examples of where you'll run into this error:
355
356 ```compile_fail,E0106
357 struct Foo { x: &bool }        // error
358 struct Foo<'a> { x: &'a bool } // correct
359
360 enum Bar { A(u8), B(&bool), }        // error
361 enum Bar<'a> { A(u8), B(&'a bool), } // correct
362
363 type MyStr = &str;        // error
364 type MyStr<'a> = &'a str; // correct
365 ```
366
367 Lifetime elision is a special, limited kind of inference for lifetimes in
368 function signatures which allows you to leave out lifetimes in certain cases.
369 For more background on lifetime elision see [the book][book-le].
370
371 The lifetime elision rules require that any function signature with an elided
372 output lifetime must either have
373
374  - exactly one input lifetime
375  - or, multiple input lifetimes, but the function must also be a method with a
376    `&self` or `&mut self` receiver
377
378 In the first case, the output lifetime is inferred to be the same as the unique
379 input lifetime. In the second case, the lifetime is instead inferred to be the
380 same as the lifetime on `&self` or `&mut self`.
381
382 Here are some examples of elision errors:
383
384 ```compile_fail,E0106
385 // error, no input lifetimes
386 fn foo() -> &str { }
387
388 // error, `x` and `y` have distinct lifetimes inferred
389 fn bar(x: &str, y: &str) -> &str { }
390
391 // error, `y`'s lifetime is inferred to be distinct from `x`'s
392 fn baz<'a>(x: &'a str, y: &str) -> &str { }
393 ```
394
395 Here's an example that is currently an error, but may work in a future version
396 of Rust:
397
398 ```compile_fail,E0106
399 struct Foo<'a>(&'a str);
400
401 trait Quux { }
402 impl Quux for Foo { }
403 ```
404
405 Lifetime elision in implementation headers was part of the lifetime elision
406 RFC. It is, however, [currently unimplemented][iss15872].
407
408 [book-le]: https://doc.rust-lang.org/nightly/book/lifetimes.html#lifetime-elision
409 [iss15872]: https://github.com/rust-lang/rust/issues/15872
410 "##,
411
412 E0119: r##"
413 There are conflicting trait implementations for the same type.
414 Example of erroneous code:
415
416 ```compile_fail,E0119
417 trait MyTrait {
418     fn get(&self) -> usize;
419 }
420
421 impl<T> MyTrait for T {
422     fn get(&self) -> usize { 0 }
423 }
424
425 struct Foo {
426     value: usize
427 }
428
429 impl MyTrait for Foo { // error: conflicting implementations of trait
430                        //        `MyTrait` for type `Foo`
431     fn get(&self) -> usize { self.value }
432 }
433 ```
434
435 When looking for the implementation for the trait, the compiler finds
436 both the `impl<T> MyTrait for T` where T is all types and the `impl
437 MyTrait for Foo`. Since a trait cannot be implemented multiple times,
438 this is an error. So, when you write:
439
440 ```
441 trait MyTrait {
442     fn get(&self) -> usize;
443 }
444
445 impl<T> MyTrait for T {
446     fn get(&self) -> usize { 0 }
447 }
448 ```
449
450 This makes the trait implemented on all types in the scope. So if you
451 try to implement it on another one after that, the implementations will
452 conflict. Example:
453
454 ```
455 trait MyTrait {
456     fn get(&self) -> usize;
457 }
458
459 impl<T> MyTrait for T {
460     fn get(&self) -> usize { 0 }
461 }
462
463 struct Foo;
464
465 fn main() {
466     let f = Foo;
467
468     f.get(); // the trait is implemented so we can use it
469 }
470 ```
471 "##,
472
473 E0133: r##"
474 Unsafe code was used outside of an unsafe function or block.
475
476 Erroneous code example:
477
478 ```compile_fail,E0133
479 unsafe fn f() { return; } // This is the unsafe code
480
481 fn main() {
482     f(); // error: call to unsafe function requires unsafe function or block
483 }
484 ```
485
486 Using unsafe functionality is potentially dangerous and disallowed by safety
487 checks. Examples:
488
489 * Dereferencing raw pointers
490 * Calling functions via FFI
491 * Calling functions marked unsafe
492
493 These safety checks can be relaxed for a section of the code by wrapping the
494 unsafe instructions with an `unsafe` block. For instance:
495
496 ```
497 unsafe fn f() { return; }
498
499 fn main() {
500     unsafe { f(); } // ok!
501 }
502 ```
503
504 See also https://doc.rust-lang.org/book/unsafe.html
505 "##,
506
507 // This shouldn't really ever trigger since the repeated value error comes first
508 E0136: r##"
509 A binary can only have one entry point, and by default that entry point is the
510 function `main()`. If there are multiple such functions, please rename one.
511 "##,
512
513 E0137: r##"
514 More than one function was declared with the `#[main]` attribute.
515
516 Erroneous code example:
517
518 ```compile_fail,E0137
519 #![feature(main)]
520
521 #[main]
522 fn foo() {}
523
524 #[main]
525 fn f() {} // error: multiple functions with a #[main] attribute
526 ```
527
528 This error indicates that the compiler found multiple functions with the
529 `#[main]` attribute. This is an error because there must be a unique entry
530 point into a Rust program. Example:
531
532 ```
533 #![feature(main)]
534
535 #[main]
536 fn f() {} // ok!
537 ```
538 "##,
539
540 E0138: r##"
541 More than one function was declared with the `#[start]` attribute.
542
543 Erroneous code example:
544
545 ```compile_fail,E0138
546 #![feature(start)]
547
548 #[start]
549 fn foo(argc: isize, argv: *const *const u8) -> isize {}
550
551 #[start]
552 fn f(argc: isize, argv: *const *const u8) -> isize {}
553 // error: multiple 'start' functions
554 ```
555
556 This error indicates that the compiler found multiple functions with the
557 `#[start]` attribute. This is an error because there must be a unique entry
558 point into a Rust program. Example:
559
560 ```
561 #![feature(start)]
562
563 #[start]
564 fn foo(argc: isize, argv: *const *const u8) -> isize { 0 } // ok!
565 ```
566 "##,
567
568 // isn't thrown anymore
569 E0139: r##"
570 There are various restrictions on transmuting between types in Rust; for example
571 types being transmuted must have the same size. To apply all these restrictions,
572 the compiler must know the exact types that may be transmuted. When type
573 parameters are involved, this cannot always be done.
574
575 So, for example, the following is not allowed:
576
577 ```
578 use std::mem::transmute;
579
580 struct Foo<T>(Vec<T>);
581
582 fn foo<T>(x: Vec<T>) {
583     // we are transmuting between Vec<T> and Foo<F> here
584     let y: Foo<T> = unsafe { transmute(x) };
585     // do something with y
586 }
587 ```
588
589 In this specific case there's a good chance that the transmute is harmless (but
590 this is not guaranteed by Rust). However, when alignment and enum optimizations
591 come into the picture, it's quite likely that the sizes may or may not match
592 with different type parameter substitutions. It's not possible to check this for
593 _all_ possible types, so `transmute()` simply only accepts types without any
594 unsubstituted type parameters.
595
596 If you need this, there's a good chance you're doing something wrong. Keep in
597 mind that Rust doesn't guarantee much about the layout of different structs
598 (even two structs with identical declarations may have different layouts). If
599 there is a solution that avoids the transmute entirely, try it instead.
600
601 If it's possible, hand-monomorphize the code by writing the function for each
602 possible type substitution. It's possible to use traits to do this cleanly,
603 for example:
604
605 ```ignore
606 struct Foo<T>(Vec<T>);
607
608 trait MyTransmutableType {
609     fn transmute(Vec<Self>) -> Foo<Self>;
610 }
611
612 impl MyTransmutableType for u8 {
613     fn transmute(x: Foo<u8>) -> Vec<u8> {
614         transmute(x)
615     }
616 }
617
618 impl MyTransmutableType for String {
619     fn transmute(x: Foo<String>) -> Vec<String> {
620         transmute(x)
621     }
622 }
623
624 // ... more impls for the types you intend to transmute
625
626 fn foo<T: MyTransmutableType>(x: Vec<T>) {
627     let y: Foo<T> = <T as MyTransmutableType>::transmute(x);
628     // do something with y
629 }
630 ```
631
632 Each impl will be checked for a size match in the transmute as usual, and since
633 there are no unbound type parameters involved, this should compile unless there
634 is a size mismatch in one of the impls.
635
636 It is also possible to manually transmute:
637
638 ```ignore
639 ptr::read(&v as *const _ as *const SomeType) // `v` transmuted to `SomeType`
640 ```
641
642 Note that this does not move `v` (unlike `transmute`), and may need a
643 call to `mem::forget(v)` in case you want to avoid destructors being called.
644 "##,
645
646 E0152: r##"
647 A lang item was redefined.
648
649 Erroneous code example:
650
651 ```compile_fail,E0152
652 #![feature(lang_items)]
653
654 #[lang = "panic_fmt"]
655 struct Foo; // error: duplicate lang item found: `panic_fmt`
656 ```
657
658 Lang items are already implemented in the standard library. Unless you are
659 writing a free-standing application (e.g. a kernel), you do not need to provide
660 them yourself.
661
662 You can build a free-standing crate by adding `#![no_std]` to the crate
663 attributes:
664
665 ```ignore
666 #![no_std]
667 ```
668
669 See also https://doc.rust-lang.org/book/no-stdlib.html
670 "##,
671
672 E0261: r##"
673 When using a lifetime like `'a` in a type, it must be declared before being
674 used.
675
676 These two examples illustrate the problem:
677
678 ```compile_fail,E0261
679 // error, use of undeclared lifetime name `'a`
680 fn foo(x: &'a str) { }
681
682 struct Foo {
683     // error, use of undeclared lifetime name `'a`
684     x: &'a str,
685 }
686 ```
687
688 These can be fixed by declaring lifetime parameters:
689
690 ```
691 fn foo<'a>(x: &'a str) {}
692
693 struct Foo<'a> {
694     x: &'a str,
695 }
696 ```
697 "##,
698
699 E0262: r##"
700 Declaring certain lifetime names in parameters is disallowed. For example,
701 because the `'static` lifetime is a special built-in lifetime name denoting
702 the lifetime of the entire program, this is an error:
703
704 ```compile_fail,E0262
705 // error, invalid lifetime parameter name `'static`
706 fn foo<'static>(x: &'static str) { }
707 ```
708 "##,
709
710 E0263: r##"
711 A lifetime name cannot be declared more than once in the same scope. For
712 example:
713
714 ```compile_fail,E0263
715 // error, lifetime name `'a` declared twice in the same scope
716 fn foo<'a, 'b, 'a>(x: &'a str, y: &'b str) { }
717 ```
718 "##,
719
720 E0264: r##"
721 An unknown external lang item was used. Erroneous code example:
722
723 ```compile_fail,E0264
724 #![feature(lang_items)]
725
726 extern "C" {
727     #[lang = "cake"] // error: unknown external lang item: `cake`
728     fn cake();
729 }
730 ```
731
732 A list of available external lang items is available in
733 `src/librustc/middle/weak_lang_items.rs`. Example:
734
735 ```
736 #![feature(lang_items)]
737
738 extern "C" {
739     #[lang = "panic_fmt"] // ok!
740     fn cake();
741 }
742 ```
743 "##,
744
745 E0271: r##"
746 This is because of a type mismatch between the associated type of some
747 trait (e.g. `T::Bar`, where `T` implements `trait Quux { type Bar; }`)
748 and another type `U` that is required to be equal to `T::Bar`, but is not.
749 Examples follow.
750
751 Here is a basic example:
752
753 ```compile_fail,E0271
754 trait Trait { type AssociatedType; }
755
756 fn foo<T>(t: T) where T: Trait<AssociatedType=u32> {
757     println!("in foo");
758 }
759
760 impl Trait for i8 { type AssociatedType = &'static str; }
761
762 foo(3_i8);
763 ```
764
765 Here is that same example again, with some explanatory comments:
766
767 ```ignore
768 trait Trait { type AssociatedType; }
769
770 fn foo<T>(t: T) where T: Trait<AssociatedType=u32> {
771 //                    ~~~~~~~~ ~~~~~~~~~~~~~~~~~~
772 //                        |            |
773 //         This says `foo` can         |
774 //           only be used with         |
775 //              some type that         |
776 //         implements `Trait`.         |
777 //                                     |
778 //                             This says not only must
779 //                             `T` be an impl of `Trait`
780 //                             but also that the impl
781 //                             must assign the type `u32`
782 //                             to the associated type.
783     println!("in foo");
784 }
785
786 impl Trait for i8 { type AssociatedType = &'static str; }
787 ~~~~~~~~~~~~~~~~~   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
788 //      |                             |
789 // `i8` does have                     |
790 // implementation                     |
791 // of `Trait`...                      |
792 //                     ... but it is an implementation
793 //                     that assigns `&'static str` to
794 //                     the associated type.
795
796 foo(3_i8);
797 // Here, we invoke `foo` with an `i8`, which does not satisfy
798 // the constraint `<i8 as Trait>::AssociatedType=u32`, and
799 // therefore the type-checker complains with this error code.
800 ```
801
802 Here is a more subtle instance of the same problem, that can
803 arise with for-loops in Rust:
804
805 ```compile_fail
806 let vs: Vec<i32> = vec![1, 2, 3, 4];
807 for v in &vs {
808     match v {
809         1 => {},
810         _ => {},
811     }
812 }
813 ```
814
815 The above fails because of an analogous type mismatch,
816 though may be harder to see. Again, here are some
817 explanatory comments for the same example:
818
819 ```ignore
820 {
821     let vs = vec![1, 2, 3, 4];
822
823     // `for`-loops use a protocol based on the `Iterator`
824     // trait. Each item yielded in a `for` loop has the
825     // type `Iterator::Item` -- that is, `Item` is the
826     // associated type of the concrete iterator impl.
827     for v in &vs {
828 //      ~    ~~~
829 //      |     |
830 //      |    We borrow `vs`, iterating over a sequence of
831 //      |    *references* of type `&Elem` (where `Elem` is
832 //      |    vector's element type). Thus, the associated
833 //      |    type `Item` must be a reference `&`-type ...
834 //      |
835 //  ... and `v` has the type `Iterator::Item`, as dictated by
836 //  the `for`-loop protocol ...
837
838         match v {
839             1 => {}
840 //          ~
841 //          |
842 // ... but *here*, `v` is forced to have some integral type;
843 // only types like `u8`,`i8`,`u16`,`i16`, et cetera can
844 // match the pattern `1` ...
845
846             _ => {}
847         }
848
849 // ... therefore, the compiler complains, because it sees
850 // an attempt to solve the equations
851 // `some integral-type` = type-of-`v`
852 //                      = `Iterator::Item`
853 //                      = `&Elem` (i.e. `some reference type`)
854 //
855 // which cannot possibly all be true.
856
857     }
858 }
859 ```
860
861 To avoid those issues, you have to make the types match correctly.
862 So we can fix the previous examples like this:
863
864 ```
865 // Basic Example:
866 trait Trait { type AssociatedType; }
867
868 fn foo<T>(t: T) where T: Trait<AssociatedType = &'static str> {
869     println!("in foo");
870 }
871
872 impl Trait for i8 { type AssociatedType = &'static str; }
873
874 foo(3_i8);
875
876 // For-Loop Example:
877 let vs = vec![1, 2, 3, 4];
878 for v in &vs {
879     match v {
880         &1 => {}
881         _ => {}
882     }
883 }
884 ```
885 "##,
886
887 E0272: r##"
888 The `#[rustc_on_unimplemented]` attribute lets you specify a custom error
889 message for when a particular trait isn't implemented on a type placed in a
890 position that needs that trait. For example, when the following code is
891 compiled:
892
893 ```compile_fail
894 #![feature(on_unimplemented)]
895
896 fn foo<T: Index<u8>>(x: T){}
897
898 #[rustc_on_unimplemented = "the type `{Self}` cannot be indexed by `{Idx}`"]
899 trait Index<Idx> { /* ... */ }
900
901 foo(true); // `bool` does not implement `Index<u8>`
902 ```
903
904 There will be an error about `bool` not implementing `Index<u8>`, followed by a
905 note saying "the type `bool` cannot be indexed by `u8`".
906
907 As you can see, you can specify type parameters in curly braces for
908 substitution with the actual types (using the regular format string syntax) in
909 a given situation. Furthermore, `{Self}` will substitute to the type (in this
910 case, `bool`) that we tried to use.
911
912 This error appears when the curly braces contain an identifier which doesn't
913 match with any of the type parameters or the string `Self`. This might happen
914 if you misspelled a type parameter, or if you intended to use literal curly
915 braces. If it is the latter, escape the curly braces with a second curly brace
916 of the same type; e.g. a literal `{` is `{{`.
917 "##,
918
919 E0273: r##"
920 The `#[rustc_on_unimplemented]` attribute lets you specify a custom error
921 message for when a particular trait isn't implemented on a type placed in a
922 position that needs that trait. For example, when the following code is
923 compiled:
924
925 ```compile_fail
926 #![feature(on_unimplemented)]
927
928 fn foo<T: Index<u8>>(x: T){}
929
930 #[rustc_on_unimplemented = "the type `{Self}` cannot be indexed by `{Idx}`"]
931 trait Index<Idx> { /* ... */ }
932
933 foo(true); // `bool` does not implement `Index<u8>`
934 ```
935
936 there will be an error about `bool` not implementing `Index<u8>`, followed by a
937 note saying "the type `bool` cannot be indexed by `u8`".
938
939 As you can see, you can specify type parameters in curly braces for
940 substitution with the actual types (using the regular format string syntax) in
941 a given situation. Furthermore, `{Self}` will substitute to the type (in this
942 case, `bool`) that we tried to use.
943
944 This error appears when the curly braces do not contain an identifier. Please
945 add one of the same name as a type parameter. If you intended to use literal
946 braces, use `{{` and `}}` to escape them.
947 "##,
948
949 E0274: r##"
950 The `#[rustc_on_unimplemented]` attribute lets you specify a custom error
951 message for when a particular trait isn't implemented on a type placed in a
952 position that needs that trait. For example, when the following code is
953 compiled:
954
955 ```compile_fail
956 #![feature(on_unimplemented)]
957
958 fn foo<T: Index<u8>>(x: T){}
959
960 #[rustc_on_unimplemented = "the type `{Self}` cannot be indexed by `{Idx}`"]
961 trait Index<Idx> { /* ... */ }
962
963 foo(true); // `bool` does not implement `Index<u8>`
964 ```
965
966 there will be an error about `bool` not implementing `Index<u8>`, followed by a
967 note saying "the type `bool` cannot be indexed by `u8`".
968
969 For this to work, some note must be specified. An empty attribute will not do
970 anything, please remove the attribute or add some helpful note for users of the
971 trait.
972 "##,
973
974 E0275: r##"
975 This error occurs when there was a recursive trait requirement that overflowed
976 before it could be evaluated. Often this means that there is unbounded
977 recursion in resolving some type bounds.
978
979 For example, in the following code:
980
981 ```compile_fail,E0275
982 trait Foo {}
983
984 struct Bar<T>(T);
985
986 impl<T> Foo for T where Bar<T>: Foo {}
987 ```
988
989 To determine if a `T` is `Foo`, we need to check if `Bar<T>` is `Foo`. However,
990 to do this check, we need to determine that `Bar<Bar<T>>` is `Foo`. To
991 determine this, we check if `Bar<Bar<Bar<T>>>` is `Foo`, and so on. This is
992 clearly a recursive requirement that can't be resolved directly.
993
994 Consider changing your trait bounds so that they're less self-referential.
995 "##,
996
997 E0276: r##"
998 This error occurs when a bound in an implementation of a trait does not match
999 the bounds specified in the original trait. For example:
1000
1001 ```compile_fail,E0276
1002 trait Foo {
1003     fn foo<T>(x: T);
1004 }
1005
1006 impl Foo for bool {
1007     fn foo<T>(x: T) where T: Copy {}
1008 }
1009 ```
1010
1011 Here, all types implementing `Foo` must have a method `foo<T>(x: T)` which can
1012 take any type `T`. However, in the `impl` for `bool`, we have added an extra
1013 bound that `T` is `Copy`, which isn't compatible with the original trait.
1014
1015 Consider removing the bound from the method or adding the bound to the original
1016 method definition in the trait.
1017 "##,
1018
1019 E0277: r##"
1020 You tried to use a type which doesn't implement some trait in a place which
1021 expected that trait. Erroneous code example:
1022
1023 ```compile_fail,E0277
1024 // here we declare the Foo trait with a bar method
1025 trait Foo {
1026     fn bar(&self);
1027 }
1028
1029 // we now declare a function which takes an object implementing the Foo trait
1030 fn some_func<T: Foo>(foo: T) {
1031     foo.bar();
1032 }
1033
1034 fn main() {
1035     // we now call the method with the i32 type, which doesn't implement
1036     // the Foo trait
1037     some_func(5i32); // error: the trait bound `i32 : Foo` is not satisfied
1038 }
1039 ```
1040
1041 In order to fix this error, verify that the type you're using does implement
1042 the trait. Example:
1043
1044 ```
1045 trait Foo {
1046     fn bar(&self);
1047 }
1048
1049 fn some_func<T: Foo>(foo: T) {
1050     foo.bar(); // we can now use this method since i32 implements the
1051                // Foo trait
1052 }
1053
1054 // we implement the trait on the i32 type
1055 impl Foo for i32 {
1056     fn bar(&self) {}
1057 }
1058
1059 fn main() {
1060     some_func(5i32); // ok!
1061 }
1062 ```
1063
1064 Or in a generic context, an erroneous code example would look like:
1065
1066 ```compile_fail,E0277
1067 fn some_func<T>(foo: T) {
1068     println!("{:?}", foo); // error: the trait `core::fmt::Debug` is not
1069                            //        implemented for the type `T`
1070 }
1071
1072 fn main() {
1073     // We now call the method with the i32 type,
1074     // which *does* implement the Debug trait.
1075     some_func(5i32);
1076 }
1077 ```
1078
1079 Note that the error here is in the definition of the generic function: Although
1080 we only call it with a parameter that does implement `Debug`, the compiler
1081 still rejects the function: It must work with all possible input types. In
1082 order to make this example compile, we need to restrict the generic type we're
1083 accepting:
1084
1085 ```
1086 use std::fmt;
1087
1088 // Restrict the input type to types that implement Debug.
1089 fn some_func<T: fmt::Debug>(foo: T) {
1090     println!("{:?}", foo);
1091 }
1092
1093 fn main() {
1094     // Calling the method is still fine, as i32 implements Debug.
1095     some_func(5i32);
1096
1097     // This would fail to compile now:
1098     // struct WithoutDebug;
1099     // some_func(WithoutDebug);
1100 }
1101 ```
1102
1103 Rust only looks at the signature of the called function, as such it must
1104 already specify all requirements that will be used for every type parameter.
1105 "##,
1106
1107 E0281: r##"
1108 You tried to supply a type which doesn't implement some trait in a location
1109 which expected that trait. This error typically occurs when working with
1110 `Fn`-based types. Erroneous code example:
1111
1112 ```compile_fail,E0281
1113 fn foo<F: Fn(usize)>(x: F) { }
1114
1115 fn main() {
1116     // type mismatch: ... implements the trait `core::ops::Fn<(String,)>`,
1117     // but the trait `core::ops::Fn<(usize,)>` is required
1118     // [E0281]
1119     foo(|y: String| { });
1120 }
1121 ```
1122
1123 The issue in this case is that `foo` is defined as accepting a `Fn` with one
1124 argument of type `String`, but the closure we attempted to pass to it requires
1125 one arguments of type `usize`.
1126 "##,
1127
1128 E0282: r##"
1129 This error indicates that type inference did not result in one unique possible
1130 type, and extra information is required. In most cases this can be provided
1131 by adding a type annotation. Sometimes you need to specify a generic type
1132 parameter manually.
1133
1134 A common example is the `collect` method on `Iterator`. It has a generic type
1135 parameter with a `FromIterator` bound, which for a `char` iterator is
1136 implemented by `Vec` and `String` among others. Consider the following snippet
1137 that reverses the characters of a string:
1138
1139 ```compile_fail,E0282
1140 let x = "hello".chars().rev().collect();
1141 ```
1142
1143 In this case, the compiler cannot infer what the type of `x` should be:
1144 `Vec<char>` and `String` are both suitable candidates. To specify which type to
1145 use, you can use a type annotation on `x`:
1146
1147 ```
1148 let x: Vec<char> = "hello".chars().rev().collect();
1149 ```
1150
1151 It is not necessary to annotate the full type. Once the ambiguity is resolved,
1152 the compiler can infer the rest:
1153
1154 ```
1155 let x: Vec<_> = "hello".chars().rev().collect();
1156 ```
1157
1158 Another way to provide the compiler with enough information, is to specify the
1159 generic type parameter:
1160
1161 ```
1162 let x = "hello".chars().rev().collect::<Vec<char>>();
1163 ```
1164
1165 Again, you need not specify the full type if the compiler can infer it:
1166
1167 ```
1168 let x = "hello".chars().rev().collect::<Vec<_>>();
1169 ```
1170
1171 Apart from a method or function with a generic type parameter, this error can
1172 occur when a type parameter of a struct or trait cannot be inferred. In that
1173 case it is not always possible to use a type annotation, because all candidates
1174 have the same return type. For instance:
1175
1176 ```compile_fail,E0282
1177 struct Foo<T> {
1178     num: T,
1179 }
1180
1181 impl<T> Foo<T> {
1182     fn bar() -> i32 {
1183         0
1184     }
1185
1186     fn baz() {
1187         let number = Foo::bar();
1188     }
1189 }
1190 ```
1191
1192 This will fail because the compiler does not know which instance of `Foo` to
1193 call `bar` on. Change `Foo::bar()` to `Foo::<T>::bar()` to resolve the error.
1194 "##,
1195
1196 E0283: r##"
1197 This error occurs when the compiler doesn't have enough information
1198 to unambiguously choose an implementation.
1199
1200 For example:
1201
1202 ```compile_fail,E0283
1203 trait Generator {
1204     fn create() -> u32;
1205 }
1206
1207 struct Impl;
1208
1209 impl Generator for Impl {
1210     fn create() -> u32 { 1 }
1211 }
1212
1213 struct AnotherImpl;
1214
1215 impl Generator for AnotherImpl {
1216     fn create() -> u32 { 2 }
1217 }
1218
1219 fn main() {
1220     let cont: u32 = Generator::create();
1221     // error, impossible to choose one of Generator trait implementation
1222     // Impl or AnotherImpl? Maybe anything else?
1223 }
1224 ```
1225
1226 To resolve this error use the concrete type:
1227
1228 ```
1229 trait Generator {
1230     fn create() -> u32;
1231 }
1232
1233 struct AnotherImpl;
1234
1235 impl Generator for AnotherImpl {
1236     fn create() -> u32 { 2 }
1237 }
1238
1239 fn main() {
1240     let gen1 = AnotherImpl::create();
1241
1242     // if there are multiple methods with same name (different traits)
1243     let gen2 = <AnotherImpl as Generator>::create();
1244 }
1245 ```
1246 "##,
1247
1248 E0296: r##"
1249 This error indicates that the given recursion limit could not be parsed. Ensure
1250 that the value provided is a positive integer between quotes.
1251
1252 Erroneous code example:
1253
1254 ```compile_fail,E0296
1255 #![recursion_limit]
1256
1257 fn main() {}
1258 ```
1259
1260 And a working example:
1261
1262 ```
1263 #![recursion_limit="1000"]
1264
1265 fn main() {}
1266 ```
1267 "##,
1268
1269 E0308: r##"
1270 This error occurs when the compiler was unable to infer the concrete type of a
1271 variable. It can occur for several cases, the most common of which is a
1272 mismatch in the expected type that the compiler inferred for a variable's
1273 initializing expression, and the actual type explicitly assigned to the
1274 variable.
1275
1276 For example:
1277
1278 ```compile_fail,E0308
1279 let x: i32 = "I am not a number!";
1280 //     ~~~   ~~~~~~~~~~~~~~~~~~~~
1281 //      |             |
1282 //      |    initializing expression;
1283 //      |    compiler infers type `&str`
1284 //      |
1285 //    type `i32` assigned to variable `x`
1286 ```
1287 "##,
1288
1289 E0309: r##"
1290 Types in type definitions have lifetimes associated with them that represent
1291 how long the data stored within them is guaranteed to be live. This lifetime
1292 must be as long as the data needs to be alive, and missing the constraint that
1293 denotes this will cause this error.
1294
1295 ```compile_fail,E0309
1296 // This won't compile because T is not constrained, meaning the data
1297 // stored in it is not guaranteed to last as long as the reference
1298 struct Foo<'a, T> {
1299     foo: &'a T
1300 }
1301 ```
1302
1303 This will compile, because it has the constraint on the type parameter:
1304
1305 ```
1306 struct Foo<'a, T: 'a> {
1307     foo: &'a T
1308 }
1309 ```
1310
1311 To see why this is important, consider the case where `T` is itself a reference
1312 (e.g., `T = &str`). If we don't include the restriction that `T: 'a`, the
1313 following code would be perfectly legal:
1314
1315 ```compile_fail,E0309
1316 struct Foo<'a, T> {
1317     foo: &'a T
1318 }
1319
1320 fn main() {
1321     let v = "42".to_string();
1322     let f = Foo{foo: &v};
1323     drop(v);
1324     println!("{}", f.foo); // but we've already dropped v!
1325 }
1326 ```
1327 "##,
1328
1329 E0310: r##"
1330 Types in type definitions have lifetimes associated with them that represent
1331 how long the data stored within them is guaranteed to be live. This lifetime
1332 must be as long as the data needs to be alive, and missing the constraint that
1333 denotes this will cause this error.
1334
1335 ```compile_fail,E0310
1336 // This won't compile because T is not constrained to the static lifetime
1337 // the reference needs
1338 struct Foo<T> {
1339     foo: &'static T
1340 }
1341 ```
1342
1343 This will compile, because it has the constraint on the type parameter:
1344
1345 ```
1346 struct Foo<T: 'static> {
1347     foo: &'static T
1348 }
1349 ```
1350 "##,
1351
1352 E0312: r##"
1353 A lifetime of reference outlives lifetime of borrowed content.
1354
1355 Erroneous code example:
1356
1357 ```compile_fail,E0312
1358 fn make_child<'human, 'elve>(x: &mut &'human isize, y: &mut &'elve isize) {
1359     *x = *y;
1360     // error: lifetime of reference outlives lifetime of borrowed content
1361 }
1362 ```
1363
1364 The compiler cannot determine if the `human` lifetime will live long enough
1365 to keep up on the elve one. To solve this error, you have to give an
1366 explicit lifetime hierarchy:
1367
1368 ```
1369 fn make_child<'human, 'elve: 'human>(x: &mut &'human isize,
1370                                      y: &mut &'elve isize) {
1371     *x = *y; // ok!
1372 }
1373 ```
1374
1375 Or use the same lifetime for every variable:
1376
1377 ```
1378 fn make_child<'elve>(x: &mut &'elve isize, y: &mut &'elve isize) {
1379     *x = *y; // ok!
1380 }
1381 ```
1382 "##,
1383
1384 E0317: r##"
1385 This error occurs when an `if` expression without an `else` block is used in a
1386 context where a type other than `()` is expected, for example a `let`
1387 expression:
1388
1389 ```compile_fail,E0317
1390 fn main() {
1391     let x = 5;
1392     let a = if x == 5 { 1 };
1393 }
1394 ```
1395
1396 An `if` expression without an `else` block has the type `()`, so this is a type
1397 error. To resolve it, add an `else` block having the same type as the `if`
1398 block.
1399 "##,
1400
1401 E0391: r##"
1402 This error indicates that some types or traits depend on each other
1403 and therefore cannot be constructed.
1404
1405 The following example contains a circular dependency between two traits:
1406
1407 ```compile_fail,E0391
1408 trait FirstTrait : SecondTrait {
1409
1410 }
1411
1412 trait SecondTrait : FirstTrait {
1413
1414 }
1415 ```
1416 "##,
1417
1418 E0398: r##"
1419 In Rust 1.3, the default object lifetime bounds are expected to change, as
1420 described in [RFC 1156]. You are getting a warning because the compiler
1421 thinks it is possible that this change will cause a compilation error in your
1422 code. It is possible, though unlikely, that this is a false alarm.
1423
1424 The heart of the change is that where `&'a Box<SomeTrait>` used to default to
1425 `&'a Box<SomeTrait+'a>`, it now defaults to `&'a Box<SomeTrait+'static>` (here,
1426 `SomeTrait` is the name of some trait type). Note that the only types which are
1427 affected are references to boxes, like `&Box<SomeTrait>` or
1428 `&[Box<SomeTrait>]`. More common types like `&SomeTrait` or `Box<SomeTrait>`
1429 are unaffected.
1430
1431 To silence this warning, edit your code to use an explicit bound. Most of the
1432 time, this means that you will want to change the signature of a function that
1433 you are calling. For example, if the error is reported on a call like `foo(x)`,
1434 and `foo` is defined as follows:
1435
1436 ```ignore
1437 fn foo(arg: &Box<SomeTrait>) { ... }
1438 ```
1439
1440 You might change it to:
1441
1442 ```ignore
1443 fn foo<'a>(arg: &Box<SomeTrait+'a>) { ... }
1444 ```
1445
1446 This explicitly states that you expect the trait object `SomeTrait` to contain
1447 references (with a maximum lifetime of `'a`).
1448
1449 [RFC 1156]: https://github.com/rust-lang/rfcs/blob/master/text/1156-adjust-default-object-bounds.md
1450 "##,
1451
1452 E0452: r##"
1453 An invalid lint attribute has been given. Erroneous code example:
1454
1455 ```compile_fail,E0452
1456 #![allow(foo = "")] // error: malformed lint attribute
1457 ```
1458
1459 Lint attributes only accept a list of identifiers (where each identifier is a
1460 lint name). Ensure the attribute is of this form:
1461
1462 ```
1463 #![allow(foo)] // ok!
1464 // or:
1465 #![allow(foo, foo2)] // ok!
1466 ```
1467 "##,
1468
1469 E0453: r##"
1470 A lint check attribute was overruled by a `forbid` directive set as an
1471 attribute on an enclosing scope, or on the command line with the `-F` option.
1472
1473 Example of erroneous code:
1474
1475 ```compile_fail,E0453
1476 #![forbid(non_snake_case)]
1477
1478 #[allow(non_snake_case)]
1479 fn main() {
1480     let MyNumber = 2; // error: allow(non_snake_case) overruled by outer
1481                       //        forbid(non_snake_case)
1482 }
1483 ```
1484
1485 The `forbid` lint setting, like `deny`, turns the corresponding compiler
1486 warning into a hard error. Unlike `deny`, `forbid` prevents itself from being
1487 overridden by inner attributes.
1488
1489 If you're sure you want to override the lint check, you can change `forbid` to
1490 `deny` (or use `-D` instead of `-F` if the `forbid` setting was given as a
1491 command-line option) to allow the inner lint check attribute:
1492
1493 ```
1494 #![deny(non_snake_case)]
1495
1496 #[allow(non_snake_case)]
1497 fn main() {
1498     let MyNumber = 2; // ok!
1499 }
1500 ```
1501
1502 Otherwise, edit the code to pass the lint check, and remove the overruled
1503 attribute:
1504
1505 ```
1506 #![forbid(non_snake_case)]
1507
1508 fn main() {
1509     let my_number = 2;
1510 }
1511 ```
1512 "##,
1513
1514 E0478: r##"
1515 A lifetime bound was not satisfied.
1516
1517 Erroneous code example:
1518
1519 ```compile_fail,E0478
1520 // Check that the explicit lifetime bound (`'SnowWhite`, in this example) must
1521 // outlive all the superbounds from the trait (`'kiss`, in this example).
1522
1523 trait Wedding<'t>: 't { }
1524
1525 struct Prince<'kiss, 'SnowWhite> {
1526     child: Box<Wedding<'kiss> + 'SnowWhite>,
1527     // error: lifetime bound not satisfied
1528 }
1529 ```
1530
1531 In this example, the `'SnowWhite` lifetime is supposed to outlive the `'kiss`
1532 lifetime but the declaration of the `Prince` struct doesn't enforce it. To fix
1533 this issue, you need to specify it:
1534
1535 ```
1536 trait Wedding<'t>: 't { }
1537
1538 struct Prince<'kiss, 'SnowWhite: 'kiss> { // You say here that 'kiss must live
1539                                           // longer than 'SnowWhite.
1540     child: Box<Wedding<'kiss> + 'SnowWhite>, // And now it's all good!
1541 }
1542 ```
1543 "##,
1544
1545 E0491: r##"
1546 A reference has a longer lifetime than the data it references.
1547
1548 Erroneous code example:
1549
1550 ```compile_fail,E0491
1551 // struct containing a reference requires a lifetime parameter,
1552 // because the data the reference points to must outlive the struct (see E0106)
1553 struct Struct<'a> {
1554     ref_i32: &'a i32,
1555 }
1556
1557 // However, a nested struct like this, the signature itself does not tell
1558 // whether 'a outlives 'b or the other way around.
1559 // So it could be possible that 'b of reference outlives 'a of the data.
1560 struct Nested<'a, 'b> {
1561     ref_struct: &'b Struct<'a>, // compile error E0491
1562 }
1563 ```
1564
1565 To fix this issue, you can specify a bound to the lifetime like below:
1566
1567 ```
1568 struct Struct<'a> {
1569     ref_i32: &'a i32,
1570 }
1571
1572 // 'a: 'b means 'a outlives 'b
1573 struct Nested<'a: 'b, 'b> {
1574     ref_struct: &'b Struct<'a>,
1575 }
1576 ```
1577 "##,
1578
1579 E0496: r##"
1580 A lifetime name is shadowing another lifetime name. Erroneous code example:
1581
1582 ```compile_fail,E0496
1583 struct Foo<'a> {
1584     a: &'a i32,
1585 }
1586
1587 impl<'a> Foo<'a> {
1588     fn f<'a>(x: &'a i32) { // error: lifetime name `'a` shadows a lifetime
1589                            //        name that is already in scope
1590     }
1591 }
1592 ```
1593
1594 Please change the name of one of the lifetimes to remove this error. Example:
1595
1596 ```
1597 struct Foo<'a> {
1598     a: &'a i32,
1599 }
1600
1601 impl<'a> Foo<'a> {
1602     fn f<'b>(x: &'b i32) { // ok!
1603     }
1604 }
1605
1606 fn main() {
1607 }
1608 ```
1609 "##,
1610
1611 E0497: r##"
1612 A stability attribute was used outside of the standard library. Erroneous code
1613 example:
1614
1615 ```compile_fail
1616 #[stable] // error: stability attributes may not be used outside of the
1617           //        standard library
1618 fn foo() {}
1619 ```
1620
1621 It is not possible to use stability attributes outside of the standard library.
1622 Also, for now, it is not possible to write deprecation messages either.
1623 "##,
1624
1625 E0512: r##"
1626 Transmute with two differently sized types was attempted. Erroneous code
1627 example:
1628
1629 ```compile_fail,E0512
1630 fn takes_u8(_: u8) {}
1631
1632 fn main() {
1633     unsafe { takes_u8(::std::mem::transmute(0u16)); }
1634     // error: transmute called with differently sized types
1635 }
1636 ```
1637
1638 Please use types with same size or use the expected type directly. Example:
1639
1640 ```
1641 fn takes_u8(_: u8) {}
1642
1643 fn main() {
1644     unsafe { takes_u8(::std::mem::transmute(0i8)); } // ok!
1645     // or:
1646     unsafe { takes_u8(0u8); } // ok!
1647 }
1648 ```
1649 "##,
1650
1651 E0517: r##"
1652 This error indicates that a `#[repr(..)]` attribute was placed on an
1653 unsupported item.
1654
1655 Examples of erroneous code:
1656
1657 ```compile_fail,E0517
1658 #[repr(C)]
1659 type Foo = u8;
1660
1661 #[repr(packed)]
1662 enum Foo {Bar, Baz}
1663
1664 #[repr(u8)]
1665 struct Foo {bar: bool, baz: bool}
1666
1667 #[repr(C)]
1668 impl Foo {
1669     // ...
1670 }
1671 ```
1672
1673 * The `#[repr(C)]` attribute can only be placed on structs and enums.
1674 * The `#[repr(packed)]` and `#[repr(simd)]` attributes only work on structs.
1675 * The `#[repr(u8)]`, `#[repr(i16)]`, etc attributes only work on enums.
1676
1677 These attributes do not work on typedefs, since typedefs are just aliases.
1678
1679 Representations like `#[repr(u8)]`, `#[repr(i64)]` are for selecting the
1680 discriminant size for C-like enums (when there is no associated data, e.g.
1681 `enum Color {Red, Blue, Green}`), effectively setting the size of the enum to
1682 the size of the provided type. Such an enum can be cast to a value of the same
1683 type as well. In short, `#[repr(u8)]` makes the enum behave like an integer
1684 with a constrained set of allowed values.
1685
1686 Only C-like enums can be cast to numerical primitives, so this attribute will
1687 not apply to structs.
1688
1689 `#[repr(packed)]` reduces padding to make the struct size smaller. The
1690 representation of enums isn't strictly defined in Rust, and this attribute
1691 won't work on enums.
1692
1693 `#[repr(simd)]` will give a struct consisting of a homogenous series of machine
1694 types (i.e. `u8`, `i32`, etc) a representation that permits vectorization via
1695 SIMD. This doesn't make much sense for enums since they don't consist of a
1696 single list of data.
1697 "##,
1698
1699 E0518: r##"
1700 This error indicates that an `#[inline(..)]` attribute was incorrectly placed
1701 on something other than a function or method.
1702
1703 Examples of erroneous code:
1704
1705 ```compile_fail,E0518
1706 #[inline(always)]
1707 struct Foo;
1708
1709 #[inline(never)]
1710 impl Foo {
1711     // ...
1712 }
1713 ```
1714
1715 `#[inline]` hints the compiler whether or not to attempt to inline a method or
1716 function. By default, the compiler does a pretty good job of figuring this out
1717 itself, but if you feel the need for annotations, `#[inline(always)]` and
1718 `#[inline(never)]` can override or force the compiler's decision.
1719
1720 If you wish to apply this attribute to all methods in an impl, manually annotate
1721 each method; it is not possible to annotate the entire impl with an `#[inline]`
1722 attribute.
1723 "##,
1724
1725 E0522: r##"
1726 The lang attribute is intended for marking special items that are built-in to
1727 Rust itself. This includes special traits (like `Copy` and `Sized`) that affect
1728 how the compiler behaves, as well as special functions that may be automatically
1729 invoked (such as the handler for out-of-bounds accesses when indexing a slice).
1730 Erroneous code example:
1731
1732 ```compile_fail,E0522
1733 #![feature(lang_items)]
1734
1735 #[lang = "cookie"]
1736 fn cookie() -> ! { // error: definition of an unknown language item: `cookie`
1737     loop {}
1738 }
1739 ```
1740 "##,
1741
1742 E0525: r##"
1743 A closure was used but didn't implement the expected trait.
1744
1745 Erroneous code example:
1746
1747 ```compile_fail,E0525
1748 struct X;
1749
1750 fn foo<T>(_: T) {}
1751 fn bar<T: Fn(u32)>(_: T) {}
1752
1753 fn main() {
1754     let x = X;
1755     let closure = |_| foo(x); // error: expected a closure that implements
1756                               //        the `Fn` trait, but this closure only
1757                               //        implements `FnOnce`
1758     bar(closure);
1759 }
1760 ```
1761
1762 In the example above, `closure` is an `FnOnce` closure whereas the `bar`
1763 function expected an `Fn` closure. In this case, it's simple to fix the issue,
1764 you just have to implement `Copy` and `Clone` traits on `struct X` and it'll
1765 be ok:
1766
1767 ```
1768 #[derive(Clone, Copy)] // We implement `Clone` and `Copy` traits.
1769 struct X;
1770
1771 fn foo<T>(_: T) {}
1772 fn bar<T: Fn(u32)>(_: T) {}
1773
1774 fn main() {
1775     let x = X;
1776     let closure = |_| foo(x);
1777     bar(closure); // ok!
1778 }
1779 ```
1780
1781 To understand better how closures work in Rust, read:
1782 https://doc.rust-lang.org/book/closures.html
1783 "##,
1784
1785 E0580: r##"
1786 The `main` function was incorrectly declared.
1787
1788 Erroneous code example:
1789
1790 ```compile_fail,E0580
1791 fn main() -> i32 { // error: main function has wrong type
1792     0
1793 }
1794 ```
1795
1796 The `main` function prototype should never take arguments or return type.
1797 Example:
1798
1799 ```
1800 fn main() {
1801     // your code
1802 }
1803 ```
1804
1805 If you want to get command-line arguments, use `std::env::args`. To exit with a
1806 specified exit code, use `std::process::exit`.
1807 "##,
1808
1809 E0591: r##"
1810 Per [RFC 401][rfc401], if you have a function declaration `foo`:
1811
1812 ```rust,ignore
1813 // For the purposes of this explanation, all of these
1814 // different kinds of `fn` declarations are equivalent:
1815 fn foo(x: i32) { ... }
1816 extern "C" fn foo(x: i32);
1817 impl i32 { fn foo(x: self) { ... } }
1818 ```
1819
1820 the type of `foo` is **not** `fn(i32)`, as one might expect.
1821 Rather, it is a unique, zero-sized marker type written here as `typeof(foo)`.
1822 However, `typeof(foo)` can be _coerced_ to a function pointer `fn(i32)`,
1823 so you rarely notice this:
1824
1825 ```rust,ignore
1826 let x: fn(i32) = foo; // OK, coerces
1827 ```
1828
1829 The reason that this matter is that the type `fn(i32)` is not specific to
1830 any particular function: it's a function _pointer_. So calling `x()` results
1831 in a virtual call, whereas `foo()` is statically dispatched, because the type
1832 of `foo` tells us precisely what function is being called.
1833
1834 As noted above, coercions mean that most code doesn't have to be
1835 concerned with this distinction. However, you can tell the difference
1836 when using **transmute** to convert a fn item into a fn pointer.
1837
1838 This is sometimes done as part of an FFI:
1839
1840 ```rust,ignore
1841 extern "C" fn foo(userdata: Box<i32>) {
1842    ...
1843 }
1844
1845 let f: extern "C" fn(*mut i32) = transmute(foo);
1846 callback(f);
1847
1848 ```
1849
1850 Here, transmute is being used to convert the types of the fn arguments.
1851 This pattern is incorrect because, because the type of `foo` is a function
1852 **item** (`typeof(foo)`), which is zero-sized, and the target type (`fn()`)
1853 is a function pointer, which is not zero-sized.
1854 This pattern should be rewritten. There are a few possible ways to do this:
1855
1856 - change the original fn declaration to match the expected signature,
1857   and do the cast in the fn body (the prefered option)
1858 - cast the fn item fo a fn pointer before calling transmute, as shown here:
1859   - `let f: extern "C" fn(*mut i32) = transmute(foo as extern "C" fn(_))`
1860   - `let f: extern "C" fn(*mut i32) = transmute(foo as usize) /* works too */`
1861
1862 The same applies to transmutes to `*mut fn()`, which were observedin practice.
1863 Note though that use of this type is generally incorrect.
1864 The intention is typically to describe a function pointer, but just `fn()`
1865 alone suffices for that. `*mut fn()` is a pointer to a fn pointer.
1866 (Since these values are typically just passed to C code, however, this rarely
1867 makes a difference in practice.)
1868
1869 [rfc401]: https://github.com/rust-lang/rfcs/blob/master/text/0401-coercions.md
1870 "##,
1871
1872 E0593: r##"
1873 You tried to supply an `Fn`-based type with an incorrect number of arguments
1874 than what was expected.
1875
1876 Erroneous code example:
1877
1878 ```compile_fail,E0593
1879 fn foo<F: Fn()>(x: F) { }
1880
1881 fn main() {
1882     // [E0593] closure takes 1 argument but 0 arguments are required
1883     foo(|y| { });
1884 }
1885 ```
1886 "##,
1887
1888 E0601: r##"
1889 No `main` function was found in a binary crate. To fix this error, just add a
1890 `main` function. For example:
1891
1892 ```
1893 fn main() {
1894     // Your program will start here.
1895     println!("Hello world!");
1896 }
1897 ```
1898
1899 If you don't know the basics of Rust, you can go look to the Rust Book to get
1900 started: https://doc.rust-lang.org/book/
1901 "##,
1902
1903 }
1904
1905
1906 register_diagnostics! {
1907 //  E0006 // merged with E0005
1908 //  E0101, // replaced with E0282
1909 //  E0102, // replaced with E0282
1910 //  E0134,
1911 //  E0135,
1912     E0278, // requirement is not satisfied
1913     E0279, // requirement is not satisfied
1914     E0280, // requirement is not satisfied
1915     E0284, // cannot resolve type
1916 //  E0285, // overflow evaluation builtin bounds
1917 //  E0300, // unexpanded macro
1918 //  E0304, // expected signed integer constant
1919 //  E0305, // expected constant
1920     E0311, // thing may not live long enough
1921     E0313, // lifetime of borrowed pointer outlives lifetime of captured variable
1922     E0314, // closure outlives stack frame
1923     E0315, // cannot invoke closure outside of its lifetime
1924     E0316, // nested quantification of lifetimes
1925     E0320, // recursive overflow during dropck
1926     E0473, // dereference of reference outside its lifetime
1927     E0474, // captured variable `..` does not outlive the enclosing closure
1928     E0475, // index of slice outside its lifetime
1929     E0476, // lifetime of the source pointer does not outlive lifetime bound...
1930     E0477, // the type `..` does not fulfill the required lifetime...
1931     E0479, // the type `..` (provided as the value of a type parameter) is...
1932     E0480, // lifetime of method receiver does not outlive the method call
1933     E0481, // lifetime of function argument does not outlive the function call
1934     E0482, // lifetime of return value does not outlive the function call
1935     E0483, // lifetime of operand does not outlive the operation
1936     E0484, // reference is not valid at the time of borrow
1937     E0485, // automatically reference is not valid at the time of borrow
1938     E0486, // type of expression contains references that are not valid during...
1939     E0487, // unsafe use of destructor: destructor might be called while...
1940     E0488, // lifetime of variable does not enclose its declaration
1941     E0489, // type/lifetime parameter not in scope here
1942     E0490, // a value of type `..` is borrowed for too long
1943     E0495, // cannot infer an appropriate lifetime due to conflicting requirements
1944     E0566, // conflicting representation hints
1945     E0587, // conflicting packed and align representation hints
1946 }