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

Weird heap overflow when compiling a single line of code with recursive type definition. #30831

Closed
Veetaha opened this issue Apr 9, 2019 · 4 comments
Assignees
Labels
Bug A bug in TypeScript Crash For flagging bugs which are compiler or service crashes or unclean exits, rather than bad output

Comments

@Veetaha
Copy link

Veetaha commented Apr 9, 2019

TypeScript Version: typescript@3.4.2, typescript@3.5.0-dev.20190409

Search Terms:

  • Heap overflow

Code

type Foo<T> = Extract<{[i in keyof T]: Foo<T[i]>; }[0], 0>;

Expected behavior:
Should print an error for indexing object literal with 0 or using circular references.
Or at least the same behavior as in v3.3.4000:

Output when compiled with `typescript@3.3.4000`

image

Actual behavior:

NodeJS standard heap crash report: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory

Details

image

Playground from typescriptlang.com produces in-browser heap memory allocation failure:

Details

image

Playground Link:

Klick me if you are not afraid of crashing your browser's heap

Related Issues:

@hjkcai
Copy link

hjkcai commented Apr 9, 2019

Great! I think this will be the minimal reproduction of this issue. Thanks a lot!

@RyanCavanaugh RyanCavanaugh added Bug A bug in TypeScript Crash For flagging bugs which are compiler or service crashes or unclean exits, rather than bad output labels Apr 9, 2019
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 3.5.0 milestone Apr 9, 2019
@ahejlsberg
Copy link
Member

The example above produces the expected errors with #30769.

t.ts:1:6 - error TS2456: Type alias 'Foo' circularly references itself.

1 type Foo<T> = Extract<{[i in keyof T]: Foo<T[i]>; }[0], 0>;
       ~~~

t.ts:1:23 - error TS2536: Type '0' cannot be used to index type '{ [i in keyof T]: any; }'.

1 type Foo<T> = Extract<{[i in keyof T]: Foo<T[i]>; }[0], 0>;
                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

t.ts:1:40 - error TS2315: Type 'Foo' is not generic.

1 type Foo<T> = Extract<{[i in keyof T]: Foo<T[i]>; }[0], 0>;
                                         ~~~~~~~~~


Found 3 errors.

@andrewbranch
Copy link
Member

Confirmed that this doesn't crash in 3.5.0-rc, although the only error I get is

main.ts:1:23 - error TS2536: Type '0' cannot be used to index type '{ [i in keyof T]: Extract<{ [i in keyof T[i]]: Extract<{ [i in keyof T[i][i]]: Extract<{ [i in keyof T[i][i][i]]: Extract<{ [i in keyof T[i][i][i][i]]: Extract<{ [i in keyof T[i][i][i][i][i]]: Extract<{ [i in keyof T[i][i][i][i][i][i]]: Extract<{ [i in keyof T[i][i][i][i][i][i][i]]: Extract<{ [i in keyof T[i][i][i][i][i][i][i][i]]: Extract<{ [i in keyof T[i][i][i][i][i][i][i][i][i]]: Extract<{ [i in keyof T[i][i][i][i][i][i][i][i][i][i]]: Extract<any[0], 0>; }[0], 0>; }[0], 0>; }[0], 0>; }[0], 0>; }[0], 0>; }[0], 0>; }[0], 0>; }[0], 0>; }[0], 0>; }[0], 0>; }'.

1 type Foo = Extract<{[i in keyof T]: Foo<T[i]>; }[0], 0>;

🤔

@andrewbranch
Copy link
Member

Talked to @weswigham and this is probably caused by #31354. I think the new output here is not super desirable, but it's not related to this issue per se. If there are new real-world issues with this code or other conditional types in the RC, a new issue is welcomed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Crash For flagging bugs which are compiler or service crashes or unclean exits, rather than bad output
Projects
None yet
Development

No branches or pull requests

5 participants