Skip to content

Commit

Permalink
Merge pull request #163 from andersk/ignore-trace-panic
Browse files Browse the repository at this point in the history
Allow dereferencing a rooted Gc during collection
  • Loading branch information
Manishearth authored Feb 5, 2023
2 parents de3769d + 9752d3d commit 2b9e451
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
4 changes: 2 additions & 2 deletions gc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,12 @@ impl<T: ?Sized> Gc<T> {
#[inline]
fn inner_ptr(&self) -> *mut GcBox<T> {
// If we are currently in the dropping phase of garbage collection,
// it would be undefined behavior to dereference this pointer.
// it would be undefined behavior to dereference an unrooted Gc.
// By opting into `Trace` you agree to not dereference this pointer
// within your drop method, meaning that it should be safe.
//
// This assert exists just in case.
assert!(finalizer_safe());
assert!(finalizer_safe() || self.rooted());

unsafe { clear_root_bit(self.ptr_root.get()).as_ptr() }
}
Expand Down
12 changes: 12 additions & 0 deletions gc/tests/ignore_trace.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use gc::{force_collect, Finalize, Gc, Trace};

#[derive(Finalize, Trace)]
struct S(#[unsafe_ignore_trace] Gc<()>);

/// Using `#[unsafe_ignore_trace]` on a `Gc` may inhibit collection of
/// cycles through that `Gc`, but it should not result in panics.
#[test]
fn ignore_trace_gc() {
Gc::new(S(Gc::new(())));
force_collect();
}

0 comments on commit 2b9e451

Please sign in to comment.