Compiler can suggest #[derive(move Trait)] · Issue #55146 · rust-lang/rust (original) (raw)

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.