Skip to content

Generators should be able to yield references to local variables #98740

Open
@M1cha

Description

@M1cha

Consider this:

let mut generator = || {
    let mut buf = [0u8; 4];
    yield &buf[..];
    
    buf[0] = 0;
    yield &buf[..];
};

Currently, this causes:
error[E0515]: cannot yield value referencing local variable buf

I don't see a reason why the compiler couldn't track that lifetime like it does when returning references to self from synchronous functions. As long as you want to use the return value, you'd simply not be allowed to call resume() since you'd need a reference to the generator for that.

Admittedly it'd currently be impossible (to my knowledge) to write a reference-yielding Generator manually by implementing the trait since you can't specify the lifetimes properly. It might be possible using GAT but afaik that'd require a change to the signature of the Generator trait.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions