rust-typescript-type-def icon indicating copy to clipboard operation
rust-typescript-type-def copied to clipboard

Handle circular references

Open adwhit opened this issue 1 year ago • 5 comments

Hi, thanks for the crate and feel free to close if out of scope.

This crate fails when dealing with circular references. Example:

#[derive(TypeDef)]
enum SelfRef {
    One(u32),
    Many(Vec<SelfRef>),
}

This could result in a (valid) TS type something like:

export type SelfRef = { One: number } | { Many: SelfRef[] };

Instead upon compilation produces error like:

error[E0391]: cycle detected when elaborating drops for `<impl at src/main.rs:343:10: 343:17>::INFO`
   --> src/main.rs:343:10
    |
343 | #[derive(TypeDef)]
    |          ^^^^^^^
    |
note: ...which requires const-evaluating + checking `<impl at src/main.rs:343:10: 343:17>::INFO::promoted[1]`...
   --> src/main.rs:343:10
    |
343 | #[derive(TypeDef)]
    |          ^^^^^^^
note: ...which requires const-evaluating + checking `<impl at src/main.rs:343:10: 343:17>::INFO::promoted[1]`...
   --> src/main.rs:343:10
    |
343 | #[derive(TypeDef)]
    |          ^^^^^^^
note: ...which requires const-evaluating + checking `typescript_type_def::impls::<impl typescript_type_def::emit::TypeDef for alloc::vec::Vec<T>>::INFO`...
   --> /Users/user/.cargo/registry/src/github.com-1ecc6299db9ec823/typescript-type-def-0.5.6/src/impls.rs:158:28
    |
158 |     const INFO: TypeInfo = list_type_info!(T);
    |                            ^^^^^^^^^^^^^^^^^^
note: ...which requires const-evaluating + checking `<impl at src/main.rs:343:10: 343:17>::INFO`...
   --> src/main.rs:343:10
    |
343 | #[derive(TypeDef)]
    |          ^^^^^^^
note: ...which requires caching mir of `<impl at src/main.rs:343:10: 343:17>::INFO` for CTFE...
   --> src/main.rs:343:10
    |
343 | #[derive(TypeDef)]
    |          ^^^^^^^
    = note: ...which again requires elaborating drops for `<impl at src/main.rs:343:10: 343:17>::INFO`, completing the cycle
    = note: cycle used when running analysis passes on this crate
    = note: this error originates in the derive macro `TypeDef` (in Nightly builds, run with -Z macro-backtrace for more info)

My hunch is this might not be possible to fix without significant internal changes.

adwhit avatar Apr 14 '23 13:04 adwhit