Closed
Description
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)