Skip to content

impl Generator complains about missing named lifetime if yielded expression contains a borrow #44197

Closed
@Arnavion

Description

@Arnavion

... even though the value of the expression does not borrow from anything.

rustc 1.21.0-nightly (c11f689d2 2017-08-29)
binary: rustc
commit-hash: c11f689d2475dd9ab956e881238d5d7b6b485efb
commit-date: 2017-08-29
host: x86_64-pc-windows-msvc
release: 1.21.0-nightly
LLVM version: 4.0

Case 1:

#![feature(conservative_impl_trait, generators, generator_trait)]

use std::ops::{ Generator, GeneratorState };

fn foo(_: &str) -> String {
    String::new()
}

fn bar(baz: String) -> impl Generator<Yield = String, Return = ()> {
    move || {
        yield foo(&baz);
    }
}

fn main() {
    assert_eq!(bar(String::new()).resume(), GeneratorState::Yielded(String::new()));
}

This gives:

error[E0564]: only named lifetimes are allowed in `impl Trait`, but `` was found in the type `[generator@src\main.rs:10:2: 12:3 baz:std::string::String ((), std::string::String, &str, fn(&str) -> std::string::String {foo})]`
 --> src\main.rs:9:24
  |
9 | fn bar(baz: String) -> impl Generator<Yield = String, Return = ()> {
  |                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Workaround:

-        yield foo(&baz);
+        let quux = foo(&baz);
+        yield quux;

Case 2:

#![feature(conservative_impl_trait, generators, generator_trait)]

use std::ops::{ Generator, GeneratorState };

fn foo(_: &str) -> Result<String, ()> {
    Err(())
}

fn bar(baz: String) -> impl Generator<Yield = String, Return = ()> {
    move || {
        if let Ok(quux) = foo(&baz) {
            yield quux;
        }
    }
}

fn main() {
    assert_eq!(bar(String::new()).resume(), GeneratorState::Complete(()));
}

This gives

error[E0564]: only named lifetimes are allowed in `impl Trait`, but `` was found in the type `[generator@src\main.rs:10:2: 14:3 baz:std::string::String ((), std::result::Result<std::string::String, ()>, &str, std::string::String, fn(&str) -> std::result::Result<std::string::String, ()> {foo})]`
 --> src\main.rs:9:24
  |
9 | fn bar(baz: String) -> impl Generator<Yield = String, Return = ()> {
  |                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Workaround:

-        if let Ok(quux) = foo(&baz) {
+        let quux = foo(&baz);
+        if let Ok(quux) = quux {

I assume the borrows in the expressions are the problem because in both cases, adding a + 'static bound to the impl Generator returns the error that baz does not live long enough, even though it does not need to be borrowed past the call to foo(&baz).

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-coroutinesArea: CoroutinesC-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