Skip to content

Borrow error when yielding with nested generators  #45093

Description

@da-x

Hi, I got some feedback regarding the generator feature that is currently in nightly.

I am testing whether it is possible to yield out of a generator while another generator is in scope, for example:

fn main() {
    let mut generator = || {
        yield 1;

        let mut sub_generator = || {
            yield 2;
        };

        match sub_generator.resume() {
            GeneratorState::Yielded(x) => {
                yield x;
            }
            _ => panic!("unexpected value from resume"),
        }

        yield 3;

        return "foo"
    };

    match generator.resume() {
        GeneratorState::Yielded(1) => {}
        _ => panic!("unexpected value from resume"),
    }
    match generator.resume() {
        GeneratorState::Yielded(2) => {}
        _ => panic!("unexpected value from resume"),
    }
    match generator.resume() {
        GeneratorState::Yielded(3) => {}
        _ => panic!("unexpected value from resume"),
    }
    match generator.resume() {
        GeneratorState::Complete("foo") => {}
        _ => panic!("unexpected value from resume"),
    }
}

However I am getting:

error[E0626]: borrow may still be in use when generator yields
  --> src/main.rs:17:15
   |
17 |         match sub_generator.resume() {
   |               ^^^^^^^^^^^^^
...
24 |         yield 3;
   |         ------- possible yield occurs here

rustc: rustc 1.22.0-nightly (05cbece09 2017-10-06)

I am not sure what borrow this error is talking about.

Is sub_generator not owned?

Is there an intention to support such use cases?

Activity

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

    A-coroutinesArea: CoroutinesA-diagnosticsArea: Messages for errors, warnings, and lintsC-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions