File tree 1 file changed +50
-0
lines changed
1 file changed +50
-0
lines changed Original file line number Diff line number Diff line change
1
+ // Copyright 2012-2013 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 the scope of the pointer returned from `get()` is
12
+ // limited to the deref operation itself, and does not infect the
13
+ // block as a whole.
14
+
15
+ struct Box {
16
+ x : uint
17
+ }
18
+
19
+ impl Box {
20
+ fn get < ' a > ( & ' a self ) -> & ' a uint {
21
+ & self . x
22
+ }
23
+ fn set ( & mut self , x : uint ) {
24
+ self . x = x;
25
+ }
26
+ }
27
+
28
+ fn fun1 ( ) {
29
+ // in the past, borrow checker behaved differently when
30
+ // init and decl of `v` were distinct
31
+ let v;
32
+ let mut box = Box { x : 0 } ;
33
+ box. set ( 22 ) ;
34
+ v = * box. get ( ) ;
35
+ box. set ( v+1 ) ;
36
+ assert_eq ! ( 23 , * box. get( ) ) ;
37
+ }
38
+
39
+ fn fun2 ( ) {
40
+ let mut box = Box { x : 0 } ;
41
+ box. set ( 22 ) ;
42
+ let v = * box. get ( ) ;
43
+ box. set ( v+1 ) ;
44
+ assert_eq ! ( 23 , * box. get( ) ) ;
45
+ }
46
+
47
+ pub fn main ( ) {
48
+ fun1 ( ) ;
49
+ fun2 ( ) ;
50
+ }
You can’t perform that action at this time.
0 commit comments