Skip to content

Generators requiring temporaries live too long #44257

Closed
@alexcrichton

Description

@alexcrichton

This code:

fn foo() {
    use std::ops::{GeneratorState, Generator};

    || {
        let a = {
            let mut f = || {
                if false { yield }
                format!("a")
            };
            let a = loop {
                match f.resume() {
                    GeneratorState::Complete(e) => break e,
                    GeneratorState::Yielded(()) => {}
                }
                yield
            };
            a.len()
        };
        if false { yield }
        return a
    };
}

generates the error:

error[E0597]: `a` does not live long enough
   --> tests/smoke.rs:112:9
    |
111 |             a.len()
    |             - borrow occurs here
112 |         };
    |         ^ `a` dropped here while still borrowed
...
115 |     };
    |     - borrowed value needs to live until here

error: aborting due to previous error

but naively at least I'd expect it to work!

cc @Zoxc

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-coroutinesArea: CoroutinesC-bugCategory: This is a bug.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions