- 
                Notifications
    You must be signed in to change notification settings 
- Fork 13.9k
2229: Don't move out of drop type #88477
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
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| // edition:2021 | ||
|  | ||
| #![feature(rustc_attrs)] | ||
|  | ||
| // Test that we can't move out of struct that impls `Drop`. | ||
|  | ||
|  | ||
| use std::rc::Rc; | ||
|  | ||
| // Test that we restrict precision when moving not-`Copy` types, if any of the parent paths | ||
| // implement `Drop`. This is to ensure that we don't move out of a type that implements Drop. | ||
| pub fn test1() { | ||
| struct Foo(Rc<i32>); | ||
|  | ||
| impl Drop for Foo { | ||
| fn drop(self: &mut Foo) {} | ||
| } | ||
|  | ||
| let f = Foo(Rc::new(1)); | ||
| let x = #[rustc_capture_analysis] move || { | ||
| //~^ ERROR: attributes on expressions are experimental | ||
| //~| NOTE: see issue #15701 <https://github.com/rust-lang/rust/issues/15701> | ||
| //~| ERROR: First Pass analysis includes: | ||
| //~| ERROR: Min Capture analysis includes: | ||
| println!("{:?}", f.0); | ||
| //~^ NOTE: Capturing f[(0, 0)] -> ImmBorrow | ||
| //~| NOTE: Min Capture f[] -> ByValue | ||
| }; | ||
|  | ||
| x(); | ||
| } | ||
|  | ||
| // Test that we don't restrict precision when moving `Copy` types(i.e. when copying), | ||
| // even if any of the parent paths implement `Drop`. | ||
| fn test2() { | ||
| struct Character { | ||
| hp: u32, | ||
| name: String, | ||
| } | ||
|  | ||
| impl Drop for Character { | ||
| fn drop(&mut self) {} | ||
| } | ||
|  | ||
| let character = Character { hp: 100, name: format!("A") }; | ||
|  | ||
| let c = #[rustc_capture_analysis] move || { | ||
| //~^ ERROR: attributes on expressions are experimental | ||
| //~| NOTE: see issue #15701 <https://github.com/rust-lang/rust/issues/15701> | ||
| //~| ERROR: First Pass analysis includes: | ||
| //~| ERROR: Min Capture analysis includes: | ||
| println!("{}", character.hp) | ||
| //~^ NOTE: Capturing character[(0, 0)] -> ImmBorrow | ||
| //~| NOTE: Min Capture character[(0, 0)] -> ByValue | ||
| }; | ||
|  | ||
| c(); | ||
|  | ||
| println!("{}", character.name); | ||
| } | ||
|  | ||
| fn main() {} | 
        
          
          
            97 changes: 97 additions & 0 deletions
          
          97 
        
  src/test/ui/closures/2229_closure_analysis/issue-88476.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,97 @@ | ||
