]> git.lizzy.rs Git - rust.git/blob - src/librustc_mir/diagnostics.rs
run EndRegion when unwinding otherwise-empty scopes
[rust.git] / src / librustc_mir / diagnostics.rs
1 // Copyright 2015 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 register_long_diagnostics! {
14
15 E0010: r##"
16 The value of statics and constants must be known at compile time, and they live
17 for the entire lifetime of a program. Creating a boxed value allocates memory on
18 the heap at runtime, and therefore cannot be done at compile time. Erroneous
19 code example:
20
21 ```compile_fail,E0010
22 #![feature(box_syntax)]
23
24 const CON : Box<i32> = box 0;
25 ```
26 "##,
27
28 E0013: r##"
29 Static and const variables can refer to other const variables. But a const
30 variable cannot refer to a static variable. For example, `Y` cannot refer to
31 `X` here:
32
33 ```compile_fail,E0013
34 static X: i32 = 42;
35 const Y: i32 = X;
36 ```
37
38 To fix this, the value can be extracted as a const and then used:
39
40 ```
41 const A: i32 = 42;
42 static X: i32 = A;
43 const Y: i32 = A;
44 ```
45 "##,
46
47 // FIXME(#24111) Change the language here when const fn stabilizes
48 E0015: r##"
49 The only functions that can be called in static or constant expressions are
50 `const` functions, and struct/enum constructors. `const` functions are only
51 available on a nightly compiler. Rust currently does not support more general
52 compile-time function execution.
53
54 ```
55 const FOO: Option<u8> = Some(1); // enum constructor
56 struct Bar {x: u8}
57 const BAR: Bar = Bar {x: 1}; // struct constructor
58 ```
59
60 See [RFC 911] for more details on the design of `const fn`s.
61
62 [RFC 911]: https://github.com/rust-lang/rfcs/blob/master/text/0911-const-fn.md
63 "##,
64
65 E0016: r##"
66 Blocks in constants may only contain items (such as constant, function
67 definition, etc...) and a tail expression. Erroneous code example:
68
69 ```compile_fail,E0016
70 const FOO: i32 = { let x = 0; x }; // 'x' isn't an item!
71 ```
72
73 To avoid it, you have to replace the non-item object:
74
75 ```
76 const FOO: i32 = { const X : i32 = 0; X };
77 ```
78 "##,
79
80 E0017: r##"
81 References in statics and constants may only refer to immutable values.
82 Erroneous code example:
83
84 ```compile_fail,E0017
85 static X: i32 = 1;
86 const C: i32 = 2;
87
88 // these three are not allowed:
89 const CR: &'static mut i32 = &mut C;
90 static STATIC_REF: &'static mut i32 = &mut X;
91 static CONST_REF: &'static mut i32 = &mut C;
92 ```
93
94 Statics are shared everywhere, and if they refer to mutable data one might
95 violate memory safety since holding multiple mutable references to shared data
96 is not allowed.
97
98 If you really want global mutable state, try using `static mut` or a global
99 `UnsafeCell`.
100 "##,
101
102 E0018: r##"
103
104 The value of static and constant integers must be known at compile time. You
105 can't cast a pointer to an integer because the address of a pointer can
106 vary.
107
108 For example, if you write:
109
110 ```compile_fail,E0018
111 static MY_STATIC: u32 = 42;
112 static MY_STATIC_ADDR: usize = &MY_STATIC as *const _ as usize;
113 static WHAT: usize = (MY_STATIC_ADDR^17) + MY_STATIC_ADDR;
114 ```
115
116 Then `MY_STATIC_ADDR` would contain the address of `MY_STATIC`. However,
117 the address can change when the program is linked, as well as change
118 between different executions due to ASLR, and many linkers would
119 not be able to calculate the value of `WHAT`.
120
121 On the other hand, static and constant pointers can point either to
122 a known numeric address or to the address of a symbol.
123
124 ```
125 static MY_STATIC: u32 = 42;
126 static MY_STATIC_ADDR: &'static u32 = &MY_STATIC;
127 const CONST_ADDR: *const u8 = 0x5f3759df as *const u8;
128 ```
129
130 This does not pose a problem by itself because they can't be
131 accessed directly.
132 "##,
133
134 E0019: r##"
135 A function call isn't allowed in the const's initialization expression
136 because the expression's value must be known at compile-time. Erroneous code
137 example:
138
139 ```compile_fail
140 enum Test {
141     V1
142 }
143
144 impl Test {
145     fn test(&self) -> i32 {
146         12
147     }
148 }
149
150 fn main() {
151     const FOO: Test = Test::V1;
152
153     const A: i32 = FOO.test(); // You can't call Test::func() here!
154 }
155 ```
156
157 Remember: you can't use a function call inside a const's initialization
158 expression! However, you can totally use it anywhere else:
159
160 ```
161 enum Test {
162     V1
163 }
164
165 impl Test {
166     fn func(&self) -> i32 {
167         12
168     }
169 }
170
171 fn main() {
172     const FOO: Test = Test::V1;
173
174     FOO.func(); // here is good
175     let x = FOO.func(); // or even here!
176 }
177 ```
178 "##,
179
180 E0022: r##"
181 Constant functions are not allowed to mutate anything. Thus, binding to an
182 argument with a mutable pattern is not allowed. For example,
183
184 ```compile_fail
185 const fn foo(mut x: u8) {
186     // do stuff
187 }
188 ```
189
190 Is incorrect because the function body may not mutate `x`.
191
192 Remove any mutable bindings from the argument list to fix this error. In case
193 you need to mutate the argument, try lazily initializing a global variable
194 instead of using a `const fn`, or refactoring the code to a functional style to
195 avoid mutation if possible.
196 "##,
197
198 E0133: r##"
199 Unsafe code was used outside of an unsafe function or block.
200
201 Erroneous code example:
202
203 ```compile_fail,E0133
204 unsafe fn f() { return; } // This is the unsafe code
205
206 fn main() {
207     f(); // error: call to unsafe function requires unsafe function or block
208 }
209 ```
210
211 Using unsafe functionality is potentially dangerous and disallowed by safety
212 checks. Examples:
213
214 * Dereferencing raw pointers
215 * Calling functions via FFI
216 * Calling functions marked unsafe
217
218 These safety checks can be relaxed for a section of the code by wrapping the
219 unsafe instructions with an `unsafe` block. For instance:
220
221 ```
222 unsafe fn f() { return; }
223
224 fn main() {
225     unsafe { f(); } // ok!
226 }
227 ```
228
229 See also https://doc.rust-lang.org/book/first-edition/unsafe.html
230 "##,
231
232 E0373: r##"
233 This error occurs when an attempt is made to use data captured by a closure,
234 when that data may no longer exist. It's most commonly seen when attempting to
235 return a closure:
236
237 ```compile_fail,E0373
238 fn foo() -> Box<Fn(u32) -> u32> {
239     let x = 0u32;
240     Box::new(|y| x + y)
241 }
242 ```
243
244 Notice that `x` is stack-allocated by `foo()`. By default, Rust captures
245 closed-over data by reference. This means that once `foo()` returns, `x` no
246 longer exists. An attempt to access `x` within the closure would thus be
247 unsafe.
248
249 Another situation where this might be encountered is when spawning threads:
250
251 ```compile_fail,E0373
252 fn foo() {
253     let x = 0u32;
254     let y = 1u32;
255
256     let thr = std::thread::spawn(|| {
257         x + y
258     });
259 }
260 ```
261
262 Since our new thread runs in parallel, the stack frame containing `x` and `y`
263 may well have disappeared by the time we try to use them. Even if we call
264 `thr.join()` within foo (which blocks until `thr` has completed, ensuring the
265 stack frame won't disappear), we will not succeed: the compiler cannot prove
266 that this behaviour is safe, and so won't let us do it.
267
268 The solution to this problem is usually to switch to using a `move` closure.
269 This approach moves (or copies, where possible) data into the closure, rather
270 than taking references to it. For example:
271
272 ```
273 fn foo() -> Box<Fn(u32) -> u32> {
274     let x = 0u32;
275     Box::new(move |y| x + y)
276 }
277 ```
278
279 Now that the closure has its own copy of the data, there's no need to worry
280 about safety.
281 "##,
282
283 E0381: r##"
284 It is not allowed to use or capture an uninitialized variable. For example:
285
286 ```compile_fail,E0381
287 fn main() {
288     let x: i32;
289     let y = x; // error, use of possibly uninitialized variable
290 }
291 ```
292
293 To fix this, ensure that any declared variables are initialized before being
294 used. Example:
295
296 ```
297 fn main() {
298     let x: i32 = 0;
299     let y = x; // ok!
300 }
301 ```
302 "##,
303
304 E0382: r##"
305 This error occurs when an attempt is made to use a variable after its contents
306 have been moved elsewhere. For example:
307
308 ```compile_fail,E0382
309 struct MyStruct { s: u32 }
310
311 fn main() {
312     let mut x = MyStruct{ s: 5u32 };
313     let y = x;
314     x.s = 6;
315     println!("{}", x.s);
316 }
317 ```
318
319 Since `MyStruct` is a type that is not marked `Copy`, the data gets moved out
320 of `x` when we set `y`. This is fundamental to Rust's ownership system: outside
321 of workarounds like `Rc`, a value cannot be owned by more than one variable.
322
323 If we own the type, the easiest way to address this problem is to implement
324 `Copy` and `Clone` on it, as shown below. This allows `y` to copy the
325 information in `x`, while leaving the original version owned by `x`. Subsequent
326 changes to `x` will not be reflected when accessing `y`.
327
328 ```
329 #[derive(Copy, Clone)]
330 struct MyStruct { s: u32 }
331
332 fn main() {
333     let mut x = MyStruct{ s: 5u32 };
334     let y = x;
335     x.s = 6;
336     println!("{}", x.s);
337 }
338 ```
339
340 Alternatively, if we don't control the struct's definition, or mutable shared
341 ownership is truly required, we can use `Rc` and `RefCell`:
342
343 ```
344 use std::cell::RefCell;
345 use std::rc::Rc;
346
347 struct MyStruct { s: u32 }
348
349 fn main() {
350     let mut x = Rc::new(RefCell::new(MyStruct{ s: 5u32 }));
351     let y = x.clone();
352     x.borrow_mut().s = 6;
353     println!("{}", x.borrow().s);
354 }
355 ```
356
357 With this approach, x and y share ownership of the data via the `Rc` (reference
358 count type). `RefCell` essentially performs runtime borrow checking: ensuring
359 that at most one writer or multiple readers can access the data at any one time.
360
361 If you wish to learn more about ownership in Rust, start with the chapter in the
362 Book:
363
364 https://doc.rust-lang.org/book/first-edition/ownership.html
365 "##,
366
367 E0383: r##"
368 This error occurs when an attempt is made to partially reinitialize a
369 structure that is currently uninitialized.
370
371 For example, this can happen when a drop has taken place:
372
373 ```compile_fail,E0383
374 struct Foo {
375     a: u32,
376 }
377 impl Drop for Foo {
378     fn drop(&mut self) { /* ... */ }
379 }
380
381 let mut x = Foo { a: 1 };
382 drop(x); // `x` is now uninitialized
383 x.a = 2; // error, partial reinitialization of uninitialized structure `t`
384 ```
385
386 This error can be fixed by fully reinitializing the structure in question:
387
388 ```
389 struct Foo {
390     a: u32,
391 }
392 impl Drop for Foo {
393     fn drop(&mut self) { /* ... */ }
394 }
395
396 let mut x = Foo { a: 1 };
397 drop(x);
398 x = Foo { a: 2 };
399 ```
400 "##,
401
402 E0384: r##"
403 This error occurs when an attempt is made to reassign an immutable variable.
404 For example:
405
406 ```compile_fail,E0384
407 fn main() {
408     let x = 3;
409     x = 5; // error, reassignment of immutable variable
410 }
411 ```
412
413 By default, variables in Rust are immutable. To fix this error, add the keyword
414 `mut` after the keyword `let` when declaring the variable. For example:
415
416 ```
417 fn main() {
418     let mut x = 3;
419     x = 5;
420 }
421 ```
422 "##,
423
424 /*E0386: r##"
425 This error occurs when an attempt is made to mutate the target of a mutable
426 reference stored inside an immutable container.
427
428 For example, this can happen when storing a `&mut` inside an immutable `Box`:
429
430 ```compile_fail,E0386
431 let mut x: i64 = 1;
432 let y: Box<_> = Box::new(&mut x);
433 **y = 2; // error, cannot assign to data in an immutable container
434 ```
435
436 This error can be fixed by making the container mutable:
437
438 ```
439 let mut x: i64 = 1;
440 let mut y: Box<_> = Box::new(&mut x);
441 **y = 2;
442 ```
443
444 It can also be fixed by using a type with interior mutability, such as `Cell`
445 or `RefCell`:
446
447 ```
448 use std::cell::Cell;
449
450 let x: i64 = 1;
451 let y: Box<Cell<_>> = Box::new(Cell::new(x));
452 y.set(2);
453 ```
454 "##,*/
455
456 E0387: r##"
457 This error occurs when an attempt is made to mutate or mutably reference data
458 that a closure has captured immutably. Examples of this error are shown below:
459
460 ```compile_fail,E0387
461 // Accepts a function or a closure that captures its environment immutably.
462 // Closures passed to foo will not be able to mutate their closed-over state.
463 fn foo<F: Fn()>(f: F) { }
464
465 // Attempts to mutate closed-over data. Error message reads:
466 // `cannot assign to data in a captured outer variable...`
467 fn mutable() {
468     let mut x = 0u32;
469     foo(|| x = 2);
470 }
471
472 // Attempts to take a mutable reference to closed-over data.  Error message
473 // reads: `cannot borrow data mutably in a captured outer variable...`
474 fn mut_addr() {
475     let mut x = 0u32;
476     foo(|| { let y = &mut x; });
477 }
478 ```
479
480 The problem here is that foo is defined as accepting a parameter of type `Fn`.
481 Closures passed into foo will thus be inferred to be of type `Fn`, meaning that
482 they capture their context immutably.
483
484 If the definition of `foo` is under your control, the simplest solution is to
485 capture the data mutably. This can be done by defining `foo` to take FnMut
486 rather than Fn:
487
488 ```
489 fn foo<F: FnMut()>(f: F) { }
490 ```
491
492 Alternatively, we can consider using the `Cell` and `RefCell` types to achieve
493 interior mutability through a shared reference. Our example's `mutable`
494 function could be redefined as below:
495
496 ```
497 use std::cell::Cell;
498
499 fn foo<F: Fn()>(f: F) { }
500
501 fn mutable() {
502     let x = Cell::new(0u32);
503     foo(|| x.set(2));
504 }
505 ```
506
507 You can read more about cell types in the API documentation:
508
509 https://doc.rust-lang.org/std/cell/
510 "##,
511
512 E0388: r##"
513 E0388 was removed and is no longer issued.
514 "##,
515
516 E0389: r##"
517 An attempt was made to mutate data using a non-mutable reference. This
518 commonly occurs when attempting to assign to a non-mutable reference of a
519 mutable reference (`&(&mut T)`).
520
521 Example of erroneous code:
522
523 ```compile_fail,E0389
524 struct FancyNum {
525     num: u8,
526 }
527
528 fn main() {
529     let mut fancy = FancyNum{ num: 5 };
530     let fancy_ref = &(&mut fancy);
531     fancy_ref.num = 6; // error: cannot assign to data in a `&` reference
532     println!("{}", fancy_ref.num);
533 }
534 ```
535
536 Here, `&mut fancy` is mutable, but `&(&mut fancy)` is not. Creating an
537 immutable reference to a value borrows it immutably. There can be multiple
538 references of type `&(&mut T)` that point to the same value, so they must be
539 immutable to prevent multiple mutable references to the same value.
540
541 To fix this, either remove the outer reference:
542
543 ```
544 struct FancyNum {
545     num: u8,
546 }
547
548 fn main() {
549     let mut fancy = FancyNum{ num: 5 };
550
551     let fancy_ref = &mut fancy;
552     // `fancy_ref` is now &mut FancyNum, rather than &(&mut FancyNum)
553
554     fancy_ref.num = 6; // No error!
555
556     println!("{}", fancy_ref.num);
557 }
558 ```
559
560 Or make the outer reference mutable:
561
562 ```
563 struct FancyNum {
564     num: u8
565 }
566
567 fn main() {
568     let mut fancy = FancyNum{ num: 5 };
569
570     let fancy_ref = &mut (&mut fancy);
571     // `fancy_ref` is now &mut(&mut FancyNum), rather than &(&mut FancyNum)
572
573     fancy_ref.num = 6; // No error!
574
575     println!("{}", fancy_ref.num);
576 }
577 ```
578 "##,
579
580 E0394: r##"
581 A static was referred to by value by another static.
582
583 Erroneous code examples:
584
585 ```compile_fail,E0394
586 static A: u32 = 0;
587 static B: u32 = A; // error: cannot refer to other statics by value, use the
588                    //        address-of operator or a constant instead
589 ```
590
591 A static cannot be referred by value. To fix this issue, either use a
592 constant:
593
594 ```
595 const A: u32 = 0; // `A` is now a constant
596 static B: u32 = A; // ok!
597 ```
598
599 Or refer to `A` by reference:
600
601 ```
602 static A: u32 = 0;
603 static B: &'static u32 = &A; // ok!
604 ```
605 "##,
606
607 E0395: r##"
608 The value assigned to a constant scalar must be known at compile time,
609 which is not the case when comparing raw pointers.
610
611 Erroneous code example:
612
613 ```compile_fail,E0395
614 static FOO: i32 = 42;
615 static BAR: i32 = 42;
616
617 static BAZ: bool = { (&FOO as *const i32) == (&BAR as *const i32) };
618 // error: raw pointers cannot be compared in statics!
619 ```
620
621 The address assigned by the linker to `FOO` and `BAR` may or may not
622 be identical, so the value of `BAZ` can't be determined.
623
624 If you want to do the comparison, please do it at run-time.
625
626 For example:
627
628 ```
629 static FOO: i32 = 42;
630 static BAR: i32 = 42;
631
632 let baz: bool = { (&FOO as *const i32) == (&BAR as *const i32) };
633 // baz isn't a constant expression so it's ok
634 ```
635 "##,
636
637 E0161: r##"
638 A value was moved. However, its size was not known at compile time, and only
639 values of a known size can be moved.
640
641 Erroneous code example:
642
643 ```compile_fail
644 #![feature(box_syntax)]
645
646 fn main() {
647     let array: &[isize] = &[1, 2, 3];
648     let _x: Box<[isize]> = box *array;
649     // error: cannot move a value of type [isize]: the size of [isize] cannot
650     //        be statically determined
651 }
652 ```
653
654 In Rust, you can only move a value when its size is known at compile time.
655
656 To work around this restriction, consider "hiding" the value behind a reference:
657 either `&x` or `&mut x`. Since a reference has a fixed size, this lets you move
658 it around as usual. Example:
659
660 ```
661 #![feature(box_syntax)]
662
663 fn main() {
664     let array: &[isize] = &[1, 2, 3];
665     let _x: Box<&[isize]> = box array; // ok!
666 }
667 ```
668 "##,
669
670 E0396: r##"
671 The value behind a raw pointer can't be determined at compile-time
672 (or even link-time), which means it can't be used in a constant
673 expression. Erroneous code example:
674
675 ```compile_fail,E0396
676 const REG_ADDR: *const u8 = 0x5f3759df as *const u8;
677
678 const VALUE: u8 = unsafe { *REG_ADDR };
679 // error: raw pointers cannot be dereferenced in constants
680 ```
681
682 A possible fix is to dereference your pointer at some point in run-time.
683
684 For example:
685
686 ```
687 const REG_ADDR: *const u8 = 0x5f3759df as *const u8;
688
689 let reg_value = unsafe { *REG_ADDR };
690 ```
691 "##,
692
693 E0492: r##"
694 A borrow of a constant containing interior mutability was attempted. Erroneous
695 code example:
696
697 ```compile_fail,E0492
698 use std::sync::atomic::{AtomicUsize, ATOMIC_USIZE_INIT};
699
700 const A: AtomicUsize = ATOMIC_USIZE_INIT;
701 static B: &'static AtomicUsize = &A;
702 // error: cannot borrow a constant which may contain interior mutability,
703 //        create a static instead
704 ```
705
706 A `const` represents a constant value that should never change. If one takes
707 a `&` reference to the constant, then one is taking a pointer to some memory
708 location containing the value. Normally this is perfectly fine: most values
709 can't be changed via a shared `&` pointer, but interior mutability would allow
710 it. That is, a constant value could be mutated. On the other hand, a `static` is
711 explicitly a single memory location, which can be mutated at will.
712
713 So, in order to solve this error, either use statics which are `Sync`:
714
715 ```
716 use std::sync::atomic::{AtomicUsize, ATOMIC_USIZE_INIT};
717
718 static A: AtomicUsize = ATOMIC_USIZE_INIT;
719 static B: &'static AtomicUsize = &A; // ok!
720 ```
721
722 You can also have this error while using a cell type:
723
724 ```compile_fail,E0492
725 #![feature(const_cell_new)]
726
727 use std::cell::Cell;
728
729 const A: Cell<usize> = Cell::new(1);
730 const B: &'static Cell<usize> = &A;
731 // error: cannot borrow a constant which may contain interior mutability,
732 //        create a static instead
733
734 // or:
735 struct C { a: Cell<usize> }
736
737 const D: C = C { a: Cell::new(1) };
738 const E: &'static Cell<usize> = &D.a; // error
739
740 // or:
741 const F: &'static C = &D; // error
742 ```
743
744 This is because cell types do operations that are not thread-safe. Due to this,
745 they don't implement Sync and thus can't be placed in statics. In this
746 case, `StaticMutex` would work just fine, but it isn't stable yet:
747 https://doc.rust-lang.org/nightly/std/sync/struct.StaticMutex.html
748
749 However, if you still wish to use these types, you can achieve this by an unsafe
750 wrapper:
751
752 ```
753 #![feature(const_cell_new)]
754
755 use std::cell::Cell;
756 use std::marker::Sync;
757
758 struct NotThreadSafe<T> {
759     value: Cell<T>,
760 }
761
762 unsafe impl<T> Sync for NotThreadSafe<T> {}
763
764 static A: NotThreadSafe<usize> = NotThreadSafe { value : Cell::new(1) };
765 static B: &'static NotThreadSafe<usize> = &A; // ok!
766 ```
767
768 Remember this solution is unsafe! You will have to ensure that accesses to the
769 cell are synchronized.
770 "##,
771
772 E0494: r##"
773 A reference of an interior static was assigned to another const/static.
774 Erroneous code example:
775
776 ```compile_fail,E0494
777 struct Foo {
778     a: u32
779 }
780
781 static S : Foo = Foo { a : 0 };
782 static A : &'static u32 = &S.a;
783 // error: cannot refer to the interior of another static, use a
784 //        constant instead
785 ```
786
787 The "base" variable has to be a const if you want another static/const variable
788 to refer to one of its fields. Example:
789
790 ```
791 struct Foo {
792     a: u32
793 }
794
795 const S : Foo = Foo { a : 0 };
796 static A : &'static u32 = &S.a; // ok!
797 ```
798 "##,
799
800 E0499: r##"
801 A variable was borrowed as mutable more than once. Erroneous code example:
802
803 ```compile_fail,E0499
804 let mut i = 0;
805 let mut x = &mut i;
806 let mut a = &mut i;
807 // error: cannot borrow `i` as mutable more than once at a time
808 ```
809
810 Please note that in rust, you can either have many immutable references, or one
811 mutable reference. Take a look at
812 https://doc.rust-lang.org/stable/book/references-and-borrowing.html for more
813 information. Example:
814
815
816 ```
817 let mut i = 0;
818 let mut x = &mut i; // ok!
819
820 // or:
821 let mut i = 0;
822 let a = &i; // ok!
823 let b = &i; // still ok!
824 let c = &i; // still ok!
825 ```
826 "##,
827
828 E0500: r##"
829 A borrowed variable was used in another closure. Example of erroneous code:
830
831 ```compile_fail
832 fn you_know_nothing(jon_snow: &mut i32) {
833     let nights_watch = || {
834         *jon_snow = 2;
835     };
836     let starks = || {
837         *jon_snow = 3; // error: closure requires unique access to `jon_snow`
838                        //        but it is already borrowed
839     };
840 }
841 ```
842
843 In here, `jon_snow` is already borrowed by the `nights_watch` closure, so it
844 cannot be borrowed by the `starks` closure at the same time. To fix this issue,
845 you can put the closure in its own scope:
846
847 ```
848 fn you_know_nothing(jon_snow: &mut i32) {
849     {
850         let nights_watch = || {
851             *jon_snow = 2;
852         };
853     } // At this point, `jon_snow` is free.
854     let starks = || {
855         *jon_snow = 3;
856     };
857 }
858 ```
859
860 Or, if the type implements the `Clone` trait, you can clone it between
861 closures:
862
863 ```
864 fn you_know_nothing(jon_snow: &mut i32) {
865     let mut jon_copy = jon_snow.clone();
866     let nights_watch = || {
867         jon_copy = 2;
868     };
869     let starks = || {
870         *jon_snow = 3;
871     };
872 }
873 ```
874 "##,
875
876 E0501: r##"
877 This error indicates that a mutable variable is being used while it is still
878 captured by a closure. Because the closure has borrowed the variable, it is not
879 available for use until the closure goes out of scope.
880
881 Note that a capture will either move or borrow a variable, but in this
882 situation, the closure is borrowing the variable. Take a look at
883 http://rustbyexample.com/fn/closures/capture.html for more information about
884 capturing.
885
886 Example of erroneous code:
887
888 ```compile_fail,E0501
889 fn inside_closure(x: &mut i32) {
890     // Actions which require unique access
891 }
892
893 fn outside_closure(x: &mut i32) {
894     // Actions which require unique access
895 }
896
897 fn foo(a: &mut i32) {
898     let bar = || {
899         inside_closure(a)
900     };
901     outside_closure(a); // error: cannot borrow `*a` as mutable because previous
902                         //        closure requires unique access.
903 }
904 ```
905
906 To fix this error, you can place the closure in its own scope:
907
908 ```
909 fn inside_closure(x: &mut i32) {}
910 fn outside_closure(x: &mut i32) {}
911
912 fn foo(a: &mut i32) {
913     {
914         let bar = || {
915             inside_closure(a)
916         };
917     } // borrow on `a` ends.
918     outside_closure(a); // ok!
919 }
920 ```
921
922 Or you can pass the variable as a parameter to the closure:
923
924 ```
925 fn inside_closure(x: &mut i32) {}
926 fn outside_closure(x: &mut i32) {}
927
928 fn foo(a: &mut i32) {
929     let bar = |s: &mut i32| {
930         inside_closure(s)
931     };
932     outside_closure(a);
933     bar(a);
934 }
935 ```
936
937 It may be possible to define the closure later:
938
939 ```
940 fn inside_closure(x: &mut i32) {}
941 fn outside_closure(x: &mut i32) {}
942
943 fn foo(a: &mut i32) {
944     outside_closure(a);
945     let bar = || {
946         inside_closure(a)
947     };
948 }
949 ```
950 "##,
951
952 E0502: r##"
953 This error indicates that you are trying to borrow a variable as mutable when it
954 has already been borrowed as immutable.
955
956 Example of erroneous code:
957
958 ```compile_fail,E0502
959 fn bar(x: &mut i32) {}
960 fn foo(a: &mut i32) {
961     let ref y = a; // a is borrowed as immutable.
962     bar(a); // error: cannot borrow `*a` as mutable because `a` is also borrowed
963             //        as immutable
964 }
965 ```
966
967 To fix this error, ensure that you don't have any other references to the
968 variable before trying to access it mutably:
969
970 ```
971 fn bar(x: &mut i32) {}
972 fn foo(a: &mut i32) {
973     bar(a);
974     let ref y = a; // ok!
975 }
976 ```
977
978 For more information on the rust ownership system, take a look at
979 https://doc.rust-lang.org/stable/book/references-and-borrowing.html.
980 "##,
981
982 E0503: r##"
983 A value was used after it was mutably borrowed.
984
985 Example of erroneous code:
986
987 ```compile_fail,E0503
988 fn main() {
989     let mut value = 3;
990     // Create a mutable borrow of `value`. This borrow
991     // lives until the end of this function.
992     let _borrow = &mut value;
993     let _sum = value + 1; // error: cannot use `value` because
994                           //        it was mutably borrowed
995 }
996 ```
997
998 In this example, `value` is mutably borrowed by `borrow` and cannot be
999 used to calculate `sum`. This is not possible because this would violate
1000 Rust's mutability rules.
1001
1002 You can fix this error by limiting the scope of the borrow:
1003
1004 ```
1005 fn main() {
1006     let mut value = 3;
1007     // By creating a new block, you can limit the scope
1008     // of the reference.
1009     {
1010         let _borrow = &mut value; // Use `_borrow` inside this block.
1011     }
1012     // The block has ended and with it the borrow.
1013     // You can now use `value` again.
1014     let _sum = value + 1;
1015 }
1016 ```
1017
1018 Or by cloning `value` before borrowing it:
1019
1020 ```
1021 fn main() {
1022     let mut value = 3;
1023     // We clone `value`, creating a copy.
1024     let value_cloned = value.clone();
1025     // The mutable borrow is a reference to `value` and
1026     // not to `value_cloned`...
1027     let _borrow = &mut value;
1028     // ... which means we can still use `value_cloned`,
1029     let _sum = value_cloned + 1;
1030     // even though the borrow only ends here.
1031 }
1032 ```
1033
1034 You can find more information about borrowing in the rust-book:
1035 http://doc.rust-lang.org/stable/book/references-and-borrowing.html
1036 "##,
1037
1038 E0504: r##"
1039 This error occurs when an attempt is made to move a borrowed variable into a
1040 closure.
1041
1042 Example of erroneous code:
1043
1044 ```compile_fail,E0504
1045 struct FancyNum {
1046     num: u8,
1047 }
1048
1049 fn main() {
1050     let fancy_num = FancyNum { num: 5 };
1051     let fancy_ref = &fancy_num;
1052
1053     let x = move || {
1054         println!("child function: {}", fancy_num.num);
1055         // error: cannot move `fancy_num` into closure because it is borrowed
1056     };
1057
1058     x();
1059     println!("main function: {}", fancy_ref.num);
1060 }
1061 ```
1062
1063 Here, `fancy_num` is borrowed by `fancy_ref` and so cannot be moved into
1064 the closure `x`. There is no way to move a value into a closure while it is
1065 borrowed, as that would invalidate the borrow.
1066
1067 If the closure can't outlive the value being moved, try using a reference
1068 rather than moving:
1069
1070 ```
1071 struct FancyNum {
1072     num: u8,
1073 }
1074
1075 fn main() {
1076     let fancy_num = FancyNum { num: 5 };
1077     let fancy_ref = &fancy_num;
1078
1079     let x = move || {
1080         // fancy_ref is usable here because it doesn't move `fancy_num`
1081         println!("child function: {}", fancy_ref.num);
1082     };
1083
1084     x();
1085
1086     println!("main function: {}", fancy_num.num);
1087 }
1088 ```
1089
1090 If the value has to be borrowed and then moved, try limiting the lifetime of
1091 the borrow using a scoped block:
1092
1093 ```
1094 struct FancyNum {
1095     num: u8,
1096 }
1097
1098 fn main() {
1099     let fancy_num = FancyNum { num: 5 };
1100
1101     {
1102         let fancy_ref = &fancy_num;
1103         println!("main function: {}", fancy_ref.num);
1104         // `fancy_ref` goes out of scope here
1105     }
1106
1107     let x = move || {
1108         // `fancy_num` can be moved now (no more references exist)
1109         println!("child function: {}", fancy_num.num);
1110     };
1111
1112     x();
1113 }
1114 ```
1115
1116 If the lifetime of a reference isn't enough, such as in the case of threading,
1117 consider using an `Arc` to create a reference-counted value:
1118
1119 ```
1120 use std::sync::Arc;
1121 use std::thread;
1122
1123 struct FancyNum {
1124     num: u8,
1125 }
1126
1127 fn main() {
1128     let fancy_ref1 = Arc::new(FancyNum { num: 5 });
1129     let fancy_ref2 = fancy_ref1.clone();
1130
1131     let x = thread::spawn(move || {
1132         // `fancy_ref1` can be moved and has a `'static` lifetime
1133         println!("child thread: {}", fancy_ref1.num);
1134     });
1135
1136     x.join().expect("child thread should finish");
1137     println!("main thread: {}", fancy_ref2.num);
1138 }
1139 ```
1140 "##,
1141
1142 E0505: r##"
1143 A value was moved out while it was still borrowed.
1144
1145 Erroneous code example:
1146
1147 ```compile_fail,E0505
1148 struct Value {}
1149
1150 fn eat(val: Value) {}
1151
1152 fn main() {
1153     let x = Value{};
1154     {
1155         let _ref_to_val: &Value = &x;
1156         eat(x);
1157     }
1158 }
1159 ```
1160
1161 Here, the function `eat` takes the ownership of `x`. However,
1162 `x` cannot be moved because it was borrowed to `_ref_to_val`.
1163 To fix that you can do few different things:
1164
1165 * Try to avoid moving the variable.
1166 * Release borrow before move.
1167 * Implement the `Copy` trait on the type.
1168
1169 Examples:
1170
1171 ```
1172 struct Value {}
1173
1174 fn eat(val: &Value) {}
1175
1176 fn main() {
1177     let x = Value{};
1178     {
1179         let _ref_to_val: &Value = &x;
1180         eat(&x); // pass by reference, if it's possible
1181     }
1182 }
1183 ```
1184
1185 Or:
1186
1187 ```
1188 struct Value {}
1189
1190 fn eat(val: Value) {}
1191
1192 fn main() {
1193     let x = Value{};
1194     {
1195         let _ref_to_val: &Value = &x;
1196     }
1197     eat(x); // release borrow and then move it.
1198 }
1199 ```
1200
1201 Or:
1202
1203 ```
1204 #[derive(Clone, Copy)] // implement Copy trait
1205 struct Value {}
1206
1207 fn eat(val: Value) {}
1208
1209 fn main() {
1210     let x = Value{};
1211     {
1212         let _ref_to_val: &Value = &x;
1213         eat(x); // it will be copied here.
1214     }
1215 }
1216 ```
1217
1218 You can find more information about borrowing in the rust-book:
1219 http://doc.rust-lang.org/stable/book/references-and-borrowing.html
1220 "##,
1221
1222 E0506: r##"
1223 This error occurs when an attempt is made to assign to a borrowed value.
1224
1225 Example of erroneous code:
1226
1227 ```compile_fail,E0506
1228 struct FancyNum {
1229     num: u8,
1230 }
1231
1232 fn main() {
1233     let mut fancy_num = FancyNum { num: 5 };
1234     let fancy_ref = &fancy_num;
1235     fancy_num = FancyNum { num: 6 };
1236     // error: cannot assign to `fancy_num` because it is borrowed
1237
1238     println!("Num: {}, Ref: {}", fancy_num.num, fancy_ref.num);
1239 }
1240 ```
1241
1242 Because `fancy_ref` still holds a reference to `fancy_num`, `fancy_num` can't
1243 be assigned to a new value as it would invalidate the reference.
1244
1245 Alternatively, we can move out of `fancy_num` into a second `fancy_num`:
1246
1247 ```
1248 struct FancyNum {
1249     num: u8,
1250 }
1251
1252 fn main() {
1253     let mut fancy_num = FancyNum { num: 5 };
1254     let moved_num = fancy_num;
1255     fancy_num = FancyNum { num: 6 };
1256
1257     println!("Num: {}, Moved num: {}", fancy_num.num, moved_num.num);
1258 }
1259 ```
1260
1261 If the value has to be borrowed, try limiting the lifetime of the borrow using
1262 a scoped block:
1263
1264 ```
1265 struct FancyNum {
1266     num: u8,
1267 }
1268
1269 fn main() {
1270     let mut fancy_num = FancyNum { num: 5 };
1271
1272     {
1273         let fancy_ref = &fancy_num;
1274         println!("Ref: {}", fancy_ref.num);
1275     }
1276
1277     // Works because `fancy_ref` is no longer in scope
1278     fancy_num = FancyNum { num: 6 };
1279     println!("Num: {}", fancy_num.num);
1280 }
1281 ```
1282
1283 Or by moving the reference into a function:
1284
1285 ```
1286 struct FancyNum {
1287     num: u8,
1288 }
1289
1290 fn main() {
1291     let mut fancy_num = FancyNum { num: 5 };
1292
1293     print_fancy_ref(&fancy_num);
1294
1295     // Works because function borrow has ended
1296     fancy_num = FancyNum { num: 6 };
1297     println!("Num: {}", fancy_num.num);
1298 }
1299
1300 fn print_fancy_ref(fancy_ref: &FancyNum){
1301     println!("Ref: {}", fancy_ref.num);
1302 }
1303 ```
1304 "##,
1305
1306 E0507: r##"
1307 You tried to move out of a value which was borrowed. Erroneous code example:
1308
1309 ```compile_fail,E0507
1310 use std::cell::RefCell;
1311
1312 struct TheDarkKnight;
1313
1314 impl TheDarkKnight {
1315     fn nothing_is_true(self) {}
1316 }
1317
1318 fn main() {
1319     let x = RefCell::new(TheDarkKnight);
1320
1321     x.borrow().nothing_is_true(); // error: cannot move out of borrowed content
1322 }
1323 ```
1324
1325 Here, the `nothing_is_true` method takes the ownership of `self`. However,
1326 `self` cannot be moved because `.borrow()` only provides an `&TheDarkKnight`,
1327 which is a borrow of the content owned by the `RefCell`. To fix this error,
1328 you have three choices:
1329
1330 * Try to avoid moving the variable.
1331 * Somehow reclaim the ownership.
1332 * Implement the `Copy` trait on the type.
1333
1334 Examples:
1335
1336 ```
1337 use std::cell::RefCell;
1338
1339 struct TheDarkKnight;
1340
1341 impl TheDarkKnight {
1342     fn nothing_is_true(&self) {} // First case, we don't take ownership
1343 }
1344
1345 fn main() {
1346     let x = RefCell::new(TheDarkKnight);
1347
1348     x.borrow().nothing_is_true(); // ok!
1349 }
1350 ```
1351
1352 Or:
1353
1354 ```
1355 use std::cell::RefCell;
1356
1357 struct TheDarkKnight;
1358
1359 impl TheDarkKnight {
1360     fn nothing_is_true(self) {}
1361 }
1362
1363 fn main() {
1364     let x = RefCell::new(TheDarkKnight);
1365     let x = x.into_inner(); // we get back ownership
1366
1367     x.nothing_is_true(); // ok!
1368 }
1369 ```
1370
1371 Or:
1372
1373 ```
1374 use std::cell::RefCell;
1375
1376 #[derive(Clone, Copy)] // we implement the Copy trait
1377 struct TheDarkKnight;
1378
1379 impl TheDarkKnight {
1380     fn nothing_is_true(self) {}
1381 }
1382
1383 fn main() {
1384     let x = RefCell::new(TheDarkKnight);
1385
1386     x.borrow().nothing_is_true(); // ok!
1387 }
1388 ```
1389
1390 Moving a member out of a mutably borrowed struct will also cause E0507 error:
1391
1392 ```compile_fail,E0507
1393 struct TheDarkKnight;
1394
1395 impl TheDarkKnight {
1396     fn nothing_is_true(self) {}
1397 }
1398
1399 struct Batcave {
1400     knight: TheDarkKnight
1401 }
1402
1403 fn main() {
1404     let mut cave = Batcave {
1405         knight: TheDarkKnight
1406     };
1407     let borrowed = &mut cave;
1408
1409     borrowed.knight.nothing_is_true(); // E0507
1410 }
1411 ```
1412
1413 It is fine only if you put something back. `mem::replace` can be used for that:
1414
1415 ```
1416 # struct TheDarkKnight;
1417 # impl TheDarkKnight { fn nothing_is_true(self) {} }
1418 # struct Batcave { knight: TheDarkKnight }
1419 use std::mem;
1420
1421 let mut cave = Batcave {
1422     knight: TheDarkKnight
1423 };
1424 let borrowed = &mut cave;
1425
1426 mem::replace(&mut borrowed.knight, TheDarkKnight).nothing_is_true(); // ok!
1427 ```
1428
1429 You can find more information about borrowing in the rust-book:
1430 http://doc.rust-lang.org/book/first-edition/references-and-borrowing.html
1431 "##,
1432
1433 E0508: r##"
1434 A value was moved out of a non-copy fixed-size array.
1435
1436 Example of erroneous code:
1437
1438 ```compile_fail,E0508
1439 struct NonCopy;
1440
1441 fn main() {
1442     let array = [NonCopy; 1];
1443     let _value = array[0]; // error: cannot move out of type `[NonCopy; 1]`,
1444                            //        a non-copy fixed-size array
1445 }
1446 ```
1447
1448 The first element was moved out of the array, but this is not
1449 possible because `NonCopy` does not implement the `Copy` trait.
1450
1451 Consider borrowing the element instead of moving it:
1452
1453 ```
1454 struct NonCopy;
1455
1456 fn main() {
1457     let array = [NonCopy; 1];
1458     let _value = &array[0]; // Borrowing is allowed, unlike moving.
1459 }
1460 ```
1461
1462 Alternatively, if your type implements `Clone` and you need to own the value,
1463 consider borrowing and then cloning:
1464
1465 ```
1466 #[derive(Clone)]
1467 struct NonCopy;
1468
1469 fn main() {
1470     let array = [NonCopy; 1];
1471     // Now you can clone the array element.
1472     let _value = array[0].clone();
1473 }
1474 ```
1475 "##,
1476
1477 E0509: r##"
1478 This error occurs when an attempt is made to move out of a value whose type
1479 implements the `Drop` trait.
1480
1481 Example of erroneous code:
1482
1483 ```compile_fail,E0509
1484 struct FancyNum {
1485     num: usize
1486 }
1487
1488 struct DropStruct {
1489     fancy: FancyNum
1490 }
1491
1492 impl Drop for DropStruct {
1493     fn drop(&mut self) {
1494         // Destruct DropStruct, possibly using FancyNum
1495     }
1496 }
1497
1498 fn main() {
1499     let drop_struct = DropStruct{fancy: FancyNum{num: 5}};
1500     let fancy_field = drop_struct.fancy; // Error E0509
1501     println!("Fancy: {}", fancy_field.num);
1502     // implicit call to `drop_struct.drop()` as drop_struct goes out of scope
1503 }
1504 ```
1505
1506 Here, we tried to move a field out of a struct of type `DropStruct` which
1507 implements the `Drop` trait. However, a struct cannot be dropped if one or
1508 more of its fields have been moved.
1509
1510 Structs implementing the `Drop` trait have an implicit destructor that gets
1511 called when they go out of scope. This destructor may use the fields of the
1512 struct, so moving out of the struct could make it impossible to run the
1513 destructor. Therefore, we must think of all values whose type implements the
1514 `Drop` trait as single units whose fields cannot be moved.
1515
1516 This error can be fixed by creating a reference to the fields of a struct,
1517 enum, or tuple using the `ref` keyword:
1518
1519 ```
1520 struct FancyNum {
1521     num: usize
1522 }
1523
1524 struct DropStruct {
1525     fancy: FancyNum
1526 }
1527
1528 impl Drop for DropStruct {
1529     fn drop(&mut self) {
1530         // Destruct DropStruct, possibly using FancyNum
1531     }
1532 }
1533
1534 fn main() {
1535     let drop_struct = DropStruct{fancy: FancyNum{num: 5}};
1536     let ref fancy_field = drop_struct.fancy; // No more errors!
1537     println!("Fancy: {}", fancy_field.num);
1538     // implicit call to `drop_struct.drop()` as drop_struct goes out of scope
1539 }
1540 ```
1541
1542 Note that this technique can also be used in the arms of a match expression:
1543
1544 ```
1545 struct FancyNum {
1546     num: usize
1547 }
1548
1549 enum DropEnum {
1550     Fancy(FancyNum)
1551 }
1552
1553 impl Drop for DropEnum {
1554     fn drop(&mut self) {
1555         // Destruct DropEnum, possibly using FancyNum
1556     }
1557 }
1558
1559 fn main() {
1560     // Creates and enum of type `DropEnum`, which implements `Drop`
1561     let drop_enum = DropEnum::Fancy(FancyNum{num: 10});
1562     match drop_enum {
1563         // Creates a reference to the inside of `DropEnum::Fancy`
1564         DropEnum::Fancy(ref fancy_field) => // No error!
1565             println!("It was fancy-- {}!", fancy_field.num),
1566     }
1567     // implicit call to `drop_enum.drop()` as drop_enum goes out of scope
1568 }
1569 ```
1570 "##,
1571
1572 E0595: r##"
1573 Closures cannot mutate immutable captured variables.
1574
1575 Erroneous code example:
1576
1577 ```compile_fail,E0595
1578 let x = 3; // error: closure cannot assign to immutable local variable `x`
1579 let mut c = || { x += 1 };
1580 ```
1581
1582 Make the variable binding mutable:
1583
1584 ```
1585 let mut x = 3; // ok!
1586 let mut c = || { x += 1 };
1587 ```
1588 "##,
1589
1590 E0596: r##"
1591 This error occurs because you tried to mutably borrow a non-mutable variable.
1592
1593 Example of erroneous code:
1594
1595 ```compile_fail,E0596
1596 let x = 1;
1597 let y = &mut x; // error: cannot borrow mutably
1598 ```
1599
1600 In here, `x` isn't mutable, so when we try to mutably borrow it in `y`, it
1601 fails. To fix this error, you need to make `x` mutable:
1602
1603 ```
1604 let mut x = 1;
1605 let y = &mut x; // ok!
1606 ```
1607 "##,
1608
1609 E0597: r##"
1610 This error occurs because a borrow was made inside a variable which has a
1611 greater lifetime than the borrowed one.
1612
1613 Example of erroneous code:
1614
1615 ```compile_fail,E0597
1616 struct Foo<'a> {
1617     x: Option<&'a u32>,
1618 }
1619
1620 let mut x = Foo { x: None };
1621 let y = 0;
1622 x.x = Some(&y); // error: `y` does not live long enough
1623 ```
1624
1625 In here, `x` is created before `y` and therefore has a greater lifetime. Always
1626 keep in mind that values in a scope are dropped in the opposite order they are
1627 created. So to fix the previous example, just make the `y` lifetime greater than
1628 the `x`'s one:
1629
1630 ```
1631 struct Foo<'a> {
1632     x: Option<&'a u32>,
1633 }
1634
1635 let y = 0;
1636 let mut x = Foo { x: None };
1637 x.x = Some(&y);
1638 ```
1639 "##,
1640
1641 E0626: r##"
1642 This error occurs because a borrow in a generator persists across a
1643 yield point.
1644
1645 ```compile_fail,E0626
1646 # #![feature(generators, generator_trait)]
1647 # use std::ops::Generator;
1648 let mut b = || {
1649     let a = &String::new(); // <-- This borrow...
1650     yield (); // ...is still in scope here, when the yield occurs.
1651     println!("{}", a);
1652 };
1653 b.resume();
1654 ```
1655
1656 At present, it is not permitted to have a yield that occurs while a
1657 borrow is still in scope. To resolve this error, the borrow must
1658 either be "contained" to a smaller scope that does not overlap the
1659 yield or else eliminated in another way. So, for example, we might
1660 resolve the previous example by removing the borrow and just storing
1661 the integer by value:
1662
1663 ```
1664 # #![feature(generators, generator_trait)]
1665 # use std::ops::Generator;
1666 let mut b = || {
1667     let a = 3;
1668     yield ();
1669     println!("{}", a);
1670 };
1671 b.resume();
1672 ```
1673
1674 This is a very simple case, of course. In more complex cases, we may
1675 wish to have more than one reference to the value that was borrowed --
1676 in those cases, something like the `Rc` or `Arc` types may be useful.
1677
1678 This error also frequently arises with iteration:
1679
1680 ```compile_fail,E0626
1681 # #![feature(generators, generator_trait)]
1682 # use std::ops::Generator;
1683 let mut b = || {
1684   let v = vec![1,2,3];
1685   for &x in &v { // <-- borrow of `v` is still in scope...
1686     yield x; // ...when this yield occurs.
1687   }
1688 };
1689 b.resume();
1690 ```
1691
1692 Such cases can sometimes be resolved by iterating "by value" (or using
1693 `into_iter()`) to avoid borrowing:
1694
1695 ```
1696 # #![feature(generators, generator_trait)]
1697 # use std::ops::Generator;
1698 let mut b = || {
1699   let v = vec![1,2,3];
1700   for x in v { // <-- Take ownership of the values instead!
1701     yield x; // <-- Now yield is OK.
1702   }
1703 };
1704 b.resume();
1705 ```
1706
1707 If taking ownership is not an option, using indices can work too:
1708
1709 ```
1710 # #![feature(generators, generator_trait)]
1711 # use std::ops::Generator;
1712 let mut b = || {
1713   let v = vec![1,2,3];
1714   let len = v.len(); // (*)
1715   for i in 0..len {
1716     let x = v[i]; // (*)
1717     yield x; // <-- Now yield is OK.
1718   }
1719 };
1720 b.resume();
1721
1722 // (*) -- Unfortunately, these temporaries are currently required.
1723 // See <https://github.com/rust-lang/rust/issues/43122>.
1724 ```
1725 "##,
1726
1727 }
1728
1729 register_diagnostics! {
1730 //    E0385, // {} in an aliasable location
1731     E0493, // destructors cannot be evaluated at compile-time
1732     E0524, // two closures require unique access to `..` at the same time
1733     E0526, // shuffle indices are not constant
1734     E0594, // cannot assign to {}
1735     E0598, // lifetime of {} is too short to guarantee its contents can be...
1736     E0625, // thread-local statics cannot be accessed at compile-time
1737 }