forked from aptos-labs/aptos-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0c7a0ae
commit 50c9b5d
Showing
3 changed files
with
165 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
third_party/move/move-compiler-v2/tests/reference-safety/mut_ref_from_immut.exp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
|
||
Diagnostics: | ||
error: cannot mutably borrow local `s_ref` since immutable references exist | ||
┌─ tests/reference-safety/mut_ref_from_immut.move:11:17 | ||
│ | ||
10 │ public fun f1(s_ref: &S) acquires S { | ||
│ ----- previous local borrow | ||
11 │ let _ = &mut s_ref.g; | ||
│ ^^^^^^^^^^^^ mutable borrow attempted here | ||
|
||
error: cannot mutably borrow local `s` since immutable references exist | ||
┌─ tests/reference-safety/mut_ref_from_immut.move:16:9 | ||
│ | ||
15 │ let s = borrow_global<S>(@0x1); | ||
│ ---------------------- previous global borrow | ||
16 │ &mut s.g; | ||
│ ^^^^^^^^ mutable borrow attempted here | ||
|
||
error: cannot mutably borrow local `s_ref` since immutable references exist | ||
┌─ tests/reference-safety/mut_ref_from_immut.move:23:17 | ||
│ | ||
22 │ let s_ref = &s; | ||
│ -- previous local borrow | ||
23 │ let x = &mut s_ref.g; | ||
│ ^^^^^^^^^^^^ mutable borrow attempted here | ||
|
||
error: cannot mutably borrow local `b_ref` since immutable references exist | ||
┌─ tests/reference-safety/mut_ref_from_immut.move:50:17 | ||
│ | ||
49 │ let b_ref = &a_ref.b; | ||
│ -------- previous field borrow | ||
50 │ let x = &mut b_ref.g; | ||
│ ^^^^^^^^^^^^ mutable borrow attempted here |
64 changes: 64 additions & 0 deletions
64
third_party/move/move-compiler-v2/tests/reference-safety/mut_ref_from_immut.move
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
module 0x42::test{ | ||
|
||
struct S has key, drop{ | ||
g: G, | ||
} | ||
struct G has store, drop{ | ||
u: u64 | ||
} | ||
|
||
public fun f1(s_ref: &S) acquires S { | ||
let _ = &mut s_ref.g; | ||
} | ||
|
||
public fun f2() acquires S { | ||
let s = borrow_global<S>(@0x1); | ||
&mut s.g; | ||
} | ||
|
||
public fun f3(): u64 acquires S { | ||
let g = G {u:2}; | ||
let s = S {g}; | ||
let s_ref = &s; | ||
let x = &mut s_ref.g; | ||
x.u | ||
} | ||
|
||
public fun no_error(): u64 acquires S { | ||
let g = G {u:2}; | ||
let s = S {g}; | ||
let s_ref = &mut s; | ||
let x = &mut s_ref.g; | ||
x.u | ||
} | ||
|
||
|
||
struct A has key, drop{ | ||
b: B, | ||
} | ||
|
||
struct B has store, drop{ | ||
g: G, | ||
} | ||
|
||
public fun f4(): u64 acquires S { | ||
let g = G {u:2}; | ||
let b = B {g}; | ||
let a = A {b}; | ||
let a_ref = &mut a; | ||
let b_ref = &a_ref.b; | ||
let x = &mut b_ref.g; | ||
x.u | ||
} | ||
|
||
public fun no_error_2(): u64 acquires S { | ||
let g = G {u:2}; | ||
let b = B {g}; | ||
let a = A {b}; | ||
let a_ref = &mut a; | ||
let b_ref = &mut a_ref.b; | ||
let x = &mut b_ref.g; | ||
x.u | ||
} | ||
|
||
} |