-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Closed
Labels
A-closuresArea: Closures (`|…| { … }`)Area: Closures (`|…| { … }`)A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.Category: An issue proposing an enhancement or a PR with one.D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.Diagnostics: Confusing error or lint; hard to understand for new users.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.
Description
#[derive(Copy, Clone)]
struct Bar;
fn foo(bar: Bar) -> impl FnOnce() -> Bar {
|| bar
}
(playground) main error message is:
error[E0373]: closure may outlive the current function, but it borrows `bar`, which is owned by the current function
--> src/lib.rs:5:5
|
5 | || bar
| ^^ --- `bar` is borrowed here
| |
| may outlive borrowed value `bar`
|
note: closure is returned here
I spent a long time trying to figure out where I was creating a reference until I finally figured out that Copy
types are always captured by reference, even if you move them in the closure. It would be helpful if it mentioned the fact that while this looks like a move it's actually copying out of a reference because the type is Copy
.
ia0
Metadata
Metadata
Assignees
Labels
A-closuresArea: Closures (`|…| { … }`)Area: Closures (`|…| { … }`)A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.Category: An issue proposing an enhancement or a PR with one.D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.Diagnostics: Confusing error or lint; hard to understand for new users.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.