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

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

Closed
Tom-Goring opened this issue Feb 12, 2024 · 4 comments · Fixed by #241
Closed
Labels
bug Something isn't working P-HIGH This will be treated with high priority

Comments

@Tom-Goring
Copy link

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!

@NyxCode NyxCode added bug Something isn't working P-HIGH This will be treated with high priority labels Feb 12, 2024
@NyxCode
Copy link
Collaborator

NyxCode commented Feb 12, 2024

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 }>")].

@escritorio-gustavo
Copy link
Contributor

escritorio-gustavo commented Feb 14, 2024

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.

@escritorio-gustavo
Copy link
Contributor

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

@escritorio-gustavo
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working P-HIGH This will be treated with high priority
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants