Graydon Hoare | d1affff | 2012-12-11 01:32:48 | [diff] [blame] | 1 | // Copyright 2012 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 | |
Niko Matsakis | adb61e3 | 2012-05-15 03:32:29 | [diff] [blame] | 11 | // exec-env:RUST_POISON_ON_FREE=1 |
| 12 | |
Patrick Walton | 206ab89 | 2013-05-25 02:35:29 | [diff] [blame] | 13 | use std::ptr; |
| 14 | |
Patrick Walton | d18f785 | 2013-03-07 22:38:38 | [diff] [blame] | 15 | fn borrow(x: &int, f: &fn(x: &int)) { |
Niko Matsakis | adb61e3 | 2012-05-15 03:32:29 | [diff] [blame] | 16 | let before = *x; |
| 17 | f(x); |
| 18 | let after = *x; |
Corey Richardson | cc57ca0 | 2013-05-19 02:02:45 | [diff] [blame] | 19 | assert_eq!(before, after); |
Niko Matsakis | adb61e3 | 2012-05-15 03:32:29 | [diff] [blame] | 20 | } |
| 21 | |
Tim Chevalier | 6d4907a | 2013-01-26 06:46:32 | [diff] [blame] | 22 | struct F { f: ~int } |
| 23 | |
Graydon Hoare | 89c8ef7 | 2013-02-02 03:43:17 | [diff] [blame] | 24 | pub fn main() { |
Patrick Walton | e3d43e4 | 2013-02-25 22:04:32 | [diff] [blame] | 25 | let mut x = ~@F{f: ~3}; |
Brian Anderson | d1fc2b5 | 2012-06-30 23:19:07 | [diff] [blame] | 26 | do borrow(x.f) |b_x| { |
Corey Richardson | cc57ca0 | 2013-05-19 02:02:45 | [diff] [blame] | 27 | assert_eq!(*b_x, 3); |
| 28 | assert_eq!(ptr::to_unsafe_ptr(&(*x.f)), ptr::to_unsafe_ptr(&(*b_x))); |
Tim Chevalier | 6d4907a | 2013-01-26 06:46:32 | [diff] [blame] | 29 | *x = @F{f: ~4}; |
Niko Matsakis | adb61e3 | 2012-05-15 03:32:29 | [diff] [blame] | 30 | |
Huon Wilson | e4f7561 | 2013-07-16 17:08:08 | [diff] [blame] | 31 | info!("ptr::to_unsafe_ptr(*b_x) = %x", |
Patrick Walton | b0522a4 | 2013-04-22 21:27:30 | [diff] [blame] | 32 | ptr::to_unsafe_ptr(&(*b_x)) as uint); |
Corey Richardson | cc57ca0 | 2013-05-19 02:02:45 | [diff] [blame] | 33 | assert_eq!(*b_x, 3); |
Patrick Walton | b0522a4 | 2013-04-22 21:27:30 | [diff] [blame] | 34 | assert!(ptr::to_unsafe_ptr(&(*x.f)) != ptr::to_unsafe_ptr(&(*b_x))); |
Niko Matsakis | adb61e3 | 2012-05-15 03:32:29 | [diff] [blame] | 35 | } |
Patrick Walton | 9143688 | 2013-02-14 19:47:00 | [diff] [blame] | 36 | } |