Skip to content

Suboptimal error when moving a Copy type in a closure #58497

Description

@Nemo157
#[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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-closuresArea: Closures (`|…| { … }`)A-diagnosticsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.D-newcomer-roadblockDiagnostics: 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.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions