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

[move compiler] Report unnecessary mutable references #14216

Merged
merged 12 commits into from
Oct 26, 2023

Conversation

tnowacki
Copy link
Contributor

@tnowacki tnowacki commented Oct 11, 2023

Description

  • Report unused mutable references
  • The system is quite exhaustive and reports even locally created mutable references. It piggy-backs on the borrow checker but globally collects usages instead of keeping it in the abstract state. This is done mostly for simplicity
  • A mutable reference is considered unused if:
    • It is mutated
    • It is passed to a function (since freeze removes any subtyping issues)
    • It is returned
  • Copies share the same abstract reference ID

Test Plan

  • Added new tests

If your changes are not user-facing and not a breaking change, you can skip the following section. Otherwise, please indicate what changed, and then add to the Release Notes section as highlighted during the release process.

Type of Change (Check all that apply)

  • protocol change
  • user-visible impact
  • breaking change for a client SDKs
  • breaking change for FNs (FN binary must upgrade)
  • breaking change for validators or node operators (must upgrade binaries)
  • breaking change for on-chain data layout
  • necessitate either a data wipe or data migration

Release notes

With this change, Move will now suggest switching mutable references &mut to immutable ones & when the reference is not modified, or passed somewhere where the mutability is required.
This can be suppressed for parameters by prefixing the parameter name with an underscore _.

@vercel
Copy link

vercel bot commented Oct 11, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
mysten-ui ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 26, 2023 10:35pm
sui-typescript-docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 26, 2023 10:35pm
3 Ignored Deployments
Name Status Preview Comments Updated (UTC)
explorer ⬜️ Ignored (Inspect) Visit Preview Oct 26, 2023 10:35pm
multisig-toolkit ⬜️ Ignored (Inspect) Visit Preview Oct 26, 2023 10:35pm
sui-kiosk ⬜️ Ignored (Inspect) Visit Preview Oct 26, 2023 10:35pm

@vercel vercel bot temporarily deployed to Preview – mysten-ui October 11, 2023 23:06 Inactive
@vercel vercel bot temporarily deployed to Preview – mysten-ui October 11, 2023 23:29 Inactive
@vercel vercel bot temporarily deployed to Preview – mysten-ui October 11, 2023 23:41 Inactive
@vercel vercel bot temporarily deployed to Preview – mysten-ui October 12, 2023 00:01 Inactive
@tnowacki tnowacki marked this pull request as ready for review October 12, 2023 21:50
@vercel vercel bot temporarily deployed to Preview – mysten-ui October 12, 2023 21:51 Inactive
@vercel vercel bot temporarily deployed to Preview – mysten-ui October 12, 2023 22:54 Inactive
@vercel vercel bot temporarily deployed to Preview – mysten-ui October 12, 2023 23:23 Inactive
@vercel vercel bot temporarily deployed to Preview – mysten-ui October 12, 2023 23:30 Inactive
Copy link
Contributor

@tzakian tzakian left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM just two small nits :)

} = info;
if *is_mut && !*used_mutably {
let msg = "Mutable reference is never used mutably, \
consider switching to an imutable reference '&' instead";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit:

Suggested change
consider switching to an imutable reference '&' instead";
consider switching to an immutable reference '&' instead";

Comment on lines 219 to 226
assert_eq!(exps.len(), 1);
let infos: &mut BTreeMap<ExpBasedID, RefExpInfo> =
&mut RefCell::borrow_mut(&self.mutably_used);
for e in exps {
let info = infos.get_mut(e).unwrap();
assert!(info.param_name.is_none());
info.param_name = Some(name)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Either there shouldn't be an assert here, or assert remains and the for loop should be rewritten to just directly access the zero-th exp so something like

let info = infos.get_mut(&exps[0]).unwrap();
...

@tnowacki tnowacki enabled auto-merge (squash) October 26, 2023 22:35
@vercel vercel bot temporarily deployed to Preview – mysten-ui October 26, 2023 22:35 Inactive
@tnowacki tnowacki merged commit e12d8ea into MystenLabs:main Oct 26, 2023
32 checks passed
jonas-lj pushed a commit to jonas-lj/sui that referenced this pull request Nov 2, 2023
## Description 

- Report unused mutable references
- The system is quite exhaustive and reports even locally created
mutable references. It piggy-backs on the borrow checker but globally
collects usages instead of keeping it in the abstract state. This is
done mostly for simplicity
- A mutable reference is considered unused if:
  - It is mutated
- It is passed to a function (since freeze removes any subtyping issues)
  - It is returned 
- Copies share the same abstract reference ID

## Test Plan 

- Added new tests

---
If your changes are not user-facing and not a breaking change, you can
skip the following section. Otherwise, please indicate what changed, and
then add to the Release Notes section as highlighted during the release
process.

### Type of Change (Check all that apply)

- [ ] protocol change
- [X] user-visible impact
- [ ] breaking change for a client SDKs
- [ ] breaking change for FNs (FN binary must upgrade)
- [ ] breaking change for validators or node operators (must upgrade
binaries)
- [ ] breaking change for on-chain data layout
- [ ] necessitate either a data wipe or data migration

### Release notes

With this change, Move will now suggest switching mutable references
`&mut` to immutable ones `&` when the reference is not modified, or
passed somewhere where the mutability is required.
This can be suppressed for parameters by prefixing the parameter name
with an underscore `_`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants