-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Open
Labels
A-lifetimesArea: Lifetimes / regionsArea: Lifetimes / regionsA-type-systemArea: Type systemArea: Type systemC-discussionCategory: Discussion or questions that doesn't represent real issues.Category: Discussion or questions that doesn't represent real issues.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.T-typesRelevant to the types team, which will review and decide on the PR/issue.Relevant to the types team, which will review and decide on the PR/issue.
Description
I tried this code:
// As soon as the next line is uncommented, the code doesn't compile.
// use std::error::Error;
fn main() {
let error = std::fs::File::open("afaf").unwrap_err();
// Using the fully qualified path here is fine
let error: &dyn std::error::Error = &error;
// This line fails to compile with above's `use`
std::iter::successors(Some(error), |sub_error| sub_error.source()).for_each(|error| {
eprintln!("{}", error);
});
}
I expected this to compile the same way regardless of whether use std::error::Error;
is present.
This expectation holds because Error is used later in the code via its fully qualified name (std::error::Error
) only, and the use statement should not affect type inference or lifetime resolution.
Instead, this happened:
When the use
line is present, the compiler reports a lifetime error:
--> src/main.rs:7:52
|
7 | std::iter::successors(Some(error), |sub_error| sub_error.source()).for_each(|error| {
| ---------- ^^^^^^^^^^^^^^^^^^ 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`
Removing the use
line eliminates the error, without changing any other code.
Meta
This bug happens at least with
- Rust stable: 1.87 and 1.58 (across editions!)
- Rust nightly: 1.89.0-nightly (45f256d 2025-05-27)
From a quick research, I didn't find a duplicate of this.
blitzfmease and workingjubilee
Metadata
Metadata
Assignees
Labels
A-lifetimesArea: Lifetimes / regionsArea: Lifetimes / regionsA-type-systemArea: Type systemArea: Type systemC-discussionCategory: Discussion or questions that doesn't represent real issues.Category: Discussion or questions that doesn't represent real issues.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.T-typesRelevant to the types team, which will review and decide on the PR/issue.Relevant to the types team, which will review and decide on the PR/issue.
Type
Projects
Milestone
Relationships
Development
Select code repository
Activity
workingjubilee commentedon May 28, 2025
...??? some extremely niche case of method res?
theemathas commentedon May 28, 2025
theemathas commentedon May 28, 2025
Smaller reproduction:
theemathas commentedon May 28, 2025
The workaround in the original reproducer is to write
(*sub_error).source()
instead ofsub_error.source()
vmm: use Error trait directly with Note for compiler bug
vmm: use Error trait directly with Note for compiler bug
vmm: use Error trait directly with Note for compiler bug
vmm: use Error trait directly with Note for compiler bug
vmm: use Error trait directly with Note for compiler bug
vmm: use Error trait directly with Note for compiler bug
vmm: use Error trait directly with Note for compiler bug