From 48cd8c646ace585153d6ed25baccdd8420742e42 Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Sat, 16 Nov 2013 14:35:35 -0800 Subject: [PATCH] More Mut tests --- src/libstd/mutable.rs | 9 +++++++++ src/test/compile-fail/mut-cant-alias.rs | 18 +++++++++++++++++ .../compile-fail/mut-ptr-cant-outlive-ref.rs | 20 +++++++++++++++++++ 3 files changed, 47 insertions(+) create mode 100644 src/test/compile-fail/mut-cant-alias.rs create mode 100644 src/test/compile-fail/mut-ptr-cant-outlive-ref.rs diff --git a/src/libstd/mutable.rs b/src/libstd/mutable.rs index 98177b3cdf57..44efbc149c17 100644 --- a/src/libstd/mutable.rs +++ b/src/libstd/mutable.rs @@ -295,4 +295,13 @@ mod test { let _b = x.borrow(); x.with_mut(|x| *x += 1); } + + #[test] + #[should_fail] + fn discard_doesnt_unborrow() { + let x = Mut::new(0); + let _b = x.borrow(); + let _ = _b; + let _b = x.borrow_mut(); + } } diff --git a/src/test/compile-fail/mut-cant-alias.rs b/src/test/compile-fail/mut-cant-alias.rs new file mode 100644 index 000000000000..8e37f88af820 --- /dev/null +++ b/src/test/compile-fail/mut-cant-alias.rs @@ -0,0 +1,18 @@ +// Copyright 2013 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +use std::mutable::Mut; + +fn main() { + let m = Mut::new(0); + let mut b = m.borrow_mut(); + let b1 = b.get(); + let b2 = b.get(); //~ ERROR cannot borrow `b` as mutable more than once at a time +} diff --git a/src/test/compile-fail/mut-ptr-cant-outlive-ref.rs b/src/test/compile-fail/mut-ptr-cant-outlive-ref.rs new file mode 100644 index 000000000000..0dbba87019aa --- /dev/null +++ b/src/test/compile-fail/mut-ptr-cant-outlive-ref.rs @@ -0,0 +1,20 @@ +// Copyright 2013 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +use std::mutable::Mut; + +fn main() { + let m = Mut::new(0); + let p; + { + let b = m.borrow(); + p = b.get(); //~ ERROR borrowed value does not live long enough + } +}