Aleph-Alpha/ts-rs

'internal error: entered unreachable code' when generating struct fields using Results

Tom-Goring opened this issue · 4 comments

When doing something like:

use ts_rs::TS;

#[derive(TS)]
#[ts(export)]
struct State {
    a: Vec<Result<String, String>>,
    b: Vec<Result<String, String>>,
}

I get the error 'internal error: entered unreachable code' - from lib.rs:520:9 on main. (I'm using the latest commit).

This could be user error as if I do:

#[derive(TS)]
#[ts(export)]
struct State {
    #[ts(inline)]
    a: Vec<Result<String, String>>,
    #[ts(inline)]
    b: Vec<Result<String, String>>,
}

it works fine.

If this behaviour is intended it might be best to replace the use of unreachable!() for clarity?

On another note - using inline like this means no imports are generated for types that appear in the inline, e.g for:

use ts_rs::TS;

#[derive(TS)]
#[ts(export)]
struct State {
    #[ts(inline)]
    a: Vec<Result<EnumWithName, String>>,
    #[ts(inline)]
    b: Vec<Result<EnumWithName, String>>,
}

#[derive(TS)]
#[ts(export)]
struct EnumWithName {
    name: String,
    inner: Enum
}

#[derive(TS)]
#[ts(export)]
enum Enum {
    A,
    B
}

the generated type I get for State is:

// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.

export type State = { a: Array<{ Ok : { name: string, inner: Enum, } } | { Err : string }>, b: Array<{ Ok : { name: string, inner: Enum, } } | { Err : string }>, }

The other files generate correctly (Enum and EnumWithName), but Enum is not imported and so the file isn't totally valid to compile. Is there a way around this, or is this behaviour or my use of inline here not intended.

Thanks for the crate as well - I've got a lot of use out of it!

Thanks for opening the issue!
I'm suprised our test suite didn't catch this, especially since this seems like a somewhat deeper issue.
In the meantime, I don't see any other workaround besides using #[ts(type = "Array<{ Ok: string } | { Err: string }>")].

I believe the unreachable! for Result is intentional, as it also happens in 7.1.1. This is the case because Result is not exported, so TS can't figure out what it means unless you #[ts(inline)] it, which converts it into a valid TS type union.

@NyxCode the problem with the missing import statement is probably something to do with the new dependencies system and its interaction with #[ts(inline)]

Now that #233 has been merged, you will no longer see unreachable with your first code snippet, which will now work as expected. As for the issue with inline generating incorrect import statements, there's already an issue to track that: #168