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

[verifier] Add ID immutability verifier #95

Merged
merged 1 commit into from
Dec 29, 2021
Merged

[verifier] Add ID immutability verifier #95

merged 1 commit into from
Dec 29, 2021

Conversation

lxfind
Copy link
Contributor

@lxfind lxfind commented Dec 27, 2021

Add a verifier to check that we never mut borrow the ID field of a key struct.

@lxfind lxfind requested a review from sblackshear December 27, 2021 18:15
}
}

#[test]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Couple of other test ideas:

  • mutable borrow on id field of struct S { id: ID } is fine
  • mutable borrow on other_id field of struct S has key { id: ID, other_id: ID } is fine

Bytecode::MutBorrowField(field_idx) => {
check(*field_idx)?;
}
Bytecode::MutBorrowFieldGeneric(field_idx) => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we don't actually need to worry about this case. This bytecode will get generated for (e.g.)

struct S<T> { t : T }

fun foo(s: &mut S): &mut T {
  &mut s.t

Since struct_with_key will already stop you from writing a struct with key whose first field is a generic type, I think we're ok.

I don't mind having the MutFieldGeneric case covered anyway, but wanted to point this out (perhaps worth documenting in a comment or something).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

MutBorrowFieldGeneric bytecode is generated as long as the struct is type templated, not necessarily the field borrowing must be type templated.
For example, if we have

struct S<T> has key {
  id: ID,
  v: T
}

fun foo<T>(s: &mut S<T>) {
  s.id == ...

The above code will also generate MutBorrowFieldGeneric bytecode.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Good point! We should probably have a test that exercises an example like this too then.

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