diff options
| author | Niko Matsakis <niko@alum.mit.edu> | 2013-05-16 15:10:50 -0400 |
|---|---|---|
| committer | Niko Matsakis <niko@alum.mit.edu> | 2013-05-16 15:10:50 -0400 |
| commit | e665fdfc8e40dcecd2a9268ecabc4b172c709ed1 (patch) | |
| tree | e45bba43d3b3108d94fbe457b3676c119714ccd2 /src | |
| parent | 87398f31e5615bef744f2ac3f66c00b1a7d4959e (diff) | |
| download | rust-e665fdfc8e40dcecd2a9268ecabc4b172c709ed1.tar.gz rust-e665fdfc8e40dcecd2a9268ecabc4b172c709ed1.zip | |
Add two tests for rvalue mutability
Diffstat (limited to 'src')
| -rw-r--r-- | src/test/compile-fail/borrowck-rvalues-mutable-bad.rs | 38 | ||||
| -rw-r--r-- | src/test/run-pass/borrowck-rvalues-mutable.rs | 30 |
2 files changed, 68 insertions, 0 deletions
diff --git a/src/test/compile-fail/borrowck-rvalues-mutable-bad.rs b/src/test/compile-fail/borrowck-rvalues-mutable-bad.rs new file mode 100644 index 00000000000..10bef907a28 --- /dev/null +++ b/src/test/compile-fail/borrowck-rvalues-mutable-bad.rs @@ -0,0 +1,38 @@ +// Copyright 2012 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// Tests that rvalue lifetimes is limited to the enclosing trans +// cleanup scope. It is unclear that this is the correct lifetime for +// rvalues, but that's what it is right now. + +struct Counter { + value: uint +} + +impl Counter { + fn new(v: uint) -> Counter { + Counter {value: v} + } + + fn inc<'a>(&'a mut self) -> &'a mut Counter { + self.value += 1; + self + } + + fn get(&self) -> uint { + self.value + } +} + +pub fn main() { + let v = Counter::new(22).inc().inc().get(); + //~^ ERROR borrowed value does not live long enough + assert_eq!(v, 24);; +} diff --git a/src/test/run-pass/borrowck-rvalues-mutable.rs b/src/test/run-pass/borrowck-rvalues-mutable.rs new file mode 100644 index 00000000000..cf5a9341c9d --- /dev/null +++ b/src/test/run-pass/borrowck-rvalues-mutable.rs @@ -0,0 +1,30 @@ +// Copyright 2012 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +struct Counter { + value: uint +} + +impl Counter { + fn new(v: uint) -> Counter { + Counter {value: v} + } + + fn get_and_inc(&mut self) -> uint { + let v = self.value; + self.value += 1; + v + } +} + +pub fn main() { + let v = Counter::new(22).get_and_inc(); + assert_eq!(v, 22); +} |
