Skip to content

Compiler hangs or crashes by a few code #10431

@falsandtru

Description

@falsandtru

I believe TypeScript must not hang by the following small code.

TypeScript Version: 2.1.0-dev.20160819

Code

export class Sequence<a, z> {
  constructor(
    cons: (z: z, cons: (a?: a, z?: z) => Sequence.Data<a, z>) => Sequence.Data<a, z>,
    memory?: Map<number, Sequence.Data<a, z>>
  ) {
  }
}
export namespace Sequence {
  export type Data<a, z> = [a, z];
  export namespace Data {
    export function cons<a, z>(a?: a, b?: z): Sequence.Data<a, z> {
      switch (arguments.length) {
        case 0:
          return <Sequence.Data<a, z>>[];
        case 1:
          return <Sequence.Data<a, z>><any[]>[a];
        case 2:
          return <Sequence.Data<a, z>>[a, b];
        default:
          throw Sequence.Exception.invalidConsError(arguments);
      }
    }
  }
  export type Thunk<a> = [a, Iterator<a>, number];
  export namespace Thunk {
    export function value<a>(thunk: Thunk<a>): a {
      return thunk[0];
    }
    export function iterator<a>(thunk: Thunk<a>): Iterator<a> {
      return thunk[1];
    }
    export function index<a>(thunk: Thunk<a>): number {
      return thunk[2];
    }
  }
  export type Iterator<a> = () => Thunk<a>;
  export namespace Iterator {
    export const done: Sequence.Iterator<any> = () => <Sequence.Thunk<any>>[void 0, done, -1];
    export function when<a, b>(
      thunk: Thunk<a>,
      caseDone: (thunk: Thunk<a>) => b,
      caseIterable: (thunk: Thunk<a>, recur: () => b) => b
    ): b {
      return Sequence.isIterable(thunk)
        ? caseIterable(thunk, () => when(Thunk.iterator(thunk)(), caseDone, caseIterable))
        : caseDone(thunk);
    }
  }
  export function isIterable(thunk: Thunk<any>): boolean {
    return Thunk.iterator(thunk) !== Iterator.done;
  }
  export namespace Exception {
    export function invalidConsError(args: IArguments): TypeError {
      console.error(args, args.length, args[0], args[1]);
      return new TypeError(`Spica: Sequence: Invalid parameters of cons.`);
    }
    export function invalidDataError(data: Sequence.Data<any, any>): TypeError {
      console.error(data);
      return new TypeError(`Spica: Sequence: Invalid data.`);
    }
    export function invalidThunkError(thunk: Sequence.Thunk<any>): TypeError {
      console.error(thunk);
      return new TypeError(`Spica: Sequence: Invalid thunk.`);
    }
  }
}

Expected behavior:

It can compile quickly.

Actual behavior:

Too slow. In my actual case, compiler will be crashed.

$ $(npm bin)/tsc --noEmit -t es5 --lib es6,dom --diagnostics src/lib/monad/sequence/core.ts
Files:              13
Lines:           20588
Nodes:          102533
Identifiers:     37365
Symbols:       1679795
Types:          787098
Memory used:  1285938K
I/O read:        0.03s
I/O write:       0.00s
Parse time:      0.91s
Bind time:       0.67s
Check time:     94.28s
Emit time:       0.00s
Total time:     95.87s

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFixedA PR has been merged for this issue

Type

No type
No fields configured for issues without a type.

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions