Skip to content

Mutably capturing in a closure doesn't count as a borrow #11302

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

Closed
alexcrichton opened this issue Jan 4, 2014 · 4 comments
Closed

Mutably capturing in a closure doesn't count as a borrow #11302

alexcrichton opened this issue Jan 4, 2014 · 4 comments
Milestone

Comments

@alexcrichton
Copy link
Member

The compiler accepts this code when it definitely shouldn't

 fn foo() {                       
     let mut a = ~[];             
     let push = |b: &mut ~[int]| {
         a.push(b[0]);            
     };                           
     push(&mut a);                
 }                                

cc @nikomatsakis

Nominating (although this is probably a dupe of an existing issue).

@pnkfelix
Copy link
Member

pnkfelix commented Jan 9, 2014

Accepted for 1.0 P-backcompat-lang.

@nikomatsakis
Copy link
Contributor

With PR #12158 I get:

Running /home/nmatsakis/versioned/rust-2/build/i686-unknown-linux-gnu/stage2/bin/rustc:
/home/nmatsakis/tmp/issue-11302.rs:6:11: 6:17 error: cannot borrow `a` as mutable more than once at a time
/home/nmatsakis/tmp/issue-11302.rs:6      push(&mut a);                
                                               ^~~~~~
/home/nmatsakis/tmp/issue-11302.rs:3:17: 5:7 note: previous borrow of `a` occurs here due to use in closure; the mutable borrow prevents subsequent moves, borrows, or modification of `a` until the borrow ends
/home/nmatsakis/tmp/issue-11302.rs:3      let push = |b: &mut ~[int]| {
/home/nmatsakis/tmp/issue-11302.rs:4          a.push(b[0]);            
/home/nmatsakis/tmp/issue-11302.rs:5      };                           
/home/nmatsakis/tmp/issue-11302.rs:7:3: 7:3 note: previous borrow ends here
/home/nmatsakis/tmp/issue-11302.rs:1  fn foo() {                       
...
/home/nmatsakis/tmp/issue-11302.rs:7  } 
                                      ^
error: aborting due to previous error

@nikomatsakis
Copy link
Contributor

I won't add a test since I think this is case is well covered by the existing tests I added

@emberian
Copy link
Member

Fixed by #12158

flip1995 pushed a commit to flip1995/rust that referenced this issue Sep 25, 2023
[`extra_unused_type_parameters`]: Fix edge case FP for parameters in where bounds

Generic parameters can end up being used on the left side of where-bounds if they are not directly bound but instead appear nested in some concrete generic type. Therefore, we should walk the left side of where bounds, but only if the bounded type is *not* a generic param, in which case we still need to ignore the bound.

Fixes rust-lang#11302

changelog: [`extra_unused_type_parameters`]: Fix edge case false positive for parameters in where bounds
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants