- 
        Couldn't load subscription status. 
- Fork 13.9k
Fix #95079 unhelpful error when missing move in nested closure #99612
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Merged
      
      
    
  
     Merged
                    Changes from all commits
      Commits
    
    
  File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
        
          
          
            14 changes: 14 additions & 0 deletions
          
          14 
        
  src/test/ui/borrowck/issue-95079-missing-move-in-nested-closure.rs
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| fn foo1(s: &str) -> impl Iterator<Item = String> + '_ { | ||
| None.into_iter() | ||
| .flat_map(move |()| s.chars().map(|c| format!("{}{}", c, s))) | ||
| //~^ ERROR captured variable cannot escape `FnMut` closure body | ||
| //~| HELP consider adding 'move' keyword before the nested closure | ||
| } | ||
|  | ||
| fn foo2(s: &str) -> impl Sized + '_ { | ||
| move |()| s.chars().map(|c| format!("{}{}", c, s)) | ||
| //~^ ERROR lifetime may not live long enough | ||
| //~| HELP consider adding 'move' keyword before the nested closure | ||
| } | ||
|  | ||
| fn main() {} | 
        
          
          
            37 changes: 37 additions & 0 deletions
          
          37 
        
  src/test/ui/borrowck/issue-95079-missing-move-in-nested-closure.stderr
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| error: captured variable cannot escape `FnMut` closure body | ||
| --> $DIR/issue-95079-missing-move-in-nested-closure.rs:3:29 | ||
| | | ||
| LL | fn foo1(s: &str) -> impl Iterator<Item = String> + '_ { | ||
| | - variable defined here | ||
| LL | None.into_iter() | ||
| LL | .flat_map(move |()| s.chars().map(|c| format!("{}{}", c, s))) | ||
| | - -^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | | | | ||
| | | returns a reference to a captured variable which escapes the closure body | ||
| | | variable captured here | ||
| | inferred to be a `FnMut` closure | ||
| | | ||
| = note: `FnMut` closures only have access to their captured variables while they are executing... | ||
| = note: ...therefore, they cannot allow references to captured variables to escape | ||
| help: consider adding 'move' keyword before the nested closure | ||
| | | ||
| LL | .flat_map(move |()| s.chars().map(move |c| format!("{}{}", c, s))) | ||
| | ++++ | ||
|  | ||
| error: lifetime may not live long enough | ||
| --> $DIR/issue-95079-missing-move-in-nested-closure.rs:9:15 | ||
| | | ||
| LL | move |()| s.chars().map(|c| format!("{}{}", c, s)) | ||
| | --------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ returning this value requires that `'1` must outlive `'2` | ||
| | | | | ||
| | | return type of closure `Map<Chars<'_>, [closure@$DIR/issue-95079-missing-move-in-nested-closure.rs:9:29: 9:32]>` contains a lifetime `'2` | ||
| | lifetime `'1` represents this closure's body | ||
| | | ||
| = note: closure implements `Fn`, so references to captured variables can't escape the closure | ||
| help: consider adding 'move' keyword before the nested closure | ||
| | | ||
| LL | move |()| s.chars().map(move |c| format!("{}{}", c, s)) | ||
| | ++++ | ||
|  | ||
| error: aborting due to 2 previous errors | ||
|  | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.