]> git.lizzy.rs Git - rust.git/blob - src/librustc/diagnostics.rs
Auto merge of #28274 - arielb1:split-ty, 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 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 E0015: r##"
302 The only functions that can be called in static or constant expressions are
303 `const` functions. Rust currently does not support more general compile-time
304 function execution.
305
306 See [RFC 911] for more details on the design of `const fn`s.
307
308 [RFC 911]: https://github.com/rust-lang/rfcs/blob/master/text/0911-const-fn.md
309 "##,
310
311 E0016: r##"
312 Blocks in constants may only contain items (such as constant, function
313 definition, etc...) and a tail expression. Example:
314
315 ```
316 const FOO: i32 = { let x = 0; x }; // 'x' isn't an item!
317 ```
318
319 To avoid it, you have to replace the non-item object:
320
321 ```
322 const FOO: i32 = { const X : i32 = 0; X };
323 ```
324 "##,
325
326 E0017: r##"
327 References in statics and constants may only refer to immutable values. Example:
328
329 ```
330 static X: i32 = 1;
331 const C: i32 = 2;
332
333 // these three are not allowed:
334 const CR: &'static mut i32 = &mut C;
335 static STATIC_REF: &'static mut i32 = &mut X;
336 static CONST_REF: &'static mut i32 = &mut C;
337 ```
338
339 Statics are shared everywhere, and if they refer to mutable data one might
340 violate memory safety since holding multiple mutable references to shared data
341 is not allowed.
342
343 If you really want global mutable state, try using `static mut` or a global
344 `UnsafeCell`.
345 "##,
346
347 E0018: r##"
348 The value of static and const variables must be known at compile time. You
349 can't cast a pointer as an integer because we can't know what value the
350 address will take.
351
352 However, pointers to other constants' addresses are allowed in constants,
353 example:
354
355 ```
356 const X: u32 = 50;
357 const Y: *const u32 = &X;
358 ```
359
360 Therefore, casting one of these non-constant pointers to an integer results
361 in a non-constant integer which lead to this error. Example:
362
363 ```
364 const X: u32 = 1;
365 const Y: usize = &X as *const u32 as usize;
366 println!("{}", Y);
367 ```
368 "##,
369
370 E0019: r##"
371 A function call isn't allowed in the const's initialization expression
372 because the expression's value must be known at compile-time. Example of
373 erroneous code:
374
375 ```
376 enum Test {
377     V1
378 }
379
380 impl Test {
381     fn test(&self) -> i32 {
382         12
383     }
384 }
385
386 fn main() {
387     const FOO: Test = Test::V1;
388
389     const A: i32 = FOO.test(); // You can't call Test::func() here !
390 }
391 ```
392
393 Remember: you can't use a function call inside a const's initialization
394 expression! However, you can totally use it anywhere else:
395
396 ```
397 fn main() {
398     const FOO: Test = Test::V1;
399
400     FOO.func(); // here is good
401     let x = FOO.func(); // or even here!
402 }
403 ```
404 "##,
405
406 E0020: r##"
407 This error indicates that an attempt was made to divide by zero (or take the
408 remainder of a zero divisor) in a static or constant expression. Erroneous
409 code example:
410
411 ```
412 const X: i32 = 42 / 0;
413 // error: attempted to divide by zero in a constant expression
414 ```
415 "##,
416
417 E0022: r##"
418 Constant functions are not allowed to mutate anything. Thus, binding to an
419 argument with a mutable pattern is not allowed. For example,
420
421 ```
422 const fn foo(mut x: u8) {
423     // do stuff
424 }
425 ```
426
427 is bad because the function body may not mutate `x`.
428
429 Remove any mutable bindings from the argument list to fix this error. In case
430 you need to mutate the argument, try lazily initializing a global variable
431 instead of using a `const fn`, or refactoring the code to a functional style to
432 avoid mutation if possible.
433 "##,
434
435 E0030: r##"
436 When matching against a range, the compiler verifies that the range is
437 non-empty.  Range patterns include both end-points, so this is equivalent to
438 requiring the start of the range to be less than or equal to the end of the
439 range.
440
441 For example:
442
443 ```
444 match 5u32 {
445     // This range is ok, albeit pointless.
446     1 ... 1 => ...
447     // This range is empty, and the compiler can tell.
448     1000 ... 5 => ...
449 }
450 ```
451 "##,
452
453 E0038: r####"
454 Trait objects like `Box<Trait>` can only be constructed when certain
455 requirements are satisfied by the trait in question.
456
457 Trait objects are a form of dynamic dispatch and use a dynamically sized type
458 for the inner type. So, for a given trait `Trait`, when `Trait` is treated as a
459 type, as in `Box<Trait>`, the inner type is 'unsized'. In such cases the boxed
460 pointer is a 'fat pointer' that contains an extra pointer to a table of methods
461 (among other things) for dynamic dispatch. This design mandates some
462 restrictions on the types of traits that are allowed to be used in trait
463 objects, which are collectively termed as 'object safety' rules.
464
465 Attempting to create a trait object for a non object-safe trait will trigger
466 this error.
467
468 There are various rules:
469
470 ### The trait cannot require `Self: Sized`
471
472 When `Trait` is treated as a type, the type does not implement the special
473 `Sized` trait, because the type does not have a known size at compile time and
474 can only be accessed behind a pointer. Thus, if we have a trait like the
475 following:
476
477 ```
478 trait Foo where Self: Sized {
479
480 }
481 ```
482
483 we cannot create an object of type `Box<Foo>` or `&Foo` since in this case
484 `Self` would not be `Sized`.
485
486 Generally, `Self : Sized` is used to indicate that the trait should not be used
487 as a trait object. If the trait comes from your own crate, consider removing
488 this restriction.
489
490 ### Method references the `Self` type in its arguments or return type
491
492 This happens when a trait has a method like the following:
493
494 ```
495 trait Trait {
496     fn foo(&self) -> Self;
497 }
498
499 impl Trait for String {
500     fn foo(&self) -> Self {
501         "hi".to_owned()
502     }
503 }
504
505 impl Trait for u8 {
506     fn foo(&self) -> Self {
507         1
508     }
509 }
510 ```
511
512 (Note that `&self` and `&mut self` are okay, it's additional `Self` types which
513 cause this problem)
514
515 In such a case, the compiler cannot predict the return type of `foo()` in a
516 situation like the following:
517
518 ```
519 fn call_foo(x: Box<Trait>) {
520     let y = x.foo(); // What type is y?
521     // ...
522 }
523 ```
524
525 If only some methods aren't object-safe, you can add a `where Self: Sized` bound
526 on them to mark them as explicitly unavailable to trait objects. The
527 functionality will still be available to all other implementers, including
528 `Box<Trait>` which is itself sized (assuming you `impl Trait for Box<Trait>`).
529
530 ```
531 trait Trait {
532     fn foo(&self) -> Self where Self: Sized;
533     // more functions
534 }
535 ```
536
537 Now, `foo()` can no longer be called on a trait object, but you will now be
538 allowed to make a trait object, and that will be able to call any object-safe
539 methods". With such a bound, one can still call `foo()` on types implementing
540 that trait that aren't behind trait objects.
541
542 ### Method has generic type parameters
543
544 As mentioned before, trait objects contain pointers to method tables. So, if we
545 have:
546
547 ```
548 trait Trait {
549     fn foo(&self);
550 }
551 impl Trait for String {
552     fn foo(&self) {
553         // implementation 1
554     }
555 }
556 impl Trait for u8 {
557     fn foo(&self) {
558         // implementation 2
559     }
560 }
561 // ...
562 ```
563
564 At compile time each implementation of `Trait` will produce a table containing
565 the various methods (and other items) related to the implementation.
566
567 This works fine, but when the method gains generic parameters, we can have a
568 problem.
569
570 Usually, generic parameters get _monomorphized_. For example, if I have
571
572 ```
573 fn foo<T>(x: T) {
574     // ...
575 }
576 ```
577
578 the machine code for `foo::<u8>()`, `foo::<bool>()`, `foo::<String>()`, or any
579 other type substitution is different. Hence the compiler generates the
580 implementation on-demand. If you call `foo()` with a `bool` parameter, the
581 compiler will only generate code for `foo::<bool>()`. When we have additional
582 type parameters, the number of monomorphized implementations the compiler
583 generates does not grow drastically, since the compiler will only generate an
584 implementation if the function is called with unparametrized substitutions
585 (i.e., substitutions where none of the substituted types are themselves
586 parametrized).
587
588 However, with trait objects we have to make a table containing _every_ object
589 that implements the trait. Now, if it has type parameters, we need to add
590 implementations for every type that implements the trait, and there could
591 theoretically be an infinite number of types.
592
593 For example, with:
594
595 ```
596 trait Trait {
597     fn foo<T>(&self, on: T);
598     // more methods
599 }
600 impl Trait for String {
601     fn foo<T>(&self, on: T) {
602         // implementation 1
603     }
604 }
605 impl Trait for u8 {
606     fn foo<T>(&self, on: T) {
607         // implementation 2
608     }
609 }
610 // 8 more implementations
611 ```
612
613 Now, if we have the following code:
614
615 ```
616 fn call_foo(thing: Box<Trait>) {
617     thing.foo(true); // this could be any one of the 8 types above
618     thing.foo(1);
619     thing.foo("hello");
620 }
621 ```
622
623 we don't just need to create a table of all implementations of all methods of
624 `Trait`, we need to create such a table, for each different type fed to
625 `foo()`. In this case this turns out to be (10 types implementing `Trait`)*(3
626 types being fed to `foo()`) = 30 implementations!
627
628 With real world traits these numbers can grow drastically.
629
630 To fix this, it is suggested to use a `where Self: Sized` bound similar to the
631 fix for the sub-error above if you do not intend to call the method with type
632 parameters:
633
634 ```
635 trait Trait {
636     fn foo<T>(&self, on: T) where Self: Sized;
637     // more methods
638 }
639 ```
640
641 If this is not an option, consider replacing the type parameter with another
642 trait object (e.g. if `T: OtherTrait`, use `on: Box<OtherTrait>`). If the number
643 of types you intend to feed to this method is limited, consider manually listing
644 out the methods of different types.
645
646 ### Method has no receiver
647
648 Methods that do not take a `self` parameter can't be called since there won't be
649 a way to get a pointer to the method table for them
650
651 ```
652 trait Foo {
653     fn foo() -> u8;
654 }
655 ```
656
657 This could be called as `<Foo as Foo>::foo()`, which would not be able to pick
658 an implementation.
659
660 Adding a `Self: Sized` bound to these methods will generally make this compile.
661
662 ```
663 trait Foo {
664     fn foo() -> u8 where Self: Sized;
665 }
666 ```
667
668 ### The trait cannot use `Self` as a type parameter in the supertrait listing
669
670 This is similar to the second sub-error, but subtler. It happens in situations
671 like the following:
672
673 ```
674 trait Super<A> {}
675
676 trait Trait: Super<Self> {
677 }
678
679 struct Foo;
680
681 impl Super<Foo> for Foo{}
682
683 impl Trait for Foo {}
684 ```
685
686 Here, the supertrait might have methods as follows:
687
688 ```
689 trait Super<A> {
690     fn get_a(&self) -> A; // note that this is object safe!
691 }
692 ```
693
694 If the trait `Foo` was deriving from something like `Super<String>` or
695 `Super<T>` (where `Foo` itself is `Foo<T>`), this is okay, because given a type
696 `get_a()` will definitely return an object of that type.
697
698 However, if it derives from `Super<Self>`, even though `Super` is object safe,
699 the method `get_a()` would return an object of unknown type when called on the
700 function. `Self` type parameters let us make object safe traits no longer safe,
701 so they are forbidden when specifying supertraits.
702
703 There's no easy fix for this, generally code will need to be refactored so that
704 you no longer need to derive from `Super<Self>`.
705 "####,
706
707 E0109: r##"
708 You tried to give a type parameter to a type which doesn't need it. Erroneous
709 code example:
710
711 ```
712 type X = u32<i32>; // error: type parameters are not allowed on this type
713 ```
714
715 Please check that you used the correct type and recheck its definition. Perhaps
716 it doesn't need the type parameter.
717
718 Example:
719
720 ```
721 type X = u32; // this compiles
722 ```
723
724 Note that type parameters for enum-variant constructors go after the variant,
725 not after the enum (Option::None::<u32>, not Option::<u32>::None).
726 "##,
727
728 E0110: r##"
729 You tried to give a lifetime parameter to a type which doesn't need it.
730 Erroneous code example:
731
732 ```
733 type X = u32<'static>; // error: lifetime parameters are not allowed on
734                        //        this type
735 ```
736
737 Please check that the correct type was used and recheck its definition; perhaps
738 it doesn't need the lifetime parameter. Example:
739
740 ```
741 type X = u32; // ok!
742 ```
743 "##,
744
745 E0133: r##"
746 Using unsafe functionality, is potentially dangerous and disallowed
747 by safety checks. Examples:
748
749 - Dereferencing raw pointers
750 - Calling functions via FFI
751 - Calling functions marked unsafe
752
753 These safety checks can be relaxed for a section of the code
754 by wrapping the unsafe instructions with an `unsafe` block. For instance:
755
756 ```
757 unsafe fn f() { return; }
758
759 fn main() {
760     unsafe { f(); }
761 }
762 ```
763
764 See also https://doc.rust-lang.org/book/unsafe.html
765 "##,
766
767 // This shouldn't really ever trigger since the repeated value error comes first
768 E0136: r##"
769 A binary can only have one entry point, and by default that entry point is the
770 function `main()`. If there are multiple such functions, please rename one.
771 "##,
772
773 E0137: r##"
774 This error indicates that the compiler found multiple functions with the
775 `#[main]` attribute. This is an error because there must be a unique entry
776 point into a Rust program.
777 "##,
778
779 E0138: r##"
780 This error indicates that the compiler found multiple functions with the
781 `#[start]` attribute. This is an error because there must be a unique entry
782 point into a Rust program.
783 "##,
784
785 // FIXME link this to the relevant turpl chapters for instilling fear of the
786 //       transmute gods in the user
787 E0139: r##"
788 There are various restrictions on transmuting between types in Rust; for example
789 types being transmuted must have the same size. To apply all these restrictions,
790 the compiler must know the exact types that may be transmuted. When type
791 parameters are involved, this cannot always be done.
792
793 So, for example, the following is not allowed:
794
795 ```
796 struct Foo<T>(Vec<T>)
797
798 fn foo<T>(x: Vec<T>) {
799     // we are transmuting between Vec<T> and Foo<T> here
800     let y: Foo<T> = unsafe { transmute(x) };
801     // do something with y
802 }
803 ```
804
805 In this specific case there's a good chance that the transmute is harmless (but
806 this is not guaranteed by Rust). However, when alignment and enum optimizations
807 come into the picture, it's quite likely that the sizes may or may not match
808 with different type parameter substitutions. It's not possible to check this for
809 _all_ possible types, so `transmute()` simply only accepts types without any
810 unsubstituted type parameters.
811
812 If you need this, there's a good chance you're doing something wrong. Keep in
813 mind that Rust doesn't guarantee much about the layout of different structs
814 (even two structs with identical declarations may have different layouts). If
815 there is a solution that avoids the transmute entirely, try it instead.
816
817 If it's possible, hand-monomorphize the code by writing the function for each
818 possible type substitution. It's possible to use traits to do this cleanly,
819 for example:
820
821 ```
822 trait MyTransmutableType {
823     fn transmute(Vec<Self>) -> Foo<Self>
824 }
825
826 impl MyTransmutableType for u8 {
827     fn transmute(x: Foo<u8>) -> Vec<u8> {
828         transmute(x)
829     }
830 }
831 impl MyTransmutableType for String {
832     fn transmute(x: Foo<String>) -> Vec<String> {
833         transmute(x)
834     }
835 }
836 // ... more impls for the types you intend to transmute
837
838 fn foo<T: MyTransmutableType>(x: Vec<T>) {
839     let y: Foo<T> = <T as MyTransmutableType>::transmute(x);
840     // do something with y
841 }
842 ```
843
844 Each impl will be checked for a size match in the transmute as usual, and since
845 there are no unbound type parameters involved, this should compile unless there
846 is a size mismatch in one of the impls.
847
848 It is also possible to manually transmute:
849
850 ```
851 ptr::read(&v as *const _ as *const SomeType) // `v` transmuted to `SomeType`
852 ```
853 "##,
854
855 E0152: r##"
856 Lang items are already implemented in the standard library. Unless you are
857 writing a free-standing application (e.g. a kernel), you do not need to provide
858 them yourself.
859
860 You can build a free-standing crate by adding `#![no_std]` to the crate
861 attributes:
862
863 ```
864 #![feature(no_std)]
865 #![no_std]
866 ```
867
868 See also https://doc.rust-lang.org/book/no-stdlib.html
869 "##,
870
871 E0158: r##"
872 `const` and `static` mean different things. A `const` is a compile-time
873 constant, an alias for a literal value. This property means you can match it
874 directly within a pattern.
875
876 The `static` keyword, on the other hand, guarantees a fixed location in memory.
877 This does not always mean that the value is constant. For example, a global
878 mutex can be declared `static` as well.
879
880 If you want to match against a `static`, consider using a guard instead:
881
882 ```
883 static FORTY_TWO: i32 = 42;
884 match Some(42) {
885     Some(x) if x == FORTY_TWO => ...
886     ...
887 }
888 ```
889 "##,
890
891 E0161: r##"
892 In Rust, you can only move a value when its size is known at compile time.
893
894 To work around this restriction, consider "hiding" the value behind a reference:
895 either `&x` or `&mut x`. Since a reference has a fixed size, this lets you move
896 it around as usual.
897 "##,
898
899 E0162: r##"
900 An if-let pattern attempts to match the pattern, and enters the body if the
901 match was successful. If the match is irrefutable (when it cannot fail to
902 match), use a regular `let`-binding instead. For instance:
903
904 ```
905 struct Irrefutable(i32);
906 let irr = Irrefutable(0);
907
908 // This fails to compile because the match is irrefutable.
909 if let Irrefutable(x) = irr {
910     // This body will always be executed.
911     foo(x);
912 }
913
914 // Try this instead:
915 let Irrefutable(x) = irr;
916 foo(x);
917 ```
918 "##,
919
920 E0165: r##"
921 A while-let pattern attempts to match the pattern, and enters the body if the
922 match was successful. If the match is irrefutable (when it cannot fail to
923 match), use a regular `let`-binding inside a `loop` instead. For instance:
924
925 ```
926 struct Irrefutable(i32);
927 let irr = Irrefutable(0);
928
929 // This fails to compile because the match is irrefutable.
930 while let Irrefutable(x) = irr {
931     ...
932 }
933
934 // Try this instead:
935 loop {
936     let Irrefutable(x) = irr;
937     ...
938 }
939 ```
940 "##,
941
942 E0170: r##"
943 Enum variants are qualified by default. For example, given this type:
944
945 ```
946 enum Method {
947     GET,
948     POST
949 }
950 ```
951
952 you would match it using:
953
954 ```
955 match m {
956     Method::GET => ...
957     Method::POST => ...
958 }
959 ```
960
961 If you don't qualify the names, the code will bind new variables named "GET" and
962 "POST" instead. This behavior is likely not what you want, so `rustc` warns when
963 that happens.
964
965 Qualified names are good practice, and most code works well with them. But if
966 you prefer them unqualified, you can import the variants into scope:
967
968 ```
969 use Method::*;
970 enum Method { GET, POST }
971 ```
972
973 If you want others to be able to import variants from your module directly, use
974 `pub use`:
975
976 ```
977 pub use Method::*;
978 enum Method { GET, POST }
979 ```
980 "##,
981
982 E0261: r##"
983 When using a lifetime like `'a` in a type, it must be declared before being
984 used.
985
986 These two examples illustrate the problem:
987
988 ```
989 // error, use of undeclared lifetime name `'a`
990 fn foo(x: &'a str) { }
991
992 struct Foo {
993     // error, use of undeclared lifetime name `'a`
994     x: &'a str,
995 }
996 ```
997
998 These can be fixed by declaring lifetime parameters:
999
1000 ```
1001 fn foo<'a>(x: &'a str) { }
1002
1003 struct Foo<'a> {
1004     x: &'a str,
1005 }
1006 ```
1007 "##,
1008
1009 E0262: r##"
1010 Declaring certain lifetime names in parameters is disallowed. For example,
1011 because the `'static` lifetime is a special built-in lifetime name denoting
1012 the lifetime of the entire program, this is an error:
1013
1014 ```
1015 // error, invalid lifetime parameter name `'static`
1016 fn foo<'static>(x: &'static str) { }
1017 ```
1018 "##,
1019
1020 E0263: r##"
1021 A lifetime name cannot be declared more than once in the same scope. For
1022 example:
1023
1024 ```
1025 // error, lifetime name `'a` declared twice in the same scope
1026 fn foo<'a, 'b, 'a>(x: &'a str, y: &'b str) { }
1027 ```
1028 "##,
1029
1030 E0265: r##"
1031 This error indicates that a static or constant references itself.
1032 All statics and constants need to resolve to a value in an acyclic manner.
1033
1034 For example, neither of the following can be sensibly compiled:
1035
1036 ```
1037 const X: u32 = X;
1038 ```
1039
1040 ```
1041 const X: u32 = Y;
1042 const Y: u32 = X;
1043 ```
1044 "##,
1045
1046 E0267: r##"
1047 This error indicates the use of a loop keyword (`break` or `continue`) inside a
1048 closure but outside of any loop. Erroneous code example:
1049
1050 ```
1051 let w = || { break; }; // error: `break` inside of a closure
1052 ```
1053
1054 `break` and `continue` keywords can be used as normal inside closures as long as
1055 they are also contained within a loop. To halt the execution of a closure you
1056 should instead use a return statement. Example:
1057
1058 ```
1059 let w = || {
1060     for _ in 0..10 {
1061         break;
1062     }
1063 };
1064
1065 w();
1066 ```
1067 "##,
1068
1069 E0268: r##"
1070 This error indicates the use of a loop keyword (`break` or `continue`) outside
1071 of a loop. Without a loop to break out of or continue in, no sensible action can
1072 be taken. Erroneous code example:
1073
1074 ```
1075 fn some_func() {
1076     break; // error: `break` outside of loop
1077 }
1078 ```
1079
1080 Please verify that you are using `break` and `continue` only in loops. Example:
1081
1082 ```
1083 fn some_func() {
1084     for _ in 0..10 {
1085         break; // ok!
1086     }
1087 }
1088 ```
1089 "##,
1090
1091 E0269: r##"
1092 Functions must eventually return a value of their return type. For example, in
1093 the following function
1094
1095 ```
1096 fn foo(x: u8) -> u8 {
1097     if x > 0 {
1098         x // alternatively, `return x`
1099     }
1100     // nothing here
1101 }
1102 ```
1103
1104 if the condition is true, the value `x` is returned, but if the condition is
1105 false, control exits the `if` block and reaches a place where nothing is being
1106 returned. All possible control paths must eventually return a `u8`, which is not
1107 happening here.
1108
1109 An easy fix for this in a complicated function is to specify a default return
1110 value, if possible:
1111
1112 ```
1113 fn foo(x: u8) -> u8 {
1114     if x > 0 {
1115         x // alternatively, `return x`
1116     }
1117     // lots of other if branches
1118     0 // return 0 if all else fails
1119 }
1120 ```
1121
1122 It is advisable to find out what the unhandled cases are and check for them,
1123 returning an appropriate value or panicking if necessary.
1124 "##,
1125
1126 E0270: r##"
1127 Rust lets you define functions which are known to never return, i.e. are
1128 'diverging', by marking its return type as `!`.
1129
1130 For example, the following functions never return:
1131
1132 ```
1133 fn foo() -> ! {
1134     loop {}
1135 }
1136
1137 fn bar() -> ! {
1138     foo() // foo() is diverging, so this will diverge too
1139 }
1140
1141 fn baz() -> ! {
1142     panic!(); // this macro internally expands to a call to a diverging function
1143 }
1144
1145 ```
1146
1147 Such functions can be used in a place where a value is expected without
1148 returning a value of that type,  for instance:
1149
1150 ```
1151 let y = match x {
1152     1 => 1,
1153     2 => 4,
1154     _ => foo() // diverging function called here
1155 };
1156 println!("{}", y)
1157 ```
1158
1159 If the third arm of the match block is reached, since `foo()` doesn't ever
1160 return control to the match block, it is fine to use it in a place where an
1161 integer was expected. The `match` block will never finish executing, and any
1162 point where `y` (like the print statement) is needed will not be reached.
1163
1164 However, if we had a diverging function that actually does finish execution
1165
1166 ```
1167 fn foo() -> {
1168     loop {break;}
1169 }
1170 ```
1171
1172 then we would have an unknown value for `y` in the following code:
1173
1174 ```
1175 let y = match x {
1176     1 => 1,
1177     2 => 4,
1178     _ => foo()
1179 };
1180 println!("{}", y);
1181 ```
1182
1183 In the previous example, the print statement was never reached when the wildcard
1184 match arm was hit, so we were okay with `foo()` not returning an integer that we
1185 could set to `y`. But in this example, `foo()` actually does return control, so
1186 the print statement will be executed with an uninitialized value.
1187
1188 Obviously we cannot have functions which are allowed to be used in such
1189 positions and yet can return control. So, if you are defining a function that
1190 returns `!`, make sure that there is no way for it to actually finish executing.
1191 "##,
1192
1193 E0271: r##"
1194 This is because of a type mismatch between the associated type of some
1195 trait (e.g. `T::Bar`, where `T` implements `trait Quux { type Bar; }`)
1196 and another type `U` that is required to be equal to `T::Bar`, but is not.
1197 Examples follow.
1198
1199 Here is a basic example:
1200
1201 ```
1202 trait Trait { type AssociatedType; }
1203 fn foo<T>(t: T) where T: Trait<AssociatedType=u32> {
1204     println!("in foo");
1205 }
1206 impl Trait for i8 { type AssociatedType = &'static str; }
1207 foo(3_i8);
1208 ```
1209
1210 Here is that same example again, with some explanatory comments:
1211
1212 ```
1213 trait Trait { type AssociatedType; }
1214
1215 fn foo<T>(t: T) where T: Trait<AssociatedType=u32> {
1216 //                    ~~~~~~~~ ~~~~~~~~~~~~~~~~~~
1217 //                        |            |
1218 //         This says `foo` can         |
1219 //           only be used with         |
1220 //              some type that         |
1221 //         implements `Trait`.         |
1222 //                                     |
1223 //                             This says not only must
1224 //                             `T` be an impl of `Trait`
1225 //                             but also that the impl
1226 //                             must assign the type `u32`
1227 //                             to the associated type.
1228     println!("in foo");
1229 }
1230
1231 impl Trait for i8 { type AssociatedType = &'static str; }
1232 ~~~~~~~~~~~~~~~~~   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1233 //      |                             |
1234 // `i8` does have                     |
1235 // implementation                     |
1236 // of `Trait`...                      |
1237 //                     ... but it is an implementation
1238 //                     that assigns `&'static str` to
1239 //                     the associated type.
1240
1241 foo(3_i8);
1242 // Here, we invoke `foo` with an `i8`, which does not satisfy
1243 // the constraint `<i8 as Trait>::AssociatedType=u32`, and
1244 // therefore the type-checker complains with this error code.
1245 ```
1246
1247 Here is a more subtle instance of the same problem, that can
1248 arise with for-loops in Rust:
1249
1250 ```
1251 let vs: Vec<i32> = vec![1, 2, 3, 4];
1252 for v in &vs {
1253     match v {
1254         1 => {}
1255         _ => {}
1256     }
1257 }
1258 ```
1259
1260 The above fails because of an analogous type mismatch,
1261 though may be harder to see. Again, here are some
1262 explanatory comments for the same example:
1263
1264 ```
1265 {
1266     let vs = vec![1, 2, 3, 4];
1267
1268     // `for`-loops use a protocol based on the `Iterator`
1269     // trait. Each item yielded in a `for` loop has the
1270     // type `Iterator::Item` -- that is,I `Item` is the
1271     // associated type of the concrete iterator impl.
1272     for v in &vs {
1273 //      ~    ~~~
1274 //      |     |
1275 //      |    We borrow `vs`, iterating over a sequence of
1276 //      |    *references* of type `&Elem` (where `Elem` is
1277 //      |    vector's element type). Thus, the associated
1278 //      |    type `Item` must be a reference `&`-type ...
1279 //      |
1280 //  ... and `v` has the type `Iterator::Item`, as dictated by
1281 //  the `for`-loop protocol ...
1282
1283         match v {
1284             1 => {}
1285 //          ~
1286 //          |
1287 // ... but *here*, `v` is forced to have some integral type;
1288 // only types like `u8`,`i8`,`u16`,`i16`, et cetera can
1289 // match the pattern `1` ...
1290
1291             _ => {}
1292         }
1293
1294 // ... therefore, the compiler complains, because it sees
1295 // an attempt to solve the equations
1296 // `some integral-type` = type-of-`v`
1297 //                      = `Iterator::Item`
1298 //                      = `&Elem` (i.e. `some reference type`)
1299 //
1300 // which cannot possibly all be true.
1301
1302     }
1303 }
1304 ```
1305
1306 To avoid those issues, you have to make the types match correctly.
1307 So we can fix the previous examples like this:
1308
1309 ```
1310 // Basic Example:
1311 trait Trait { type AssociatedType; }
1312 fn foo<T>(t: T) where T: Trait<AssociatedType = &'static str> {
1313     println!("in foo");
1314 }
1315 impl Trait for i8 { type AssociatedType = &'static str; }
1316 foo(3_i8);
1317
1318 // For-Loop Example:
1319 let vs = vec![1, 2, 3, 4];
1320 for v in &vs {
1321     match v {
1322         &1 => {}
1323         _ => {}
1324     }
1325 }
1326 ```
1327 "##,
1328
1329 E0272: r##"
1330 The `#[rustc_on_unimplemented]` attribute lets you specify a custom error
1331 message for when a particular trait isn't implemented on a type placed in a
1332 position that needs that trait. For example, when the following code is
1333 compiled:
1334
1335 ```
1336 fn foo<T: Index<u8>>(x: T){}
1337
1338 #[rustc_on_unimplemented = "the type `{Self}` cannot be indexed by `{Idx}`"]
1339 trait Index<Idx> { ... }
1340
1341 foo(true); // `bool` does not implement `Index<u8>`
1342 ```
1343
1344 there will be an error about `bool` not implementing `Index<u8>`, followed by a
1345 note saying "the type `bool` cannot be indexed by `u8`".
1346
1347 As you can see, you can specify type parameters in curly braces for substitution
1348 with the actual types (using the regular format string syntax) in a given
1349 situation. Furthermore, `{Self}` will substitute to the type (in this case,
1350 `bool`) that we tried to use.
1351
1352 This error appears when the curly braces contain an identifier which doesn't
1353 match with any of the type parameters or the string `Self`. This might happen if
1354 you misspelled a type parameter, or if you intended to use literal curly braces.
1355 If it is the latter, escape the curly braces with a second curly brace of the
1356 same type; e.g. a literal `{` is `{{`
1357 "##,
1358
1359 E0273: r##"
1360 The `#[rustc_on_unimplemented]` attribute lets you specify a custom error
1361 message for when a particular trait isn't implemented on a type placed in a
1362 position that needs that trait. For example, when the following code is
1363 compiled:
1364
1365 ```
1366 fn foo<T: Index<u8>>(x: T){}
1367
1368 #[rustc_on_unimplemented = "the type `{Self}` cannot be indexed by `{Idx}`"]
1369 trait Index<Idx> { ... }
1370
1371 foo(true); // `bool` does not implement `Index<u8>`
1372 ```
1373
1374 there will be an error about `bool` not implementing `Index<u8>`, followed by a
1375 note saying "the type `bool` cannot be indexed by `u8`".
1376
1377 As you can see, you can specify type parameters in curly braces for substitution
1378 with the actual types (using the regular format string syntax) in a given
1379 situation. Furthermore, `{Self}` will substitute to the type (in this case,
1380 `bool`) that we tried to use.
1381
1382 This error appears when the curly braces do not contain an identifier. Please
1383 add one of the same name as a type parameter. If you intended to use literal
1384 braces, use `{{` and `}}` to escape them.
1385 "##,
1386
1387 E0274: r##"
1388 The `#[rustc_on_unimplemented]` attribute lets you specify a custom error
1389 message for when a particular trait isn't implemented on a type placed in a
1390 position that needs that trait. For example, when the following code is
1391 compiled:
1392
1393 ```
1394 fn foo<T: Index<u8>>(x: T){}
1395
1396 #[rustc_on_unimplemented = "the type `{Self}` cannot be indexed by `{Idx}`"]
1397 trait Index<Idx> { ... }
1398
1399 foo(true); // `bool` does not implement `Index<u8>`
1400 ```
1401
1402 there will be an error about `bool` not implementing `Index<u8>`, followed by a
1403 note saying "the type `bool` cannot be indexed by `u8`".
1404
1405 For this to work, some note must be specified. An empty attribute will not do
1406 anything, please remove the attribute or add some helpful note for users of the
1407 trait.
1408 "##,
1409
1410 E0275: r##"
1411 This error occurs when there was a recursive trait requirement that overflowed
1412 before it could be evaluated. Often this means that there is unbounded recursion
1413 in resolving some type bounds.
1414
1415 For example, in the following code
1416
1417 ```
1418 trait Foo {}
1419
1420 struct Bar<T>(T);
1421
1422 impl<T> Foo for T where Bar<T>: Foo {}
1423 ```
1424
1425 to determine if a `T` is `Foo`, we need to check if `Bar<T>` is `Foo`. However,
1426 to do this check, we need to determine that `Bar<Bar<T>>` is `Foo`. To determine
1427 this, we check if `Bar<Bar<Bar<T>>>` is `Foo`, and so on. This is clearly a
1428 recursive requirement that can't be resolved directly.
1429
1430 Consider changing your trait bounds so that they're less self-referential.
1431 "##,
1432
1433 E0276: r##"
1434 This error occurs when a bound in an implementation of a trait does not match
1435 the bounds specified in the original trait. For example:
1436
1437 ```
1438 trait Foo {
1439  fn foo<T>(x: T);
1440 }
1441
1442 impl Foo for bool {
1443  fn foo<T>(x: T) where T: Copy {}
1444 }
1445 ```
1446
1447 Here, all types implementing `Foo` must have a method `foo<T>(x: T)` which can
1448 take any type `T`. However, in the `impl` for `bool`, we have added an extra
1449 bound that `T` is `Copy`, which isn't compatible with the original trait.
1450
1451 Consider removing the bound from the method or adding the bound to the original
1452 method definition in the trait.
1453 "##,
1454
1455 E0277: r##"
1456 You tried to use a type which doesn't implement some trait in a place which
1457 expected that trait. Erroneous code example:
1458
1459 ```
1460 // here we declare the Foo trait with a bar method
1461 trait Foo {
1462     fn bar(&self);
1463 }
1464
1465 // we now declare a function which takes an object implementing the Foo trait
1466 fn some_func<T: Foo>(foo: T) {
1467     foo.bar();
1468 }
1469
1470 fn main() {
1471     // we now call the method with the i32 type, which doesn't implement
1472     // the Foo trait
1473     some_func(5i32); // error: the trait `Foo` is not implemented for the
1474                      //     type `i32`
1475 }
1476 ```
1477
1478 In order to fix this error, verify that the type you're using does implement
1479 the trait. Example:
1480
1481 ```
1482 trait Foo {
1483     fn bar(&self);
1484 }
1485
1486 fn some_func<T: Foo>(foo: T) {
1487     foo.bar(); // we can now use this method since i32 implements the
1488                // Foo trait
1489 }
1490
1491 // we implement the trait on the i32 type
1492 impl Foo for i32 {
1493     fn bar(&self) {}
1494 }
1495
1496 fn main() {
1497     some_func(5i32); // ok!
1498 }
1499 ```
1500 "##,
1501
1502 E0281: r##"
1503 You tried to supply a type which doesn't implement some trait in a location
1504 which expected that trait. This error typically occurs when working with
1505 `Fn`-based types. Erroneous code example:
1506
1507 ```
1508 fn foo<F: Fn()>(x: F) { }
1509
1510 fn main() {
1511     // type mismatch: the type ... implements the trait `core::ops::Fn<(_,)>`,
1512     // but the trait `core::ops::Fn<()>` is required (expected (), found tuple
1513     // [E0281]
1514     foo(|y| { });
1515 }
1516 ```
1517
1518 The issue in this case is that `foo` is defined as accepting a `Fn` with no
1519 arguments, but the closure we attempted to pass to it requires one argument.
1520 "##,
1521
1522 E0282: r##"
1523 This error indicates that type inference did not result in one unique possible
1524 type, and extra information is required. In most cases this can be provided
1525 by adding a type annotation. Sometimes you need to specify a generic type
1526 parameter manually.
1527
1528 A common example is the `collect` method on `Iterator`. It has a generic type
1529 parameter with a `FromIterator` bound, which for a `char` iterator is
1530 implemented by `Vec` and `String` among others. Consider the following snippet
1531 that reverses the characters of a string:
1532
1533 ```
1534 let x = "hello".chars().rev().collect();
1535 ```
1536
1537 In this case, the compiler cannot infer what the type of `x` should be:
1538 `Vec<char>` and `String` are both suitable candidates. To specify which type to
1539 use, you can use a type annotation on `x`:
1540
1541 ```
1542 let x: Vec<char> = "hello".chars().rev().collect();
1543 ```
1544
1545 It is not necessary to annotate the full type. Once the ambiguity is resolved,
1546 the compiler can infer the rest:
1547
1548 ```
1549 let x: Vec<_> = "hello".chars().rev().collect();
1550 ```
1551
1552 Another way to provide the compiler with enough information, is to specify the
1553 generic type parameter:
1554
1555 ```
1556 let x = "hello".chars().rev().collect::<Vec<char>>();
1557 ```
1558
1559 Again, you need not specify the full type if the compiler can infer it:
1560
1561 ```
1562 let x = "hello".chars().rev().collect::<Vec<_>>();
1563 ```
1564
1565 Apart from a method or function with a generic type parameter, this error can
1566 occur when a type parameter of a struct or trait cannot be inferred. In that
1567 case it is not always possible to use a type annotation, because all candidates
1568 have the same return type. For instance:
1569
1570 ```
1571 struct Foo<T> {
1572     // Some fields omitted.
1573 }
1574
1575 impl<T> Foo<T> {
1576     fn bar() -> i32 {
1577         0
1578     }
1579
1580     fn baz() {
1581         let number = Foo::bar();
1582     }
1583 }
1584 ```
1585
1586 This will fail because the compiler does not know which instance of `Foo` to
1587 call `bar` on. Change `Foo::bar()` to `Foo::<T>::bar()` to resolve the error.
1588 "##,
1589
1590 E0296: r##"
1591 This error indicates that the given recursion limit could not be parsed. Ensure
1592 that the value provided is a positive integer between quotes, like so:
1593
1594 ```
1595 #![recursion_limit="1000"]
1596 ```
1597 "##,
1598
1599 E0297: r##"
1600 Patterns used to bind names must be irrefutable. That is, they must guarantee
1601 that a name will be extracted in all cases. Instead of pattern matching the
1602 loop variable, consider using a `match` or `if let` inside the loop body. For
1603 instance:
1604
1605 ```
1606 // This fails because `None` is not covered.
1607 for Some(x) in xs {
1608     ...
1609 }
1610
1611 // Match inside the loop instead:
1612 for item in xs {
1613     match item {
1614         Some(x) => ...
1615         None => ...
1616     }
1617 }
1618
1619 // Or use `if let`:
1620 for item in xs {
1621     if let Some(x) = item {
1622         ...
1623     }
1624 }
1625 ```
1626 "##,
1627
1628 E0301: r##"
1629 Mutable borrows are not allowed in pattern guards, because matching cannot have
1630 side effects. Side effects could alter the matched object or the environment
1631 on which the match depends in such a way, that the match would not be
1632 exhaustive. For instance, the following would not match any arm if mutable
1633 borrows were allowed:
1634
1635 ```
1636 match Some(()) {
1637     None => { },
1638     option if option.take().is_none() => { /* impossible, option is `Some` */ },
1639     Some(_) => { } // When the previous match failed, the option became `None`.
1640 }
1641 ```
1642 "##,
1643
1644 E0302: r##"
1645 Assignments are not allowed in pattern guards, because matching cannot have
1646 side effects. Side effects could alter the matched object or the environment
1647 on which the match depends in such a way, that the match would not be
1648 exhaustive. For instance, the following would not match any arm if assignments
1649 were allowed:
1650
1651 ```
1652 match Some(()) {
1653     None => { },
1654     option if { option = None; false } { },
1655     Some(_) => { } // When the previous match failed, the option became `None`.
1656 }
1657 ```
1658 "##,
1659
1660 E0303: r##"
1661 In certain cases it is possible for sub-bindings to violate memory safety.
1662 Updates to the borrow checker in a future version of Rust may remove this
1663 restriction, but for now patterns must be rewritten without sub-bindings.
1664
1665 ```
1666 // Before.
1667 match Some("hi".to_string()) {
1668     ref op_string_ref @ Some(ref s) => ...
1669     None => ...
1670 }
1671
1672 // After.
1673 match Some("hi".to_string()) {
1674     Some(ref s) => {
1675         let op_string_ref = &Some(s);
1676         ...
1677     }
1678     None => ...
1679 }
1680 ```
1681
1682 The `op_string_ref` binding has type `&Option<&String>` in both cases.
1683
1684 See also https://github.com/rust-lang/rust/issues/14587
1685 "##,
1686
1687 E0306: r##"
1688 In an array literal `[x; N]`, `N` is the number of elements in the array. This
1689 number cannot be negative.
1690 "##,
1691
1692 E0307: r##"
1693 The length of an array is part of its type. For this reason, this length must be
1694 a compile-time constant.
1695 "##,
1696
1697 E0308: r##"
1698 This error occurs when the compiler was unable to infer the concrete type of a
1699 variable. It can occur for several cases, the most common of which is a
1700 mismatch in the expected type that the compiler inferred for a variable's
1701 initializing expression, and the actual type explicitly assigned to the
1702 variable.
1703
1704 For example:
1705
1706 ```
1707 let x: i32 = "I am not a number!";
1708 //     ~~~   ~~~~~~~~~~~~~~~~~~~~
1709 //      |             |
1710 //      |    initializing expression;
1711 //      |    compiler infers type `&str`
1712 //      |
1713 //    type `i32` assigned to variable `x`
1714 ```
1715 "##,
1716
1717 E0309: r##"
1718 Types in type definitions have lifetimes associated with them that represent
1719 how long the data stored within them is guaranteed to be live. This lifetime
1720 must be as long as the data needs to be alive, and missing the constraint that
1721 denotes this will cause this error.
1722
1723 ```
1724 // This won't compile because T is not constrained, meaning the data
1725 // stored in it is not guaranteed to last as long as the reference
1726 struct Foo<'a, T> {
1727     foo: &'a T
1728 }
1729
1730 // This will compile, because it has the constraint on the type parameter
1731 struct Foo<'a, T: 'a> {
1732     foo: &'a T
1733 }
1734 ```
1735 "##,
1736
1737 E0310: r##"
1738 Types in type definitions have lifetimes associated with them that represent
1739 how long the data stored within them is guaranteed to be live. This lifetime
1740 must be as long as the data needs to be alive, and missing the constraint that
1741 denotes this will cause this error.
1742
1743 ```
1744 // This won't compile because T is not constrained to the static lifetime
1745 // the reference needs
1746 struct Foo<T> {
1747     foo: &'static T
1748 }
1749
1750 // This will compile, because it has the constraint on the type parameter
1751 struct Foo<T: 'static> {
1752     foo: &'static T
1753 }
1754 ```
1755 "##,
1756
1757 E0378: r##"
1758 Method calls that aren't calls to inherent `const` methods are disallowed
1759 in statics, constants, and constant functions.
1760
1761 For example:
1762
1763 ```
1764 const BAZ: i32 = Foo(25).bar(); // error, `bar` isn't `const`
1765
1766 struct Foo(i32);
1767
1768 impl Foo {
1769     const fn foo(&self) -> i32 {
1770         self.bar() // error, `bar` isn't `const`
1771     }
1772
1773     fn bar(&self) -> i32 { self.0 }
1774 }
1775 ```
1776
1777 For more information about `const fn`'s, see [RFC 911].
1778
1779 [RFC 911]: https://github.com/rust-lang/rfcs/blob/master/text/0911-const-fn.md
1780 "##,
1781
1782 E0394: r##"
1783 From [RFC 246]:
1784
1785  > It is invalid for a static to reference another static by value. It is
1786  > required that all references be borrowed.
1787
1788 [RFC 246]: https://github.com/rust-lang/rfcs/pull/246
1789 "##,
1790
1791 E0395: r##"
1792 The value assigned to a constant expression must be known at compile time,
1793 which is not the case when comparing raw pointers. Erroneous code example:
1794
1795 ```
1796 static foo: i32 = 42;
1797 static bar: i32 = 43;
1798
1799 static baz: bool = { (&foo as *const i32) == (&bar as *const i32) };
1800 // error: raw pointers cannot be compared in statics!
1801 ```
1802
1803 Please check that the result of the comparison can be determined at compile time
1804 or isn't assigned to a constant expression. Example:
1805
1806 ```
1807 static foo: i32 = 42;
1808 static bar: i32 = 43;
1809
1810 let baz: bool = { (&foo as *const i32) == (&bar as *const i32) };
1811 // baz isn't a constant expression so it's ok
1812 ```
1813 "##,
1814
1815 E0396: r##"
1816 The value assigned to a constant expression must be known at compile time,
1817 which is not the case when dereferencing raw pointers. Erroneous code
1818 example:
1819
1820 ```
1821 const foo: i32 = 42;
1822 const baz: *const i32 = (&foo as *const i32);
1823
1824 const deref: i32 = *baz;
1825 // error: raw pointers cannot be dereferenced in constants
1826 ```
1827
1828 To fix this error, please do not assign this value to a constant expression.
1829 Example:
1830
1831 ```
1832 const foo: i32 = 42;
1833 const baz: *const i32 = (&foo as *const i32);
1834
1835 unsafe { let deref: i32 = *baz; }
1836 // baz isn't a constant expression so it's ok
1837 ```
1838
1839 You'll also note that this assignment must be done in an unsafe block!
1840 "##,
1841
1842 E0397: r##"
1843 It is not allowed for a mutable static to allocate or have destructors. For
1844 example:
1845
1846 ```
1847 // error: mutable statics are not allowed to have boxes
1848 static mut FOO: Option<Box<usize>> = None;
1849
1850 // error: mutable statics are not allowed to have destructors
1851 static mut BAR: Option<Vec<i32>> = None;
1852 ```
1853 "##,
1854
1855 E0398: r##"
1856 In Rust 1.3, the default object lifetime bounds are expected to
1857 change, as described in RFC #1156 [1]. You are getting a warning
1858 because the compiler thinks it is possible that this change will cause
1859 a compilation error in your code. It is possible, though unlikely,
1860 that this is a false alarm.
1861
1862 The heart of the change is that where `&'a Box<SomeTrait>` used to
1863 default to `&'a Box<SomeTrait+'a>`, it now defaults to `&'a
1864 Box<SomeTrait+'static>` (here, `SomeTrait` is the name of some trait
1865 type). Note that the only types which are affected are references to
1866 boxes, like `&Box<SomeTrait>` or `&[Box<SomeTrait>]`.  More common
1867 types like `&SomeTrait` or `Box<SomeTrait>` are unaffected.
1868
1869 To silence this warning, edit your code to use an explicit bound.
1870 Most of the time, this means that you will want to change the
1871 signature of a function that you are calling. For example, if
1872 the error is reported on a call like `foo(x)`, and `foo` is
1873 defined as follows:
1874
1875 ```
1876 fn foo(arg: &Box<SomeTrait>) { ... }
1877 ```
1878
1879 you might change it to:
1880
1881 ```
1882 fn foo<'a>(arg: &Box<SomeTrait+'a>) { ... }
1883 ```
1884
1885 This explicitly states that you expect the trait object `SomeTrait` to
1886 contain references (with a maximum lifetime of `'a`).
1887
1888 [1]: https://github.com/rust-lang/rfcs/pull/1156
1889 "##
1890
1891 }
1892
1893
1894 register_diagnostics! {
1895     // E0006 // merged with E0005
1896 //  E0134,
1897 //  E0135,
1898     E0229, // associated type bindings are not allowed here
1899     E0264, // unknown external lang item
1900     E0278, // requirement is not satisfied
1901     E0279, // requirement is not satisfied
1902     E0280, // requirement is not satisfied
1903     E0283, // cannot resolve type
1904     E0284, // cannot resolve type
1905     E0285, // overflow evaluation builtin bounds
1906     E0298, // mismatched types between arms
1907     E0299, // mismatched types between arms
1908     E0300, // unexpanded macro
1909     E0304, // expected signed integer constant
1910     E0305, // expected constant
1911     E0311, // thing may not live long enough
1912     E0312, // lifetime of reference outlives lifetime of borrowed content
1913     E0313, // lifetime of borrowed pointer outlives lifetime of captured variable
1914     E0314, // closure outlives stack frame
1915     E0315, // cannot invoke closure outside of its lifetime
1916     E0316, // nested quantification of lifetimes
1917     E0400  // overloaded derefs are not allowed in constants
1918 }