X-Git-Url: https://git.ralfj.de/web.git/blobdiff_plain/70570aa6154c2871e78fd9221fc1664131ae9991..11a1015cc9817cc435d99ddd612983a383284a8a:/personal/_posts/2019-04-30-stacked-borrows-2.md?ds=inline diff --git a/personal/_posts/2019-04-30-stacked-borrows-2.md b/personal/_posts/2019-04-30-stacked-borrows-2.md index 5b2abb0..669d49d 100644 --- a/personal/_posts/2019-04-30-stacked-borrows-2.md +++ b/personal/_posts/2019-04-30-stacked-borrows-2.md @@ -22,7 +22,7 @@ I didn't plan this out in advance, so things are a bit more messy than I would l ## The problem The problem I wanted to solve with Stacked Borrows 2 was that the first version of Stacked Borrows only performed very little tracking of shared references. -My thinking was, if the location is read-only anyway, then it does not harm to grant anyone read access. +My thinking was, if the location is read-only anyway, then it is not harmful to grant anyone read access. However, [as @arielby noted](https://github.com/rust-lang/unsafe-code-guidelines/issues/87), this leads to loss of optimization potential in cases where a function receives a mutable reference (which is supposed to have no aliases) and then creates a shared reference from it: {% highlight rust %} fn main() { @@ -80,7 +80,7 @@ pub struct Stack { {% endhighlight %} The *tag* is also simpler than it was before: there are no longer separate tags for mutable and shared references. {% highlight rust %} -pub type PtrId = NonZeroU64; +pub type PtrId = u64; pub enum Tag { Tagged(PtrId), Untagged, @@ -209,7 +209,7 @@ fn foo(a: &mut u32, y: *mut u32) -> u32 { {% endhighlight %} Initially, `x` with tag `Tagged(0)` is the only reference, and the stack says that this is the only pointer with any kind of permission. Next, we cast `x` to a raw pointer. -The raw retagging of `p` turns `p` into an `Untagged` pointer, and adds a new item granting thusly tagged pointers `SharedReadWrite` permission. +The raw retagging of `p` turns `p` into an `Untagged` pointer, and adds a new item granting `Untagged` pointers `SharedReadWrite` permission. (Really, in the MIR it will say `&mut *x as *mut u32`, so there will be an additional `Unique` permission for the temporary mutable reference, but that makes no difference and I hope [we will change that eventually](https://github.com/rust-lang/rfcs/pull/2582).) Then `foo` gets called, which starts with the usual retagging of all reference arguments.