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

Mutability check on function arguments #111

Merged
merged 8 commits into from May 5, 2016

Conversation

@bvssvni
Copy link
Member

commented May 4, 2016

Added a mutability check on function arguments. All function arguments are immutable by default.

  • Mutability information is appended to function name, e.g. foo(mut a, b) is foo(mut,_)
  • Pure functions have no mutability information, e.g. foo(a, b) is just foo
  • Lifetime checker prevents assigning to immutable arguments
  • Lifetime checker prevents calling a function taking a mutable parameter with an immutable argument
  • Added function suggestions when declaration of a function can not be found
  • Added function suggestions when expected different number of arguments to a function

Example:

fn foo(mut a, b) {
    a[0] = clone(b)
}

fn bar(a, b) {
    foo(mut a, b)
}

fn main() {
    a := [4]
    b := 5
    bar(a, b)
}
 --- ERROR --- 
In `source/test.rs`:
Requires `mut a`
7,13:     foo(mut a, b)
7,13:             ^

It is allowed to declare multiple functions with same name using different mutability patterns.

Example:

fn reset_x(pos) -> {
    return [0, clone(pos[1])]
}

fn reset_x(mut pos) {
    pos[0] = 0
}

fn main() {
    pos := [1, 2]
    println(pos)
    println(reset_x(pos))
    reset_x(mut pos)
    println(pos)
}

bvssvni added some commits May 4, 2016

Append mutability information to function name
This allows declaring functions of same name with different mutability
for the arguments.

- Append mutability information in AST
- Append mutability information in lifetime checker
Fixed `mut` in piston_window example
- Report unknown intrinsic
- Fixed converting of named call meta data to AST
Added mutability information for intrinsics
- Fix `is_reference` in lifetime checker

@bvssvni bvssvni merged commit 3ef4838 into PistonDevelopers:master May 5, 2016

@bvssvni bvssvni deleted the bvssvni:mut branch May 5, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
1 participant
You can’t perform that action at this time.