|
| 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 | +// Tests that two closures cannot simultaneously have mutable |
| 12 | +// and immutable access to the variable. Issue #6801. |
| 13 | + |
| 14 | +fn get(x: &int) -> int { |
| 15 | + *x |
| 16 | +} |
| 17 | + |
| 18 | +fn set(x: &mut int) { |
| 19 | + *x = 4; |
| 20 | +} |
| 21 | + |
| 22 | +fn a() { |
| 23 | + let mut x = 3; |
| 24 | + let c1 = || x = 4; |
| 25 | + let c2 = || x * 5; //~ ERROR cannot borrow `x` |
| 26 | +} |
| 27 | + |
| 28 | +fn b() { |
| 29 | + let mut x = 3; |
| 30 | + let c1 = || set(&mut x); |
| 31 | + let c2 = || get(&x); //~ ERROR cannot borrow `x` |
| 32 | +} |
| 33 | + |
| 34 | +fn c() { |
| 35 | + let mut x = 3; |
| 36 | + let c1 = || set(&mut x); |
| 37 | + let c2 = || x * 5; //~ ERROR cannot borrow `x` |
| 38 | +} |
| 39 | + |
| 40 | +fn d() { |
| 41 | + let mut x = 3; |
| 42 | + let c2 = || x * 5; |
| 43 | + x = 5; //~ ERROR cannot assign |
| 44 | +} |
| 45 | + |
| 46 | +fn e() { |
| 47 | + let mut x = 3; |
| 48 | + let c1 = || get(&x); |
| 49 | + x = 5; //~ ERROR cannot assign |
| 50 | +} |
| 51 | + |
| 52 | +fn f() { |
| 53 | + let mut x = ~3; |
| 54 | + let c1 = || get(&*x); |
| 55 | + *x = 5; //~ ERROR cannot assign |
| 56 | +} |
| 57 | + |
| 58 | +fn g() { |
| 59 | + struct Foo { |
| 60 | + f: ~int |
| 61 | + } |
| 62 | + |
| 63 | + let mut x = ~Foo { f: ~3 }; |
| 64 | + let c1 = || get(&*x.f); |
| 65 | + *x.f = 5; //~ ERROR cannot assign to `*x.f` |
| 66 | +} |
| 67 | + |
| 68 | +fn h() { |
| 69 | + struct Foo { |
| 70 | + f: ~int |
| 71 | + } |
| 72 | + |
| 73 | + let mut x = ~Foo { f: ~3 }; |
| 74 | + let c1 = || get(&*x.f); |
| 75 | + let c2 = || *x.f = 5; //~ ERROR cannot borrow `x` as mutable |
| 76 | +} |
| 77 | + |
| 78 | +fn main() { |
| 79 | +} |
0 commit comments