Skip to content

Compiler can suggest #[derive(move Trait)] #55146

Open
@jonas-schievink

Description

@jonas-schievink

When a custom derive generates a closure, and that closure causes a compiler error because it borrows its environment instead of correctly moving it, rustc suggests to put the move keyword into the #[derive] attribute:

Actual error message I just got:

error[E0373]: closure may outlive the current function, but it borrows `route_kind`, which is owned by the current function                                         
  --> modules/debug/src/lib.rs:19:10                                                                                                                                
   |                                                                                                                                                                
19 | #[derive(FromRequest)]                                                                                                                                         
   |          ^^^^^^^^^^^                                                                                                                                           
   |          |                                                                                                                                                     
   |          `route_kind` is borrowed here                                                                                                                         
   |          may outlive borrowed value `route_kind`                                                                                                               
help: to force the closure to take ownership of `route_kind` (and any other referenced variables), use the `move` keyword                                           
   |                                                                                                                                                                
19 | #[derive(move FromRequest)]                                                                                                                                    
   |          ^^^^^^^^^^^^^^^^                                                                                                                                      
                                                                                                                                                                    
error: aborting due to previous error                                                                                                                               

(this is happening in a rather convoluted production codebase, so unfortunately I don't have a test case yet)

If I'm not mistaken, this can only happen when the custom derive macro outputs incorrect code, so the impact is pretty limited.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-attributesArea: Attributes (`#[…]`, `#![…]`)A-diagnosticsArea: Messages for errors, warnings, and lintsA-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)C-bugCategory: This is a bug.D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.E-needs-mcveCall for participation: This issue has a repro, but needs a Minimal Complete and Verifiable ExampleT-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