Hi rust-dev,

How should these errors be dealt with?

For example, given the following code:


fn takes_an_option(_x: option<int>) {}

fn takes_a_rec(x: @{mut a: option<int>}) {
    takes_an_option(x.a);
}

fn main() {
    let x = @{mut a: some(4)};
    takes_a_rec(x);
}


rustc produces:


test.rs:6:20: 6:23 error: illegal borrow unless pure: creating immutable alias to aliasable, mutable memory
test.rs:6     takes_an_option(x.a);
                              ^~~
test.rs:6:4: 6:19 note: impure due to access to impure function
test.rs:6     takes_an_option(x.a);
              ^~~~~~~~~~~~~~~
error: aborting due to previous error


I can think of 3 ways of fixing the issue:

1. Declaring takes_an_option as pure. This can't be done in a library controlled by someone else, though, and it also can't be done if the function isn't actually pure.

2. Declaring the argument mode for takes_an_option to be ++ (pass by copy). Again this can only be done if the library isn't someone else's, but I can't see any other
downsides apart from more sigil-filled source code.

3. Copying the argument explicitly when invoking the function: takes_an_option(copy a.x);
This doesn't require owning the library.

Are there other ways of fixing the issue?

I also have read that argument modes are being removed. How will that affect this situation? If the single remaining argument mode is by-copy, does that mean this issue will go away?

Thanks
Gareth


_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to