gc-arena icon indicating copy to clipboard operation
gc-arena copied to clipboard

Overflow on recursive struct

Open puckipedia opened this issue 5 years ago • 3 comments

#[derive(gc_arena::Collect)]
#[collect(no_drop)]
struct Test<'gc, T>(Option<gc_arena::Gc<'gc, Test<'gc, T>>>);

This seems to be because Gc<T> requires T to be Collect, but that depends on Gc<T> being Collect. I'm not sure on how this would be fixed, if even possible.

puckipedia avatar Jan 09 '20 13:01 puckipedia

Just to be clear (I haven't tried this), is it the automatic Collect implementation that triggers an error? During compilation?

kyren avatar Jan 10 '20 02:01 kyren

Indeed. Error trace is:

error[E0275]: overflow evaluating the requirement `gc_arena::gc::Gc<'_, Test<'_, T>>: std::marker::Sized`
 --> src/main.rs:3:21
  |
3 | struct Test<'gc, T>(Option<gc_arena::Gc<'gc, Test<'gc, T>>>);
  |                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = help: consider adding a `#![recursion_limit="256"]` attribute to your crate
  = note: required because of the requirements on the impl of `gc_arena::collect::Collect` for `std::option::Option<gc_arena::gc::Gc<'_, Test<'_, T>>>`
  = note: required because of the requirements on the impl of `gc_arena::collect::Collect` for `Test<'_, T>`
  = note: required because of the requirements on the impl of `gc_arena::collect::Collect` for `gc_arena::gc::Gc<'_, Test<'_, T>>`
[... above three lines repeat a bunch of times]
  = note: required because of the requirements on the impl of `gc_arena::collect::Collect` for `std::option::Option<gc_arena::gc::Gc<'_, Test<'_, T>>>`
  = note: required because of the requirements on the impl of `gc_arena::collect::Collect` for `Test<'gc, T>`
  = note: required by `gc_arena::gc::Gc`

puckipedia avatar Jan 10 '20 11:01 puckipedia

You can work around this by replacing the direct recursion with Self. (I’ve added a T field because T has to be used somewhere.)

#[derive(gc_arena::Collect)]
#[collect(no_drop)]
struct Test<'gc, T>(T, Option<gc_arena::Gc<'gc, Self>>)
where
    T: gc_arena::Collect + 'gc;

andersk avatar Dec 30 '20 09:12 andersk

I think this is fixed by #23!

kyren avatar Nov 25 '22 22:11 kyren