Skip to content

Commit

Permalink
Fix soundness bug with Rt<Option<T>>
Browse files Browse the repository at this point in the history
Was thinking about the implications of this, and if you ever have a &mut
Option<Rt<T>> you could call `Option::take` and get the `Rt<T>` out. This would
be unsound. Still need to think about how to support optionals in functions that
take `&mut Context`.
  • Loading branch information
CeleritasCelery committed Dec 8, 2023
1 parent 442409f commit 9dc25ab
Showing 1 changed file with 3 additions and 6 deletions.
9 changes: 3 additions & 6 deletions src/core/gc/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -444,19 +444,16 @@ impl<T, U> DerefMut for Rt<(T, U)> {
}
}

// Can't implement [`DerefMut`] because it would allow you to call
// [`Option::take`] which would return an owned Rt and break the chain of
// traceability
impl<T> Deref for Rt<Option<T>> {
type Target = Option<Rt<T>>;
fn deref(&self) -> &Self::Target {
unsafe { &*(self as *const Self).cast::<Self::Target>() }
}
}

impl<T> DerefMut for Rt<Option<T>> {
fn deref_mut(&mut self) -> &mut Self::Target {
unsafe { &mut *(self as *mut Self).cast::<Self::Target>() }
}
}

impl<T> Rt<Option<T>> {
pub(crate) fn set<U: IntoRoot<T>>(&mut self, obj: U) {
unsafe {
Expand Down

0 comments on commit 9dc25ab

Please sign in to comment.