| error[E0658]: attributes on expressions are experimental | ||
| --> $DIR/issue-88476.rs:20:13 | ||
| | | ||
| LL | let x = #[rustc_capture_analysis] move || { | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | | ||
| = note: see issue #15701 <https://github.com/rust-lang/rust/issues/15701> for more information | ||
| = help: add `#![feature(stmt_expr_attributes)]` to the crate attributes to enable | ||
|  | ||
| error[E0658]: attributes on expressions are experimental | ||
| --> $DIR/issue-88476.rs:47:13 | ||
| | | ||
| LL | let c = #[rustc_capture_analysis] move || { | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | | ||
| = note: see issue #15701 <https://github.com/rust-lang/rust/issues/15701> for more information | ||
| = help: add `#![feature(stmt_expr_attributes)]` to the crate attributes to enable | ||
|  | ||
| error: First Pass analysis includes: | ||
| --> $DIR/issue-88476.rs:20:39 | ||
| | | ||
| LL | let x = #[rustc_capture_analysis] move || { | ||
| | _______________________________________^ | ||
| LL | | | ||
| LL | | | ||
| LL | | | ||
| ... | | ||
| LL | | | ||
| LL | | }; | ||
| | |_____^ | ||
| | | ||
| note: Capturing f[(0, 0)] -> ImmBorrow | ||
| --> $DIR/issue-88476.rs:25:26 | ||
| | | ||
| LL | println!("{:?}", f.0); | ||
| | ^^^ | ||
|  | ||
| error: Min Capture analysis includes: | ||
| --> $DIR/issue-88476.rs:20:39 | ||
| | | ||
| LL | let x = #[rustc_capture_analysis] move || { | ||
| | _______________________________________^ | ||
| LL | | | ||
| LL | | | ||
| LL | | | ||
| ... | | ||
| LL | | | ||
| LL | | }; | ||
| | |_____^ | ||
| | | ||
| note: Min Capture f[] -> ByValue | ||
| --> $DIR/issue-88476.rs:25:26 | ||
| | | ||
| LL | println!("{:?}", f.0); | ||
| | ^^^ | ||
|  | ||
| error: First Pass analysis includes: | ||
| --> $DIR/issue-88476.rs:47:39 | ||
| | | ||
| LL | let c = #[rustc_capture_analysis] move || { | ||
| | _______________________________________^ | ||
| LL | | | ||
| LL | | | ||
| LL | | | ||
| ... | | ||
| LL | | | ||
| LL | | }; | ||
| | |_____^ | ||
| | | ||
| note: Capturing character[(0, 0)] -> ImmBorrow | ||
| --> $DIR/issue-88476.rs:52:24 | ||
| | | ||
| LL | println!("{}", character.hp) | ||
| | ^^^^^^^^^^^^ | ||
|  | ||
| error: Min Capture analysis includes: | ||
| --> $DIR/issue-88476.rs:47:39 | ||
| | | ||
| LL | let c = #[rustc_capture_analysis] move || { | ||
| | _______________________________________^ | ||
| LL | | | ||
| LL | | | ||
| LL | | | ||
| ... | | ||
| LL | | | ||
| LL | | }; | ||
| | |_____^ | ||
| | | ||
| note: Min Capture character[(0, 0)] -> ByValue | ||
| --> $DIR/issue-88476.rs:52:24 | ||
| | | ||
| LL | println!("{}", character.hp) | ||
| | ^^^^^^^^^^^^ | ||
|  | ||
| error: aborting due to 6 previous errors | ||
|  | ||
| For more information about this error, try `rustc --explain E0658`. | 
        
          
          
            47 changes: 47 additions & 0 deletions
          
          47 
        
  src/test/ui/closures/2229_closure_analysis/run_pass/issue-88476.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,47 @@ | ||
| // check-pass | ||
| // edition:2021 | ||
|  | ||
| use std::rc::Rc; | ||
|  | ||
| // Test that we restrict precision when moving not-`Copy` types, if any of the parent paths | ||
| // implement `Drop`. This is to ensure that we don't move out of a type that implements Drop. | ||
| pub fn test1() { | ||
| struct Foo(Rc<i32>); | ||
|  | ||
| impl Drop for Foo { | ||
| fn drop(self: &mut Foo) {} | ||
| } | ||
|  | ||
| let f = Foo(Rc::new(1)); | ||
| let x = move || { | ||
| println!("{:?}", f.0); | ||
| }; | ||
|  | ||
| x(); | ||
| } | ||
|  | ||
|  | ||
| // Test that we don't restrict precision when moving `Copy` types(i.e. when copying), | ||
| // even if any of the parent paths implement `Drop`. | ||
| pub fn test2() { | ||
| struct Character { | ||
| hp: u32, | ||
| name: String, | ||
| } | ||
|  | ||
| impl Drop for Character { | ||
| fn drop(&mut self) {} | ||
| } | ||
|  | ||
| let character = Character { hp: 100, name: format!("A") }; | ||
|  | ||
| let c = move || { | ||
| println!("{}", character.hp) | ||
| }; | ||
|  | ||
| c(); | ||
|  | ||
| println!("{}", character.name); | ||
| } | ||
|  | ||
| fn main() {} | 
  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.
I am a bit surprised that rustfmt isn't complaining about this extra space.
Uh oh!
There was an error while loading. Please reload this page.
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.
I'm surprised
./x.py fmtmade this change 😂