Skip to content

bind allows data to be moved multiple times #1261

@nikomatsakis

Description

@nikomatsakis
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.

Activity

marijnh

marijnh commented on Dec 7, 2011

@marijnh
Contributor

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

marijnh

marijnh commented on Dec 7, 2011

@marijnh
Contributor

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

nikomatsakis

nikomatsakis commented on Dec 7, 2011

@nikomatsakis
ContributorAuthor

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.

added a commit that references this issue on Aug 5, 2023
added a commit that references this issue on Jun 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @marijnh@nikomatsakis

        Issue actions

          bind allows data to be moved multiple times · Issue #1261 · rust-lang/rust