Skip to content

Put 2 and 2 together and make unique_ptr Sync.#646

Open
copybara-service[bot] wants to merge 1 commit intomainfrom
test_874698118
Open

Put 2 and 2 together and make unique_ptr Sync.#646
copybara-service[bot] wants to merge 1 commit intomainfrom
test_874698118

Conversation

@copybara-service
Copy link

Put 2 and 2 together and make unique_ptr Sync.

As long as we don't expose access to the T for a &unique_ptr, it doesn't matter that it's internally mutable! So making it !Sync is equivocating and delaying a decision, here.

The model used in this CL is to make it Sync, and not allow any safe access to the T when behind a &unique_ptr. C++ can mutably access T, but this is unsafe in C++ just as it is in Rust, and there's no new UB from this.

When we have safe aliasing references, we should likely be able to project a &unique_ptr<T> to a CRef<T>, equally safely to C++. To enable this operation, uh, we might need to tighten when unique_ptr is Sync... I haven't thought about it.

Anyway, dealing with unique_ptr won't be nice and tidy. It's much better to use a Box if possible. (We need to merge the allocators, first, though!)

As long as we don't expose access to the `T` for a `&unique_ptr`, it doesn't matter that it's internally mutable! So making it `!Sync` is equivocating and delaying a decision, here.

The model used in this CL is to make it `Sync`, and not allow any safe access to the `T` when behind a `&unique_ptr`. C++ can mutably access `T`, but this is unsafe in C++ just as it is in Rust, and there's no _new_ UB from this.

When we have safe aliasing references, we should likely be able to project a `&unique_ptr<T>` to a `CRef<T>`, equally safely to C++. To enable this operation, uh, we might need to tighten when `unique_ptr` is `Sync`... I haven't thought about it.

Anyway, dealing with unique_ptr won't be nice and tidy. It's much better to use a `Box` if possible. (We need to merge the allocators, first, though!)

PiperOrigin-RevId: 874698118
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

Successfully merging this pull request may close these issues.

1 participant