Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow dereferencing a rooted Gc during collection #163

Merged
merged 1 commit into from
Feb 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}