Skip to content

Lifetime errors on nightly-2019-09-11 but not nightly-2019-09-10 #64433

Closed
@Bunogi

Description

@Bunogi

The code below will compile without warnings on nightly-2019-09-10, but returns a lifetime error on nightly-2019-09-11 and later.

#[derive(Debug)]
struct A<'a> {
    inner: Vec<&'a str>,
}

struct B {}

impl B {
    async fn something_with_a(&mut self, a: A<'_>) -> Result<(), String> {
        println!("{:?}", a);
        Ok(())
    }
}

async fn can_error(some_string: &str) -> Result<(), String> {
    let a = A { inner: vec![some_string, "foo"] };
    let mut b = B {};
    Ok(b.something_with_a(a).await.map(|_| ())?)
}

fn main() {
    futures::executor::block_on(can_error("foo")).unwrap();
}

cargo +nightly-2019-09-10 build compiles the code without warnings, and cargo +nightly-2019-09-11 build gives the following error:

error[E0597]: `b` does not live long enough
  --> src/main.rs:18:8
   |
18 |     Ok(b.something_with_a(a).await.map(|_| ())?)
   |        ^--------------------
   |        |
   |        borrowed value does not live long enough
   |        a temporary with access to the borrow is created here ...
19 | }
   | -
   | |
   | `b` dropped here while still borrowed
   | ... and the borrow might be used here, when that temporary is dropped and runs the destructor for type `impl core::future::future::Future`
   |
   = note: The temporary is part of an expression at the end of a block. Consider forcing this temporary to be dropped sooner, before the block's local variables are dropped. For example, you could save the expression's value in a new local variable `x` and then make `x` be the expression at the end of the block.

error: aborting due to previous error

For more information about this error, try `rustc --explain E0597`.
error: Could not compile `bugreport`.

To learn more, run the command again with --verbose.

Meta

$ rustc +nightly-2019-09-11 --version
rustc 1.39.0-nightly (34e82a7b7 2019-09-10)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-async-awaitArea: Async & AwaitA-lifetimesArea: Lifetimes / regionsAsyncAwait-PolishAsync-await issues that are part of the "polish" areaT-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