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

[Bug]tsc does not finish #26314

Closed
kgtkr opened this issue Aug 9, 2018 · 6 comments
Closed

[Bug]tsc does not finish #26314

kgtkr opened this issue Aug 9, 2018 · 6 comments
Labels
Bug A bug in TypeScript
Milestone

Comments

@kgtkr
Copy link

kgtkr commented Aug 9, 2018

TypeScript Version: 3.0.1/3.1.0-dev.20180808

Search Terms:

Code

type Head<T> = T extends [infer P, ...any[]] ? P : never;
type Tail<L extends any[]> = ((...x: L) => void) extends ((h: any, ...rest: infer T) => void) ? T : never

type Last<T extends any[]> = {
    0: never,
    1: Head<T>,
    2: Last<Tail<T>>,
}[T extends [] ? 0 : T extends [any] ? 1 : 2];

type Reverse<L extends any[], X extends any[]=[]> = {
    1: X, 0: Reverse<Tail<L>, Cons<Head<L>, X>>
}[L extends [] ? 1 : 0]

type Fn<R extends any[]> = (...args: R) => void
type Fn1<H, T extends any[]> = (h: H, ...args: T) => void
type Cons<H, T extends any[]> = Fn1<H, T> extends Fn<infer R> ? R : []

type Zip<A extends any[], B extends any[], R extends any[]=[]> = {
    0: Reverse<R>,
    1: Zip<Tail<A>, Tail<B>, Cons<[Head<A>, Head<B>], R>>
}[A extends [] ? 0 : B extends [] ? 0 : 1];

type ShiftZip<T extends any[]> = Zip<T, Tail<T>>;

type Tuple2Fn<T> = T extends [infer A, infer B] ? (x: A) => B : never;

type _Pipe<T extends any[], R extends any[]=[]> = {
    0: Reverse<R>,
    1: _Pipe<Tail<T>, Cons<Tuple2Fn<Head<T>>, R>>
}[T extends [] ? 0 : 1];

type Pipe<T extends any[]>=_Pipe<ShiftZip<T>>;

declare function pipe<T extends any[]>(...f: Pipe<T>): (x: Head<T>) => Last<T>;

Expected behavior:
tsc finish

Actual behavior:
tsc does not finish

Playground Link:

Related Issues:
#24897

@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Aug 9, 2018
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 3.1 milestone Aug 9, 2018
@KiaraGrouwstra
Copy link
Contributor

KiaraGrouwstra commented Aug 9, 2018

I gotta admit non-terminating compilation is the toughest issue to debug in TypeScript, as it doesn't leave you with a stack trace... type recursion is still pretty much 'here be dragons' territory. :)

To debug, you may wanna first comment bits to see which call is actually triggering the non-terminating recursion, then figure out which recursion termination condition is not going as expected (what's the type it's getting in you expected to terminate execution yet didn't?).

Trying to unit-test with recursive types and random issues like this gets even worse. :)

Edit: すみません、英語が下手だと見落としてしまいました。
TSは確かにこういうエラーがもっともめんどくさいですね。(w)
コードはまだためしてないが、まずタイプを一つ一つコメントしたら、エラッたタイプを見つけるはずです。
見つかったら、回帰の条件をなぜ満たしていないのが見分けるはずです。

@AlCalzone
Copy link
Contributor

AlCalzone commented Aug 9, 2018

Related to, or maybe even a duplicate of #26155?

I've found the recursive Reverse and Concat/Zip definitions to be extremely prone to stalling the language server.

@kgtkr
Copy link
Author

kgtkr commented Aug 9, 2018

tsserverのクラッシュは何度も発生しました
しかしそのようなコードもtscを使えば正常にコンパイルすることが出来ましたし、tsserverを再起動すれば復活します
しかしこのコードはtscでもコンパイル出来ませんし、tsserverを再起動しても復活しません
tscの詳しい内部を知らないので #26155 と関連している問題かは分かりません

Tsserver crash occurred many times
However, such code could be successfully compiled using tsc, restarting tsserver will restore
However, this code can not be compiled with tsc, nor is it restored by restarting tsserver
I do not know the detailed internal of tsc so I do not know if it is related to # 26155

@KiaraGrouwstra
Copy link
Contributor

@kgtkr: Yeah, #26155 is mostly just another bug that occurs with recursive types.

Trying your code at TS Playground (version 3.0.1-insiders.20180726), Head, Tail, Last and Reverse work okay for me.
From Zip I get any as a return type, but when I add Pipe the browser tab hangs for me too...

Maybe the recursion is failing because Zip (and later types) aren't working well?

@RyanCavanaugh
Copy link
Member

See comments in #25942 - we don't have any near-term ideas for detecting non-terminating type computations nor good ideas on how to issue useful diagnostics for them.

@RyanCavanaugh
Copy link
Member

This quickly errors now.

halting problem: solved

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript
Projects
None yet
Development

No branches or pull requests

5 participants