Skip to content

Commit 0bee77a

Browse files
committed
types: implement Send/Sync for BoundRef
Added a block comment inline explaining the safety.
1 parent 01da7e5 commit 0bee77a

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

src/types/context.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,19 @@ pub struct BoundRef {
245245
index: usize,
246246
}
247247

248+
// SAFETY: The pointer inside `BoundRef` is always (eventually) constructed from Arc::as_ptr
249+
// from the slab of a type-inference context.
250+
//
251+
// Arc will prevent the pointer from ever changing, except to be deallocated when the last
252+
// Arc goes away. But this occurs only when the context itself goes away, which in turn
253+
// happens only when every type bound referring to the context goes away.
254+
//
255+
// If this were untrue, our use of `BoundRef` would lead to dereferences of a dangling
256+
// pointer, and `Send`/`Sync` would be the least of our concerns!
257+
unsafe impl Send for BoundRef {}
258+
// SAFETY: see comment on `Send`
259+
unsafe impl Sync for BoundRef {}
260+
248261
impl BoundRef {
249262
pub fn assert_matches_context(&self, ctx: &Context) {
250263
assert_eq!(

0 commit comments

Comments
 (0)