-
-
Notifications
You must be signed in to change notification settings - Fork 14.8k
Surprising lifetime inference with closures #31998
Copy link
Copy link
Open
Labels
A-closuresArea: Closures (`|…| { … }`)Area: Closures (`|…| { … }`)A-lifetimesArea: Lifetimes / regionsArea: Lifetimes / regionsC-bugCategory: This is a bug.Category: This is a bug.D-confusingDiagnostics: Confusing error or lint that should be reworked.Diagnostics: Confusing error or lint that should be reworked.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.
Metadata
Metadata
Assignees
Labels
A-closuresArea: Closures (`|…| { … }`)Area: Closures (`|…| { … }`)A-lifetimesArea: Lifetimes / regionsArea: Lifetimes / regionsC-bugCategory: This is a bug.Category: This is a bug.D-confusingDiagnostics: Confusing error or lint that should be reworked.Diagnostics: Confusing error or lint that should be reworked.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.
Type
Fields
Give feedbackNo fields configured for issues without a type.
The following code fails to compile due to a lifetime error: the intent is for the closure to mutably borrow the variable only until it returns, but somehow the lifetime of the borrow gets inferred to last as long as the variable itself.
Either of the commented alternatives makes compilation succeed. The first, making the lifetime signature (semi-)explicit, makes sense; it's odd that simply removing a useless reference to the variable also works, though.
Anyway, while I don't know how the lifetime inference algorithm works, and in particular I don't know whether improving the situation could impact backwards compatibility, it's surprising to me that it can't figure out a reasonable lifetime in this case.