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

FnMut closures are not supported yet #1060

Open
franziskuskiefer opened this issue Oct 28, 2024 · 4 comments
Open

FnMut closures are not supported yet #1060

franziskuskiefer opened this issue Oct 28, 2024 · 4 comments
Labels
bug Something isn't working engine Issue in the engine unsupported-rust Rust code rejected by hax. Unless marked wontfix, we want to support it soon.

Comments

@franziskuskiefer
Copy link
Member

In the code below, the test_fails functions throws errors, while test_works is fine.

struct Test {
    k: Option<u8>,
    y: u8,
}

impl Test {
    fn test_works(&mut self) {
        match self.k.take() {
            Some(k) => {
                self.y = 5;
            },
            None => (),
        }
    }

    fn test_fails(&mut self) {
        self.k.take().map(|k| {
            self.y = 5;
        });
    }
}

Open this code snippet in the playground

@franziskuskiefer franziskuskiefer added bug Something isn't working engine Issue in the engine labels Oct 28, 2024
@maximebuyse
Copy link
Contributor

I think it is reasonable to fail on test_fails and not on test_works because the mutation of self happens inside a closure in test_fails. Those two implementations look similar in the Rust world but if you try to "functionalize" this mutation this means the closure must return the new value of self, but then that would not go well with the closure type expected by map.

@karthikbhargavan
Copy link
Contributor

Does that mean that we do not currently support mutation within map? If so, that would be good to document.

@maximebuyse
Copy link
Contributor

Does that mean that we do not currently support mutation within map? If so, that would be good to document.

Yes, except maybe if the mutation is on the argument of the closure.

@franziskuskiefer franziskuskiefer changed the title Mutation fails on take Mutation in map is not supported yet Nov 11, 2024
@franziskuskiefer franziskuskiefer added the unsupported-rust Rust code rejected by hax. Unless marked wontfix, we want to support it soon. label Nov 11, 2024
@W95Psp W95Psp changed the title Mutation in map is not supported yet FnMut closures are not supported yet Nov 12, 2024
@W95Psp
Copy link
Collaborator

W95Psp commented Nov 12, 2024

Ah yes, thanks @maximebuyse, somehow I missed that closure!
Clearly, we do not support mutable closures (aka FnMut).

Some quick notes on supporting FnMuts:

  • normal closures to FP is great, the capture is a primitive feature of our target languages
  • some of our backend cannot support lambdas
  • in presence of FnMut, the environment becomes something we don't want only to consume but also to mut.
  • calling an FnMut requires the fn to be &mut

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working engine Issue in the engine unsupported-rust Rust code rejected by hax. Unless marked wontfix, we want to support it soon.
Projects
None yet
Development

No branches or pull requests

4 participants