- 
                Notifications
    
You must be signed in to change notification settings  - Fork 13.9k
 
Temporary lifetime extension for blocks #146098
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
          
     Open
      
      
            dianne
  wants to merge
  6
  commits into
  rust-lang:master
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
dianne:extending-blocks
  
      
      
   
  
    
  
  
  
 
  
      
    base: master
Could not load branches
            
              
  
    Branch not found: {{ refName }}
  
            
                
      Loading
              
            Could not load tags
            
            
              Nothing to show
            
              
  
            
                
      Loading
              
            Are you sure you want to change the base?
            Some commits from the old base branch may be removed from the timeline,
            and old review comments may become outdated.
          
          
      
        
          +335
        
        
          −174
        
        
          
        
      
    
  
  
     Open
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            6 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      db7ea4b
              
                relocate and reindent `record_rvalue_scope_if_borrow_expr`
              
              
                dianne 9ce5268
              
                update comment on `record_rvalue_scope_if_borrow_expr`
              
              
                dianne d706992
              
                add drop order test for destructuring assignments
              
              
                dianne b1f6db7
              
                add test for `&format!(...)` in an `if` expression's block
              
              
                dianne aff997c
              
                add test for `super let` in non-extending `if` blocks
              
              
                dianne e41c711
              
                lifetime extension for blocks
              
              
                dianne 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
    
  
  
    
              
              
        
          
          
            16 changes: 16 additions & 0 deletions
          
          16 
        
  tests/ui/borrowck/format-args-temporary-scopes.e2021.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,16 @@ | ||
| error[E0716]: temporary value dropped while borrowed | ||
| --> $DIR/format-args-temporary-scopes.rs:33:64 | ||
| | | ||
| LL | println!("{:?}{:?}", (), if true { std::convert::identity(&format!("")) } else { "" }); | ||
| | ----------------------------------^^^^^^^^^^^--------------- | ||
| | | | | | ||
| | | | temporary value is freed at the end of this statement | ||
| | | creates a temporary value which is freed while still in use | ||
| | borrow later used here | ||
| | | ||
| = note: consider using a `let` binding to create a longer lived value | ||
| = note: this error originates in the macro `format` (in Nightly builds, run with -Z macro-backtrace for more info) | ||
| 
     | 
||
| error: aborting due to 1 previous error | ||
| 
     | 
||
| For more information about this error, try `rustc --explain E0716`. | 
        
          
          
            39 changes: 26 additions & 13 deletions
          
          39 
        
  tests/ui/borrowck/format-args-temporary-scopes.e2024.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 | 
|---|---|---|
| @@ -1,27 +1,40 @@ | ||
| error[E0716]: temporary value dropped while borrowed | ||
| --> $DIR/format-args-temporary-scopes.rs:13:25 | ||
| --> $DIR/format-args-temporary-scopes.rs:17:48 | ||
| | | ||
| LL | println!("{:?}", { &temp() }); | ||
| | ---^^^^^--- | ||
| | | | | | ||
| | | | temporary value is freed at the end of this statement | ||
| | | creates a temporary value which is freed while still in use | ||
| LL | println!("{:?}", { std::convert::identity(&temp()) }); | ||
| | --------------------------^^^^^^--- | ||
| | | | | | ||
| | | | temporary value is freed at the end of this statement | ||
| | | creates a temporary value which is freed while still in use | ||
| | borrow later used here | ||
| | | ||
| = note: consider using a `let` binding to create a longer lived value | ||
| 
     | 
||
| error[E0716]: temporary value dropped while borrowed | ||
| --> $DIR/format-args-temporary-scopes.rs:19:29 | ||
| --> $DIR/format-args-temporary-scopes.rs:24:52 | ||
| | | ||
| LL | println!("{:?}{:?}", { &temp() }, ()); | ||
| | ---^^^^^--- | ||
| | | | | | ||
| | | | temporary value is freed at the end of this statement | ||
| | | creates a temporary value which is freed while still in use | ||
| LL | println!("{:?}{:?}", { std::convert::identity(&temp()) }, ()); | ||
| | --------------------------^^^^^^--- | ||
| | | | | | ||
| | | | temporary value is freed at the end of this statement | ||
| | | creates a temporary value which is freed while still in use | ||
| | borrow later used here | ||
| | | ||
| = note: consider using a `let` binding to create a longer lived value | ||
| 
     | 
||
| error: aborting due to 2 previous errors | ||
| error[E0716]: temporary value dropped while borrowed | ||
| --> $DIR/format-args-temporary-scopes.rs:33:64 | ||
| | | ||
| LL | println!("{:?}{:?}", (), if true { std::convert::identity(&format!("")) } else { "" }); | ||
| | ------------------------^^^^^^^^^^^- | ||
| | | | | | ||
| | | | temporary value is freed at the end of this statement | ||
| | | creates a temporary value which is freed while still in use | ||
| | borrow later used here | ||
| | | ||
| = note: consider using a `let` binding to create a longer lived value | ||
| = note: this error originates in the macro `format` (in Nightly builds, run with -Z macro-backtrace for more info) | ||
| 
     | 
||
| error: aborting due to 3 previous errors | ||
| 
     | 
||
| For more information about this error, try `rustc --explain E0716`. | 
      
      Oops, something went wrong.
        
    
  
      
      Oops, something went wrong.
        
    
  
  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.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This appears to be inlined from
rust/library/core/src/slice/index.rs
Lines 432 to 439 in 82ae0ee
where previously the storage for the slice pointer returned by
get_offset_len_noubcheckwas dropped before the result of the bounds check1 and now it's dropped after, since it now lives past the execution of theifexpression.Footnotes
See here and here for why that bool lives until then: individual boolean conditions have their scopes overridden so they live to the end of the
ifexpression. I have to assume this is so they can each can have a singleStorageDead, rather than one in each branch following each condition's switch terminator. ↩