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