Skip to content

bind allows data to be moved multiple times #1261

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

Closed
nikomatsakis opened this issue Dec 6, 2011 · 3 comments
Closed

bind allows data to be moved multiple times #1261

nikomatsakis opened this issue Dec 6, 2011 · 3 comments

Comments

@nikomatsakis
Copy link
Contributor

This code crashes today

// 'x' can have any mode but & or &&
fn foo(-x: ~int) { }
fn main() {
     let b = bind foo(~3);
     b();
     b();
}

The error is that bind needs to "re-copy" the ~3 each time b() is invoked, but it doesn't. In other words, that bind code is equivalent to let x = ~3 in fn () -> foo(x) (in some weird O'Caml/Rust hybrid) but it would need to be fn() -> let x = ~3 in foo(x) to be safe.

Not sure the best fix here. One thought is to ensure that bind let b = bind f(E1, ..., En) is syntactic sugar for something like:

let b = {
    let v1 = E1;
    ...
    let vN = En;
    lambda() { f(v1, ..., vN) }
}

Similarly, upvars in lambdas should have reference mode (referencing the environment, not creator stack frame) which would prevent moves.

@marijnh
Copy link
Contributor

marijnh commented Dec 7, 2011

I think disallowing binding of by-move or by-copy arguments is the sanest solution to this.

@marijnh marijnh closed this as completed in 03a6e54 Dec 7, 2011
@marijnh
Copy link
Contributor

marijnh commented Dec 7, 2011

@nikomatsakis Feel free to reopen if you feel this solution is unsatisfactory.

@nikomatsakis
Copy link
Contributor Author

Yes, that's fine, as long as lambdas also prevent moving out of upvars (and I just tested and found that they do). That is what I meant, not sure why I phrased it in such a complicated way. I guess I wanted to know if there were other corner cases I was not thinking of.

coastalwhite pushed a commit to coastalwhite/rust that referenced this issue Aug 5, 2023
celinval pushed a commit to celinval/rust-dev that referenced this issue Jun 4, 2024
Kobzol pushed a commit to Kobzol/rust that referenced this issue Dec 30, 2024
bors pushed a commit to rust-lang-ci/rust that referenced this issue Jan 2, 2025
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

No branches or pull requests

2 participants