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

feat: allow deriving Trace without Drop/Finalize #116

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
11 changes: 11 additions & 0 deletions gc/tests/trace_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ use gc::Trace;
#[derive(Copy, Clone, Finalize)]
struct Foo;

#[derive(Trace, Finalize)]
struct FooWithoutCopy;

unsafe impl Trace for Foo {
unsafe fn trace(&self) {
X.with(|x| {
Expand Down Expand Up @@ -57,6 +60,12 @@ struct Baz {
b: Bar,
}

#[derive(Trace)]
#[trivially_drop]
struct Dereferenced {
a: FooWithoutCopy,
}

#[test]
fn test() {
let bar = Bar { inner: Foo };
Expand All @@ -72,4 +81,6 @@ fn test() {
baz.trace();
}
X.with(|x| assert!(*x.borrow() == 3));

let Dereferenced { a: _a } = Dereferenced { a: FooWithoutCopy };
}
55 changes: 39 additions & 16 deletions gc_derive/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
use quote::quote;
use synstructure::{decl_derive, AddBounds, Structure};
use synstructure::{decl_derive, AddBounds, BindStyle, Structure};

decl_derive!([Trace, attributes(unsafe_ignore_trace)] => derive_trace);
decl_derive!([Trace, attributes(unsafe_ignore_trace, trivially_drop)] => derive_trace);

fn derive_trace(mut s: Structure<'_>) -> proc_macro2::TokenStream {
let is_trivially_drop = s
.ast()
.attrs
.iter()
.any(|attr| attr.path.is_ident("trivially_drop"));
s.filter(|bi| {
!bi.ast()
.attrs
Expand Down Expand Up @@ -52,23 +57,41 @@ fn derive_trace(mut s: Structure<'_>) -> proc_macro2::TokenStream {
},
);

// We also implement drop to prevent unsafe drop implementations on this
// type and encourage people to use Finalize. This implementation will
// call `Finalize::finalize` if it is safe to do so.
let drop_impl = s.unbound_impl(
quote!(::std::ops::Drop),
if !is_trivially_drop {
// We also implement drop to prevent unsafe drop implementations on this
// type and encourage people to use Finalize. This implementation will
// call `Finalize::finalize` if it is safe to do so.
let drop_impl = s.unbound_impl(
quote!(::std::ops::Drop),
quote! {
fn drop(&mut self) {
if ::gc::finalizer_safe() {
::gc::Finalize::finalize(self);
}
}
},
);

quote! {
fn drop(&mut self) {
if ::gc::finalizer_safe() {
::gc::Finalize::finalize(self);
#trace_impl
#drop_impl
}
} else {
s.bind_with(|_| BindStyle::Move);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add some comments here as to why this branch is safe?

let trivially_drop_body = s.each(|_| quote! {});
let finalize_impl = s.bound_impl(
quote!(::gc::Finalize),
quote!(
fn finalize(&self) {
let _trivially_drop = |t: Self| match t { #trivially_drop_body };
}
}
},
);
),
);

quote! {
#trace_impl
#drop_impl
quote! {
#trace_impl
#finalize_impl
}
}
}

Expand Down