Skip to content

[Bug] Different compilation results when mutating a variable and using it on RHS at the same time #17510

@zzjas

Description

@zzjas

🐛 Bug

//# publish
module 0xCAFE::Module0 {
    public fun cannot_compile(x: u64) {
        *(&mut x) = x;
    }
}

//# publish
module 0xCAFE::Module1 {
    public fun can_compile(x: u64) {
        *(&mut x) = x + 1;
    }
}

Both functions are mutating a variable and using it on the RHS expression at the same time but the first one can be compiled and the second one can be compiled.

Error message from the first one:

task 0 'publish'. lines 1-6:
Error: compilation errors:
 error: cannot copy local `x` which is still mutably borrowed
  ┌─ TEMPFILE:4:9
  │
4 │         *(&mut x) = x;
  │         ^^^^^^^^^^^^^
  │         ││
  │         │local `x` previously mutably borrowed here
  │         copy attempted here

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions