Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug][Compiler-v2] V2 does not generate the error mutable ownership violated #13148

Open
rahxephon89 opened this issue May 1, 2024 · 1 comment
Assignees
Labels
bug Something isn't working compiler-v2-stable compiler-v2 stale-exempt Prevents issues from being automatically marked and closed as stale

Comments

@rahxephon89
Copy link
Contributor

🐛 Bug

For the following code:

module 0x8675309::A {
    use std::signer;
    struct T has key {v: u64}

    public fun t0(account: &signer) acquires T {
        let sender = signer::address_of(account);
        let x = borrow_global_mut<T>(sender);
        copy x;
        x = borrow_global_mut<T>(sender);
        copy x;
    }
}

V1 generates the error:

error[E07002]: mutable ownership violated
  ┌─ tests/move_check/translated_ir_tests/move/borrow_tests/borrow_global_bad0.move:9:13
  │
7 │         let x = borrow_global_mut<T>(sender);
  │                 ---------------------------- It is still being mutably borrowed by this reference
8 │         copy x;
9 │         x = borrow_global_mut<T>(sender);
  │             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Invalid borrowing of resource 'T'

While V2 does not generate any errors.

@rahxephon89 rahxephon89 added bug Something isn't working stale-exempt Prevents issues from being automatically marked and closed as stale compiler-v2 labels May 1, 2024
@rahxephon89
Copy link
Contributor Author

This example V1 is overly restricted because copy x is not assigned and used after the second borrow global.

Moreover, for the following program:

module 0x8675309::A {
    use std::signer;
    struct T has key {v: u64}

    public fun t0(account: &signer) acquires T {
        let sender = signer::address_of(account);
        let x = borrow_global_mut<T>(sender);
        let y = copy x;
        x = borrow_global_mut<T>(sender);
        copy y;
    }
}

V2 can correctly generate the following error:

Diagnostics:
error: cannot mutably borrow since mutable references exist
   ┌─ tests/reference-safety/debug-13148.move:9:13
   │
 7 │         let x = borrow_global_mut<T>(sender);
   │                 ---------------------------- previous mutable global borrow
 8 │         let y = copy x;
 9 │         x = borrow_global_mut<T>(sender);
   │         ----^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   │         │   │
   │         │   mutable borrow attempted here
   │         requirement enforced here
10 │         copy y;
   │         ------ conflicting reference `y` used here

IMHO, we can close this issue? @wrwg

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working compiler-v2-stable compiler-v2 stale-exempt Prevents issues from being automatically marked and closed as stale
Projects
Status: Assigned
Development

No branches or pull requests

2 participants