Skip to content

regression 1.51: lifetime may not live long enough with &impl Error #81460

Closed
@camelid

Description

@camelid

This compiles on stable and beta, but not nightly (1.51):

use std::error::Error;
use std::iter;

fn walk_source_chain(error: &impl Error) {
    for e in iter::successors(error.source(), |e| e.source()) {
        println!("{}", e);
    }
}

Error with nightly-2021-01-27:

error: lifetime may not live long enough
 --> src/lib.rs:5:51
  |
5 |     for e in iter::successors(error.source(), |e| e.source()) {
  |                                                -- ^^^^^^^^^^ returning this value requires that `'1` must outlive `'2`
  |                                                ||
  |                                                |return type of closure is Option<&'2 dyn std::error::Error>
  |                                                has type `&'1 &dyn std::error::Error`

error: aborting due to previous error

Changing the closure parameter pattern to match &e fixes the borrow checker error in nightly.

Originally posted by @mzabaluev in #80949 (comment)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lifetimesArea: Lifetimes / regionsP-highHigh priorityT-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.regression-from-stable-to-nightlyPerformance or correctness regression from stable to nightly.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions