]> git.proxmox.com Git - rustc.git/blame - src/librustc_borrowck/diagnostics.rs
New upstream version 1.12.0+dfsg1
[rustc.git] / src / librustc_borrowck / diagnostics.rs
CommitLineData
1a4d82fc 1// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
223e47cc
LB
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
9346a6ac 11#![allow(non_snake_case)]
c34b1796 12
62682a34
SL
13register_long_diagnostics! {
14
c1a9b12d
SL
15E0373: r##"
16This error occurs when an attempt is made to use data captured by a closure,
17when that data may no longer exist. It's most commonly seen when attempting to
18return a closure:
19
5bcae85e 20```compile_fail,E0373
c1a9b12d
SL
21fn foo() -> Box<Fn(u32) -> u32> {
22 let x = 0u32;
23 Box::new(|y| x + y)
24}
25```
26
27Notice that `x` is stack-allocated by `foo()`. By default, Rust captures
28closed-over data by reference. This means that once `foo()` returns, `x` no
7453a54e
SL
29longer exists. An attempt to access `x` within the closure would thus be
30unsafe.
c1a9b12d
SL
31
32Another situation where this might be encountered is when spawning threads:
33
5bcae85e 34```compile_fail,E0373
c1a9b12d
SL
35fn foo() {
36 let x = 0u32;
37 let y = 1u32;
38
39 let thr = std::thread::spawn(|| {
40 x + y
41 });
42}
43```
44
45Since our new thread runs in parallel, the stack frame containing `x` and `y`
46may well have disappeared by the time we try to use them. Even if we call
47`thr.join()` within foo (which blocks until `thr` has completed, ensuring the
48stack frame won't disappear), we will not succeed: the compiler cannot prove
49that this behaviour is safe, and so won't let us do it.
50
51The solution to this problem is usually to switch to using a `move` closure.
52This approach moves (or copies, where possible) data into the closure, rather
53than taking references to it. For example:
54
55```
56fn foo() -> Box<Fn(u32) -> u32> {
57 let x = 0u32;
58 Box::new(move |y| x + y)
59}
60```
61
62Now that the closure has its own copy of the data, there's no need to worry
63about safety.
64"##,
65
62682a34
SL
66E0381: r##"
67It is not allowed to use or capture an uninitialized variable. For example:
68
5bcae85e 69```compile_fail,E0381
62682a34
SL
70fn main() {
71 let x: i32;
72 let y = x; // error, use of possibly uninitialized variable
7453a54e 73}
62682a34
SL
74```
75
76To fix this, ensure that any declared variables are initialized before being
7453a54e
SL
77used. Example:
78
79```
80fn main() {
81 let x: i32 = 0;
82 let y = x; // ok!
83}
84```
c1a9b12d
SL
85"##,
86
e9174d1e
SL
87E0382: r##"
88This error occurs when an attempt is made to use a variable after its contents
89have been moved elsewhere. For example:
90
5bcae85e 91```compile_fail,E0382
e9174d1e
SL
92struct MyStruct { s: u32 }
93
94fn main() {
95 let mut x = MyStruct{ s: 5u32 };
96 let y = x;
97 x.s = 6;
98 println!("{}", x.s);
99}
100```
101
102Since `MyStruct` is a type that is not marked `Copy`, the data gets moved out
103of `x` when we set `y`. This is fundamental to Rust's ownership system: outside
104of workarounds like `Rc`, a value cannot be owned by more than one variable.
105
106If we own the type, the easiest way to address this problem is to implement
107`Copy` and `Clone` on it, as shown below. This allows `y` to copy the
108information in `x`, while leaving the original version owned by `x`. Subsequent
109changes to `x` will not be reflected when accessing `y`.
110
111```
112#[derive(Copy, Clone)]
113struct MyStruct { s: u32 }
114
115fn main() {
116 let mut x = MyStruct{ s: 5u32 };
117 let y = x;
118 x.s = 6;
119 println!("{}", x.s);
120}
121```
122
123Alternatively, if we don't control the struct's definition, or mutable shared
124ownership is truly required, we can use `Rc` and `RefCell`:
125
126```
127use std::cell::RefCell;
128use std::rc::Rc;
129
130struct MyStruct { s: u32 }
131
132fn main() {
133 let mut x = Rc::new(RefCell::new(MyStruct{ s: 5u32 }));
134 let y = x.clone();
135 x.borrow_mut().s = 6;
7453a54e 136 println!("{}", x.borrow().s);
e9174d1e
SL
137}
138```
139
140With this approach, x and y share ownership of the data via the `Rc` (reference
141count type). `RefCell` essentially performs runtime borrow checking: ensuring
142that at most one writer or multiple readers can access the data at any one time.
143
144If you wish to learn more about ownership in Rust, start with the chapter in the
145Book:
146
147https://doc.rust-lang.org/book/ownership.html
148"##,
149
150E0383: r##"
151This error occurs when an attempt is made to partially reinitialize a
152structure that is currently uninitialized.
153
154For example, this can happen when a drop has taken place:
155
3157f602 156```ignore
7453a54e
SL
157struct Foo {
158 a: u32,
159}
160
e9174d1e
SL
161let mut x = Foo { a: 1 };
162drop(x); // `x` is now uninitialized
163x.a = 2; // error, partial reinitialization of uninitialized structure `t`
164```
165
166This error can be fixed by fully reinitializing the structure in question:
167
168```
7453a54e
SL
169struct Foo {
170 a: u32,
171}
172
e9174d1e
SL
173let mut x = Foo { a: 1 };
174drop(x);
175x = Foo { a: 2 };
176```
177"##,
178
c1a9b12d
SL
179E0384: r##"
180This error occurs when an attempt is made to reassign an immutable variable.
181For example:
182
5bcae85e
SL
183```compile_fail,E0384
184fn main() {
c1a9b12d
SL
185 let x = 3;
186 x = 5; // error, reassignment of immutable variable
187}
188```
189
190By default, variables in Rust are immutable. To fix this error, add the keyword
191`mut` after the keyword `let` when declaring the variable. For example:
192
193```
5bcae85e 194fn main() {
c1a9b12d
SL
195 let mut x = 3;
196 x = 5;
197}
198```
e9174d1e
SL
199"##,
200
201E0386: r##"
202This error occurs when an attempt is made to mutate the target of a mutable
203reference stored inside an immutable container.
204
205For example, this can happen when storing a `&mut` inside an immutable `Box`:
206
5bcae85e 207```compile_fail,E0386
e9174d1e
SL
208let mut x: i64 = 1;
209let y: Box<_> = Box::new(&mut x);
210**y = 2; // error, cannot assign to data in an immutable container
211```
212
213This error can be fixed by making the container mutable:
214
215```
216let mut x: i64 = 1;
217let mut y: Box<_> = Box::new(&mut x);
218**y = 2;
219```
220
7453a54e
SL
221It can also be fixed by using a type with interior mutability, such as `Cell`
222or `RefCell`:
e9174d1e
SL
223
224```
7453a54e
SL
225use std::cell::Cell;
226
e9174d1e
SL
227let x: i64 = 1;
228let y: Box<Cell<_>> = Box::new(Cell::new(x));
229y.set(2);
230```
231"##,
232
233E0387: r##"
234This error occurs when an attempt is made to mutate or mutably reference data
235that a closure has captured immutably. Examples of this error are shown below:
236
5bcae85e 237```compile_fail,E0387
e9174d1e
SL
238// Accepts a function or a closure that captures its environment immutably.
239// Closures passed to foo will not be able to mutate their closed-over state.
240fn foo<F: Fn()>(f: F) { }
241
7453a54e 242// Attempts to mutate closed-over data. Error message reads:
e9174d1e
SL
243// `cannot assign to data in a captured outer variable...`
244fn mutable() {
245 let mut x = 0u32;
246 foo(|| x = 2);
247}
248
249// Attempts to take a mutable reference to closed-over data. Error message
250// reads: `cannot borrow data mutably in a captured outer variable...`
251fn mut_addr() {
252 let mut x = 0u32;
253 foo(|| { let y = &mut x; });
254}
255```
256
257The problem here is that foo is defined as accepting a parameter of type `Fn`.
258Closures passed into foo will thus be inferred to be of type `Fn`, meaning that
259they capture their context immutably.
260
261If the definition of `foo` is under your control, the simplest solution is to
262capture the data mutably. This can be done by defining `foo` to take FnMut
263rather than Fn:
264
265```
266fn foo<F: FnMut()>(f: F) { }
267```
268
269Alternatively, we can consider using the `Cell` and `RefCell` types to achieve
7453a54e
SL
270interior mutability through a shared reference. Our example's `mutable`
271function could be redefined as below:
e9174d1e
SL
272
273```
274use std::cell::Cell;
275
7453a54e
SL
276fn foo<F: Fn()>(f: F) { }
277
e9174d1e
SL
278fn mutable() {
279 let x = Cell::new(0u32);
280 foo(|| x.set(2));
281}
282```
283
284You can read more about cell types in the API documentation:
285
286https://doc.rust-lang.org/std/cell/
b039eaaf
SL
287"##,
288
5bcae85e
SL
289E0388: r##"
290A mutable borrow was attempted in a static location.
291
292Erroneous code example:
293
294```compile_fail,E0388
295static X: i32 = 1;
296
297static STATIC_REF: &'static mut i32 = &mut X;
298// error: cannot borrow data mutably in a static location
299
300const CONST_REF: &'static mut i32 = &mut X;
301// error: cannot borrow data mutably in a static location
302```
303
304To fix this error, you have to use constant borrow:
305
306```
307static X: i32 = 1;
308
309static STATIC_REF: &'static i32 = &X;
310```
311"##,
312
a7813a04
XL
313E0389: r##"
314An attempt was made to mutate data using a non-mutable reference. This
315commonly occurs when attempting to assign to a non-mutable reference of a
316mutable reference (`&(&mut T)`).
317
318Example of erroneous code:
319
5bcae85e 320```compile_fail,E0389
a7813a04 321struct FancyNum {
5bcae85e 322 num: u8,
a7813a04
XL
323}
324
325fn main() {
326 let mut fancy = FancyNum{ num: 5 };
327 let fancy_ref = &(&mut fancy);
328 fancy_ref.num = 6; // error: cannot assign to data in a `&` reference
329 println!("{}", fancy_ref.num);
330}
331```
332
333Here, `&mut fancy` is mutable, but `&(&mut fancy)` is not. Creating an
334immutable reference to a value borrows it immutably. There can be multiple
335references of type `&(&mut T)` that point to the same value, so they must be
336immutable to prevent multiple mutable references to the same value.
337
338To fix this, either remove the outer reference:
339
340```
341struct FancyNum {
5bcae85e 342 num: u8,
a7813a04
XL
343}
344
345fn main() {
346 let mut fancy = FancyNum{ num: 5 };
347
348 let fancy_ref = &mut fancy;
349 // `fancy_ref` is now &mut FancyNum, rather than &(&mut FancyNum)
350
351 fancy_ref.num = 6; // No error!
352
353 println!("{}", fancy_ref.num);
354}
355```
356
357Or make the outer reference mutable:
358
359```
360struct FancyNum {
361 num: u8
362}
363
364fn main() {
365 let mut fancy = FancyNum{ num: 5 };
366
367 let fancy_ref = &mut (&mut fancy);
368 // `fancy_ref` is now &mut(&mut FancyNum), rather than &(&mut FancyNum)
369
370 fancy_ref.num = 6; // No error!
371
372 println!("{}", fancy_ref.num);
373}
374```
375"##,
376
b039eaaf
SL
377E0499: r##"
378A variable was borrowed as mutable more than once. Erroneous code example:
379
5bcae85e 380```compile_fail,E0499
b039eaaf
SL
381let mut i = 0;
382let mut x = &mut i;
383let mut a = &mut i;
384// error: cannot borrow `i` as mutable more than once at a time
385```
386
387Please note that in rust, you can either have many immutable references, or one
388mutable reference. Take a look at
389https://doc.rust-lang.org/stable/book/references-and-borrowing.html for more
390information. Example:
391
392
393```
394let mut i = 0;
395let mut x = &mut i; // ok!
396
397// or:
398let mut i = 0;
399let a = &i; // ok!
400let b = &i; // still ok!
401let c = &i; // still ok!
402```
403"##,
62682a34 404
a7813a04
XL
405E0500: r##"
406A borrowed variable was used in another closure. Example of erroneous code:
407
408```compile_fail
409fn you_know_nothing(jon_snow: &mut i32) {
410 let nights_watch = || {
411 *jon_snow = 2;
412 };
413 let starks = || {
414 *jon_snow = 3; // error: closure requires unique access to `jon_snow`
415 // but it is already borrowed
416 };
417}
418```
419
420In here, `jon_snow` is already borrowed by the `nights_watch` closure, so it
421cannot be borrowed by the `starks` closure at the same time. To fix this issue,
422you can put the closure in its own scope:
423
424```
425fn you_know_nothing(jon_snow: &mut i32) {
426 {
427 let nights_watch = || {
428 *jon_snow = 2;
429 };
430 } // At this point, `jon_snow` is free.
431 let starks = || {
432 *jon_snow = 3;
433 };
434}
435```
436
437Or, if the type implements the `Clone` trait, you can clone it between
438closures:
439
440```
441fn you_know_nothing(jon_snow: &mut i32) {
442 let mut jon_copy = jon_snow.clone();
443 let nights_watch = || {
444 jon_copy = 2;
445 };
446 let starks = || {
447 *jon_snow = 3;
448 };
449}
450```
451"##,
452
453E0501: r##"
454This error indicates that a mutable variable is being used while it is still
455captured by a closure. Because the closure has borrowed the variable, it is not
456available for use until the closure goes out of scope.
457
458Note that a capture will either move or borrow a variable, but in this
459situation, the closure is borrowing the variable. Take a look at
460http://rustbyexample.com/fn/closures/capture.html for more information about
461capturing.
462
463Example of erroneous code:
464
5bcae85e 465```compile_fail,E0501
a7813a04
XL
466fn inside_closure(x: &mut i32) {
467 // Actions which require unique access
468}
469
470fn outside_closure(x: &mut i32) {
471 // Actions which require unique access
472}
473
474fn foo(a: &mut i32) {
475 let bar = || {
476 inside_closure(a)
477 };
478 outside_closure(a); // error: cannot borrow `*a` as mutable because previous
479 // closure requires unique access.
480}
481```
482
483To fix this error, you can place the closure in its own scope:
484
485```
486fn inside_closure(x: &mut i32) {}
487fn outside_closure(x: &mut i32) {}
488
489fn foo(a: &mut i32) {
490 {
491 let bar = || {
492 inside_closure(a)
493 };
494 } // borrow on `a` ends.
495 outside_closure(a); // ok!
496}
497```
498
499Or you can pass the variable as a parameter to the closure:
500
501```
502fn inside_closure(x: &mut i32) {}
503fn outside_closure(x: &mut i32) {}
504
505fn foo(a: &mut i32) {
506 let bar = |s: &mut i32| {
507 inside_closure(s)
508 };
509 outside_closure(a);
510 bar(a);
511}
512```
513
514It may be possible to define the closure later:
515
516```
517fn inside_closure(x: &mut i32) {}
518fn outside_closure(x: &mut i32) {}
519
520fn foo(a: &mut i32) {
521 outside_closure(a);
522 let bar = || {
523 inside_closure(a)
524 };
525}
526```
527"##,
528
529E0502: r##"
530This error indicates that you are trying to borrow a variable as mutable when it
531has already been borrowed as immutable.
532
533Example of erroneous code:
534
5bcae85e 535```compile_fail,E0502
a7813a04
XL
536fn bar(x: &mut i32) {}
537fn foo(a: &mut i32) {
538 let ref y = a; // a is borrowed as immutable.
539 bar(a); // error: cannot borrow `*a` as mutable because `a` is also borrowed
540 // as immutable
541}
542```
3157f602 543
a7813a04
XL
544To fix this error, ensure that you don't have any other references to the
545variable before trying to access it mutably:
3157f602 546
a7813a04
XL
547```
548fn bar(x: &mut i32) {}
549fn foo(a: &mut i32) {
550 bar(a);
551 let ref y = a; // ok!
552}
553```
3157f602 554
a7813a04
XL
555For more information on the rust ownership system, take a look at
556https://doc.rust-lang.org/stable/book/references-and-borrowing.html.
557"##,
558
3157f602
XL
559E0503: r##"
560A value was used after it was mutably borrowed.
561
562Example of erroneous code:
563
5bcae85e 564```compile_fail,E0503
3157f602
XL
565fn main() {
566 let mut value = 3;
567 // Create a mutable borrow of `value`. This borrow
568 // lives until the end of this function.
569 let _borrow = &mut value;
570 let _sum = value + 1; // error: cannot use `value` because
571 // it was mutably borrowed
572}
573```
574
575In this example, `value` is mutably borrowed by `borrow` and cannot be
576used to calculate `sum`. This is not possible because this would violate
577Rust's mutability rules.
578
579You can fix this error by limiting the scope of the borrow:
580
581```
582fn main() {
583 let mut value = 3;
584 // By creating a new block, you can limit the scope
585 // of the reference.
586 {
587 let _borrow = &mut value; // Use `_borrow` inside this block.
588 }
589 // The block has ended and with it the borrow.
590 // You can now use `value` again.
591 let _sum = value + 1;
592}
593```
594
595Or by cloning `value` before borrowing it:
596
597```
598fn main() {
599 let mut value = 3;
600 // We clone `value`, creating a copy.
601 let value_cloned = value.clone();
602 // The mutable borrow is a reference to `value` and
603 // not to `value_cloned`...
604 let _borrow = &mut value;
605 // ... which means we can still use `value_cloned`,
606 let _sum = value_cloned + 1;
607 // even though the borrow only ends here.
608}
609```
610
611You can find more information about borrowing in the rust-book:
612http://doc.rust-lang.org/stable/book/references-and-borrowing.html
613"##,
614
a7813a04
XL
615E0504: r##"
616This error occurs when an attempt is made to move a borrowed variable into a
617closure.
618
619Example of erroneous code:
620
5bcae85e 621```compile_fail,E0504
a7813a04 622struct FancyNum {
5bcae85e 623 num: u8,
a7813a04
XL
624}
625
626fn main() {
627 let fancy_num = FancyNum { num: 5 };
628 let fancy_ref = &fancy_num;
629
630 let x = move || {
631 println!("child function: {}", fancy_num.num);
632 // error: cannot move `fancy_num` into closure because it is borrowed
633 };
634
635 x();
636 println!("main function: {}", fancy_ref.num);
637}
638```
639
640Here, `fancy_num` is borrowed by `fancy_ref` and so cannot be moved into
641the closure `x`. There is no way to move a value into a closure while it is
642borrowed, as that would invalidate the borrow.
643
644If the closure can't outlive the value being moved, try using a reference
645rather than moving:
646
647```
648struct FancyNum {
5bcae85e 649 num: u8,
a7813a04
XL
650}
651
652fn main() {
653 let fancy_num = FancyNum { num: 5 };
654 let fancy_ref = &fancy_num;
655
656 let x = move || {
657 // fancy_ref is usable here because it doesn't move `fancy_num`
658 println!("child function: {}", fancy_ref.num);
659 };
660
661 x();
662
663 println!("main function: {}", fancy_num.num);
664}
665```
666
667If the value has to be borrowed and then moved, try limiting the lifetime of
668the borrow using a scoped block:
669
670```
671struct FancyNum {
5bcae85e 672 num: u8,
a7813a04
XL
673}
674
675fn main() {
676 let fancy_num = FancyNum { num: 5 };
677
678 {
679 let fancy_ref = &fancy_num;
680 println!("main function: {}", fancy_ref.num);
681 // `fancy_ref` goes out of scope here
682 }
683
684 let x = move || {
685 // `fancy_num` can be moved now (no more references exist)
686 println!("child function: {}", fancy_num.num);
687 };
688
689 x();
690}
691```
692
693If the lifetime of a reference isn't enough, such as in the case of threading,
694consider using an `Arc` to create a reference-counted value:
695
696```
697use std::sync::Arc;
698use std::thread;
699
700struct FancyNum {
5bcae85e 701 num: u8,
a7813a04
XL
702}
703
704fn main() {
705 let fancy_ref1 = Arc::new(FancyNum { num: 5 });
706 let fancy_ref2 = fancy_ref1.clone();
707
708 let x = thread::spawn(move || {
709 // `fancy_ref1` can be moved and has a `'static` lifetime
710 println!("child thread: {}", fancy_ref1.num);
711 });
712
713 x.join().expect("child thread should finish");
714 println!("main thread: {}", fancy_ref2.num);
715}
716```
717"##,
718
5bcae85e
SL
719E0505: r##"
720A value was moved out while it was still borrowed.
721
722Erroneous code example:
723
724```compile_fail,E0505
725struct Value {}
726
727fn eat(val: Value) {}
728
729fn main() {
730 let x = Value{};
731 {
732 let _ref_to_val: &Value = &x;
733 eat(x);
734 }
735}
736```
737
738Here, the function `eat` takes the ownership of `x`. However,
739`x` cannot be moved because it was borrowed to `_ref_to_val`.
740To fix that you can do few different things:
741
742* Try to avoid moving the variable.
743* Release borrow before move.
744* Implement the `Copy` trait on the type.
745
746Examples:
747
748```
749struct Value {}
750
751fn eat(val: &Value) {}
752
753fn main() {
754 let x = Value{};
755 {
756 let _ref_to_val: &Value = &x;
757 eat(&x); // pass by reference, if it's possible
758 }
759}
760```
761
762Or:
763
764```
765struct Value {}
766
767fn eat(val: Value) {}
768
769fn main() {
770 let x = Value{};
771 {
772 let _ref_to_val: &Value = &x;
773 }
774 eat(x); // release borrow and then move it.
775}
776```
777
778Or:
779
780```
781#[derive(Clone, Copy)] // implement Copy trait
782struct Value {}
783
784fn eat(val: Value) {}
785
786fn main() {
787 let x = Value{};
788 {
789 let _ref_to_val: &Value = &x;
790 eat(x); // it will be copied here.
791 }
792}
793```
794
795You can find more information about borrowing in the rust-book:
796http://doc.rust-lang.org/stable/book/references-and-borrowing.html
797"##,
798
a7813a04
XL
799E0506: r##"
800This error occurs when an attempt is made to assign to a borrowed value.
801
802Example of erroneous code:
803
5bcae85e 804```compile_fail,E0506
a7813a04 805struct FancyNum {
5bcae85e 806 num: u8,
a7813a04
XL
807}
808
809fn main() {
810 let mut fancy_num = FancyNum { num: 5 };
811 let fancy_ref = &fancy_num;
812 fancy_num = FancyNum { num: 6 };
813 // error: cannot assign to `fancy_num` because it is borrowed
814
815 println!("Num: {}, Ref: {}", fancy_num.num, fancy_ref.num);
816}
817```
818
819Because `fancy_ref` still holds a reference to `fancy_num`, `fancy_num` can't
820be assigned to a new value as it would invalidate the reference.
821
822Alternatively, we can move out of `fancy_num` into a second `fancy_num`:
823
824```
825struct FancyNum {
5bcae85e 826 num: u8,
a7813a04
XL
827}
828
829fn main() {
830 let mut fancy_num = FancyNum { num: 5 };
831 let moved_num = fancy_num;
832 fancy_num = FancyNum { num: 6 };
833
834 println!("Num: {}, Moved num: {}", fancy_num.num, moved_num.num);
835}
836```
837
838If the value has to be borrowed, try limiting the lifetime of the borrow using
839a scoped block:
840
841```
842struct FancyNum {
5bcae85e 843 num: u8,
a7813a04
XL
844}
845
846fn main() {
847 let mut fancy_num = FancyNum { num: 5 };
848
849 {
850 let fancy_ref = &fancy_num;
851 println!("Ref: {}", fancy_ref.num);
852 }
853
854 // Works because `fancy_ref` is no longer in scope
855 fancy_num = FancyNum { num: 6 };
856 println!("Num: {}", fancy_num.num);
857}
858```
859
860Or by moving the reference into a function:
861
862```
863struct FancyNum {
5bcae85e 864 num: u8,
a7813a04
XL
865}
866
867fn main() {
868 let mut fancy_num = FancyNum { num: 5 };
869
870 print_fancy_ref(&fancy_num);
871
872 // Works because function borrow has ended
873 fancy_num = FancyNum { num: 6 };
874 println!("Num: {}", fancy_num.num);
875}
876
877fn print_fancy_ref(fancy_ref: &FancyNum){
878 println!("Ref: {}", fancy_ref.num);
879}
880```
881"##,
882
9cc50fc6
SL
883E0507: r##"
884You tried to move out of a value which was borrowed. Erroneous code example:
885
5bcae85e 886```compile_fail,E0507
9cc50fc6
SL
887use std::cell::RefCell;
888
889struct TheDarkKnight;
890
891impl TheDarkKnight {
892 fn nothing_is_true(self) {}
893}
894
895fn main() {
896 let x = RefCell::new(TheDarkKnight);
897
898 x.borrow().nothing_is_true(); // error: cannot move out of borrowed content
899}
900```
901
902Here, the `nothing_is_true` method takes the ownership of `self`. However,
903`self` cannot be moved because `.borrow()` only provides an `&TheDarkKnight`,
904which is a borrow of the content owned by the `RefCell`. To fix this error,
905you have three choices:
906
907* Try to avoid moving the variable.
908* Somehow reclaim the ownership.
909* Implement the `Copy` trait on the type.
910
911Examples:
912
913```
914use std::cell::RefCell;
915
916struct TheDarkKnight;
917
918impl TheDarkKnight {
919 fn nothing_is_true(&self) {} // First case, we don't take ownership
920}
921
922fn main() {
923 let x = RefCell::new(TheDarkKnight);
924
925 x.borrow().nothing_is_true(); // ok!
926}
927```
928
929Or:
930
931```
932use std::cell::RefCell;
933
934struct TheDarkKnight;
935
936impl TheDarkKnight {
937 fn nothing_is_true(self) {}
938}
939
940fn main() {
941 let x = RefCell::new(TheDarkKnight);
942 let x = x.into_inner(); // we get back ownership
943
944 x.nothing_is_true(); // ok!
945}
946```
947
948Or:
949
950```
951use std::cell::RefCell;
952
953#[derive(Clone, Copy)] // we implement the Copy trait
954struct TheDarkKnight;
955
956impl TheDarkKnight {
957 fn nothing_is_true(self) {}
958}
959
960fn main() {
961 let x = RefCell::new(TheDarkKnight);
962
963 x.borrow().nothing_is_true(); // ok!
964}
965```
966
7453a54e
SL
967Moving out of a member of a mutably borrowed struct is fine if you put something
968back. `mem::replace` can be used for that:
969
970```ignore
971struct TheDarkKnight;
972
973impl TheDarkKnight {
974 fn nothing_is_true(self) {}
975}
976
977struct Batcave {
978 knight: TheDarkKnight
979}
980
981fn main() {
982 use std::mem;
983
984 let mut cave = Batcave {
985 knight: TheDarkKnight
986 };
987 let borrowed = &mut cave;
988
989 borrowed.knight.nothing_is_true(); // E0507
990 mem::replace(&mut borrowed.knight, TheDarkKnight).nothing_is_true(); // ok!
991}
992```
993
9cc50fc6
SL
994You can find more information about borrowing in the rust-book:
995http://doc.rust-lang.org/stable/book/references-and-borrowing.html
996"##,
997
3157f602
XL
998E0508: r##"
999A value was moved out of a non-copy fixed-size array.
1000
1001Example of erroneous code:
1002
5bcae85e 1003```compile_fail,E0508
3157f602
XL
1004struct NonCopy;
1005
1006fn main() {
1007 let array = [NonCopy; 1];
1008 let _value = array[0]; // error: cannot move out of type `[NonCopy; 1]`,
1009 // a non-copy fixed-size array
1010}
1011```
1012
1013The first element was moved out of the array, but this is not
1014possible because `NonCopy` does not implement the `Copy` trait.
1015
1016Consider borrowing the element instead of moving it:
1017
1018```
1019struct NonCopy;
1020
1021fn main() {
1022 let array = [NonCopy; 1];
1023 let _value = &array[0]; // Borrowing is allowed, unlike moving.
1024}
1025```
1026
1027Alternatively, if your type implements `Clone` and you need to own the value,
1028consider borrowing and then cloning:
1029
1030```
1031#[derive(Clone)]
1032struct NonCopy;
1033
1034fn main() {
1035 let array = [NonCopy; 1];
1036 // Now you can clone the array element.
1037 let _value = array[0].clone();
1038}
1039```
1040"##,
1041
a7813a04
XL
1042E0509: r##"
1043This error occurs when an attempt is made to move out of a value whose type
1044implements the `Drop` trait.
1045
1046Example of erroneous code:
1047
5bcae85e 1048```compile_fail,E0509
a7813a04
XL
1049struct FancyNum {
1050 num: usize
1051}
1052
1053struct DropStruct {
1054 fancy: FancyNum
1055}
1056
1057impl Drop for DropStruct {
1058 fn drop(&mut self) {
1059 // Destruct DropStruct, possibly using FancyNum
1060 }
1061}
1062
1063fn main() {
1064 let drop_struct = DropStruct{fancy: FancyNum{num: 5}};
1065 let fancy_field = drop_struct.fancy; // Error E0509
1066 println!("Fancy: {}", fancy_field.num);
1067 // implicit call to `drop_struct.drop()` as drop_struct goes out of scope
1068}
1069```
1070
1071Here, we tried to move a field out of a struct of type `DropStruct` which
1072implements the `Drop` trait. However, a struct cannot be dropped if one or
1073more of its fields have been moved.
1074
1075Structs implementing the `Drop` trait have an implicit destructor that gets
1076called when they go out of scope. This destructor may use the fields of the
1077struct, so moving out of the struct could make it impossible to run the
1078destructor. Therefore, we must think of all values whose type implements the
1079`Drop` trait as single units whose fields cannot be moved.
1080
1081This error can be fixed by creating a reference to the fields of a struct,
1082enum, or tuple using the `ref` keyword:
1083
1084```
1085struct FancyNum {
1086 num: usize
1087}
1088
1089struct DropStruct {
1090 fancy: FancyNum
1091}
1092
1093impl Drop for DropStruct {
1094 fn drop(&mut self) {
1095 // Destruct DropStruct, possibly using FancyNum
1096 }
1097}
1098
1099fn main() {
1100 let drop_struct = DropStruct{fancy: FancyNum{num: 5}};
1101 let ref fancy_field = drop_struct.fancy; // No more errors!
1102 println!("Fancy: {}", fancy_field.num);
1103 // implicit call to `drop_struct.drop()` as drop_struct goes out of scope
1104}
1105```
1106
1107Note that this technique can also be used in the arms of a match expression:
1108
1109```
1110struct FancyNum {
1111 num: usize
1112}
1113
1114enum DropEnum {
1115 Fancy(FancyNum)
1116}
1117
1118impl Drop for DropEnum {
1119 fn drop(&mut self) {
1120 // Destruct DropEnum, possibly using FancyNum
1121 }
1122}
1123
1124fn main() {
1125 // Creates and enum of type `DropEnum`, which implements `Drop`
1126 let drop_enum = DropEnum::Fancy(FancyNum{num: 10});
1127 match drop_enum {
1128 // Creates a reference to the inside of `DropEnum::Fancy`
1129 DropEnum::Fancy(ref fancy_field) => // No error!
1130 println!("It was fancy-- {}!", fancy_field.num),
1131 }
1132 // implicit call to `drop_enum.drop()` as drop_enum goes out of scope
1133}
1134```
1135"##,
1136
62682a34
SL
1137}
1138
9346a6ac 1139register_diagnostics! {
62682a34 1140 E0385, // {} in an aliasable location
a7813a04 1141 E0524, // two closures require unique access to `..` at the same time
223e47cc 1142}