aptos-core
aptos-core copied to clipboard
[Bug][compiler-v2] Difference in compiler v1 and v2 when actively borrowed variable is destroyed
🐛 Bug
Consider the following code:
module 0x8675309::M {
struct R has key, copy, drop { f: u64 }
fun t4(cond: bool, addr: address): bool acquires R {
let r = R { f: 0 };
let f = &borrow_global<R>(addr).f;
let r1; if (cond) r1 = borrow_global<R>(addr) else r1 = &mut r;
R { f: _ } = r;
let res = f == &r1.f;
res
}
}
Compiler v1 - provides the following error:
Error: error[E07006]: ambiguous usage of variable
- ┌─ TEMPFILE:8:22
- │
- 7 │ let r1; if (cond) r1 = borrow_global<R>(addr) else r1 = &mut r;
- │ ------ It is still being borrowed by this reference
- 8 │ R { f: _ } = r;
- │ ^
- │ │
- │ Ambiguous usage of variable 'r'
- │ Try an explicit annotation, e.g. 'move r' or 'copy r'
- │
- = Ambiguous inference of 'move' or 'copy' for a borrowed variable's last usage: A 'move' would invalidate the borrowing reference, but a 'copy' might not be the expected implicit behavior since this the last direct usage of the variable.
But compiler v2 accepts this code (and inserts a copy of the actively borrowed variable, which is then destroyed). Such a silent copy insertion may not be obvious to the user, so we may want to do what compiler v1 does here.