{"action":"created","issue":{"url":"https://api.github.com/repos/rust-lang/rust/issues/19004","labels_url":"https://api.github.com/repos/rust-lang/rust/issues/19004/labels{/name}","comments_url":"https://api.github.com/repos/rust-lang/rust/issues/19004/comments","events_url":"https://api.github.com/repos/rust-lang/rust/issues/19004/events","html_url":"https://github.com/rust-lang/rust/issues/19004","id":48988438,"number":19004,"title":"Closure tries to borrow &mut to entire struct, when only a field is required","user":{"login":"mihneadb","id":643127,"avatar_url":"https://avatars.githubusercontent.com/u/643127?v=3","gravatar_id":"","url":"https://api.github.com/users/mihneadb","html_url":"https://github.com/mihneadb","followers_url":"https://api.github.com/users/mihneadb/followers","following_url":"https://api.github.com/users/mihneadb/following{/other_user}","gists_url":"https://api.github.com/users/mihneadb/gists{/gist_id}","starred_url":"https://api.github.com/users/mihneadb/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mihneadb/subscriptions","organizations_url":"https://api.github.com/users/mihneadb/orgs","repos_url":"https://api.github.com/users/mihneadb/repos","events_url":"https://api.github.com/users/mihneadb/events{/privacy}","received_events_url":"https://api.github.com/users/mihneadb/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/rust-lang/rust/labels/A-closures","name":"A-closures","color":"d7e102"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2014-11-16T14:05:03Z","updated_at":"2015-04-25T05:03:58Z","closed_at":null,"body":"Offending code:\r\n\r\n```rust\r\nstruct Foo {\r\n    b: i32,\r\n    v: Vec<i32>\r\n}\r\n\r\nfn incr(x: &mut i32) {\r\n    *x += 1;\r\n}\r\n\r\nfn main() {\r\n    let mut f = Foo { b: 0, v: vec![1, 2, 3] };\r\n\r\n    f.v.iter().map(|s| {\r\n        incr(&mut f.b);\r\n        println!(\"{}: {}\", f.b, s);\r\n    }).last();\r\n}\r\n```\r\n\r\nError: ```error: cannot borrow `f` as mutable because `f.v` is also borrowed as immutable```\r\n\r\nOn playpen: http://is.gd/PTXhJq\r\n\r\nVersion info:\r\n```\r\nrustc 0.13.0-nightly (d91a015ab 2014-11-14 23:37:27 +0000)\r\nbinary: rustc\r\ncommit-hash: d91a015ab4c538f02fbbc03dfb08193a381c2e3b\r\ncommit-date: 2014-11-14 23:37:27 +0000\r\nhost: x86_64-unknown-linux-gnu\r\nrelease: 0.13.0-nightly\r\n```\r\n\r\nThanks @bjz for helping me submit this."},"comment":{"url":"https://api.github.com/repos/rust-lang/rust/issues/comments/96131494","html_url":"https://github.com/rust-lang/rust/issues/19004#issuecomment-96131494","issue_url":"https://api.github.com/repos/rust-lang/rust/issues/19004","id":96131494,"user":{"login":"joshtriplett","id":162737,"avatar_url":"https://avatars.githubusercontent.com/u/162737?v=3","gravatar_id":"","url":"https://api.github.com/users/joshtriplett","html_url":"https://github.com/joshtriplett","followers_url":"https://api.github.com/users/joshtriplett/followers","following_url":"https://api.github.com/users/joshtriplett/following{/other_user}","gists_url":"https://api.github.com/users/joshtriplett/gists{/gist_id}","starred_url":"https://api.github.com/users/joshtriplett/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/joshtriplett/subscriptions","organizations_url":"https://api.github.com/users/joshtriplett/orgs","repos_url":"https://api.github.com/users/joshtriplett/repos","events_url":"https://api.github.com/users/joshtriplett/events{/privacy}","received_events_url":"https://api.github.com/users/joshtriplett/received_events","type":"User","site_admin":false},"created_at":"2015-04-25T05:03:58Z","updated_at":"2015-04-25T05:03:58Z","body":"I just ran into this one today, with 'f' being 'self'; that seems like a rather common case, which will come up any time you write self.somefield.method(|| { ... self.otherfield.method(...) ... }) (and at least one of the methods wants a &mut).\r\n\r\nBased on some discussion in the #rust IRC channel, this seems potentially feasible to fix.  The closure desugaring just needs to recognize references to fields and capture the field in that case, rather than capturing the entire top-level label."}}