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