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