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

[Linter] Checks for explicit self-assignments. #17124

Merged
merged 6 commits into from
Sep 30, 2024

Conversation

tx-tomcat
Copy link
Contributor

@tx-tomcat tx-tomcat commented Apr 11, 2024

Description

This code implements a linter check to detect self-assignments in Move code.
The check focuses on two types of expressions:

  1. Mutate expressions (*a = b)
  2. Assign expressions (a = b)

In each case, the compiler tracks for the same "memory locations" of the assignment, attempting to track the same local variable. It does not have any sort of aliasing notion nor does it track references in any meaningful way.

Test plan

Added more use case

Release notes

  • Protocol:
  • Nodes (Validators and Full nodes):
  • Indexer:
  • JSON-RPC:
  • GraphQL:
  • CLI: Move lint now warns against unnecessary self-assignments.
  • Rust SDK:

Copy link

vercel bot commented Apr 11, 2024

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

Name Status Preview Comments Updated (UTC)
multisig-toolkit ✅ Ready (Inspect) Visit Preview 💬 Add feedback Sep 30, 2024 9:30pm
sui-core ✅ Ready (Inspect) Visit Preview 💬 Add feedback Sep 30, 2024 9:30pm
sui-docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Sep 30, 2024 9:30pm
sui-kiosk ✅ Ready (Inspect) Visit Preview 💬 Add feedback Sep 30, 2024 9:30pm
sui-typescript-docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Sep 30, 2024 9:30pm

Comment on lines 4 to 5
let x = 5;
let y = 10;
Copy link
Contributor

Choose a reason for hiding this comment

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

Could this be extended to handle, e.g., let (x, y) = (x, y) ?

Choose a reason for hiding this comment

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

let (x, y) = (x, y) does not work in sui move. Tuple only appear in expressions (usually in the return position for a function)

Comment on lines 60 to 62
if let UnannotatedExp_::Copy {
var: sp!(_, rhs), ..
} = &assign_exp.exp.value
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this also handle the Move case?

Choose a reason for hiding this comment

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

refactored

Copy link

vercel bot commented May 31, 2024

@tx-tomcat is attempting to deploy a commit to the Mysten Labs Team on Vercel.

A member of the Team first needs to authorize it.

let diag = diag!(SELF_ASSIGNMENT_DIAG, (loc, msg));
self.env.add_diag(diag);
}
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The function takes three parameters:
&mut self:A mutable reference to the struct this method belongs to.
value_list:A reference to an LValueList, which likely contains the left-hand side of the assignment.
assign_exp: A reference to the right-hand side of the assignment.
loc: A Loc value, probably representing the location of this assignment in the source code.

It first checks if the assign_exp is an ExpList (a list of expressions).
If it is, it then checks for self-assignment by iterating over the left-hand side values (value_list.value) and the right-hand side expressions (rhs_expressions) simultaneously.
For each pair, it checks if:

The left-hand side is a LValue_::Var.
The right-hand side is a Single expression.
The right-hand side expression is either a Copy or Move operation.
The variable being copied or moved on the right side is the same as the variable on the left side.

If all pairs in the assignment satisfy these conditions, is_self_assignment is set to true.

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.

5 participants