rust-lang / unsafe-code-guidelines

Forum for discussion about what unsafe code can and can't do
https://rust-lang.github.io/unsafe-code-guidelines
Apache License 2.0
670 stars 58 forks source link

Confusion of retagging in the blog stacked-borrows-implementation #536

Closed hanayashiki closed 1 month ago

hanayashiki commented 1 month ago

https://www.ralfj.de/blog/2018/11/16/stacked-borrows-implementation.html

I think in the part of retagging,

Check if the new tag is Shr(Some(t)) and the location is inside an UnsafeCell. If both conditions apply, freeze the location with timestamp t. If it is already frozen, do nothing. Otherwise, push a new item onto the stack: Shr if the tag is a Shr(_), Uniq(id) if the tag is Uniq(id).

Isn't it "the location is NOT inside an UnsafeCell"?

According to the update note

Creating a shared reference does not push a Shr item to the stack (unless there is an UnsafeCell). Moreover, creating a raw pointer is a special kind of retagging.

According to my English, it means "If there is an UnsafeCell, creating a shared reference will push a Shr item"

Let's assume the update note is correct. Thus, in the part of retagging, there is a conflict, namely:

When we create a shared reference, and if the location IS inside an UnsafeCell, it does not push any a Shr to the stack.

This makes me very confused, and seems confusing to other, I think.

RalfJung commented 1 month ago

Isn't it "the location is NOT inside an UnsafeCell"?

Oh, good catch -- yes we only freeze outside of UnsafeCell. Thanks for pointing this out!

Note however that Stacked Borrows evolved quite a bit since then. See here for an up-to-date version.

Closing the issue as the typo got fixed.