blob: b25d731680133fcfa2c8ec1dbbf63243f7c99da9 [file] [log] [blame]
Graydon Hoared1affff2012-12-11 01:32:481// 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 Matsakisadb61e32012-05-15 03:32:2911// exec-env:RUST_POISON_ON_FREE=1
12
Patrick Walton206ab892013-05-25 02:35:2913use std::ptr;
14
Patrick Waltond18f7852013-03-07 22:38:3815fn borrow(x: &int, f: &fn(x: &int)) {
Niko Matsakisadb61e32012-05-15 03:32:2916 let before = *x;
17 f(x);
18 let after = *x;
Corey Richardsoncc57ca02013-05-19 02:02:4519 assert_eq!(before, after);
Niko Matsakisadb61e32012-05-15 03:32:2920}
21
Tim Chevalier6d4907a2013-01-26 06:46:3222struct F { f: ~int }
23
Graydon Hoare89c8ef72013-02-02 03:43:1724pub fn main() {
Patrick Waltone3d43e42013-02-25 22:04:3225 let mut x = ~@F{f: ~3};
Brian Andersond1fc2b52012-06-30 23:19:0726 do borrow(x.f) |b_x| {
Corey Richardsoncc57ca02013-05-19 02:02:4527 assert_eq!(*b_x, 3);
28 assert_eq!(ptr::to_unsafe_ptr(&(*x.f)), ptr::to_unsafe_ptr(&(*b_x)));
Tim Chevalier6d4907a2013-01-26 06:46:3229 *x = @F{f: ~4};
Niko Matsakisadb61e32012-05-15 03:32:2930
Huon Wilsone4f75612013-07-16 17:08:0831 info!("ptr::to_unsafe_ptr(*b_x) = %x",
Patrick Waltonb0522a42013-04-22 21:27:3032 ptr::to_unsafe_ptr(&(*b_x)) as uint);
Corey Richardsoncc57ca02013-05-19 02:02:4533 assert_eq!(*b_x, 3);
Patrick Waltonb0522a42013-04-22 21:27:3034 assert!(ptr::to_unsafe_ptr(&(*x.f)) != ptr::to_unsafe_ptr(&(*b_x)));
Niko Matsakisadb61e32012-05-15 03:32:2935 }
Patrick Walton91436882013-02-14 19:47:0036}