{"action":"created","issue":{"url":"https://api.github.com/repos/nikomatsakis/rust-memory-model/issues/1","repository_url":"https://api.github.com/repos/nikomatsakis/rust-memory-model","labels_url":"https://api.github.com/repos/nikomatsakis/rust-memory-model/issues/1/labels{/name}","comments_url":"https://api.github.com/repos/nikomatsakis/rust-memory-model/issues/1/comments","events_url":"https://api.github.com/repos/nikomatsakis/rust-memory-model/issues/1/events","html_url":"https://github.com/nikomatsakis/rust-memory-model/issues/1","id":169334184,"number":1,"title":"Using `*mut` with an active `&mut` to a parent container","user":{"login":"aidanhs","id":1050652,"avatar_url":"https://avatars.githubusercontent.com/u/1050652?v=3","gravatar_id":"","url":"https://api.github.com/users/aidanhs","html_url":"https://github.com/aidanhs","followers_url":"https://api.github.com/users/aidanhs/followers","following_url":"https://api.github.com/users/aidanhs/following{/other_user}","gists_url":"https://api.github.com/users/aidanhs/gists{/gist_id}","starred_url":"https://api.github.com/users/aidanhs/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/aidanhs/subscriptions","organizations_url":"https://api.github.com/users/aidanhs/orgs","repos_url":"https://api.github.com/users/aidanhs/repos","events_url":"https://api.github.com/users/aidanhs/events{/privacy}","received_events_url":"https://api.github.com/users/aidanhs/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":3,"created_at":"2016-08-04T09:45:18Z","updated_at":"2016-08-17T20:54:46Z","closed_at":null,"body":"Probably covered by the 'key question' in https://github.com/nikomatsakis/rust-memory-model/blob/master/litmus_tests/overlapping_ref_mut_star_mut.md, but this is something I've encountered recently so thought I'd raise it.\r\n```\r\nfn x() -> Vec<usize> {\r\n    let mut x = vec![1, 2, 3, 4, 5, 6];\r\n    let mut p = ptr::null_mut();\r\n    for v in x.iter_mut() {\r\n        if *v % 2 == 0 && p != ptr::null_mut() {\r\n            unsafe { *p = *v }\r\n        }\r\n        p = v as *mut _;\r\n    }\r\n    x\r\n}\r\n```\r\nIs the compiler allowed to return `vec![1, 2, 3, 4, 5, 6]` (e.g. generating a new vector after the loop and pointer writes) because it cannot see any change occurring through the mutable reference to the overall vec? Or is this something you could get away with because you're mutating the previous elements of the vec (without a direct mutable reference to them)?\r\n\r\n(in the code that I'm looking at, this is split across 3 different functions for 1) the traversal over a custom tree-like structure, 2) storing pointers into the structure and 3) mutation of pointers based on a condition. So using a function as the maximum boundary of unsafety wouldn't be sufficient to support it)"},"comment":{"url":"https://api.github.com/repos/nikomatsakis/rust-memory-model/issues/comments/240544916","html_url":"https://github.com/nikomatsakis/rust-memory-model/issues/1#issuecomment-240544916","issue_url":"https://api.github.com/repos/nikomatsakis/rust-memory-model/issues/1","id":240544916,"user":{"login":"arielb1","id":1830974,"avatar_url":"https://avatars.githubusercontent.com/u/1830974?v=3","gravatar_id":"","url":"https://api.github.com/users/arielb1","html_url":"https://github.com/arielb1","followers_url":"https://api.github.com/users/arielb1/followers","following_url":"https://api.github.com/users/arielb1/following{/other_user}","gists_url":"https://api.github.com/users/arielb1/gists{/gist_id}","starred_url":"https://api.github.com/users/arielb1/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/arielb1/subscriptions","organizations_url":"https://api.github.com/users/arielb1/orgs","repos_url":"https://api.github.com/users/arielb1/repos","events_url":"https://api.github.com/users/arielb1/events{/privacy}","received_events_url":"https://api.github.com/users/arielb1/received_events","type":"User","site_admin":false},"created_at":"2016-08-17T20:54:46Z","updated_at":"2016-08-17T20:54:46Z","body":"Well, it works with a safe pointer:\r\n```Rust\r\nfn x() -> Vec<usize> {\r\n    let mut x = vec![1, 2, 3, 4, 5, 6];\r\n    { // extra block only needed because of non-lexical lifetimes\r\n    let mut p = &mut 0;\r\n    for v in x.iter_mut() {\r\n        if *v % 2 == 0 {\r\n            *p = *v;\r\n        }\r\n        p = v;\r\n    }\r\n    }\r\n    x\r\n}\r\n\r\nfn main() { println!(\"{:?}\", x()); }\r\n```"}}
{"actor":{"display_login":"arielb1"}}