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