aptos-labs / aptos-labs/aptos-core

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

Open
#17,510 1 comment 0 reactions 0 assignees View on GitHub
bug compiler-v2 stale-exempt
Dominant language
Rust
Stars
6.4k
Forks
3.9k
Avg merge
2d 13h
Merged PRs (30d)
81

Description

# 🐛 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
```

Contributor guide

Open the contributing guide

Research direction

Reproduce the two Move snippets from the issue and compare their compilation results with the reported diagnostic. Clarify which behavior is expected for mutating a variable while reading it on the right-hand side, then identify the compiler path responsible and add coverage showing consistent behavior.

Written by the indexing model from the issue text.

Assessment

Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.