aptos-labs / aptos-core

Aptos is a layer 1 blockchain built to support the widespread use of blockchain through better technology and user experience.
https://aptosfoundation.org
Other
5.97k stars 3.58k forks source link

[Bug][move-compiler-v2] Mutable borrow with copy on RHS of equality check #13912

Open zzjas opened 3 weeks ago

zzjas commented 3 weeks ago

πŸ› Bug

Compiler V1 can compile the following program but compiler V2 will give an error.

To reproduce

Code snippet to reproduce

module 0xCAFE::Module0 {
    public fun function2(var4: u8): bool {
        (&mut (var4) != &mut (copy var4))
    }
}

Stack trace/error message V2 gives:

error: cannot copy local `var4` which is still mutably borrowed
  β”Œβ”€ ~/debug_package/sources/repro.move:3:30
  β”‚
3 β”‚         (&mut (var4) != &mut (copy var4))
  β”‚         ---------------------^^^^^^^^^^^-
  β”‚         β”‚β”‚                   β”‚
  β”‚         β”‚β”‚                   copied here
  β”‚         β”‚previous mutable local borrow
  β”‚         β”‚used by freeze
  β”‚         conflicting reference used here
zzjas commented 2 weeks ago

A variant from #13952 might be related to this:

If we compile the following code with V2:

module 0xCAFE::Module0 {
    public fun function5(var21: bool, var23: bool) {
        let var67 =  (&(var21) != &((var21 || var23)));
    }
}

The bytecode verifier will give MOVELOC_EXISTS_BORROW_ERROR (currently marked as known issue).

If the order of operands is flipped, i.e.:

module 0xCAFE::Module0 {
    public fun function5(var21: bool, var23: bool) {
        let var67 =  (&((var21 || var23)) != &(var21));
    }
}

It will be compiled successfully.

Both can be compiled by V1 successfully.