]> git.lizzy.rs Git - rust.git/blob - src/librustc/diagnostics.rs
Rollup merge of #41456 - jessicah:haiku-support, r=alexcrichton
[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 E0133: r##"
413 Unsafe code was used outside of an unsafe function or block.
414
415 Erroneous code example:
416
417 ```compile_fail,E0133
418 unsafe fn f() { return; } // This is the unsafe code
419
420 fn main() {
421     f(); // error: call to unsafe function requires unsafe function or block
422 }
423 ```
424
425 Using unsafe functionality is potentially dangerous and disallowed by safety
426 checks. Examples:
427
428 * Dereferencing raw pointers
429 * Calling functions via FFI
430 * Calling functions marked unsafe
431
432 These safety checks can be relaxed for a section of the code by wrapping the
433 unsafe instructions with an `unsafe` block. For instance:
434
435 ```
436 unsafe fn f() { return; }
437
438 fn main() {
439     unsafe { f(); } // ok!
440 }
441 ```
442
443 See also https://doc.rust-lang.org/book/unsafe.html
444 "##,
445
446 // This shouldn't really ever trigger since the repeated value error comes first
447 E0136: r##"
448 A binary can only have one entry point, and by default that entry point is the
449 function `main()`. If there are multiple such functions, please rename one.
450 "##,
451
452 E0137: r##"
453 More than one function was declared with the `#[main]` attribute.
454
455 Erroneous code example:
456
457 ```compile_fail,E0137
458 #![feature(main)]
459
460 #[main]
461 fn foo() {}
462
463 #[main]
464 fn f() {} // error: multiple functions with a #[main] attribute
465 ```
466
467 This error indicates that the compiler found multiple functions with the
468 `#[main]` attribute. This is an error because there must be a unique entry
469 point into a Rust program. Example:
470
471 ```
472 #![feature(main)]
473
474 #[main]
475 fn f() {} // ok!
476 ```
477 "##,
478
479 E0138: r##"
480 More than one function was declared with the `#[start]` attribute.
481
482 Erroneous code example:
483
484 ```compile_fail,E0138
485 #![feature(start)]
486
487 #[start]
488 fn foo(argc: isize, argv: *const *const u8) -> isize {}
489
490 #[start]
491 fn f(argc: isize, argv: *const *const u8) -> isize {}
492 // error: multiple 'start' functions
493 ```
494
495 This error indicates that the compiler found multiple functions with the
496 `#[start]` attribute. This is an error because there must be a unique entry
497 point into a Rust program. Example:
498
499 ```
500 #![feature(start)]
501
502 #[start]
503 fn foo(argc: isize, argv: *const *const u8) -> isize { 0 } // ok!
504 ```
505 "##,
506
507 // isn't thrown anymore
508 E0139: r##"
509 There are various restrictions on transmuting between types in Rust; for example
510 types being transmuted must have the same size. To apply all these restrictions,
511 the compiler must know the exact types that may be transmuted. When type
512 parameters are involved, this cannot always be done.
513
514 So, for example, the following is not allowed:
515
516 ```
517 use std::mem::transmute;
518
519 struct Foo<T>(Vec<T>);
520
521 fn foo<T>(x: Vec<T>) {
522     // we are transmuting between Vec<T> and Foo<F> here
523     let y: Foo<T> = unsafe { transmute(x) };
524     // do something with y
525 }
526 ```
527
528 In this specific case there's a good chance that the transmute is harmless (but
529 this is not guaranteed by Rust). However, when alignment and enum optimizations
530 come into the picture, it's quite likely that the sizes may or may not match
531 with different type parameter substitutions. It's not possible to check this for
532 _all_ possible types, so `transmute()` simply only accepts types without any
533 unsubstituted type parameters.
534
535 If you need this, there's a good chance you're doing something wrong. Keep in
536 mind that Rust doesn't guarantee much about the layout of different structs
537 (even two structs with identical declarations may have different layouts). If
538 there is a solution that avoids the transmute entirely, try it instead.
539
540 If it's possible, hand-monomorphize the code by writing the function for each
541 possible type substitution. It's possible to use traits to do this cleanly,
542 for example:
543
544 ```ignore
545 struct Foo<T>(Vec<T>);
546
547 trait MyTransmutableType {
548     fn transmute(Vec<Self>) -> Foo<Self>;
549 }
550
551 impl MyTransmutableType for u8 {
552     fn transmute(x: Foo<u8>) -> Vec<u8> {
553         transmute(x)
554     }
555 }
556
557 impl MyTransmutableType for String {
558     fn transmute(x: Foo<String>) -> Vec<String> {
559         transmute(x)
560     }
561 }
562
563 // ... more impls for the types you intend to transmute
564
565 fn foo<T: MyTransmutableType>(x: Vec<T>) {
566     let y: Foo<T> = <T as MyTransmutableType>::transmute(x);
567     // do something with y
568 }
569 ```
570
571 Each impl will be checked for a size match in the transmute as usual, and since
572 there are no unbound type parameters involved, this should compile unless there
573 is a size mismatch in one of the impls.
574
575 It is also possible to manually transmute:
576
577 ```ignore
578 ptr::read(&v as *const _ as *const SomeType) // `v` transmuted to `SomeType`
579 ```
580
581 Note that this does not move `v` (unlike `transmute`), and may need a
582 call to `mem::forget(v)` in case you want to avoid destructors being called.
583 "##,
584
585 E0152: r##"
586 A lang item was redefined.
587
588 Erroneous code example:
589
590 ```compile_fail,E0152
591 #![feature(lang_items)]
592
593 #[lang = "panic_fmt"]
594 struct Foo; // error: duplicate lang item found: `panic_fmt`
595 ```
596
597 Lang items are already implemented in the standard library. Unless you are
598 writing a free-standing application (e.g. a kernel), you do not need to provide
599 them yourself.
600
601 You can build a free-standing crate by adding `#![no_std]` to the crate
602 attributes:
603
604 ```ignore
605 #![no_std]
606 ```
607
608 See also https://doc.rust-lang.org/book/no-stdlib.html
609 "##,
610
611 E0261: r##"
612 When using a lifetime like `'a` in a type, it must be declared before being
613 used.
614
615 These two examples illustrate the problem:
616
617 ```compile_fail,E0261
618 // error, use of undeclared lifetime name `'a`
619 fn foo(x: &'a str) { }
620
621 struct Foo {
622     // error, use of undeclared lifetime name `'a`
623     x: &'a str,
624 }
625 ```
626
627 These can be fixed by declaring lifetime parameters:
628
629 ```
630 fn foo<'a>(x: &'a str) {}
631
632 struct Foo<'a> {
633     x: &'a str,
634 }
635 ```
636 "##,
637
638 E0262: r##"
639 Declaring certain lifetime names in parameters is disallowed. For example,
640 because the `'static` lifetime is a special built-in lifetime name denoting
641 the lifetime of the entire program, this is an error:
642
643 ```compile_fail,E0262
644 // error, invalid lifetime parameter name `'static`
645 fn foo<'static>(x: &'static str) { }
646 ```
647 "##,
648
649 E0263: r##"
650 A lifetime name cannot be declared more than once in the same scope. For
651 example:
652
653 ```compile_fail,E0263
654 // error, lifetime name `'a` declared twice in the same scope
655 fn foo<'a, 'b, 'a>(x: &'a str, y: &'b str) { }
656 ```
657 "##,
658
659 E0264: r##"
660 An unknown external lang item was used. Erroneous code example:
661
662 ```compile_fail,E0264
663 #![feature(lang_items)]
664
665 extern "C" {
666     #[lang = "cake"] // error: unknown external lang item: `cake`
667     fn cake();
668 }
669 ```
670
671 A list of available external lang items is available in
672 `src/librustc/middle/weak_lang_items.rs`. Example:
673
674 ```
675 #![feature(lang_items)]
676
677 extern "C" {
678     #[lang = "panic_fmt"] // ok!
679     fn cake();
680 }
681 ```
682 "##,
683
684 E0271: r##"
685 This is because of a type mismatch between the associated type of some
686 trait (e.g. `T::Bar`, where `T` implements `trait Quux { type Bar; }`)
687 and another type `U` that is required to be equal to `T::Bar`, but is not.
688 Examples follow.
689
690 Here is a basic example:
691
692 ```compile_fail,E0271
693 trait Trait { type AssociatedType; }
694
695 fn foo<T>(t: T) where T: Trait<AssociatedType=u32> {
696     println!("in foo");
697 }
698
699 impl Trait for i8 { type AssociatedType = &'static str; }
700
701 foo(3_i8);
702 ```
703
704 Here is that same example again, with some explanatory comments:
705
706 ```ignore
707 trait Trait { type AssociatedType; }
708
709 fn foo<T>(t: T) where T: Trait<AssociatedType=u32> {
710 //                    ~~~~~~~~ ~~~~~~~~~~~~~~~~~~
711 //                        |            |
712 //         This says `foo` can         |
713 //           only be used with         |
714 //              some type that         |
715 //         implements `Trait`.         |
716 //                                     |
717 //                             This says not only must
718 //                             `T` be an impl of `Trait`
719 //                             but also that the impl
720 //                             must assign the type `u32`
721 //                             to the associated type.
722     println!("in foo");
723 }
724
725 impl Trait for i8 { type AssociatedType = &'static str; }
726 ~~~~~~~~~~~~~~~~~   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
727 //      |                             |
728 // `i8` does have                     |
729 // implementation                     |
730 // of `Trait`...                      |
731 //                     ... but it is an implementation
732 //                     that assigns `&'static str` to
733 //                     the associated type.
734
735 foo(3_i8);
736 // Here, we invoke `foo` with an `i8`, which does not satisfy
737 // the constraint `<i8 as Trait>::AssociatedType=u32`, and
738 // therefore the type-checker complains with this error code.
739 ```
740
741 Here is a more subtle instance of the same problem, that can
742 arise with for-loops in Rust:
743
744 ```compile_fail
745 let vs: Vec<i32> = vec![1, 2, 3, 4];
746 for v in &vs {
747     match v {
748         1 => {},
749         _ => {},
750     }
751 }
752 ```
753
754 The above fails because of an analogous type mismatch,
755 though may be harder to see. Again, here are some
756 explanatory comments for the same example:
757
758 ```ignore
759 {
760     let vs = vec![1, 2, 3, 4];
761
762     // `for`-loops use a protocol based on the `Iterator`
763     // trait. Each item yielded in a `for` loop has the
764     // type `Iterator::Item` -- that is, `Item` is the
765     // associated type of the concrete iterator impl.
766     for v in &vs {
767 //      ~    ~~~
768 //      |     |
769 //      |    We borrow `vs`, iterating over a sequence of
770 //      |    *references* of type `&Elem` (where `Elem` is
771 //      |    vector's element type). Thus, the associated
772 //      |    type `Item` must be a reference `&`-type ...
773 //      |
774 //  ... and `v` has the type `Iterator::Item`, as dictated by
775 //  the `for`-loop protocol ...
776
777         match v {
778             1 => {}
779 //          ~
780 //          |
781 // ... but *here*, `v` is forced to have some integral type;
782 // only types like `u8`,`i8`,`u16`,`i16`, et cetera can
783 // match the pattern `1` ...
784
785             _ => {}
786         }
787
788 // ... therefore, the compiler complains, because it sees
789 // an attempt to solve the equations
790 // `some integral-type` = type-of-`v`
791 //                      = `Iterator::Item`
792 //                      = `&Elem` (i.e. `some reference type`)
793 //
794 // which cannot possibly all be true.
795
796     }
797 }
798 ```
799
800 To avoid those issues, you have to make the types match correctly.
801 So we can fix the previous examples like this:
802
803 ```
804 // Basic Example:
805 trait Trait { type AssociatedType; }
806
807 fn foo<T>(t: T) where T: Trait<AssociatedType = &'static str> {
808     println!("in foo");
809 }
810
811 impl Trait for i8 { type AssociatedType = &'static str; }
812
813 foo(3_i8);
814
815 // For-Loop Example:
816 let vs = vec![1, 2, 3, 4];
817 for v in &vs {
818     match v {
819         &1 => {}
820         _ => {}
821     }
822 }
823 ```
824 "##,
825
826 E0272: r##"
827 The `#[rustc_on_unimplemented]` attribute lets you specify a custom error
828 message for when a particular trait isn't implemented on a type placed in a
829 position that needs that trait. For example, when the following code is
830 compiled:
831
832 ```compile_fail
833 #![feature(on_unimplemented)]
834
835 fn foo<T: Index<u8>>(x: T){}
836
837 #[rustc_on_unimplemented = "the type `{Self}` cannot be indexed by `{Idx}`"]
838 trait Index<Idx> { /* ... */ }
839
840 foo(true); // `bool` does not implement `Index<u8>`
841 ```
842
843 There will be an error about `bool` not implementing `Index<u8>`, followed by a
844 note saying "the type `bool` cannot be indexed by `u8`".
845
846 As you can see, you can specify type parameters in curly braces for
847 substitution with the actual types (using the regular format string syntax) in
848 a given situation. Furthermore, `{Self}` will substitute to the type (in this
849 case, `bool`) that we tried to use.
850
851 This error appears when the curly braces contain an identifier which doesn't
852 match with any of the type parameters or the string `Self`. This might happen
853 if you misspelled a type parameter, or if you intended to use literal curly
854 braces. If it is the latter, escape the curly braces with a second curly brace
855 of the same type; e.g. a literal `{` is `{{`.
856 "##,
857
858 E0273: r##"
859 The `#[rustc_on_unimplemented]` attribute lets you specify a custom error
860 message for when a particular trait isn't implemented on a type placed in a
861 position that needs that trait. For example, when the following code is
862 compiled:
863
864 ```compile_fail
865 #![feature(on_unimplemented)]
866
867 fn foo<T: Index<u8>>(x: T){}
868
869 #[rustc_on_unimplemented = "the type `{Self}` cannot be indexed by `{Idx}`"]
870 trait Index<Idx> { /* ... */ }
871
872 foo(true); // `bool` does not implement `Index<u8>`
873 ```
874
875 there will be an error about `bool` not implementing `Index<u8>`, followed by a
876 note saying "the type `bool` cannot be indexed by `u8`".
877
878 As you can see, you can specify type parameters in curly braces for
879 substitution with the actual types (using the regular format string syntax) in
880 a given situation. Furthermore, `{Self}` will substitute to the type (in this
881 case, `bool`) that we tried to use.
882
883 This error appears when the curly braces do not contain an identifier. Please
884 add one of the same name as a type parameter. If you intended to use literal
885 braces, use `{{` and `}}` to escape them.
886 "##,
887
888 E0274: r##"
889 The `#[rustc_on_unimplemented]` attribute lets you specify a custom error
890 message for when a particular trait isn't implemented on a type placed in a
891 position that needs that trait. For example, when the following code is
892 compiled:
893
894 ```compile_fail
895 #![feature(on_unimplemented)]
896
897 fn foo<T: Index<u8>>(x: T){}
898
899 #[rustc_on_unimplemented = "the type `{Self}` cannot be indexed by `{Idx}`"]
900 trait Index<Idx> { /* ... */ }
901
902 foo(true); // `bool` does not implement `Index<u8>`
903 ```
904
905 there will be an error about `bool` not implementing `Index<u8>`, followed by a
906 note saying "the type `bool` cannot be indexed by `u8`".
907
908 For this to work, some note must be specified. An empty attribute will not do
909 anything, please remove the attribute or add some helpful note for users of the
910 trait.
911 "##,
912
913 E0275: r##"
914 This error occurs when there was a recursive trait requirement that overflowed
915 before it could be evaluated. Often this means that there is unbounded
916 recursion in resolving some type bounds.
917
918 For example, in the following code:
919
920 ```compile_fail,E0275
921 trait Foo {}
922
923 struct Bar<T>(T);
924
925 impl<T> Foo for T where Bar<T>: Foo {}
926 ```
927
928 To determine if a `T` is `Foo`, we need to check if `Bar<T>` is `Foo`. However,
929 to do this check, we need to determine that `Bar<Bar<T>>` is `Foo`. To
930 determine this, we check if `Bar<Bar<Bar<T>>>` is `Foo`, and so on. This is
931 clearly a recursive requirement that can't be resolved directly.
932
933 Consider changing your trait bounds so that they're less self-referential.
934 "##,
935
936 E0276: r##"
937 This error occurs when a bound in an implementation of a trait does not match
938 the bounds specified in the original trait. For example:
939
940 ```compile_fail,E0276
941 trait Foo {
942     fn foo<T>(x: T);
943 }
944
945 impl Foo for bool {
946     fn foo<T>(x: T) where T: Copy {}
947 }
948 ```
949
950 Here, all types implementing `Foo` must have a method `foo<T>(x: T)` which can
951 take any type `T`. However, in the `impl` for `bool`, we have added an extra
952 bound that `T` is `Copy`, which isn't compatible with the original trait.
953
954 Consider removing the bound from the method or adding the bound to the original
955 method definition in the trait.
956 "##,
957
958 E0277: r##"
959 You tried to use a type which doesn't implement some trait in a place which
960 expected that trait. Erroneous code example:
961
962 ```compile_fail,E0277
963 // here we declare the Foo trait with a bar method
964 trait Foo {
965     fn bar(&self);
966 }
967
968 // we now declare a function which takes an object implementing the Foo trait
969 fn some_func<T: Foo>(foo: T) {
970     foo.bar();
971 }
972
973 fn main() {
974     // we now call the method with the i32 type, which doesn't implement
975     // the Foo trait
976     some_func(5i32); // error: the trait bound `i32 : Foo` is not satisfied
977 }
978 ```
979
980 In order to fix this error, verify that the type you're using does implement
981 the trait. Example:
982
983 ```
984 trait Foo {
985     fn bar(&self);
986 }
987
988 fn some_func<T: Foo>(foo: T) {
989     foo.bar(); // we can now use this method since i32 implements the
990                // Foo trait
991 }
992
993 // we implement the trait on the i32 type
994 impl Foo for i32 {
995     fn bar(&self) {}
996 }
997
998 fn main() {
999     some_func(5i32); // ok!
1000 }
1001 ```
1002
1003 Or in a generic context, an erroneous code example would look like:
1004
1005 ```compile_fail,E0277
1006 fn some_func<T>(foo: T) {
1007     println!("{:?}", foo); // error: the trait `core::fmt::Debug` is not
1008                            //        implemented for the type `T`
1009 }
1010
1011 fn main() {
1012     // We now call the method with the i32 type,
1013     // which *does* implement the Debug trait.
1014     some_func(5i32);
1015 }
1016 ```
1017
1018 Note that the error here is in the definition of the generic function: Although
1019 we only call it with a parameter that does implement `Debug`, the compiler
1020 still rejects the function: It must work with all possible input types. In
1021 order to make this example compile, we need to restrict the generic type we're
1022 accepting:
1023
1024 ```
1025 use std::fmt;
1026
1027 // Restrict the input type to types that implement Debug.
1028 fn some_func<T: fmt::Debug>(foo: T) {
1029     println!("{:?}", foo);
1030 }
1031
1032 fn main() {
1033     // Calling the method is still fine, as i32 implements Debug.
1034     some_func(5i32);
1035
1036     // This would fail to compile now:
1037     // struct WithoutDebug;
1038     // some_func(WithoutDebug);
1039 }
1040 ```
1041
1042 Rust only looks at the signature of the called function, as such it must
1043 already specify all requirements that will be used for every type parameter.
1044 "##,
1045
1046 E0281: r##"
1047 You tried to supply a type which doesn't implement some trait in a location
1048 which expected that trait. This error typically occurs when working with
1049 `Fn`-based types. Erroneous code example:
1050
1051 ```compile_fail,E0281
1052 fn foo<F: Fn()>(x: F) { }
1053
1054 fn main() {
1055     // type mismatch: the type ... implements the trait `core::ops::Fn<(_,)>`,
1056     // but the trait `core::ops::Fn<()>` is required (expected (), found tuple
1057     // [E0281]
1058     foo(|y| { });
1059 }
1060 ```
1061
1062 The issue in this case is that `foo` is defined as accepting a `Fn` with no
1063 arguments, but the closure we attempted to pass to it requires one argument.
1064 "##,
1065
1066 E0282: r##"
1067 This error indicates that type inference did not result in one unique possible
1068 type, and extra information is required. In most cases this can be provided
1069 by adding a type annotation. Sometimes you need to specify a generic type
1070 parameter manually.
1071
1072 A common example is the `collect` method on `Iterator`. It has a generic type
1073 parameter with a `FromIterator` bound, which for a `char` iterator is
1074 implemented by `Vec` and `String` among others. Consider the following snippet
1075 that reverses the characters of a string:
1076
1077 ```compile_fail,E0282
1078 let x = "hello".chars().rev().collect();
1079 ```
1080
1081 In this case, the compiler cannot infer what the type of `x` should be:
1082 `Vec<char>` and `String` are both suitable candidates. To specify which type to
1083 use, you can use a type annotation on `x`:
1084
1085 ```
1086 let x: Vec<char> = "hello".chars().rev().collect();
1087 ```
1088
1089 It is not necessary to annotate the full type. Once the ambiguity is resolved,
1090 the compiler can infer the rest:
1091
1092 ```
1093 let x: Vec<_> = "hello".chars().rev().collect();
1094 ```
1095
1096 Another way to provide the compiler with enough information, is to specify the
1097 generic type parameter:
1098
1099 ```
1100 let x = "hello".chars().rev().collect::<Vec<char>>();
1101 ```
1102
1103 Again, you need not specify the full type if the compiler can infer it:
1104
1105 ```
1106 let x = "hello".chars().rev().collect::<Vec<_>>();
1107 ```
1108
1109 Apart from a method or function with a generic type parameter, this error can
1110 occur when a type parameter of a struct or trait cannot be inferred. In that
1111 case it is not always possible to use a type annotation, because all candidates
1112 have the same return type. For instance:
1113
1114 ```compile_fail,E0282
1115 struct Foo<T> {
1116     num: T,
1117 }
1118
1119 impl<T> Foo<T> {
1120     fn bar() -> i32 {
1121         0
1122     }
1123
1124     fn baz() {
1125         let number = Foo::bar();
1126     }
1127 }
1128 ```
1129
1130 This will fail because the compiler does not know which instance of `Foo` to
1131 call `bar` on. Change `Foo::bar()` to `Foo::<T>::bar()` to resolve the error.
1132 "##,
1133
1134 E0283: r##"
1135 This error occurs when the compiler doesn't have enough information
1136 to unambiguously choose an implementation.
1137
1138 For example:
1139
1140 ```compile_fail,E0283
1141 trait Generator {
1142     fn create() -> u32;
1143 }
1144
1145 struct Impl;
1146
1147 impl Generator for Impl {
1148     fn create() -> u32 { 1 }
1149 }
1150
1151 struct AnotherImpl;
1152
1153 impl Generator for AnotherImpl {
1154     fn create() -> u32 { 2 }
1155 }
1156
1157 fn main() {
1158     let cont: u32 = Generator::create();
1159     // error, impossible to choose one of Generator trait implementation
1160     // Impl or AnotherImpl? Maybe anything else?
1161 }
1162 ```
1163
1164 To resolve this error use the concrete type:
1165
1166 ```
1167 trait Generator {
1168     fn create() -> u32;
1169 }
1170
1171 struct AnotherImpl;
1172
1173 impl Generator for AnotherImpl {
1174     fn create() -> u32 { 2 }
1175 }
1176
1177 fn main() {
1178     let gen1 = AnotherImpl::create();
1179
1180     // if there are multiple methods with same name (different traits)
1181     let gen2 = <AnotherImpl as Generator>::create();
1182 }
1183 ```
1184 "##,
1185
1186 E0296: r##"
1187 This error indicates that the given recursion limit could not be parsed. Ensure
1188 that the value provided is a positive integer between quotes.
1189
1190 Erroneous code example:
1191
1192 ```compile_fail,E0296
1193 #![recursion_limit]
1194
1195 fn main() {}
1196 ```
1197
1198 And a working example:
1199
1200 ```
1201 #![recursion_limit="1000"]
1202
1203 fn main() {}
1204 ```
1205 "##,
1206
1207 E0308: r##"
1208 This error occurs when the compiler was unable to infer the concrete type of a
1209 variable. It can occur for several cases, the most common of which is a
1210 mismatch in the expected type that the compiler inferred for a variable's
1211 initializing expression, and the actual type explicitly assigned to the
1212 variable.
1213
1214 For example:
1215
1216 ```compile_fail,E0308
1217 let x: i32 = "I am not a number!";
1218 //     ~~~   ~~~~~~~~~~~~~~~~~~~~
1219 //      |             |
1220 //      |    initializing expression;
1221 //      |    compiler infers type `&str`
1222 //      |
1223 //    type `i32` assigned to variable `x`
1224 ```
1225 "##,
1226
1227 E0309: r##"
1228 Types in type definitions have lifetimes associated with them that represent
1229 how long the data stored within them is guaranteed to be live. This lifetime
1230 must be as long as the data needs to be alive, and missing the constraint that
1231 denotes this will cause this error.
1232
1233 ```compile_fail,E0309
1234 // This won't compile because T is not constrained, meaning the data
1235 // stored in it is not guaranteed to last as long as the reference
1236 struct Foo<'a, T> {
1237     foo: &'a T
1238 }
1239 ```
1240
1241 This will compile, because it has the constraint on the type parameter:
1242
1243 ```
1244 struct Foo<'a, T: 'a> {
1245     foo: &'a T
1246 }
1247 ```
1248
1249 To see why this is important, consider the case where `T` is itself a reference
1250 (e.g., `T = &str`). If we don't include the restriction that `T: 'a`, the
1251 following code would be perfectly legal:
1252
1253 ```compile_fail,E0309
1254 struct Foo<'a, T> {
1255     foo: &'a T
1256 }
1257
1258 fn main() {
1259     let v = "42".to_string();
1260     let f = Foo{foo: &v};
1261     drop(v);
1262     println!("{}", f.foo); // but we've already dropped v!
1263 }
1264 ```
1265 "##,
1266
1267 E0310: r##"
1268 Types in type definitions have lifetimes associated with them that represent
1269 how long the data stored within them is guaranteed to be live. This lifetime
1270 must be as long as the data needs to be alive, and missing the constraint that
1271 denotes this will cause this error.
1272
1273 ```compile_fail,E0310
1274 // This won't compile because T is not constrained to the static lifetime
1275 // the reference needs
1276 struct Foo<T> {
1277     foo: &'static T
1278 }
1279 ```
1280
1281 This will compile, because it has the constraint on the type parameter:
1282
1283 ```
1284 struct Foo<T: 'static> {
1285     foo: &'static T
1286 }
1287 ```
1288 "##,
1289
1290 E0312: r##"
1291 A lifetime of reference outlives lifetime of borrowed content.
1292
1293 Erroneous code example:
1294
1295 ```compile_fail,E0312
1296 fn make_child<'human, 'elve>(x: &mut &'human isize, y: &mut &'elve isize) {
1297     *x = *y;
1298     // error: lifetime of reference outlives lifetime of borrowed content
1299 }
1300 ```
1301
1302 The compiler cannot determine if the `human` lifetime will live long enough
1303 to keep up on the elve one. To solve this error, you have to give an
1304 explicit lifetime hierarchy:
1305
1306 ```
1307 fn make_child<'human, 'elve: 'human>(x: &mut &'human isize,
1308                                      y: &mut &'elve isize) {
1309     *x = *y; // ok!
1310 }
1311 ```
1312
1313 Or use the same lifetime for every variable:
1314
1315 ```
1316 fn make_child<'elve>(x: &mut &'elve isize, y: &mut &'elve isize) {
1317     *x = *y; // ok!
1318 }
1319 ```
1320 "##,
1321
1322 E0317: r##"
1323 This error occurs when an `if` expression without an `else` block is used in a
1324 context where a type other than `()` is expected, for example a `let`
1325 expression:
1326
1327 ```compile_fail,E0317
1328 fn main() {
1329     let x = 5;
1330     let a = if x == 5 { 1 };
1331 }
1332 ```
1333
1334 An `if` expression without an `else` block has the type `()`, so this is a type
1335 error. To resolve it, add an `else` block having the same type as the `if`
1336 block.
1337 "##,
1338
1339 E0391: r##"
1340 This error indicates that some types or traits depend on each other
1341 and therefore cannot be constructed.
1342
1343 The following example contains a circular dependency between two traits:
1344
1345 ```compile_fail,E0391
1346 trait FirstTrait : SecondTrait {
1347
1348 }
1349
1350 trait SecondTrait : FirstTrait {
1351
1352 }
1353 ```
1354 "##,
1355
1356 E0398: r##"
1357 In Rust 1.3, the default object lifetime bounds are expected to change, as
1358 described in [RFC 1156]. You are getting a warning because the compiler
1359 thinks it is possible that this change will cause a compilation error in your
1360 code. It is possible, though unlikely, that this is a false alarm.
1361
1362 The heart of the change is that where `&'a Box<SomeTrait>` used to default to
1363 `&'a Box<SomeTrait+'a>`, it now defaults to `&'a Box<SomeTrait+'static>` (here,
1364 `SomeTrait` is the name of some trait type). Note that the only types which are
1365 affected are references to boxes, like `&Box<SomeTrait>` or
1366 `&[Box<SomeTrait>]`. More common types like `&SomeTrait` or `Box<SomeTrait>`
1367 are unaffected.
1368
1369 To silence this warning, edit your code to use an explicit bound. Most of the
1370 time, this means that you will want to change the signature of a function that
1371 you are calling. For example, if the error is reported on a call like `foo(x)`,
1372 and `foo` is defined as follows:
1373
1374 ```ignore
1375 fn foo(arg: &Box<SomeTrait>) { ... }
1376 ```
1377
1378 You might change it to:
1379
1380 ```ignore
1381 fn foo<'a>(arg: &Box<SomeTrait+'a>) { ... }
1382 ```
1383
1384 This explicitly states that you expect the trait object `SomeTrait` to contain
1385 references (with a maximum lifetime of `'a`).
1386
1387 [RFC 1156]: https://github.com/rust-lang/rfcs/blob/master/text/1156-adjust-default-object-bounds.md
1388 "##,
1389
1390 E0452: r##"
1391 An invalid lint attribute has been given. Erroneous code example:
1392
1393 ```compile_fail,E0452
1394 #![allow(foo = "")] // error: malformed lint attribute
1395 ```
1396
1397 Lint attributes only accept a list of identifiers (where each identifier is a
1398 lint name). Ensure the attribute is of this form:
1399
1400 ```
1401 #![allow(foo)] // ok!
1402 // or:
1403 #![allow(foo, foo2)] // ok!
1404 ```
1405 "##,
1406
1407 E0453: r##"
1408 A lint check attribute was overruled by a `forbid` directive set as an
1409 attribute on an enclosing scope, or on the command line with the `-F` option.
1410
1411 Example of erroneous code:
1412
1413 ```compile_fail,E0453
1414 #![forbid(non_snake_case)]
1415
1416 #[allow(non_snake_case)]
1417 fn main() {
1418     let MyNumber = 2; // error: allow(non_snake_case) overruled by outer
1419                       //        forbid(non_snake_case)
1420 }
1421 ```
1422
1423 The `forbid` lint setting, like `deny`, turns the corresponding compiler
1424 warning into a hard error. Unlike `deny`, `forbid` prevents itself from being
1425 overridden by inner attributes.
1426
1427 If you're sure you want to override the lint check, you can change `forbid` to
1428 `deny` (or use `-D` instead of `-F` if the `forbid` setting was given as a
1429 command-line option) to allow the inner lint check attribute:
1430
1431 ```
1432 #![deny(non_snake_case)]
1433
1434 #[allow(non_snake_case)]
1435 fn main() {
1436     let MyNumber = 2; // ok!
1437 }
1438 ```
1439
1440 Otherwise, edit the code to pass the lint check, and remove the overruled
1441 attribute:
1442
1443 ```
1444 #![forbid(non_snake_case)]
1445
1446 fn main() {
1447     let my_number = 2;
1448 }
1449 ```
1450 "##,
1451
1452 E0478: r##"
1453 A lifetime bound was not satisfied.
1454
1455 Erroneous code example:
1456
1457 ```compile_fail,E0478
1458 // Check that the explicit lifetime bound (`'SnowWhite`, in this example) must
1459 // outlive all the superbounds from the trait (`'kiss`, in this example).
1460
1461 trait Wedding<'t>: 't { }
1462
1463 struct Prince<'kiss, 'SnowWhite> {
1464     child: Box<Wedding<'kiss> + 'SnowWhite>,
1465     // error: lifetime bound not satisfied
1466 }
1467 ```
1468
1469 In this example, the `'SnowWhite` lifetime is supposed to outlive the `'kiss`
1470 lifetime but the declaration of the `Prince` struct doesn't enforce it. To fix
1471 this issue, you need to specify it:
1472
1473 ```
1474 trait Wedding<'t>: 't { }
1475
1476 struct Prince<'kiss, 'SnowWhite: 'kiss> { // You say here that 'kiss must live
1477                                           // longer than 'SnowWhite.
1478     child: Box<Wedding<'kiss> + 'SnowWhite>, // And now it's all good!
1479 }
1480 ```
1481 "##,
1482
1483 E0491: r##"
1484 A reference has a longer lifetime than the data it references.
1485
1486 Erroneous code example:
1487
1488 ```compile_fail,E0491
1489 // struct containing a reference requires a lifetime parameter,
1490 // because the data the reference points to must outlive the struct (see E0106)
1491 struct Struct<'a> {
1492     ref_i32: &'a i32,
1493 }
1494
1495 // However, a nested struct like this, the signature itself does not tell
1496 // whether 'a outlives 'b or the other way around.
1497 // So it could be possible that 'b of reference outlives 'a of the data.
1498 struct Nested<'a, 'b> {
1499     ref_struct: &'b Struct<'a>, // compile error E0491
1500 }
1501 ```
1502
1503 To fix this issue, you can specify a bound to the lifetime like below:
1504
1505 ```
1506 struct Struct<'a> {
1507     ref_i32: &'a i32,
1508 }
1509
1510 // 'a: 'b means 'a outlives 'b
1511 struct Nested<'a: 'b, 'b> {
1512     ref_struct: &'b Struct<'a>,
1513 }
1514 ```
1515 "##,
1516
1517 E0496: r##"
1518 A lifetime name is shadowing another lifetime name. Erroneous code example:
1519
1520 ```compile_fail,E0496
1521 struct Foo<'a> {
1522     a: &'a i32,
1523 }
1524
1525 impl<'a> Foo<'a> {
1526     fn f<'a>(x: &'a i32) { // error: lifetime name `'a` shadows a lifetime
1527                            //        name that is already in scope
1528     }
1529 }
1530 ```
1531
1532 Please change the name of one of the lifetimes to remove this error. Example:
1533
1534 ```
1535 struct Foo<'a> {
1536     a: &'a i32,
1537 }
1538
1539 impl<'a> Foo<'a> {
1540     fn f<'b>(x: &'b i32) { // ok!
1541     }
1542 }
1543
1544 fn main() {
1545 }
1546 ```
1547 "##,
1548
1549 E0497: r##"
1550 A stability attribute was used outside of the standard library. Erroneous code
1551 example:
1552
1553 ```compile_fail
1554 #[stable] // error: stability attributes may not be used outside of the
1555           //        standard library
1556 fn foo() {}
1557 ```
1558
1559 It is not possible to use stability attributes outside of the standard library.
1560 Also, for now, it is not possible to write deprecation messages either.
1561 "##,
1562
1563 E0512: r##"
1564 Transmute with two differently sized types was attempted. Erroneous code
1565 example:
1566
1567 ```compile_fail,E0512
1568 fn takes_u8(_: u8) {}
1569
1570 fn main() {
1571     unsafe { takes_u8(::std::mem::transmute(0u16)); }
1572     // error: transmute called with differently sized types
1573 }
1574 ```
1575
1576 Please use types with same size or use the expected type directly. Example:
1577
1578 ```
1579 fn takes_u8(_: u8) {}
1580
1581 fn main() {
1582     unsafe { takes_u8(::std::mem::transmute(0i8)); } // ok!
1583     // or:
1584     unsafe { takes_u8(0u8); } // ok!
1585 }
1586 ```
1587 "##,
1588
1589 E0517: r##"
1590 This error indicates that a `#[repr(..)]` attribute was placed on an
1591 unsupported item.
1592
1593 Examples of erroneous code:
1594
1595 ```compile_fail,E0517
1596 #[repr(C)]
1597 type Foo = u8;
1598
1599 #[repr(packed)]
1600 enum Foo {Bar, Baz}
1601
1602 #[repr(u8)]
1603 struct Foo {bar: bool, baz: bool}
1604
1605 #[repr(C)]
1606 impl Foo {
1607     // ...
1608 }
1609 ```
1610
1611 * The `#[repr(C)]` attribute can only be placed on structs and enums.
1612 * The `#[repr(packed)]` and `#[repr(simd)]` attributes only work on structs.
1613 * The `#[repr(u8)]`, `#[repr(i16)]`, etc attributes only work on enums.
1614
1615 These attributes do not work on typedefs, since typedefs are just aliases.
1616
1617 Representations like `#[repr(u8)]`, `#[repr(i64)]` are for selecting the
1618 discriminant size for C-like enums (when there is no associated data, e.g.
1619 `enum Color {Red, Blue, Green}`), effectively setting the size of the enum to
1620 the size of the provided type. Such an enum can be cast to a value of the same
1621 type as well. In short, `#[repr(u8)]` makes the enum behave like an integer
1622 with a constrained set of allowed values.
1623
1624 Only C-like enums can be cast to numerical primitives, so this attribute will
1625 not apply to structs.
1626
1627 `#[repr(packed)]` reduces padding to make the struct size smaller. The
1628 representation of enums isn't strictly defined in Rust, and this attribute
1629 won't work on enums.
1630
1631 `#[repr(simd)]` will give a struct consisting of a homogenous series of machine
1632 types (i.e. `u8`, `i32`, etc) a representation that permits vectorization via
1633 SIMD. This doesn't make much sense for enums since they don't consist of a
1634 single list of data.
1635 "##,
1636
1637 E0518: r##"
1638 This error indicates that an `#[inline(..)]` attribute was incorrectly placed
1639 on something other than a function or method.
1640
1641 Examples of erroneous code:
1642
1643 ```compile_fail,E0518
1644 #[inline(always)]
1645 struct Foo;
1646
1647 #[inline(never)]
1648 impl Foo {
1649     // ...
1650 }
1651 ```
1652
1653 `#[inline]` hints the compiler whether or not to attempt to inline a method or
1654 function. By default, the compiler does a pretty good job of figuring this out
1655 itself, but if you feel the need for annotations, `#[inline(always)]` and
1656 `#[inline(never)]` can override or force the compiler's decision.
1657
1658 If you wish to apply this attribute to all methods in an impl, manually annotate
1659 each method; it is not possible to annotate the entire impl with an `#[inline]`
1660 attribute.
1661 "##,
1662
1663 E0522: r##"
1664 The lang attribute is intended for marking special items that are built-in to
1665 Rust itself. This includes special traits (like `Copy` and `Sized`) that affect
1666 how the compiler behaves, as well as special functions that may be automatically
1667 invoked (such as the handler for out-of-bounds accesses when indexing a slice).
1668 Erroneous code example:
1669
1670 ```compile_fail,E0522
1671 #![feature(lang_items)]
1672
1673 #[lang = "cookie"]
1674 fn cookie() -> ! { // error: definition of an unknown language item: `cookie`
1675     loop {}
1676 }
1677 ```
1678 "##,
1679
1680 E0525: r##"
1681 A closure was used but didn't implement the expected trait.
1682
1683 Erroneous code example:
1684
1685 ```compile_fail,E0525
1686 struct X;
1687
1688 fn foo<T>(_: T) {}
1689 fn bar<T: Fn(u32)>(_: T) {}
1690
1691 fn main() {
1692     let x = X;
1693     let closure = |_| foo(x); // error: expected a closure that implements
1694                               //        the `Fn` trait, but this closure only
1695                               //        implements `FnOnce`
1696     bar(closure);
1697 }
1698 ```
1699
1700 In the example above, `closure` is an `FnOnce` closure whereas the `bar`
1701 function expected an `Fn` closure. In this case, it's simple to fix the issue,
1702 you just have to implement `Copy` and `Clone` traits on `struct X` and it'll
1703 be ok:
1704
1705 ```
1706 #[derive(Clone, Copy)] // We implement `Clone` and `Copy` traits.
1707 struct X;
1708
1709 fn foo<T>(_: T) {}
1710 fn bar<T: Fn(u32)>(_: T) {}
1711
1712 fn main() {
1713     let x = X;
1714     let closure = |_| foo(x);
1715     bar(closure); // ok!
1716 }
1717 ```
1718
1719 To understand better how closures work in Rust, read:
1720 https://doc.rust-lang.org/book/closures.html
1721 "##,
1722
1723 E0580: r##"
1724 The `main` function was incorrectly declared.
1725
1726 Erroneous code example:
1727
1728 ```compile_fail,E0580
1729 fn main() -> i32 { // error: main function has wrong type
1730     0
1731 }
1732 ```
1733
1734 The `main` function prototype should never take arguments or return type.
1735 Example:
1736
1737 ```
1738 fn main() {
1739     // your code
1740 }
1741 ```
1742
1743 If you want to get command-line arguments, use `std::env::args`. To exit with a
1744 specified exit code, use `std::process::exit`.
1745 "##,
1746
1747 E0591: r##"
1748 Per [RFC 401][rfc401], if you have a function declaration `foo`:
1749
1750 ```rust,ignore
1751 // For the purposes of this explanation, all of these
1752 // different kinds of `fn` declarations are equivalent:
1753 fn foo(x: i32) { ... }
1754 extern "C" fn foo(x: i32);
1755 impl i32 { fn foo(x: self) { ... } }
1756 ```
1757
1758 the type of `foo` is **not** `fn(i32)`, as one might expect.
1759 Rather, it is a unique, zero-sized marker type written here as `typeof(foo)`.
1760 However, `typeof(foo)` can be _coerced_ to a function pointer `fn(i32)`,
1761 so you rarely notice this:
1762
1763 ```rust,ignore
1764 let x: fn(i32) = foo; // OK, coerces
1765 ```
1766
1767 The reason that this matter is that the type `fn(i32)` is not specific to
1768 any particular function: it's a function _pointer_. So calling `x()` results
1769 in a virtual call, whereas `foo()` is statically dispatched, because the type
1770 of `foo` tells us precisely what function is being called.
1771
1772 As noted above, coercions mean that most code doesn't have to be
1773 concerned with this distinction. However, you can tell the difference
1774 when using **transmute** to convert a fn item into a fn pointer.
1775
1776 This is sometimes done as part of an FFI:
1777
1778 ```rust,ignore
1779 extern "C" fn foo(userdata: Box<i32>) {
1780    ...
1781 }
1782
1783 let f: extern "C" fn(*mut i32) = transmute(foo);
1784 callback(f);
1785
1786 ```
1787
1788 Here, transmute is being used to convert the types of the fn arguments.
1789 This pattern is incorrect because, because the type of `foo` is a function
1790 **item** (`typeof(foo)`), which is zero-sized, and the target type (`fn()`)
1791 is a function pointer, which is not zero-sized.
1792 This pattern should be rewritten. There are a few possible ways to do this:
1793
1794 - change the original fn declaration to match the expected signature,
1795   and do the cast in the fn body (the prefered option)
1796 - cast the fn item fo a fn pointer before calling transmute, as shown here:
1797   - `let f: extern "C" fn(*mut i32) = transmute(foo as extern "C" fn(_))`
1798   - `let f: extern "C" fn(*mut i32) = transmute(foo as usize) /* works too */`
1799
1800 The same applies to transmutes to `*mut fn()`, which were observedin practice.
1801 Note though that use of this type is generally incorrect.
1802 The intention is typically to describe a function pointer, but just `fn()`
1803 alone suffices for that. `*mut fn()` is a pointer to a fn pointer.
1804 (Since these values are typically just passed to C code, however, this rarely
1805 makes a difference in practice.)
1806
1807 [rfc401]: https://github.com/rust-lang/rfcs/blob/master/text/0401-coercions.md
1808 "##,
1809
1810 }
1811
1812
1813 register_diagnostics! {
1814 //  E0006 // merged with E0005
1815 //  E0101, // replaced with E0282
1816 //  E0102, // replaced with E0282
1817 //  E0134,
1818 //  E0135,
1819     E0278, // requirement is not satisfied
1820     E0279, // requirement is not satisfied
1821     E0280, // requirement is not satisfied
1822     E0284, // cannot resolve type
1823 //  E0285, // overflow evaluation builtin bounds
1824 //  E0300, // unexpanded macro
1825 //  E0304, // expected signed integer constant
1826 //  E0305, // expected constant
1827     E0311, // thing may not live long enough
1828     E0313, // lifetime of borrowed pointer outlives lifetime of captured variable
1829     E0314, // closure outlives stack frame
1830     E0315, // cannot invoke closure outside of its lifetime
1831     E0316, // nested quantification of lifetimes
1832     E0320, // recursive overflow during dropck
1833     E0473, // dereference of reference outside its lifetime
1834     E0474, // captured variable `..` does not outlive the enclosing closure
1835     E0475, // index of slice outside its lifetime
1836     E0476, // lifetime of the source pointer does not outlive lifetime bound...
1837     E0477, // the type `..` does not fulfill the required lifetime...
1838     E0479, // the type `..` (provided as the value of a type parameter) is...
1839     E0480, // lifetime of method receiver does not outlive the method call
1840     E0481, // lifetime of function argument does not outlive the function call
1841     E0482, // lifetime of return value does not outlive the function call
1842     E0483, // lifetime of operand does not outlive the operation
1843     E0484, // reference is not valid at the time of borrow
1844     E0485, // automatically reference is not valid at the time of borrow
1845     E0486, // type of expression contains references that are not valid during...
1846     E0487, // unsafe use of destructor: destructor might be called while...
1847     E0488, // lifetime of variable does not enclose its declaration
1848     E0489, // type/lifetime parameter not in scope here
1849     E0490, // a value of type `..` is borrowed for too long
1850     E0495, // cannot infer an appropriate lifetime due to conflicting requirements
1851     E0566, // conflicting representation hints
1852     E0587, // conflicting packed and align representation hints
1853 }